Forest_Client/Forest/Assets/PhxhSDK/Phxh/Game/SubSystemManager.cs

74 lines
2.1 KiB
C#

using System.Collections.Generic;
namespace PhxhSDK
{
public class SubSystemManager : IUpdatable, IInitable, IStart, IOnApplicationPause
{
private readonly List<IOnApplicationPause> _appPauseList = new(4);
private readonly List<IInitable> _initables = new(8);
private readonly List<IStart> _startables = new(4);
private readonly List<IUpdatable> _updateList = new(4);
public void Init()
{
}
public void Release()
{
for (var i = _initables.Count - 1; i >= 0; i--)
{
var initable = _initables[i];
initable.Release();
}
_updateList.Clear();
}
public void OnApplicationPause(bool pauseStatus)
{
foreach (var appPause in _appPauseList) appPause.OnApplicationPause(pauseStatus);
}
public void Start()
{
foreach (var st in _startables) st.Start();
}
public void Update(float deltaTime)
{
foreach (var updatable in _updateList) updatable.Update(deltaTime);
}
public void AddSubSystem<T>() where T : class, new()
{
// if T is subclass of Singleton, use Instance instead of new
T subSystem = null;
if (typeof(T).IsSubclassOf(typeof(Singlenton<T>)))
subSystem = Singlenton<T>.Instance;
else
subSystem = new T();
var initable = subSystem as IInitable;
if (initable != null)
{
initable.Init();
_initables.Add(initable);
}
var startable = subSystem as IStart;
if (startable != null) _startables.Add(startable);
var globalObj = subSystem as IGlobal;
if (globalObj != null) globalObj.ToGlobal();
var updatable = subSystem as IUpdatable;
if (updatable != null) _updateList.Add(updatable);
var appPause = subSystem as IOnApplicationPause;
if (appPause != null) _appPauseList.Add(appPause);
}
}
}