using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Stary.Evo
{
///
/// 架构
///
///
public abstract class Architecture : IDisposable, IArchitecture where T : Architecture, new()
{
///
/// 是否已经初始化完成
///
private bool mInited = false;
///
/// 用于初始化的 Datas 的缓存
///
private List mDatas = new List();
///
/// 用于初始化的 Systems 的缓存
///
private List mSystems = new List();
///
/// IOC
///
private IOCContainer mContainer = new IOCContainer();
// 留给子类注册模块
protected abstract void Init();
#region 类似单例模式 但是仅在内部访问
private static T mArchitecture = null;
public static IArchitecture Interface
{
get
{
if (mArchitecture == null)
{
MakeSureArchitecture();
}
return mArchitecture;
}
}
///
/// 增加注册
///
public static Action OnRegisterPatch = architecture => { Debug.Log("调用了"); };
// 确保 Container 是有实例的
static void MakeSureArchitecture()
{
Debug.Log((mArchitecture == null) + "----mArchitecture是否为空");
if (mArchitecture == null)
{
mArchitecture = new T();
mArchitecture.Init();
mArchitecture.IOCInitClass();
// 调用
OnRegisterPatch?.Invoke(mArchitecture);
// 初始化 Data
for (int i = 0; i < mArchitecture.mDatas.Count; i++)
{
mArchitecture.mDatas[i].Init();
}
// 清空 Data
//mArchitecture.mDatas.Clear();
// 初始化 System
for (int i = 0; i < mArchitecture.mSystems.Count; i++)
{
mArchitecture.mSystems[i].Init();
}
// 清空 System
//mArchitecture.mSystems.Clear();
mArchitecture.mInited = true;
}
}
// // 提供一个释放模块的 API
public void OnDispose()
{
for (int i = 0; i < mArchitecture.mSystems.Count; i++)
{
mArchitecture.mSystems[i].Dispose();
}
for (int i = 0; i < mArchitecture.mDatas.Count; i++)
{
mArchitecture.mDatas[i].Dispose();
}
_mEnumEventSystem.Dispose();
_mEnumEventSystem = null;
mArchitecture = null;
}
#endregion
#region 组件注册
// 提供一个注册 System 的 API
public void RegisterSystem(TSystem system) where TSystem : ISystem
{
// 需要给 System 赋值一下
system.SetArchitecture(this);
mContainer.Register(system);
// 如果初始化过了
if (mInited)
{
system.Init();
}
else
{
// 添加到 System 缓存中,用于初始化
mSystems.Add(system);
}
}
// 提供一个注册 Data 的 API
public void RegisterData(TData data) where TData : IData
{
// 需要给 Data 赋值一下
data.SetArchitecture(this);
mContainer.Register(data);
// 如果初始化过了
if (mInited)
{
data.Init();
}
else
{
// 添加到 Data 缓存中,用于初始化
mDatas.Add(data);
}
}
// 提供一个注册工具模块的 API
public void RegisterUtility(TUtility utility) where TUtility : IUtility
{
mContainer.Register(utility);
}
// 提供一个获取 Data 的 API
public TData GetData() where TData : class, IData
{
return mContainer.Get();
}
#endregion
#region IOC容器注册
//IOC初始化注册
private void IOCInitClass()
{
}
// // 提供一个注册模块的 API
public static void RegisterIOC(TArchitecture instance)
{
mArchitecture = null;
MakeSureArchitecture();
mArchitecture.mContainer.Register(instance);
}
// 提供一个获取模块的 API
public static TArchitecture GetIOC() where TArchitecture : class
{
MakeSureArchitecture();
return mArchitecture.mContainer.Get();
}
// 提供一个获取模块的 API
public static void UnRegisterIOC() where TArchitecture : class
{
mArchitecture.mContainer.UnRegister();
}
#endregion
// 提供一个获取工具模块的 API
public TUtility GetUtility() where TUtility : class, IUtility
{
return mContainer.Get();
}
// 提供一个获取系统的 API
public TSystem GetSystem() where TSystem : class, ISystem
{
return mContainer.Get();
}
// 提供一个传递Command的 API(不经过对象池)
public void SendCommand() where TCommand : ICommand, new()
{
var command = new TCommand();
ExecuteCommand(command);
}
// 提供一个传递Command的 API
public void SendCommand(TCommand command) where TCommand : ICommand
{
ExecuteCommand(command);
}
protected virtual void ExecuteCommand(ICommand command)
{
command.SetArchitecture(this);
command.Execute();
}
#region EnumEventSystem
private EnumEventSystem _mEnumEventSystem = new EnumEventSystem();
///
/// 事件发送
///
///
public void SendEvent(TEvent key) where TEvent : IConvertible
{
_mEnumEventSystem.Send(key);
}
///
/// 事件发送
///
///
public void SendEvent(TEvent key, Tvalue1 value) where TEvent : IConvertible
{
_mEnumEventSystem.Send(key, value);
}
///
/// 事件发送
///
///
public void SendEvent(TEvent key, Tvalue1 value1, Tvalue2 value2)
where TEvent : IConvertible
{
_mEnumEventSystem.Send(key, value1, value2);
}
///
/// 事件发送
///
///
public void SendEvent(TEvent key, Tvlue1 value1, Tvlue2 vlue2, Tvlue3 vlue3)
where TEvent : IConvertible
{
_mEnumEventSystem.Send(key, value1, vlue2, vlue3);
}
///
/// 事件发送
///
///
public void SendEvent(TEvent key, object[] values) where TEvent : IConvertible
{
_mEnumEventSystem.Send(key, values);
}
///
/// 事件监听
///
///
///
///
public IUnRegister RegisterEvent(TEvent key, Action onEvent) where TEvent : IConvertible
{
return _mEnumEventSystem.Register(key, onEvent);
}
///
/// 事件监听
///
///
///
///
public IUnRegister RegisterEvent(TEvent key, Action onEvent)
where TEvent : IConvertible
{
return _mEnumEventSystem.Register(key, onEvent);
}
///
/// 事件监听
///
///
///
///
public IUnRegister RegisterEvent(TEvent key, Action onEvent)
where TEvent : IConvertible
{
return _mEnumEventSystem.Register(key, onEvent);
}
///
/// 事件监听
///
///
///
///
public IUnRegister RegisterEvent(TEvent key,
Action onEvent) where TEvent : IConvertible
{
return _mEnumEventSystem.Register(key, onEvent);
}
///
/// 事件监听
///
///
///
///
public IUnRegister RegisterEvent(TEvent key, Action