(转)我所理解的Entitas——System(四)

这章我们将介绍ECS的三大基本概念中的System。

System 系统
System是一个单纯得逻辑处理类,在特定得时间执行系统内部的逻辑,这些逻辑中可以改变Entity上得Component得数据和状态, 原则上来说应该是只有逻辑没有数据。
Entitas给我们提供了五种系统类型,之后我将每种类型分成一个章节结果代码实例讲解方便理解。

IInitializeSystem
IExecuteSystem
ICleanupSystem
ITearDownSystem
ReactiveSystem
一般来说一个System只会用来处理一种逻辑,每个System之间相互也不需要相互调用(独立减少耦合)。完全是通过框架外部或者Entity上Component的数据来驱动。

Systems
上面我们说到了一个System一般只用来处理一种逻辑,游戏中就会有很多个System。那么就需要用到Systems来管理这些System。

Systems内部维护了4个不同的List来保存不同类型的System。

public Systems() {
        //初始化各个List
        _initializeSystems = new List<IInitializeSystem>();
        _executeSystems = new List<IExecuteSystem>();
        _cleanupSystems = new List<ICleanupSystem>();
        _tearDownSystems = new List<ITearDownSystem>();
    }

我们需要通过Add(ISystem system)将System添加到Systems中。但是不用管System会被添加到那个List中,Entitas会自动帮我们处理。

//将不同的System添加到对应的列表中
    public virtual Systems Add(ISystem system) {
        var initializeSystem = system as IInitializeSystem;
        if (initializeSystem != null) {
            _initializeSystems.Add(initializeSystem);
        }
        
        var executeSystem = system as IExecuteSystem;
        if (executeSystem != null) {
            _executeSystems.Add(executeSystem);
        }
        
        var cleanupSystem = system as ICleanupSystem;
        if (cleanupSystem != null) {
            _cleanupSystems.Add(cleanupSystem);
        }
        
        var tearDownSystem = system as ITearDownSystem;
        if (tearDownSystem != null) {
            _tearDownSystems.Add(tearDownSystem);
        }
        
        return this;
    }

Systems继承了IInitializeSystem, IExecuteSystem, ICleanupSystem, ITearDownSystem,并在内部实现了Cleanup(),Execute(),Initialize(),TearDown()等接口用来执行4个List中的System。所以我们需要只要再外部调用Systems的这4个接口即可调用了内部的所有System。

//驱动初始化系统执行Initialize()方法
public virtual void Initialize() {
    for (int i = 0; i < _initializeSystems.Count; i++) {
        _initializeSystems[i].Initialize();
    }
}

//驱动每帧执行的系统执行Execute()方法
public virtual void Execute() {
   for (int i = 0; i < _executeSystems.Count; i++) {
       _executeSystems[i].Execute();
   }
}

//驱动清理系统执行Cleanup()方法
public virtual void Cleanup() {
    for (int i = 0; i < _cleanupSystems.Count; i++) {
        _cleanupSystems[i].Cleanup();
    }
}

//驱动结束系统执行TearDown()方法
public virtual void TearDown() {
   for (int i = 0; i < _tearDownSystems.Count; i++) {
       _tearDownSystems[i].TearDown();
   }
}
为什么这里是4个List,而前面说到了Entitas提供了五种类型的System。这个问题将会在后面章节说到。

Feature
在实际开发过程中我们可能需要知道当前正在运行的有哪些System等调试信息,Entitas会为我们自动生成Feature这个类来帮我们调试。
Feature主要用于在编辑器模式下开启visual debugging时收集各个系统的数据,同时在Unity中展示。所以在开启visual debugging时Feature继承自DebugSystems,而DebugSystems又是继承自Systems,并在内部做一些数据收集的工作与展示的工作。当关闭visual debugging时Feature会直接继承自Systems。
visual debugging的开关在 菜单栏Tools->Entitas->Preferences:

 

总结:
上一章说了Component和Entity的关系,咱们接着前面的总结来说。
一个士兵(Entity)有了开飞机(Component_3)的技能也不可能一直开着飞机在天上飘,还得指挥部让你上天你才上天,让你往哪飞你才能往哪飞。那么这个指挥部就可以理解为System。例如陆军指挥部(System_1),海军指挥部(System_2),空军指挥部(System_3)。而System就是这个军区总指挥部。

原文地址:https://blog.csdn.net/u010020342/article/details/109898636?spm=1001.2014.3001.5502

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章