当前位置:网站首页>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 .
边栏推荐
- Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
- Introduction to Lichuang EDA
- Capl: timer event
- Force buckle 142 Circular linked list II
- thread
- re. Sub() usage
- Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
- Test question bank management system - database design [easy to understand]
- Usage of case when then else end statement
- Video analysis
猜你喜欢
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Automatic translation between Chinese and English
Introduction of network security research direction of Shanghai Jiaotong University
Games101 Lesson 8 shading 2 Notes
Canoe - the second simulation project -xvihicle1 bus database design (operation)
Introduction to canoe automatic test system
Detailed array expansion analysis --- take you step by step analysis
随机推荐
Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
试题库管理系统–数据库设计[通俗易懂]
Introduction to Lichuang EDA
Day01 preliminary packet capture
(August 9, 2021) example exercise of air quality index calculation (I)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
OSI seven layer model & unit
Reptile learning 4 winter vacation series (3)
Appscan installation steps
2021-10-20
Canoe-the second simulation project-xvehicle-1 bus database design (idea)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Locust installation
Common tips
Usage of case when then else end statement
Customized version of cacti host template
Object. Assign () & JS (= >) arrow function & foreach () function
LVS+Keepalived实现四层负载及高可用
Application of slice
VPS安装Virtualmin面板