From 55f68434517f0804f2161d7e46bd442c481b64aa Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 26 May 2023 06:37:15 +0800 Subject: [PATCH] moving part of the API into a separate module --- src/Builtin/Components.cs | 72 ----------- src/Builtin/Subjects.cs | 11 -- src/Pools/EcsAttachPool.cs | 224 ---------------------------------- src/Pools/EcsPool.cs | 2 +- src/Pools/EcsTagPool.cs | 2 +- src/Utils/EntityLinkedList.cs | 6 +- 6 files changed, 5 insertions(+), 312 deletions(-) delete mode 100644 src/Builtin/Components.cs delete mode 100644 src/Builtin/Subjects.cs delete mode 100644 src/Pools/EcsAttachPool.cs diff --git a/src/Builtin/Components.cs b/src/Builtin/Components.cs deleted file mode 100644 index 6cea199..0000000 --- a/src/Builtin/Components.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System.ComponentModel; -using System.Runtime.CompilerServices; - -namespace DCFApixels.DragonECS -{ - [DebugColor(DebugColor.White)] - public struct Parent : IEcsAttachComponent - { - public entlong entity; - - [EditorBrowsable(EditorBrowsableState.Never)] - public entlong Target - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => entity; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => entity = value; - } - } - - public static class ParentUtility - { - // public static int GetRootOrSelf(this HierarchySubject s, int entityID) => s.parents.GetRootOrSelf(entityID); - public static int GetRootOrSelf(this EcsAttachPool parents, int entityID) - { - while (parents.Has(entityID) && parents.Read(entityID).entity.TryGetID(out int child)) - entityID = child; - return entityID; - } - // public static bool IsRoot(this HierarchySubject s, int entityID) => s.parents.IsRoot(entityID); - public static bool IsRoot(this EcsAttachPool parents, int entityID) - { - return !(parents.Has(entityID) && parents.Read(entityID).entity.IsAlive); - } - - // public static bool TryGetRoot(this HierarchySubject s, int entityID, out int rootEntityID) => TryGetRoot(s.parents, entityID, out rootEntityID); - public static bool TryGetRoot(this EcsAttachPool parents, EcsSubject conditionSubject, int entityID, out int rootEntityID) - { - rootEntityID = entityID; - while (parents.Has(rootEntityID) && parents.Read(rootEntityID).entity.TryGetID(out int child) && !conditionSubject.IsMatches(child)) - rootEntityID = child; - return rootEntityID != entityID; - } - public static bool TryGetRoot(this EcsAttachPool parents, int entityID, out int rootEntityID) - { - rootEntityID = entityID; - while (parents.Has(rootEntityID) && parents.Read(rootEntityID).entity.TryGetID(out int child)) - rootEntityID = child; - return rootEntityID != entityID; - } - - public static bool TryFindParentWithSubject(this EcsAttachPool parents, EcsSubject conditionSubject, int entityID, out int resultEntityID) - { - resultEntityID = entityID; - while (parents.Has(resultEntityID) && parents.Read(resultEntityID).entity.TryGetID(out int child) && !conditionSubject.IsMatches(resultEntityID)) - resultEntityID = child; - return conditionSubject.IsMatches(resultEntityID); - } - public static bool TryFindParentWith(this EcsAttachPool parents, int entityID, out int resultEntityID) where TComponent : struct - { - var pool = parents.World.AllPools[parents.World.GetComponentID()]; - resultEntityID = entityID; - while (!pool.Has(resultEntityID) && - parents.Has(resultEntityID) && - parents.Read(resultEntityID).entity.TryGetID(out int child)) - { - resultEntityID = child; - } - return pool.Has(resultEntityID); - } - } -} diff --git a/src/Builtin/Subjects.cs b/src/Builtin/Subjects.cs deleted file mode 100644 index 8c3b369..0000000 --- a/src/Builtin/Subjects.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace DCFApixels.DragonECS -{ - public sealed class HierarchySubject : EcsSubject - { - public readonly EcsAttachPool parents; - public HierarchySubject(Builder b) - { - parents = b.Include(); - } - } -} diff --git a/src/Pools/EcsAttachPool.cs b/src/Pools/EcsAttachPool.cs deleted file mode 100644 index 3348174..0000000 --- a/src/Pools/EcsAttachPool.cs +++ /dev/null @@ -1,224 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Runtime.CompilerServices; - -namespace DCFApixels.DragonECS -{ - using static EcsPoolThrowHalper; - //íå âëèÿåò íà ñ÷åò÷èê êîìïîíåíòîâ íà ñóùíîñòè - /// Pool for IEcsAttachComponent components - public sealed class EcsAttachPool : IEcsPoolImplementation, IEnumerable //IntelliSense hack - where T : struct, IEcsAttachComponent - { - private EcsWorld _source; - private int _id; - - private bool[] _entityFlags;// index = entityID / value = entityFlag;/ value = 0 = no entityID - private T[] _items; //sparse - private int _count; - - private List _listeners; - - private EcsGroup _entities; - public EcsReadonlyGroup Entities - { - get - { - _entities.RemoveUnusedEntityIDs(); - return _entities.Readonly; - } - } - -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - private short _sanitizeTargetWorld = -1; -#endif - - #region Properites - public int Count => _count; - public int Capacity => _items.Length; - public int ComponentID => _id; - public Type ComponentType => typeof(T); - public EcsWorld World => _source; - #endregion - - #region Init - void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID) - { - _source = world; - _id = componentID; - - _listeners = new List(); - - _entities = EcsGroup.New(world); - - _entityFlags = new bool[world.Capacity]; - _items = new T[world.Capacity]; - _count = 0; - } - #endregion - - #region Methods - public void Add(int entityID, entlong target) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (_sanitizeTargetWorld > 0 && target.world != _sanitizeTargetWorld) ThrowWorldDifferent(entityID); - _sanitizeTargetWorld = target.world; - if (Has(entityID)) ThrowAlreadyHasComponent(entityID); -#endif - ref bool entityFlag = ref _entityFlags[entityID]; - if (entityFlag == false) - { - entityFlag = true; - _count++; - _entities.Add(entityID); - foreach (var item in _listeners) item.OnAdd(entityID); - } - foreach (var item in _listeners) item.OnWrite(entityID); - _items[entityID].Target = target; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Set(int entityID, entlong target) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); - if (_sanitizeTargetWorld >= 0 && target.world != _sanitizeTargetWorld) ThrowWorldDifferent(entityID); - _sanitizeTargetWorld = target.world; -#endif - _listeners.InvokeOnWrite(entityID); - _items[entityID].Target = target; - } - public void AddOrSet(int entityID, entlong target) - { - if (Has(entityID)) - Set(entityID, target); - else - Add(entityID, target); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref readonly T Read(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); -#endif - return ref _items[entityID]; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Has(int entityID) - { - return _entityFlags[entityID]; - } - public void Del(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); -#endif - _entities.Remove(entityID); - _entityFlags[entityID] = false; - _count--; - _listeners.InvokeOnDel(entityID); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void TryDel(int entityID) - { - if (Has(entityID)) Del(entityID); - } - public void Copy(int fromEntityID, int toEntityID) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) ThrowNotHaveComponent(fromEntityID); -#endif - if (Has(toEntityID)) - Set(toEntityID, Read(fromEntityID).Target); - else - Add(toEntityID, Read(fromEntityID).Target); - } - public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) ThrowNotHaveComponent(fromEntityID); -#endif - if (Has(toEntityID)) - toWorld.GetPool().Set(toEntityID, Read(fromEntityID).Target); - else - toWorld.GetPool().Add(toEntityID, Read(fromEntityID).Target); - } - #endregion - - #region WorldCallbacks - void IEcsPoolImplementation.OnWorldResize(int newSize) - { - Array.Resize(ref _entityFlags, newSize); - Array.Resize(ref _items, newSize); - } - void IEcsPoolImplementation.OnWorldDestroy() { } - void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) - { - foreach (var item in buffer) - TryDel(item); - } - #endregion - - #region Other - ref T IEcsPool.Add(int entityID) - { - if (!Has(entityID)) - Add(entityID, entlong.NULL); - return ref _items[entityID]; - } - ref T IEcsPool.Write(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); -#endif - return ref _items[entityID]; - } - void IEcsPool.AddRaw(int entityID, object dataRaw) => ((IEcsPool)this).Add(entityID) = (T)dataRaw; - object IEcsPool.GetRaw(int entityID) => Read(entityID); - void IEcsPool.SetRaw(int entityID, object dataRaw) => ((IEcsPool)this).Write(entityID) = (T)dataRaw; - #endregion - - #region Listeners - public void AddListener(IEcsPoolEventListener listener) - { - if (listener == null) { throw new ArgumentNullException("listener is null"); } - _listeners.Add(listener); - } - public void RemoveListener(IEcsPoolEventListener listener) - { - if (listener == null) { throw new ArgumentNullException("listener is null"); } - _listeners.Remove(listener); - } - #endregion - - #region IEnumerator - IntelliSense hack - IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); - IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); - #endregion - } - - public interface IEcsAttachComponent - { - public entlong Target { get; set; } - } - public static class EcsAttachComponentPoolExt - { - public static EcsAttachPool GetPool(this EcsWorld self) where TAttachComponent : struct, IEcsAttachComponent - { - return self.GetPool>(); - } - - public static EcsAttachPool Include(this EcsSubjectBuilderBase self) where TAttachComponent : struct, IEcsAttachComponent - { - return self.Include>(); - } - public static EcsAttachPool Exclude(this EcsSubjectBuilderBase self) where TAttachComponent : struct, IEcsAttachComponent - { - return self.Exclude>(); - } - public static EcsAttachPool Optional(this EcsSubjectBuilderBase self) where TAttachComponent : struct, IEcsAttachComponent - { - return self.Optional>(); - } - } -} diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index 23bbbd8..366af85 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -7,7 +7,7 @@ namespace DCFApixels.DragonECS { using static EcsPoolThrowHalper; /// Pool for IEcsComponent components - public sealed class EcsPool : IEcsPoolImplementation, IEnumerable //IntelliSense hack + public sealed class EcsPool : IEcsPoolImplementation, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsComponent { private EcsWorld _source; diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index bf36ebb..0f3cf8b 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -6,7 +6,7 @@ using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { using static EcsPoolThrowHalper; - public sealed class EcsTagPool : IEcsPoolImplementation, IEnumerable //IntelliSense hack + public sealed class EcsTagPool : IEcsPoolImplementation, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsTagComponent { private EcsWorld _source; diff --git a/src/Utils/EntityLinkedList.cs b/src/Utils/EntityLinkedList.cs index 7ff22ec..bafe85f 100644 --- a/src/Utils/EntityLinkedList.cs +++ b/src/Utils/EntityLinkedList.cs @@ -5,7 +5,7 @@ namespace DCFApixels.DragonECS { public class EntityLinkedList { - public const int Enter = 0; + private const int ENTER = 0; private Node[] _nodes; private int _count; @@ -35,7 +35,7 @@ namespace DCFApixels.DragonECS //ArrayUtility.Fill(_nodes, Node.Empty); for (int i = 0; i < _nodes.Length; i++) _nodes[i].next = 0; - _lastNodeIndex = Enter; + _lastNodeIndex = ENTER; _count = 0; } @@ -82,7 +82,7 @@ namespace DCFApixels.DragonECS { _nodes = nodes; _index = -1; - _next = Enter; + _next = ENTER; } public int Current => _nodes[_index].entityID; public bool MoveNext()