(轉)我所理解的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

 

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