当前位置:网站首页>Entitas learning [3] multi context system
Entitas learning [3] multi context system
2022-07-04 11:31:00 【Mercury Note】
In the previous tutorial , Reactive The system generally belongs to only one Context Of Entity, But in some cases, some Entity May belong to more than one Context, If you need each Context All respond to this kind of Entity, It takes more than one Reactive The system responds , If you want to be in one Reactive There is a response in the system Belong to many Context Of Entity, This requires a multi context system . I still don't recommend this way of writing , Because this is also a bit similar to responsibility diffusion , That is, sometimes it is necessary to judge the context of the entity in a system , Take this as the basis to do different operations . But maybe sometimes it really needs to be used , So let's learn first .
Multi context system of single component
Add new Context( Context )
Just demonstrate how to generate a new context , A multi context system can consist of two by default Context To do it , Add a new context to regenerate , Otherwise, the tag will not be found in the code
[Game, Input, UI]
public class DestroyedComponent : IComponent
{
}
Use the new UI Marked DestroyedComponent , Mark this with three words at the same time component, Express this component The added entity can be in Game, Input, UI These three Context in .
System
public class MultiDestroySystem : MultiReactiveSystem<IDestroySystem, Contexts>
{
public MultiDestroySystem(Contexts contexts) : base(contexts)
{
}
protected override ICollector[] GetTrigger(Contexts contexts)
{
throw new System.NotImplementedException();
}
protected override bool Filter(IDestroySystem entity)
{
throw new System.NotImplementedException();
}
protected override void Execute(List<IDestroySystem> entities)
{
throw new System.NotImplementedException();
}
}
public interface IDestroySystem : IEntity, IDestroyedEntity
{
}
public partial class GameEntity : IDestroySystem
{
}
public partial class InputEntity : IDestroySystem
{
}
public partial class UIEntity : IDestroySystem
{
}
New inheritance system MultiReactiveSystem, He has two parameters TEntity, TContexts,
The video says that they are inherited from IEntity and IContext Class .
In addition, pay attention to the writing of the following three distribution classes and interfaces in the above code , This is also the template writing method of multi context system .
IDestroyedEntity Is writing DestroyedComponent after ,Entitas Auto generated code ,
IDestroySystem Not at all Entitas Meaningful System, Just look at the class it inherits .
Use the distribution class to divide the three context Redefine entity classes in , This was not inherited IDestroySystem These three entity classes of inherit .
and MultiDestroySystem As defined in IDestroySystem Just inherited IEntity and IDestroyedEntity.
So when GameEntity InputEntity UIEntity Meet MultiDestroySystem Of GetTrigger When the conditions , Can be transferred to MultiDestroySystem 了
Although it doesn't matter to write the same inheritance when defining multiple distribution classes , At first I felt IDestroySystem No inheritance IEntity, because GameEntity InputEntity UIEntity They all inherited it , But when writing , The first one is the interface , This interface must inherit IEntity, So we still have to inherit .
Integration part
public class MultiFeature : Feature
{
public MultiFeature(Contexts contexts)
{
Add(new MultiDestroySystem(contexts));
}
}
public class GameController : MonoBehaviour
{
private Systems _system;
private void Start()
{
_system = new Feature("System").Add(new MultiFeature(Contexts.sharedInstance));
_system.Initialize();
}
private void Update()
{
_system.Execute();
_system.Cleanup();
}
}
public class InputController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Contexts.sharedInstance.game.CreateEntity().isDestroyed = true;
}
if (Input.GetMouseButtonDown(1))
{
Contexts.sharedInstance.input.CreateEntity().isDestroyed = true;
}
}
}
At present, it is , Put components that need to be placed in multiple contexts according to DestroyedComponent A formal statement of , Then find its relation with IDestroyedEntity Similar by Entitas Generated interfaces , Instead of IDestroyedEntity In the above System Place in script , stay Trigger Write the context that will be triggered in the function , That is, when the multi context component is triggered , Into this system .
here , When you click the left and right mouse buttons, you can print
Multi context system with multiple components
The previous is a multi context system for a component , Now it is a multi context system with multiple components . That is, a reaction system can be triggered and processed by changes of multiple components in multiple contexts
Component
[Game, Input, UI]
public class ViewComponent : IComponent
{
public Transform viewTrans;
}
System
public class MultiDestroySystem : MultiReactiveSystem<IDestroySystem, Contexts>
{
public MultiDestroySystem(Contexts contexts) : base(contexts)
{
}
protected override ICollector[] GetTrigger(Contexts contexts)
{
return new ICollector[]
{
contexts.game.CreateCollector(GameMatcher.Destroyed),
contexts.input.CreateCollector(InputMatcher.Destroyed),
contexts.uI.CreateCollector(UIMatcher.Destroyed),
};
}
protected override bool Filter(IDestroySystem entity)
{
return entity.isDestroyed;
// The following will not be written with view Component's entity Get rid of ,
// There is no view Component's entity Of System Come on ,
// Need to put hasView Put judgment on execute Function , Another one else situation
// You can deal with the situation with or without at the same time
// && entity.hasView;
}
protected override void Execute(List<IDestroySystem> entities)
{
foreach (var entity in entities)
{
if (entity.hasView)
{
Object.Destroy(entity.view.viewTrans.gameObject);
}
else
{
// ...
}
Debug.Log(" stay " + entity.contextInfo.name +" The destruction ");
}
}
}
public interface IDestroySystem : IEntity, IDestroyedEntity, IViewEntity
{
}
public partial class GameEntity : IDestroySystem
{
}
public partial class InputEntity : IDestroySystem
{
}
public partial class UIEntity : IDestroySystem
{
}
On the whole, it's IDestroySystem Inherited more IViewEntity nothing more .
边栏推荐
- Shift EC20 mode and switch
- Simple understanding of seesion, cookies, tokens
- Appscan installation steps
- C language memory layout
- Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
- Understanding of object
- Force buckle 142 Circular linked list II
- Test question bank management system - database design [easy to understand]
- Install freeradius3 in the latest version of openwrt
- 守护进程Xinted和日志记录Syslogd
猜你喜欢
Alibaba cloud server connection intranet operation
Fundamentals of software testing
Ternsort model integration summary
(August 10, 2021) web crawler learning - Chinese University ranking directed crawler
Introduction to Lichuang EDA
Climb Phoenix Mountain on December 19, 2021
OSI model notes
QQ group administrators
Digital simulation beauty match preparation -matlab basic operation No. 6
Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
随机推荐
本地Mysql忘记密码的修改方法(windows)[通俗易懂]
Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
Shift EC20 mode and switch
2020 Summary - Magic year, magic me
Force buckle 142 Circular linked list II
VPS installation virtualmin panel
QQ get group member operation time
Digital simulation beauty match preparation -matlab basic operation No. 6
Notes on writing test points in mind mapping
试题库管理系统–数据库设计[通俗易懂]
Some tips on learning database
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
Elevator dispatching (pairing project) ③
Definition and method of string
3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
Canoe - the third simulation project - bus simulation - 3-2 project implementation
How to judge the advantages and disadvantages of low code products in the market?
守护进程Xinted和日志记录Syslogd
How to create a new virtual machine