From d64d4407c9d5552ec3a6f52574f2214dc025efb5 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 27 Feb 2023 18:16:23 +0800 Subject: [PATCH] Update WIP --- .../InjectProcessor.cs.meta} | 2 +- src/EcsFilter.cs | 228 +++++++++--------- src/EcsGroup.cs | 2 +- src/EcsPool.cs | 53 ++-- src/EcsSession.cs | 7 + src/EcsWorld.cs | 43 ++-- src/EcsWorldMap.cs | 33 --- .../EcsReactException.cs.meta} | 2 +- src/Exceptions/Exceptions.cs.meta | 11 + src/Interfaces/IEcsComponentReset.cs.meta | 11 + src/Primitives/Entity.cs | 5 - src/Utils/ComponentType.cs | 17 -- src/Utils/ComponentTypeMap.cs | 98 ++++++++ ...tType.cs.meta => ComponentTypeMap.cs.meta} | 0 src/Utils/CompressedBitMask.cs | 64 ----- test/SharedData.cs.meta | 11 + 16 files changed, 318 insertions(+), 269 deletions(-) rename src/{Utils/CompressedBitMask.cs.meta => Builtin/InjectProcessor.cs.meta} (83%) delete mode 100644 src/EcsWorldMap.cs rename src/{EcsWorldMap.cs.meta => Exceptions/EcsReactException.cs.meta} (83%) create mode 100644 src/Exceptions/Exceptions.cs.meta create mode 100644 src/Interfaces/IEcsComponentReset.cs.meta delete mode 100644 src/Utils/ComponentType.cs create mode 100644 src/Utils/ComponentTypeMap.cs rename src/Utils/{ComponentType.cs.meta => ComponentTypeMap.cs.meta} (100%) delete mode 100644 src/Utils/CompressedBitMask.cs create mode 100644 test/SharedData.cs.meta diff --git a/src/Utils/CompressedBitMask.cs.meta b/src/Builtin/InjectProcessor.cs.meta similarity index 83% rename from src/Utils/CompressedBitMask.cs.meta rename to src/Builtin/InjectProcessor.cs.meta index 64ef7e6..b62a95c 100644 --- a/src/Utils/CompressedBitMask.cs.meta +++ b/src/Builtin/InjectProcessor.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: f88c761abb374574fba2ac5aa2d5e41a +guid: 0404e70f4ccc54a41b3da9268ff287ad MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/src/EcsFilter.cs b/src/EcsFilter.cs index 5be21c1..401ee7e 100644 --- a/src/EcsFilter.cs +++ b/src/EcsFilter.cs @@ -19,7 +19,7 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID + ComponentType.globalID }; } } @@ -29,8 +29,8 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID }; } } @@ -40,9 +40,9 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -52,10 +52,10 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -65,11 +65,11 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -79,12 +79,12 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -94,13 +94,13 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -110,14 +110,14 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -127,15 +127,15 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -145,16 +145,16 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -164,17 +164,17 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -184,18 +184,18 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -210,7 +210,7 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID + ComponentType.globalID }; } } @@ -220,8 +220,8 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID }; } } @@ -231,9 +231,9 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -243,10 +243,10 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -256,11 +256,11 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -270,12 +270,12 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -285,13 +285,13 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } @@ -301,14 +301,14 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID, - ComponentType.uniqueID + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID, + ComponentType.globalID }; } } diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index 51f48e7..30bfe66 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -15,7 +15,7 @@ namespace DCFApixels.DragonECS public void Remove(int entityID); } - public class EcsGroup : IEcsGroup + public class EcsGroup : IEcsGroup { private EcsWorld _source; private SparseSet _entities; diff --git a/src/EcsPool.cs b/src/EcsPool.cs index 70e5eba..f7f73ec 100644 --- a/src/EcsPool.cs +++ b/src/EcsPool.cs @@ -13,7 +13,7 @@ namespace DCFApixels.DragonECS public EcsWorld World { get; } public int ID { get; } public bool Has(int index); - public void Add(int index); + public void Write(int index); public void Del(int index); } @@ -49,33 +49,34 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref T Write(int index) { - return ref _denseItems[_sparseSet[index]]; + if (_sparseSet.Contains(index)) + { + return ref _denseItems[_sparseSet[index]]; + } + else + { + _sparseSet.Add(index); + _sparseSet.Normalize(ref _denseItems); + return ref _denseItems[_sparseSet.IndexOf(index)]; + } } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref T Add(int index) - { - _sparseSet.Add(index); - _sparseSet.Normalize(ref _denseItems); - return ref _denseItems[_sparseSet.IndexOf(index)]; - } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Has(int index) { - return _sparseSet.Contains(index); + return _sparseSet.IndexOf(index) > 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Del(int index) { - _sparseSet.Remove(index); + _sparseSet.RemoveAt(index); } #endregion #region IEcsFieldPool - void IEcsPool.Add(int index) + void IEcsPool.Write(int index) { - Add(index); + Write(index); } #endregion @@ -87,4 +88,28 @@ namespace DCFApixels.DragonECS public override int GetHashCode() => _source.GetHashCode() + ID; #endregion } + + public static partial class EntityExtensions + { + public static ref readonly T Read(this in Entity self) + where T : struct + { + return ref self.world.GetPool().Read(self.id); + } + public static ref T Write(this in Entity self) + where T : struct + { + return ref self.world.GetPool().Write(self.id); + } + public static bool Has(this in Entity self) + where T : struct + { + return self.world.GetPool().Has(self.id); + } + public static void Del(this in Entity self) + where T : struct + { + self.world.GetPool().Del(self.id); + } + } } diff --git a/src/EcsSession.cs b/src/EcsSession.cs index 89f6d3f..ca29b1f 100644 --- a/src/EcsSession.cs +++ b/src/EcsSession.cs @@ -9,6 +9,9 @@ namespace DCFApixels.DragonECS public class EcsSession { + private int _id; + + private List _allProcessors; private ReadOnlyCollection _allProcessorsSealed; @@ -151,5 +154,9 @@ namespace DCFApixels.DragonECS // return new ent(target.id, target.world._gens[target.id], -1000); } #endregion + + + + } } diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index f6456d1..c743e53 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -7,6 +7,11 @@ using System.Threading.Tasks; namespace DCFApixels.DragonECS { + + public class EcsWorldMap + { + + } public class EcsWorld { public const int MAX_WORLDS = byte.MaxValue; //Номер последнего мира 254 @@ -14,7 +19,7 @@ namespace DCFApixels.DragonECS private byte _id = DEAD_WORLD_ID; - private float _timeScale;//TODO реализовать собсвенныйтайм склей для разных миров + //private float _timeScale;//TODO реализовать собсвенныйтайм склей для разных миров private IEcsPool[] _pools; private SparseSet _componentIDToPoolID; @@ -45,27 +50,11 @@ namespace DCFApixels.DragonECS } #endregion - #region Filters - public EcsFilter GetFilter(TMask mask) where TMask : Mask - { - if (_maskIDToFilterID.TryAdd(mask.ID, ref _filters)) - { - EcsFilter filter = new EcsFilter(this, mask, 512); - _filters[_maskIDToFilterID.IndexOf(mask.ID)] = filter; - return filter; - } - else - { - return _filters[_maskIDToFilterID.IndexOf(mask.ID)]; - } - } - #endregion - - #region GetPool + #region Pools public EcsPool GetPool() where T : struct { - int uniqueID = ComponentType.uniqueID; + int uniqueID = ComponentType.globalID; int poolIndex = _componentIDToPoolID.IndexOf(uniqueID); if (poolIndex >= 0) { @@ -88,6 +77,22 @@ namespace DCFApixels.DragonECS } #endregion + #region Filters + public EcsFilter GetFilter(TMask mask) where TMask : Mask + { + if (_maskIDToFilterID.TryAdd(mask.ID, ref _filters)) + { + EcsFilter filter = new EcsFilter(this, mask, 512); + _filters[_maskIDToFilterID.IndexOf(mask.ID)] = filter; + return filter; + } + else + { + return _filters[_maskIDToFilterID.IndexOf(mask.ID)]; + } + } + #endregion + #region NewEntity public ent NewEntity() { diff --git a/src/EcsWorldMap.cs b/src/EcsWorldMap.cs deleted file mode 100644 index 3f6791d..0000000 --- a/src/EcsWorldMap.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; - -namespace DCFApixels.DragonECS -{ - public class EcsWorldMap - { - private EcsWorld[] _worlds = new EcsWorld[8]; - private SparseSet _sparceSet = new SparseSet(8); - - public EcsWorld this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => _worlds[index]; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void InsertWorld(EcsWorld world) - { - - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void RemoveWorld(EcsWorld world) - { - - } - } -} diff --git a/src/EcsWorldMap.cs.meta b/src/Exceptions/EcsReactException.cs.meta similarity index 83% rename from src/EcsWorldMap.cs.meta rename to src/Exceptions/EcsReactException.cs.meta index 876fc75..f01411d 100644 --- a/src/EcsWorldMap.cs.meta +++ b/src/Exceptions/EcsReactException.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: c53ca292f39cf9d48a0bb60a65fc1232 +guid: b4766bca23373eb4cb95ecae89f45c3f MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/src/Exceptions/Exceptions.cs.meta b/src/Exceptions/Exceptions.cs.meta new file mode 100644 index 0000000..6870be6 --- /dev/null +++ b/src/Exceptions/Exceptions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f04087406e09f6042a341cf8fc41fabf +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Interfaces/IEcsComponentReset.cs.meta b/src/Interfaces/IEcsComponentReset.cs.meta new file mode 100644 index 0000000..8812d7a --- /dev/null +++ b/src/Interfaces/IEcsComponentReset.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8e4f5b9d0612fde4997b44362501d498 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Primitives/Entity.cs b/src/Primitives/Entity.cs index da53850..3a3ef35 100644 --- a/src/Primitives/Entity.cs +++ b/src/Primitives/Entity.cs @@ -110,11 +110,6 @@ namespace DCFApixels.DragonECS { return self == ent.NULL; } - - public static Entity UseIn(in ent self, EcsSession session) - { - session.GetWorld() - } } public ref struct Entity diff --git a/src/Utils/ComponentType.cs b/src/Utils/ComponentType.cs deleted file mode 100644 index ba5270b..0000000 --- a/src/Utils/ComponentType.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DCFApixels.DragonECS -{ - internal abstract class ComponentType - { - protected static int _increment = 0; - } - internal sealed class ComponentType : ComponentType - { - internal static int uniqueID = _increment++; - } -} diff --git a/src/Utils/ComponentTypeMap.cs b/src/Utils/ComponentTypeMap.cs new file mode 100644 index 0000000..39c5ef3 --- /dev/null +++ b/src/Utils/ComponentTypeMap.cs @@ -0,0 +1,98 @@ +using System; +using System.Runtime.CompilerServices; + +namespace DCFApixels.DragonECS +{ + internal abstract class ComponentType + { + internal static int increment = 1; + internal static int capacity = 512; + } + internal sealed class ComponentType : ComponentType + { + internal static int globalID; + + static ComponentType() + { + globalID = increment++; + if (increment > capacity) + { + capacity <<= 1; + } + } + } + + public class ComponentTypeMap + { + private int[] _dense; + private int[] _sparse; + + private int _count; + + #region Properties + public int Count + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _count; + } + #endregion + + #region Constrcutors + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ComponentTypeMap(int denseCapacity = 64) + { + _dense = new int[denseCapacity]; + _sparse = new int[ComponentType.capacity]; + + _count = 0; + } + #endregion + + #region Contains + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Contains() => Contains(ComponentType.globalID); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private bool Contains(int globalID) + { + return globalID > 0 && globalID < _sparse.Length && _sparse[globalID] > 0; + } + #endregion + + #region GetID + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int GetID() + { + int globalID = ComponentType.globalID; + + if (!Contains(globalID)) + { + Add(globalID); + } + + return _dense[globalID]; + } + #endregion + + #region Add + private void Add(int entityID) + { + if (Contains(entityID)) + return; + + if (++_count >= _dense.Length) + Array.Resize(ref _dense, _dense.Length << 1); + + if (entityID > _sparse.Length) + { + int neadedSpace = _sparse.Length; + while (entityID >= neadedSpace) + neadedSpace <<= 1; + Array.Resize(ref _sparse, neadedSpace); + } + + _dense[_count] = entityID; + _sparse[entityID] = _count; + } + #endregion + } +} diff --git a/src/Utils/ComponentType.cs.meta b/src/Utils/ComponentTypeMap.cs.meta similarity index 100% rename from src/Utils/ComponentType.cs.meta rename to src/Utils/ComponentTypeMap.cs.meta diff --git a/src/Utils/CompressedBitMask.cs b/src/Utils/CompressedBitMask.cs deleted file mode 100644 index f6bbfb2..0000000 --- a/src/Utils/CompressedBitMask.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DCFApixels.DragonECS -{ - public class CompressedBitMask - { - private const int CHUNK = 32; //int bits - - private int[] _fullChunkIndexes; // индексы чанков с полным заполнением - private int[] _sparseIndexes; - private int[] _denseMasks; - - private int _highBit = 0; - - public int HightBit => _highBit; - - public void Set(int[] indexes, int count) - { - - } - - public ref struct EqualsRequest - { - private CompressedBitMask _source; - private CompressedBitMask _other; - public void GetEnumerator() => - } - public ref struct Enumerator - { - private readonly int[] _indexes; - private readonly int[] _masks; - private int _index; - - public Enumerator(int[] indexes, int[] masks) - - { - _indexes = indexes; - _masks = masks; - _index = -1; - } - - public int Current - { - get => 0; - } - - public void Dispose() { } - - public bool MoveNext() - { - } - - public void Reset() - { - _index = -1; - } - } - } -} diff --git a/test/SharedData.cs.meta b/test/SharedData.cs.meta new file mode 100644 index 0000000..f302dab --- /dev/null +++ b/test/SharedData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ea74209e58692b3428f531e27d577cd7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: