From 1a9a2e879ece26bee5cccafb1f71d37a9afaa6a5 Mon Sep 17 00:00:00 2001 From: sebas77 Date: Sat, 23 Feb 2019 15:35:47 +0000 Subject: [PATCH] Getting closer to the final design of the EntityStreams Added the concept of QueryUniqueEntity, but don't rely on it, I am still making up my mind on it. If I keep it, it will be confined to EntityStructs only Added more ECS common components --- Svelto.Common | 2 +- Svelto.ECS/Common/Components/ECSVector2.cs | 3 +- Svelto.ECS/Common/Components/ECSVector3.cs | 14 +++ Svelto.ECS/Common/Components/EcsVector4.cs | 15 ++++ .../Common/Components/ExtensionMethods.cs | 36 ++++++++ .../InterpolateVector3EntityStruct.cs | 12 +++ .../EntityStructs/PositionEntityStruct.cs | 4 + .../EntityStructs/RotationEntityStruct.cs | 4 + .../DataStructures/TypeSafeDictionary.cs | 15 ++-- Svelto.ECS/EnginesRoot.Engines.cs | 2 +- Svelto.ECS/EnginesRoot.Entities.cs | 23 ++++- .../EnginesRoot.GenerateEntitiesStream.cs | 25 ++++++ .../EnginesRoot.GenericEntityFactory.cs | 4 +- .../EnginesRoot.GenericEntityFunctions.cs | 15 ++++ Svelto.ECS/EnginesRoot.Submission.cs | 46 +++++----- Svelto.ECS/EntitiesDB.cs | 14 +++ Svelto.ECS/EntityBuilder.cs | 2 +- Svelto.ECS/EntityStream.cs | 88 +++++++++++-------- Svelto.ECS/EntitySubmitOperation.cs | 32 +++---- .../{ExclusiveGroups.cs => ExclusiveGroup.cs} | 0 .../Unity/GenericEntityDescriptorHolder.cs | 2 + Svelto.ECS/IEntitiesDB.cs | 5 +- Svelto.ECS/IEntityFunctions.cs | 3 + 23 files changed, 269 insertions(+), 97 deletions(-) create mode 100644 Svelto.ECS/Common/Components/ECSVector3.cs create mode 100644 Svelto.ECS/Common/Components/EcsVector4.cs create mode 100644 Svelto.ECS/Common/Components/ExtensionMethods.cs create mode 100644 Svelto.ECS/Common/EntityStructs/InterpolateVector3EntityStruct.cs create mode 100644 Svelto.ECS/EnginesRoot.GenerateEntitiesStream.cs rename Svelto.ECS/{ExclusiveGroups.cs => ExclusiveGroup.cs} (100%) diff --git a/Svelto.Common b/Svelto.Common index b6ff2a5..749ff28 160000 --- a/Svelto.Common +++ b/Svelto.Common @@ -1 +1 @@ -Subproject commit b6ff2a5f1357be35efdd86ae5ea5d1d6be4f68b8 +Subproject commit 749ff282c004818f1d321a0432bba69065c412ae diff --git a/Svelto.ECS/Common/Components/ECSVector2.cs b/Svelto.ECS/Common/Components/ECSVector2.cs index 15c1748..e5bbf1e 100644 --- a/Svelto.ECS/Common/Components/ECSVector2.cs +++ b/Svelto.ECS/Common/Components/ECSVector2.cs @@ -2,8 +2,7 @@ namespace Svelto.ECS.Components { public struct ECSVector2 { - public float x; - public float y; + public float x, y; public ECSVector2(float X, float Y) { diff --git a/Svelto.ECS/Common/Components/ECSVector3.cs b/Svelto.ECS/Common/Components/ECSVector3.cs new file mode 100644 index 0000000..685b10d --- /dev/null +++ b/Svelto.ECS/Common/Components/ECSVector3.cs @@ -0,0 +1,14 @@ +namespace Svelto.ECS.Components +{ + public struct ECSVector3 + { + public float x, y, z; + + public ECSVector3(float X, float Y, float Z) + { + x = X; + y = Y; + z = Z; + } + } +} \ No newline at end of file diff --git a/Svelto.ECS/Common/Components/EcsVector4.cs b/Svelto.ECS/Common/Components/EcsVector4.cs new file mode 100644 index 0000000..a5f1c1e --- /dev/null +++ b/Svelto.ECS/Common/Components/EcsVector4.cs @@ -0,0 +1,15 @@ +namespace Svelto.ECS.Components +{ + public struct EcsVector4 + { + public float x, y, z, w; + + public EcsVector4(float X, float Y, float Z, float W) + { + x = X; + y = Y; + z = Z; + w = W; + } + } +} \ No newline at end of file diff --git a/Svelto.ECS/Common/Components/ExtensionMethods.cs b/Svelto.ECS/Common/Components/ExtensionMethods.cs new file mode 100644 index 0000000..2f2b60c --- /dev/null +++ b/Svelto.ECS/Common/Components/ExtensionMethods.cs @@ -0,0 +1,36 @@ +#if UNITY_2018_3_OR_NEWER +using UnityEngine; + +namespace Svelto.ECS.Components.Unity +{ + namespace Svelto.ECS.Components + { + public static class ExtensionMethods + { + public static Vector3 ToVector3(ref this ECSVector3 vector) { return new Vector3(vector.x, vector.y, vector.z); } + + public static void Add(ref this ECSVector3 vector1, ref ECSVector3 vector2) + { + vector1.x += vector2.x; + vector1.y += vector2.y; + vector1.z += vector2.z; + } + + public static void Add(ref this ECSVector3 vector1, float x, float y, float z) + { + vector1.x += x; + vector1.y += y; + vector1.z += z; + } + + public static void Interpolate(ref this ECSVector3 vector, ref ECSVector3 vectorS, + ref ECSVector3 vectorE, float time) + { + vector.x = vectorS.x * (1 - time) + vectorE.x * (time); + vector.y = vectorS.y * (1 - time) + vectorE.y * (time); + vector.z = vectorS.z * (1 - time) + vectorE.z * (time); + } + } + } +} +#endif \ No newline at end of file diff --git a/Svelto.ECS/Common/EntityStructs/InterpolateVector3EntityStruct.cs b/Svelto.ECS/Common/EntityStructs/InterpolateVector3EntityStruct.cs new file mode 100644 index 0000000..d91a478 --- /dev/null +++ b/Svelto.ECS/Common/EntityStructs/InterpolateVector3EntityStruct.cs @@ -0,0 +1,12 @@ +using Svelto.ECS.Components; + +namespace Svelto.ECS.EntityStructs +{ + public struct InterpolateVector3EntityStruct : IEntityStruct + { + public ECSVector3 starPos, endPos; + public float time; + + public EGID ID { get; set; } + } +} \ No newline at end of file diff --git a/Svelto.ECS/Common/EntityStructs/PositionEntityStruct.cs b/Svelto.ECS/Common/EntityStructs/PositionEntityStruct.cs index 30170f0..d0afb46 100644 --- a/Svelto.ECS/Common/EntityStructs/PositionEntityStruct.cs +++ b/Svelto.ECS/Common/EntityStructs/PositionEntityStruct.cs @@ -1,7 +1,11 @@ +using Svelto.ECS.Components; + namespace Svelto.ECS.EntityStructs { public struct PositionEntityStruct : IEntityStruct { + public ECSVector3 position; + public EGID ID { get; set; } } } \ No newline at end of file diff --git a/Svelto.ECS/Common/EntityStructs/RotationEntityStruct.cs b/Svelto.ECS/Common/EntityStructs/RotationEntityStruct.cs index 22bdcd1..d3e2932 100644 --- a/Svelto.ECS/Common/EntityStructs/RotationEntityStruct.cs +++ b/Svelto.ECS/Common/EntityStructs/RotationEntityStruct.cs @@ -1,7 +1,11 @@ +using Svelto.ECS.Components; + namespace Svelto.ECS.EntityStructs { public struct RotationEntityStruct : IEntityStruct { + public EcsVector4 rotation; + public EGID ID { get; set; } } } \ No newline at end of file diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index 5469a96..6579afc 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -63,15 +63,14 @@ namespace Svelto.ECS.Internal Dictionary> entityViewEnginesDB, ref PlatformProfiler profiler) { - int count; - TValue[] values = GetValuesArray(out count); - + var values = GetValuesArray(out var count); + + //pay attention: even if the entity is passed by ref, it won't be saved back in the database when this + //function is called from the building of an entity. This is by design. Entity structs must be initialized + //through the EntityInitializer method and not with an Add callback. + //however the struct can be modified during an add callback if this happens as consequence of a group swap for (int i = 0; i < count; i++) - { - TValue entity = values[i]; - - AddEntityViewToEngines(entityViewEnginesDB, ref entity, ref profiler); - } + AddEntityViewToEngines(entityViewEnginesDB, ref values[i], ref profiler); } public bool Has(int entityIdEntityId) diff --git a/Svelto.ECS/EnginesRoot.Engines.cs b/Svelto.ECS/EnginesRoot.Engines.cs index ddef86e..ed19b8b 100644 --- a/Svelto.ECS/EnginesRoot.Engines.cs +++ b/Svelto.ECS/EnginesRoot.Engines.cs @@ -34,7 +34,7 @@ namespace Svelto.ECS _groupedEntityToAdd = new DoubleBufferedEntitiesToAdd>>(); _entitiesDB = new EntitiesDB(_groupEntityDB, _groupsPerEntity); - _entityStreams = new EntityStreams(_entitiesDB); + _entitiesStream = new EntitiesStream(_entitiesDB); _scheduler = entityViewScheduler; _scheduler.onTick = new WeakAction(SubmitEntityViews); diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 971ae80..25019aa 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -54,9 +54,9 @@ namespace Svelto.ECS ///-------------------------------------------- /// - public EntityStreams GenerateEntityStream() + public IEntitiesStream GenerateEntityStream() { - return _entityStreams; + return new GenericEntitiesStream(new DataStructures.WeakReference(this)); } public IEntityFactory GenerateEntityFactory() @@ -226,6 +226,23 @@ namespace Svelto.ECS } } + void RemoveGroupAndEntitiesFromDB(int groupID, Type entityDescriptor) + { + /* var profiler = new PlatformProfiler(); + using (profiler.StartNewSession("Remove Group Of Entities")) + { + FasterDictionary @group; + if (_groupsPerEntity.TryGetValue(entityDescriptor, out group)) + { + if (group.TryGetValue()) + foreach (var entity in group) + { + MoveEntity(entity.); + } + } + }*/ + } + void RemoveGroupAndEntitiesFromDB(int groupID) { var profiler = new PlatformProfiler(); @@ -260,7 +277,7 @@ namespace Svelto.ECS MoveEntity(builders, fromEntityID, originalEntityDescriptor, toEntityID, toGroup); } - readonly EntityStreams _entityStreams; + readonly EntitiesStream _entitiesStream; readonly Type _entityInfoView = typeof(EntityInfoView); const string INVALID_DYNAMIC_DESCRIPTOR_ERROR = "Found an entity requesting an invalid dynamic descriptor, this " + diff --git a/Svelto.ECS/EnginesRoot.GenerateEntitiesStream.cs b/Svelto.ECS/EnginesRoot.GenerateEntitiesStream.cs new file mode 100644 index 0000000..ede7b15 --- /dev/null +++ b/Svelto.ECS/EnginesRoot.GenerateEntitiesStream.cs @@ -0,0 +1,25 @@ +namespace Svelto.ECS +{ + public partial class EnginesRoot + { + class GenericEntitiesStream : IEntitiesStream + { + public GenericEntitiesStream(DataStructures.WeakReference weakReference) + { + _weakEngine = weakReference; + } + + public Consumer GenerateConsumer(int capacity) where T : unmanaged, IEntityStruct + { + return _weakEngine.Target._entitiesStream.GenerateConsumer(capacity); + } + + public void PublishEntity(EGID id) where T : unmanaged, IEntityStruct + { + _weakEngine.Target._entitiesStream.PublishEntity(id); + } + + readonly DataStructures.WeakReference _weakEngine; + } + } +} \ No newline at end of file diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs b/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs index 73b7a93..1c9fd1a 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs @@ -4,8 +4,6 @@ { class GenericEntityFactory : IEntityFactory { - readonly DataStructures.WeakReference _weakEngine; - public GenericEntityFactory(DataStructures.WeakReference weakReference) { _weakEngine = weakReference; @@ -35,6 +33,8 @@ { _weakEngine.Target.Preallocate(groupStructId, size); } + + readonly DataStructures.WeakReference _weakEngine; } } } \ No newline at end of file diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs index 101519c..baff736 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs @@ -35,6 +35,21 @@ namespace Svelto.ECS -1, EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); } + public void RemoveEntities(int groupID) where T : IEntityDescriptor, new() + { + throw new NotImplementedException(); + //_weakReference.Target.QueueEntitySubmitOperation( +// new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, -1, groupID, -1, null, typeof(T))); + } + + public void RemoveEntities(ExclusiveGroup.ExclusiveGroupStruct groupID) + where T : IEntityDescriptor, new() + { + throw new NotImplementedException(); + //_weakReference.Target.QueueEntitySubmitOperation( + // new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, -1, groupID, -1, null, typeof(T))); + } + public void RemoveGroupAndEntities(int groupID) { _weakReference.Target.QueueEntitySubmitOperation( diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index 5b9cae2..82918b3 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -25,7 +25,7 @@ namespace Svelto.ECS _transientEntitiesOperations.FastClear(); _transientEntitiesOperations.AddRange(_entitiesOperations); _entitiesOperations.FastClear(); - + var entitiesOperations = _transientEntitiesOperations.ToArrayFast(); for (var i = 0; i < _transientEntitiesOperations.Count; i++) { @@ -39,7 +39,7 @@ namespace Svelto.ECS new EGID(entitiesOperations[i].ID, entitiesOperations[i].fromGroupID), new EGID(entitiesOperations[i].toID, - entitiesOperations[i].toGroupID)); + entitiesOperations[i].toGroupID)); break; case EntitySubmitOperationType.Remove: MoveEntity(entitiesOperations[i].builders, @@ -48,7 +48,12 @@ namespace Svelto.ECS entitiesOperations[i].entityDescriptor, new EGID()); break; case EntitySubmitOperationType.RemoveGroup: - RemoveGroupAndEntitiesFromDB(entitiesOperations[i].fromGroupID); + if (entitiesOperations[i].entityDescriptor == null) + RemoveGroupAndEntitiesFromDB(entitiesOperations[i].fromGroupID); + else + RemoveGroupAndEntitiesFromDB(entitiesOperations[i].fromGroupID, + entitiesOperations[i].entityDescriptor); + break; } } @@ -56,20 +61,16 @@ namespace Svelto.ECS { #if DEBUG && !PROFILER var str = "Crash while executing Entity Operation" - .FastConcat(entitiesOperations[i].type.ToString()) - .FastConcat(" id: ") - .FastConcat(entitiesOperations[i].ID) - .FastConcat(" to id: ") - .FastConcat(entitiesOperations[i].toID) - .FastConcat(" from groupid: ") - .FastConcat(entitiesOperations[i].fromGroupID) - .FastConcat(" to groupid: ") - .FastConcat(entitiesOperations[i].toGroupID); + .FastConcat(entitiesOperations[i].type.ToString()).FastConcat(" id: ") + .FastConcat(entitiesOperations[i].ID).FastConcat(" to id: ") + .FastConcat(entitiesOperations[i].toID).FastConcat(" from groupid: ") + .FastConcat(entitiesOperations[i].fromGroupID).FastConcat(" to groupid: ") + .FastConcat(entitiesOperations[i].toGroupID); #if RELAXED_ECS Console.LogException(str.FastConcat(" ", entitiesOperations[i].trace), e); -#else +#else throw new ECSException(str.FastConcat(" ").FastConcat(entitiesOperations[i].trace), e); -#endif +#endif #else var str = "Entity Operation is ".FastConcat(entitiesOperations[i].type.ToString()) .FastConcat(" id: ") @@ -87,7 +88,7 @@ namespace Svelto.ECS } } } - + if (_groupedEntityToAdd.current.Count > 0) { using (profiler.Sample("Add operations")) @@ -112,15 +113,16 @@ namespace Svelto.ECS finally { //other can be cleared now, but let's avoid deleting the dictionary every time - _groupedEntityToAdd.ClearOther(); + _groupedEntityToAdd.ClearOther(); } } } } } - - void AddEntityViewsToTheDBAndSuitableEngines( - FasterDictionary> groupsOfEntitiesToSubmit, PlatformProfiler profiler) + + void AddEntityViewsToTheDBAndSuitableEngines( + FasterDictionary> groupsOfEntitiesToSubmit, + PlatformProfiler profiler) { //each group is indexed by entity view type. for each type there is a dictionary indexed by entityID foreach (var groupOfEntitiesToSubmit in groupsOfEntitiesToSubmit) @@ -139,10 +141,10 @@ namespace Svelto.ECS FasterDictionary groupedGroup = null; if (groupDB.TryGetValue(entityViewTypeSafeDictionary.Key, out dbDic) == false) dbDic = groupDB[entityViewTypeSafeDictionary.Key] = entityViewTypeSafeDictionary.Value.Create(); - + if (_groupsPerEntity.TryGetValue(entityViewTypeSafeDictionary.Key, out groupedGroup) == false) groupedGroup = _groupsPerEntity[entityViewTypeSafeDictionary.Key] = - new FasterDictionary(); + new FasterDictionary(); //Fill the DB with the entity views generate this frame. dbDic.FillWithIndexedEntities(entityViewTypeSafeDictionary.Value); @@ -153,7 +155,7 @@ namespace Svelto.ECS //then submit everything in the engines, so that the DB is up to date //with all the entity views and struct created by the entity built foreach (var groupToSubmit in groupsOfEntitiesToSubmit) - { + { foreach (var entityViewsPerType in groupToSubmit.Value) { using (profiler.Sample("Add entities to engines")) diff --git a/Svelto.ECS/EntitiesDB.cs b/Svelto.ECS/EntitiesDB.cs index e46dcbe..8524ec8 100644 --- a/Svelto.ECS/EntitiesDB.cs +++ b/Svelto.ECS/EntitiesDB.cs @@ -36,7 +36,21 @@ namespace Svelto.ECS.Internal { return QueryEntityView(new EGID(id, (int) group)); } + + public ref T QueryUniqueEntity(int @group) where T : IEntityStruct + { + var entities = QueryEntities(group, out var count); + + if (count != 1) throw new ECSException("Unique entities must be unique!".FastConcat(typeof(T).ToString())); + + return ref entities[0]; + } + public ref T QueryUniqueEntity(ExclusiveGroup.ExclusiveGroupStruct @group) where T : IEntityStruct + { + return ref QueryUniqueEntity((int) @group); + } + public ref T QueryEntity(EGID entityGID) where T : IEntityStruct { T[] array; diff --git a/Svelto.ECS/EntityBuilder.cs b/Svelto.ECS/EntityBuilder.cs index 820fede..29f68d0 100644 --- a/Svelto.ECS/EntityBuilder.cs +++ b/Svelto.ECS/EntityBuilder.cs @@ -30,7 +30,7 @@ namespace Svelto.ECS { DBC.ECS.Check.Require(implementors != null, "Implementors not found while building an EntityView"); DBC.ECS.Check.Require(castedDic.ContainsKey(entityID.entityID) == false, - "building an entity with already used entity id! id".FastConcat((long)entityID).FastConcat(" ", ENTITY_VIEW_NAME)); + "building an entity with already used entity id! id: ".FastConcat((long)entityID).FastConcat(" ", ENTITY_VIEW_NAME)); T entityView; EntityView.BuildEntityView(entityID, out entityView); diff --git a/Svelto.ECS/EntityStream.cs b/Svelto.ECS/EntityStream.cs index 5c00e4c..127083f 100644 --- a/Svelto.ECS/EntityStream.cs +++ b/Svelto.ECS/EntityStream.cs @@ -1,10 +1,16 @@ using System; using System.Collections.Generic; using Svelto.DataStructures; -using Svelto.ECS.Internal; namespace Svelto.ECS { + public interface IEntitiesStream + { + Consumer GenerateConsumer(int capacity) where T : unmanaged, IEntityStruct; + + void PublishEntity(EGID id) where T : unmanaged, IEntityStruct; + } + /// /// Do not use this class in place of a normal polling. /// I eventually realised than in ECS no form of communication other than polling entity components can exist. @@ -16,14 +22,14 @@ namespace Svelto.ECS /// one only /// - you want to communicate between EnginesRoots /// - public class EntityStreams + class EntitiesStream : IEntitiesStream { - public EntityStreams(IEntitiesDB entitiesDb) + public EntitiesStream(IEntitiesDB entitiesDb) { _entitiesDB = entitiesDb; } - public EntityStream.Consumer GenerateConsumer(int capacity) where T : unmanaged, IEntityStruct + public Consumer GenerateConsumer(int capacity) where T : unmanaged, IEntityStruct { if (_streams.ContainsKey(typeof(T)) == false) _streams[typeof(T)] = new EntityStream(); @@ -38,56 +44,62 @@ namespace Svelto.ECS Console.LogWarning("No Consumers are waiting for this entity to change " .FastConcat(typeof(T).ToString())); } - - Dictionary _streams = new Dictionary(); - IEntitiesDB _entitiesDB; + + readonly Dictionary _streams = new Dictionary(); + readonly IEntitiesDB _entitiesDB; } interface ITypeSafeStream {} - public class EntityStream:ITypeSafeStream where T:unmanaged, IEntityStruct + class EntityStream:ITypeSafeStream where T:unmanaged, IEntityStruct { - public class Consumer - { - public Consumer(int capacity) - { - _ringBuffer = new RingBuffer(capacity); - _capacity = capacity; - } - - public int Count => _ringBuffer.Count; - - public void Enqueue(ref T entity) - { - if (_ringBuffer.Count >= _capacity) - throw new Exception("EntityStream capacity has been saturated"); - - _ringBuffer.Enqueue(ref entity); - } - - readonly RingBuffer _ringBuffer; - int _capacity; - - public ref T Dequeue() - { - return ref _ringBuffer.Dequeue(); - } - } - public void PublishEntity(ref T entity) { for (int i = 0; i < _buffers.Count; i++) _buffers[i].Enqueue(ref entity); } - public Consumer GenerateConsumer(int capacity) + public Consumer GenerateConsumer(int capacity) { - var consumer = new Consumer(capacity); + var consumer = new Consumer(capacity); _buffers.Add(consumer); return consumer; } - readonly FasterList _buffers = new FasterList(); + readonly FasterList> _buffers = new FasterList>(); + } + + public class Consumer where T:unmanaged, IEntityStruct + { + public Consumer(int capacity) + { + _ringBuffer = new RingBuffer(capacity); + _capacity = capacity; + } + + public void Enqueue(ref T entity) + { + if (_ringBuffer.Count >= _capacity) + throw new Exception("EntityStream capacity has been saturated"); + + _ringBuffer.Enqueue(ref entity); + } + + public bool TryDequeue(ExclusiveGroup group, out T entity) + { + if (_ringBuffer.TryDequeue(out entity) == true) + return entity.ID.groupID == @group; + + return false; + } + + public bool TryDequeue(out T entity) { return _ringBuffer.TryDequeue(out entity); } + + public void Flush() { _ringBuffer.Reset(); } + + readonly RingBuffer _ringBuffer; + readonly int _capacity; + } } \ No newline at end of file diff --git a/Svelto.ECS/EntitySubmitOperation.cs b/Svelto.ECS/EntitySubmitOperation.cs index 2a2db7d..c3bb880 100644 --- a/Svelto.ECS/EntitySubmitOperation.cs +++ b/Svelto.ECS/EntitySubmitOperation.cs @@ -5,35 +5,31 @@ namespace Svelto.ECS struct EntitySubmitOperation { public readonly EntitySubmitOperationType type; - public readonly IEntityBuilder[] builders; - public readonly int ID; - public readonly int toID; - public readonly int toGroupID; - public readonly int fromGroupID; - public readonly Type entityDescriptor; -#if DEBUG && !PROFILER + public readonly IEntityBuilder[] builders; + public readonly int ID; + public readonly int toID; + public readonly int toGroupID; + public readonly int fromGroupID; + public readonly Type entityDescriptor; +#if DEBUG && !PROFILER public string trace; -#endif +#endif - public EntitySubmitOperation(EntitySubmitOperationType operation, - int entityId, - int toId, - int fromGroupId, - int toGroupId, - IEntityBuilder[] builders, - Type entityDescriptor) + public EntitySubmitOperation( + EntitySubmitOperationType operation, int entityId, int toId, int fromGroupId, int toGroupId, + IEntityBuilder[] builders, Type entityDescriptor) { type = operation; this.builders = builders; ID = entityId; toID = toId; - + toGroupID = toGroupId; fromGroupID = fromGroupId; this.entityDescriptor = entityDescriptor; -#if DEBUG && !PROFILER +#if DEBUG && !PROFILER trace = string.Empty; -#endif +#endif } } diff --git a/Svelto.ECS/ExclusiveGroups.cs b/Svelto.ECS/ExclusiveGroup.cs similarity index 100% rename from Svelto.ECS/ExclusiveGroups.cs rename to Svelto.ECS/ExclusiveGroup.cs diff --git a/Svelto.ECS/Extensions/Unity/GenericEntityDescriptorHolder.cs b/Svelto.ECS/Extensions/Unity/GenericEntityDescriptorHolder.cs index 02d9bb8..80312b6 100644 --- a/Svelto.ECS/Extensions/Unity/GenericEntityDescriptorHolder.cs +++ b/Svelto.ECS/Extensions/Unity/GenericEntityDescriptorHolder.cs @@ -15,7 +15,9 @@ namespace Svelto.ECS.Unity public string groupName => _groupName; [SerializeField] +#pragma warning disable 649 string _groupName; +#pragma warning restore 649 } } #endif \ No newline at end of file diff --git a/Svelto.ECS/IEntitiesDB.cs b/Svelto.ECS/IEntitiesDB.cs index 026ea69..48ac501 100644 --- a/Svelto.ECS/IEntitiesDB.cs +++ b/Svelto.ECS/IEntitiesDB.cs @@ -15,6 +15,9 @@ namespace Svelto.ECS bool TryQueryEntitiesAndIndex(EGID entityGid, out uint index, out T[] array) where T : IEntityStruct; bool TryQueryEntitiesAndIndex(int id, ExclusiveGroup.ExclusiveGroupStruct group, out uint index, out T[] array) where T : IEntityStruct; + ref T QueryUniqueEntity(ExclusiveGroup.ExclusiveGroupStruct group) where T : IEntityStruct; + ref T QueryUniqueEntity(int group) where T : IEntityStruct; + ref T QueryEntity(EGID entityGid) where T : IEntityStruct; ref T QueryEntity(int id, ExclusiveGroup.ExclusiveGroupStruct group) where T : IEntityStruct; ref T QueryEntity(int id, int group) where T : IEntityStruct; @@ -90,7 +93,7 @@ namespace Svelto.ECS bool Exists(int id, int groupid) where T : IEntityStruct; bool Exists (ExclusiveGroup.ExclusiveGroupStruct gid); - bool HasAny(int group) where T:IEntityStruct; + bool HasAny(int group) where T:IEntityStruct; bool HasAny(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T:IEntityStruct; int Count(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T:IEntityStruct; diff --git a/Svelto.ECS/IEntityFunctions.cs b/Svelto.ECS/IEntityFunctions.cs index 5b88f78..6658b0f 100644 --- a/Svelto.ECS/IEntityFunctions.cs +++ b/Svelto.ECS/IEntityFunctions.cs @@ -8,6 +8,9 @@ namespace Svelto.ECS void RemoveEntity(int entityID, int groupID) where T : IEntityDescriptor, new(); void RemoveEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T : IEntityDescriptor, new(); void RemoveEntity(EGID entityegid) where T : IEntityDescriptor, new(); + + void RemoveEntities(int groupID) where T : IEntityDescriptor, new(); + void RemoveEntities(ExclusiveGroup.ExclusiveGroupStruct groupID) where T : IEntityDescriptor, new(); void RemoveGroupAndEntities(int groupID); void RemoveGroupAndEntities(ExclusiveGroup.ExclusiveGroupStruct groupID);