当前位置:网站首页>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 .
边栏推荐
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
- LVS+Keepalived实现四层负载及高可用
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
- 2021-08-09
- Ternsort model integration summary
- Object. Assign () & JS (= >) arrow function & foreach () function
- Global function Encyclopedia
- Canoe: what is vtsystem
- Canoe test: two ways to create CAPL test module
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
(2021-08-20) web crawler learning 2
2018 meisai modeling summary +latex standard meisai template sharing
Analysis function in SQL
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
OSI seven layer reference model
随机推荐
Replace() function
(August 9, 2021) example exercise of air quality index calculation (I)
netstat
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
(August 10, 2021) web crawler learning - Chinese University ranking directed crawler
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
试题库管理系统–数据库设计[通俗易懂]
LxC shared directory permission configuration
Canoe: distinguish VT, VN and vteststudio from their development history
Btrace tells you how to debug online without restarting the JVM
IO stream ----- open
OSI seven layer reference model
Realize cross tenant Vnet connection through azure virtual Wan
2020 Summary - Magic year, magic me
Process communication and thread explanation
Fundamentals of software testing
Supercomputing simulation research has determined a safe and effective carbon capture and storage route
Heartbeat error attempted replay attack
Elevator dispatching (pairing project) ①