From e56c7f94d7076fef7d0da3a4f1a5058c9bf5e72e Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:41:33 +0800 Subject: [PATCH] update --- src/EcsWorld.cs | 2 ++ src/Pools/EcsPool.cs | 25 ++++++++++++-- src/Pools/EcsPoolBase.cs | 70 +++++++++++++++++++++++++++++++++------- src/Pools/EcsTagPool.cs | 12 +++++++ src/Utils/Exceptions.cs | 7 ++++ 5 files changed, 103 insertions(+), 13 deletions(-) diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 388dd88..0bbff40 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -157,6 +157,8 @@ namespace DCFApixels.DragonECS int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.EntitiesCapacity); _entityDispenser = new IdDispenser(entitiesCapacity, 0, OnEntityDispenserResized); + + GetComponentTypeID(); } public void Destroy() { diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index 5ec6e00..76cd630 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -166,6 +166,21 @@ namespace DCFApixels.DragonECS #endif CopyComponent(ref Get(fromEntityID), ref toWorld.GetPool().TryAddOrGet(toEntityID)); } + + public void ClearAll() + { + var span = _source.Where(out SingleAspect> _); + _itemsCount = 0; + _recycledItemsCount = 0; + foreach (var entityID in span) + { + ref int itemIndex = ref _mapping[entityID]; + DisableComponent(ref _items[itemIndex]); + itemIndex = 0; + _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); + _listeners.InvokeOnDel(entityID); + } + } #endregion #region Callbacks @@ -199,9 +214,15 @@ namespace DCFApixels.DragonECS #endregion #region Other - void IEcsPool.AddRaw(int entityID, object dataRaw) { Add(entityID) = (T)dataRaw; } + void IEcsPool.AddRaw(int entityID, object dataRaw) + { + Add(entityID) = dataRaw == null ? default : (T)dataRaw; + } object IEcsReadonlyPool.GetRaw(int entityID) { return Get(entityID); } - void IEcsPool.SetRaw(int entityID, object dataRaw) { Get(entityID) = (T)dataRaw; } + void IEcsPool.SetRaw(int entityID, object dataRaw) + { + Get(entityID) = dataRaw == null ? default : (T)dataRaw; + } #endregion #region Listeners diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index 4241d20..e5b9779 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -33,6 +33,7 @@ namespace DCFApixels.DragonECS void AddRaw(int entityID, object dataRaw); void SetRaw(int entityID, object dataRaw); void Del(int entityID); + void ClearAll(); #endregion } /// A pool for struct components. @@ -103,21 +104,68 @@ namespace DCFApixels.DragonECS public static readonly EcsNullPool instance = new EcsNullPool(); #region Properties - int IEcsReadonlyPool.ComponentTypeID => -1; - Type IEcsReadonlyPool.ComponentType => typeof(NullComponent); - EcsWorld IEcsReadonlyPool.World => throw new NotImplementedException(); - public int Count => -1; + int IEcsReadonlyPool.ComponentTypeID { get { return 0; } }//TODO Првоерить что NullComponent всегда имеет id 0 + Type IEcsReadonlyPool.ComponentType { get { return typeof(NullComponent); } } + EcsWorld IEcsReadonlyPool.World + { + get + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + } + public int Count { get { return 0; } } public bool IsReadOnly { get { return true; } } #endregion #region Methods - bool IEcsReadonlyPool.Has(int index) => false; - void IEcsPool.Del(int entityID) => throw new NotImplementedException(); - void IEcsPool.AddRaw(int entityID, object dataRaw) => throw new NotImplementedException(); - object IEcsReadonlyPool.GetRaw(int entityID) => throw new NotImplementedException(); - void IEcsPool.SetRaw(int entity, object dataRaw) => throw new NotImplementedException(); - void IEcsReadonlyPool.Copy(int fromEntityID, int toEntityID) => throw new NotImplementedException(); - void IEcsReadonlyPool.Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) => throw new NotImplementedException(); + bool IEcsReadonlyPool.Has(int index) + { + return false; + } + void IEcsPool.Del(int entityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsPool.AddRaw(int entityID, object dataRaw) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + object IEcsReadonlyPool.GetRaw(int entityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsPool.SetRaw(int entity, object dataRaw) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsReadonlyPool.Copy(int fromEntityID, int toEntityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsReadonlyPool.Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsPool.ClearAll() + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } #endregion #region Callbacks diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index 43e678f..3558791 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -145,6 +145,18 @@ namespace DCFApixels.DragonECS Add(entityID); } } + + public void ClearAll() + { + var span = _source.Where(out SingleAspect> _); + _count = 0; + foreach (var entityID in span) + { + _mapping[entityID] = false; + _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); + _listeners.InvokeOnDel(entityID); + } + } #endregion #region Callbacks diff --git a/src/Utils/Exceptions.cs b/src/Utils/Exceptions.cs index 10bf0c5..1697774 100644 --- a/src/Utils/Exceptions.cs +++ b/src/Utils/Exceptions.cs @@ -11,6 +11,13 @@ namespace DCFApixels.DragonECS public EcsFrameworkException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } } [Serializable] + public class NullInstanceException : EcsFrameworkException + { + public NullInstanceException() { } + public NullInstanceException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } + public NullInstanceException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } + } + [Serializable] public class EcsRunnerImplementationException : EcsFrameworkException { public EcsRunnerImplementationException() { }