This commit is contained in:
Mikhail 2023-03-12 02:48:51 +08:00
parent 25fc3318d0
commit 73090da11a
5 changed files with 29 additions and 15 deletions

View File

@ -10,7 +10,7 @@ namespace DCFApixels.DragonECS
{ {
public void Inject(T obj); public void Inject(T obj);
} }
public class InjectRunner<T> : EcsRunner<IEcsInject<T>>, IEcsInject<T> public sealed class InjectRunner<T> : EcsRunner<IEcsInject<T>>, IEcsInject<T>
{ {
void IEcsInject<T>.Inject(T obj) void IEcsInject<T>.Inject(T obj)
{ {

View File

@ -40,23 +40,30 @@ namespace DCFApixels.DragonECS
private int _id; private int _id;
private List<IEcsProcessor> _allSystems; private readonly List<IEcsProcessor> _allSystems;
private ReadOnlyCollection<IEcsProcessor> _allSystemsSealed; private ReadOnlyCollection<IEcsProcessor> _allSystemsSealed;
private bool _isInit = false; private bool _isInit = false;
private bool _isDestoryed = false; private bool _isDestoryed = false;
private Dictionary<Type, IEcsRunner> _runners; private readonly Dictionary<Type, IEcsRunner> _runners;
private IEcsRunSystem _runRunnerCache; private IEcsRunSystem _runRunnerCache;
private EcsWorldMap _worldMap = new EcsWorldMap(); private readonly EcsWorldMap _worldMap;
#region Properties #region Properties
public ReadOnlyCollection<IEcsProcessor> AllProcessors => _allSystemsSealed; public ReadOnlyCollection<IEcsProcessor> AllProcessors => _allSystemsSealed;
#endregion #endregion
public EcsSession()
{
_allSystems = new List<IEcsProcessor>(128);
_runners = new Dictionary<Type, IEcsRunner>();
_worldMap = new EcsWorldMap();
}
#region React Runners/Messengers #region React Runners/Messengers
public T GetRunner<T>() where T : IEcsProcessor public T GetRunner<T>() where T : IEcsProcessor
{ {
@ -89,7 +96,7 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region LifeCycle #region LifeCycle
public void Init() public EcsSession Init()
{ {
CheckInitForMethod(nameof(Init)); CheckInitForMethod(nameof(Init));
_worldMap.Build(); _worldMap.Build();
@ -102,6 +109,8 @@ namespace DCFApixels.DragonECS
GetRunner<IEcsInitSystem>().Init(this); GetRunner<IEcsInitSystem>().Init(this);
_runRunnerCache = GetRunner<IEcsRunSystem>(); _runRunnerCache = GetRunner<IEcsRunSystem>();
return this;
} }
public void Run() public void Run()
{ {
@ -111,7 +120,7 @@ namespace DCFApixels.DragonECS
} }
public void Destroy() public void Destroy()
{ {
CheckDestroyForMethod(nameof(Run)); CheckDestroyForMethod(nameof(Destroy));
_isDestoryed = true; _isDestoryed = true;
GetRunner<IEcsDestroySystem>().Destroy(this); GetRunner<IEcsDestroySystem>().Destroy(this);
@ -126,7 +135,7 @@ namespace DCFApixels.DragonECS
} }
private void CheckDestroyForMethod(string methodName) private void CheckDestroyForMethod(string methodName)
{ {
if (_isInit) if (_isDestoryed)
throw new MethodAccessException($"Запрещено вызывать метод {methodName}, после уничтожения {nameof(EcsSession)}"); throw new MethodAccessException($"Запрещено вызывать метод {methodName}, после уничтожения {nameof(EcsSession)}");
} }
#endregion #endregion

View File

@ -59,7 +59,7 @@ namespace DCFApixels.DragonECS
_runnerTypes = new Dictionary<Guid, Type>(); _runnerTypes = new Dictionary<Guid, Type>();
foreach (var item in newRunnerTypes) foreach (var item in newRunnerTypes)
{ {
Type intrf = item.GetInterfaces()[2]; //TODO доработать это место. Во-первых убрать магическое число 2, во-вторых сделать так чтоб брался только наследованный интерфейс, а не все Type intrf = item.GetInterfaces().Where(o => o != typeof(IEcsRunner) && o != typeof(IEcsProcessor)).First(); //TODO оптимизировать это место
_runnerTypes.Add(intrf.GUID, item); _runnerTypes.Add(intrf.GUID, item);
} }
@ -87,7 +87,12 @@ namespace DCFApixels.DragonECS
public static void InitFor<TInterface>() where TInterface : IEcsProcessor public static void InitFor<TInterface>() where TInterface : IEcsProcessor
{ {
Type interfaceType = typeof(TInterface); Type interfaceType = typeof(TInterface);
Guid interfaceGuid = interfaceType.GUID; Type nonGenericInterfaceType = interfaceType;
if (nonGenericInterfaceType.IsGenericType)
{
nonGenericInterfaceType = nonGenericInterfaceType.GetGenericTypeDefinition();
}
Guid interfaceGuid = nonGenericInterfaceType.GUID;
if (!_runnerTypes.TryGetValue(interfaceGuid, out Type runnerType)) if (!_runnerTypes.TryGetValue(interfaceGuid, out Type runnerType))
{ {

View File

@ -13,7 +13,7 @@ namespace DCFApixels.DragonECS
private void Start() private void Start()
{ {
_ecsSession _ecsSession = new EcsSession()
.Add(new TestSystem()) .Add(new TestSystem())
.Inject(_data) .Inject(_data)
.Init(); .Init();

View File

@ -3,26 +3,26 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public class TestSystem : public class TestSystem : IEcsInject<SharedData>, IEcsSimpleCycleSystem
IEcsInject<SharedData>,
IEcsSimpleCycleSystem
{ {
private SharedData _sharedData; private SharedData _sharedData;
public void Inject(SharedData obj) => _sharedData = obj; public void Inject(SharedData obj) => _sharedData = obj;
public void Init(EcsSession session) public void Init(EcsSession session)
{ {
Debug.Log("Init");
} }
public void Run(EcsSession session) public void Run(EcsSession session)
{ {
Debug.Log("Run");
} }
public void Destroy(EcsSession session) public void Destroy(EcsSession session)
{ {
Debug.Log("Destroy");
} }
} }
} }