diff --git a/Svelto.ECS/DynamicEntityDescriptorInfo.cs b/Svelto.ECS/DynamicEntityDescriptorInfo.cs index f049330..e032957 100644 --- a/Svelto.ECS/DynamicEntityDescriptorInfo.cs +++ b/Svelto.ECS/DynamicEntityDescriptorInfo.cs @@ -11,7 +11,7 @@ namespace Svelto.ECS "don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews"); var defaultEntities = EntityDescriptorTemplate.descriptor.entitiesToBuild; - var length = defaultEntities.Length; + var length = defaultEntities.Length; entitiesToBuild = new IEntityBuilder[length + extraEntities.Count + 1]; diff --git a/Svelto.ECS/EGID.cs b/Svelto.ECS/EGID.cs index dfd8d19..a11c535 100644 --- a/Svelto.ECS/EGID.cs +++ b/Svelto.ECS/EGID.cs @@ -22,12 +22,12 @@ namespace Svelto.ECS get { return (int) (_GID >> 32); } } - public EGID(int entityID, int groupID) : this() + internal EGID(int entityID, int groupID) : this() { _GID = MAKE_GLOBAL_ID(entityID, groupID); } - public EGID(int entityID, ExclusiveGroup groupID) : this() + public EGID(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) : this() { _GID = MAKE_GLOBAL_ID(entityID, (int) groupID); } diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 801f8ba..502d50c 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -64,7 +64,7 @@ namespace Svelto.ECS return new EntityStructInitializer(entityID, dic); } -#if DEBUG && !PROFILER +#if DEBUG && !PROFILER void CheckEntityID(EGID entityID, IEntityDescriptor descriptorEntity) { Dictionary @group; @@ -79,16 +79,14 @@ namespace Svelto.ECS } } - static void CheckEntityID(EGID entityID, Type entityType, Dictionary @group, - string name) + static void CheckEntityID(EGID entityID, Type entityType, Dictionary @group, string name) { ITypeSafeDictionary entities; if (@group.TryGetValue(entityType, out entities)) { if (entities.Has(entityID.entityID) == true) { - Svelto.Utilities.Console.LogError("Entity ".FastConcat - (name, " with used ID is about to be built: ") + Svelto.Utilities.Console.LogError("Entity ".FastConcat(name, " with used ID is about to be built: ") .FastConcat(entityType) .FastConcat(" id: ") .FastConcat(entityID.entityID) @@ -97,7 +95,7 @@ namespace Svelto.ECS } } } -#endif +#endif ///-------------------------------------------- void Preallocate(int groupID, int size) where T : IEntityDescriptor, new() diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs b/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs index 9768d4c..edd42b0 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs @@ -15,9 +15,9 @@ namespace Svelto.ECS _weakEngine = weakReference; } - public EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, object[] implementors) where T : IEntityDescriptor, new() + public EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, object[] implementors) where T : IEntityDescriptor, new() { - return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupID), implementors); + return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupStructId), implementors); } public EntityStructInitializer BuildEntity(EGID egid, object[] implementors) where T : IEntityDescriptor, new() @@ -30,14 +30,14 @@ namespace Svelto.ECS return _weakEngine.Target.BuildEntity(egid, entityDescriptor, implementors); } - public EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, T descriptorEntity, object[] implementors) where T:IEntityDescriptor + public EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, T descriptorEntity, object[] implementors) where T:IEntityDescriptor { - return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupID), descriptorEntity, implementors); + return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupStructId), descriptorEntity, implementors); } - public void PreallocateEntitySpace(ExclusiveGroup groupID, int size) where T : IEntityDescriptor, new() + public void PreallocateEntitySpace(ExclusiveGroup.ExclusiveGroupStruct groupStructId, int size) where T : IEntityDescriptor, new() { - _weakEngine.Target.Preallocate((int)groupID, size); + _weakEngine.Target.Preallocate((int)groupStructId, size); } } } diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs index 38087bf..47ef1f7 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs @@ -22,7 +22,7 @@ namespace Svelto.ECS _weakReference.Target.QueueEntitySubmitOperation(new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, groupID, -1, EntityDescriptorTemplate.descriptor.entitiesToBuild)); } - public void RemoveEntity(int entityID, ExclusiveGroup groupID) where T : IEntityDescriptor, new() + public void RemoveEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T : IEntityDescriptor, new() { _weakReference.Target.QueueEntitySubmitOperation( new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, (int)groupID, -1, EntityDescriptorTemplate.descriptor.entitiesToBuild)); @@ -40,31 +40,25 @@ namespace Svelto.ECS new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, groupID, -1, null)); } - public void RemoveGroupAndEntities(ExclusiveGroup groupID) + public void RemoveGroupAndEntities(ExclusiveGroup.ExclusiveGroupStruct groupID) { _weakReference.Target.QueueEntitySubmitOperation( new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, (int)groupID, -1, null)); } - public void SwapEntityGroup(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new() + public void SwapEntityGroup(int entityID, int fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new() { _weakReference.Target.QueueEntitySubmitOperation( - new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, fromGroupID, toGroupID, EntityDescriptorTemplate.descriptor.entitiesToBuild)); + new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, fromGroupID, (int)toGroupID, EntityDescriptorTemplate.descriptor.entitiesToBuild)); } - public void SwapEntityGroup(int entityID, ExclusiveGroup fromGroupID, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new() + public void SwapEntityGroup(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new() { _weakReference.Target.QueueEntitySubmitOperation( new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, (int) fromGroupID, (int) toGroupID, EntityDescriptorTemplate.descriptor.entitiesToBuild)); } - public void SwapEntityGroup(EGID id, int toGroupID) where T : IEntityDescriptor, new() - { - _weakReference.Target.QueueEntitySubmitOperation( - new EntitySubmitOperation(EntitySubmitOperationType.Swap, id.entityID, id.groupID, toGroupID, EntityDescriptorTemplate.descriptor.entitiesToBuild)); - } - - public void SwapEntityGroup(EGID id, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new() + public void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new() { _weakReference.Target.QueueEntitySubmitOperation( new EntitySubmitOperation(EntitySubmitOperationType.Swap, id.entityID, id.groupID, (int)toGroupID, EntityDescriptorTemplate.descriptor.entitiesToBuild)); diff --git a/Svelto.ECS/EntitiesDB.cs b/Svelto.ECS/EntitiesDB.cs index 0cc5657..ed304dc 100644 --- a/Svelto.ECS/EntitiesDB.cs +++ b/Svelto.ECS/EntitiesDB.cs @@ -31,9 +31,9 @@ namespace Svelto.ECS.Internal return typeSafeDictionary.GetValuesArray(out count); } - public T[] QueryEntities(ExclusiveGroup @group, out int targetsCount) where T : IEntityStruct + public T[] QueryEntities(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out int targetsCount) where T : IEntityStruct { - return QueryEntities((int) @group, out targetsCount); + return QueryEntities((int) groupStruct, out targetsCount); } public EGIDMapper QueryMappedEntities(int groupID) where T : IEntityStruct @@ -52,9 +52,9 @@ namespace Svelto.ECS.Internal return mapper; } - public EGIDMapper QueryMappedEntities(ExclusiveGroup groupID) where T : IEntityStruct + public EGIDMapper QueryMappedEntities(ExclusiveGroup.ExclusiveGroupStruct groupStructId) where T : IEntityStruct { - return QueryMappedEntities((int) groupID); + return QueryMappedEntities((int) groupStructId); } public T[] QueryEntitiesAndIndex(EGID entityGID, out uint index) where T : IEntityStruct @@ -92,6 +92,12 @@ namespace Svelto.ECS.Internal return casted != null && casted.ContainsKey(entityGID.entityID); } + public bool Exists (ExclusiveGroup.ExclusiveGroupStruct gid) + { + //search for the group + return _groupEntityViewsDB.ContainsKey((int) @gid); + } + public bool HasAny(int @group) where T : IEntityStruct { int count; @@ -99,9 +105,9 @@ namespace Svelto.ECS.Internal return count > 0; } - public bool HasAny(ExclusiveGroup @group) where T : IEntityStruct + public bool HasAny(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T : IEntityStruct { - return HasAny((int) group); + return HasAny((int) groupStruct); } public bool TryQueryEntityView(EGID entityegid, out T entityView) where T : class, IEntityStruct diff --git a/Svelto.ECS/ExclusiveGroups.cs b/Svelto.ECS/ExclusiveGroups.cs index 6c3b906..834bab5 100644 --- a/Svelto.ECS/ExclusiveGroups.cs +++ b/Svelto.ECS/ExclusiveGroups.cs @@ -1,4 +1,8 @@ -namespace Svelto.ECS +using System; + using System.Collections.Generic; + using System.Runtime.InteropServices.WindowsRuntime; + +namespace Svelto.ECS { /// /// Exclusive Groups guarantee that the GroupID is unique. @@ -7,33 +11,111 @@ /// /// public static class MyExclusiveGroups //(can be as many as you want) /// { - /// public static MyExclusiveGroup1 = new ExclusiveGroup(); + /// public static ExclusiveGroup MyExclusiveGroup1 = new ExclusiveGroup(); + /// + /// public static ExclusiveGroup[] GroupOfGroups = { MyExclusiveGroup1, ...}; //for each on this! /// } /// - + /// public class ExclusiveGroup { public ExclusiveGroup() { - _id = _globalId; - _globalId += 1; + _group = ExclusiveGroupStruct.Generate(); } - - /// - /// Use this constructor to reserve N groups - /// - public ExclusiveGroup(int range) + + public ExclusiveGroup(ushort range) + { + _group = new ExclusiveGroupStruct(range); + } + + public static implicit operator ExclusiveGroupStruct (ExclusiveGroup group) // explicit byte to digit conversion operator { - _id = _globalId; - _globalId += range; + return group._group; } - public static explicit operator int(ExclusiveGroup group) // explicit byte to digit conversion operator + public static explicit operator int (ExclusiveGroup group) // explicit byte to digit conversion operator + { + return (int)group._group; + } + + public static ExclusiveGroupStruct operator + (ExclusiveGroup a, int b) { - return group._id; + return a._group + b; } - readonly int _id; - static int _globalId; + readonly ExclusiveGroupStruct _group; + + public struct ExclusiveGroupStruct : IEquatable, IComparable, + IEqualityComparer + { + public static bool operator == (ExclusiveGroupStruct c1, ExclusiveGroupStruct c2) + { + return c1.Equals(c2); + } + + public static bool operator != (ExclusiveGroupStruct c1, ExclusiveGroupStruct c2) + { + return c1.Equals(c2) == false; + } + + public bool Equals (ExclusiveGroupStruct other) + { + return other._id == _id; + } + + public int CompareTo (ExclusiveGroupStruct other) + { + return other._id.CompareTo(_id); + } + + public bool Equals (ExclusiveGroupStruct x, ExclusiveGroupStruct y) + { + return x._id == y._id; + } + + public int GetHashCode (ExclusiveGroupStruct obj) + { + return _id.GetHashCode(); + } + + internal static ExclusiveGroupStruct Generate() + { + ExclusiveGroupStruct groupStruct; + + groupStruct._id = (int) _globalId; + DBC.ECS.Check.Require(_globalId + 1 < ushort.MaxValue, "too many exclusive groups created"); + _globalId++; + + return groupStruct; + } + + /// + /// Use this constructor to reserve N groups + /// + public ExclusiveGroupStruct(ushort range) + { + _id = (int) _globalId; + DBC.ECS.Check.Require(_globalId + range < ushort.MaxValue, "too many exclusive groups created"); + _globalId += range; + } + + public static explicit operator int (ExclusiveGroupStruct groupStruct) // explicit byte to digit conversion operator + { + return groupStruct._id; + } + + public static ExclusiveGroupStruct operator + (ExclusiveGroupStruct a, int b) + { + var group = new ExclusiveGroupStruct(); + + group._id = a._id + b; + + return group; + } + + int _id; + static uint _globalId; + } } } \ No newline at end of file diff --git a/Svelto.ECS/ExecuteOnEntitiesDB.cs b/Svelto.ECS/ExecuteOnEntitiesDB.cs index 31487f3..b0048b4 100644 --- a/Svelto.ECS/ExecuteOnEntitiesDB.cs +++ b/Svelto.ECS/ExecuteOnEntitiesDB.cs @@ -4,7 +4,7 @@ namespace Svelto.ECS.Internal { partial class EntitiesDB { - public void ExecuteOnEntity(int id, ExclusiveGroup groupid, EntityAction action) where T : IEntityStruct + public void ExecuteOnEntity(int id, ExclusiveGroup.ExclusiveGroupStruct groupid, EntityAction action) where T : IEntityStruct { ExecuteOnEntity(id, (int)groupid, action); } @@ -50,7 +50,7 @@ namespace Svelto.ECS.Internal ExecuteOnEntity(new EGID(id, groupid), ref value, action); } - public void ExecuteOnEntity(int id, ExclusiveGroup groupid, ref W value, EntityAction action) where T : IEntityStruct + public void ExecuteOnEntity(int id, ExclusiveGroup.ExclusiveGroupStruct groupid, ref W value, EntityAction action) where T : IEntityStruct { ExecuteOnEntity(id, (int)groupid, ref value, action); } @@ -71,9 +71,9 @@ namespace Svelto.ECS.Internal SafetyChecks(typeSafeDictionary, count); } - public void ExecuteOnEntities(ExclusiveGroup groupID, EntitiesAction action) where T : IEntityStruct + public void ExecuteOnEntities(ExclusiveGroup.ExclusiveGroupStruct groupStructId, EntitiesAction action) where T : IEntityStruct { - ExecuteOnEntities((int)groupID, action); + ExecuteOnEntities((int)groupStructId, action); } public void ExecuteOnEntities(int groupID, ref W value, EntitiesAction action) where T : IEntityStruct @@ -90,9 +90,9 @@ namespace Svelto.ECS.Internal SafetyChecks(typeSafeDictionary, count); } - public void ExecuteOnEntities(ExclusiveGroup groupID, ref W value, EntitiesAction action) where T : IEntityStruct + public void ExecuteOnEntities(ExclusiveGroup.ExclusiveGroupStruct groupStructId, ref W value, EntitiesAction action) where T : IEntityStruct { - ExecuteOnEntities((int)groupID, ref value, action); + ExecuteOnEntities((int)groupStructId, ref value, action); } //----------------------------------------------------------------------------------------------------------- @@ -149,7 +149,7 @@ namespace Svelto.ECS.Internal } } - public void ExecuteOnAllEntities(ExclusiveGroup[] groups, EntitiesAction action) where T : IEntityStruct + public void ExecuteOnAllEntities(Svelto.ECS.ExclusiveGroup [] groups, EntitiesAction action) where T : IEntityStruct { foreach (var group in groups) { @@ -157,7 +157,7 @@ namespace Svelto.ECS.Internal } } - public void ExecuteOnAllEntities(ExclusiveGroup[] groups, ref W value, EntitiesAction action) where T : IEntityStruct + public void ExecuteOnAllEntities(Svelto.ECS.ExclusiveGroup [] groups, ref W value, EntitiesAction action) where T : IEntityStruct { foreach (var group in groups) { diff --git a/Svelto.ECS/IEntitiesDB.cs b/Svelto.ECS/IEntitiesDB.cs index 6bc4536..595efaf 100644 --- a/Svelto.ECS/IEntitiesDB.cs +++ b/Svelto.ECS/IEntitiesDB.cs @@ -31,7 +31,7 @@ namespace Svelto.ECS /// /// T[] QueryEntities(int group, out int count) where T : IEntityStruct; - T[] QueryEntities(ExclusiveGroup @group, out int targetsCount) where T : IEntityStruct; + T[] QueryEntities(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out int targetsCount) where T : IEntityStruct; /// /// this version returns a mapped version of the entity array so that is possible to find the /// index of the entity inside the returned buffer through it's EGID @@ -42,7 +42,7 @@ namespace Svelto.ECS /// /// EGIDMapper QueryMappedEntities(int groupID) where T : IEntityStruct; - EGIDMapper QueryMappedEntities(ExclusiveGroup groupID) where T : IEntityStruct; + EGIDMapper QueryMappedEntities(ExclusiveGroup.ExclusiveGroupStruct groupStructId) where T : IEntityStruct; /// /// Execute an action on entities. Be sure that the action is not capturing variables /// otherwise you will allocate memory which will have a great impact on the execution performance. @@ -54,9 +54,9 @@ namespace Svelto.ECS /// /// void ExecuteOnEntities(int groupID, EntitiesAction action) where T : IEntityStruct; - void ExecuteOnEntities(ExclusiveGroup groupID, EntitiesAction action) where T : IEntityStruct; + void ExecuteOnEntities(ExclusiveGroup.ExclusiveGroupStruct groupStructId, EntitiesAction action) where T : IEntityStruct; void ExecuteOnEntities(int groupID, ref W value, EntitiesAction action) where T : IEntityStruct; - void ExecuteOnEntities(ExclusiveGroup groupID, ref W value, EntitiesAction action) where T : IEntityStruct; + void ExecuteOnEntities(ExclusiveGroup.ExclusiveGroupStruct groupStructId, ref W value, EntitiesAction action) where T : IEntityStruct; /// /// Execute an action on ALL the entities regardless the group. This function doesn't guarantee cache /// friendliness even if just EntityStructs are used. @@ -91,15 +91,16 @@ namespace Svelto.ECS /// void ExecuteOnEntity(EGID egid, EntityAction action) where T : IEntityStruct; void ExecuteOnEntity(int id, int groupid, EntityAction action) where T : IEntityStruct; - void ExecuteOnEntity(int id, ExclusiveGroup groupid, EntityAction action) where T : IEntityStruct; + void ExecuteOnEntity(int id, ExclusiveGroup.ExclusiveGroupStruct groupid, EntityAction action) where T : IEntityStruct; void ExecuteOnEntity(EGID egid, ref W value, EntityAction action) where T : IEntityStruct; void ExecuteOnEntity(int id, int groupid, ref W value, EntityAction action) where T : IEntityStruct; - void ExecuteOnEntity(int id, ExclusiveGroup groupid, ref W value, EntityAction action) where T : IEntityStruct; + void ExecuteOnEntity(int id, ExclusiveGroup.ExclusiveGroupStruct groupid, ref W value, EntityAction action) where T : IEntityStruct; bool Exists(EGID egid) where T : IEntityStruct; + bool Exists (ExclusiveGroup.ExclusiveGroupStruct gid); bool HasAny(int group) where T:IEntityStruct; - bool HasAny(ExclusiveGroup group) where T:IEntityStruct; + bool HasAny(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T:IEntityStruct; } public delegate void EntityAction(ref T target, ref W value); diff --git a/Svelto.ECS/IEntityFactory.cs b/Svelto.ECS/IEntityFactory.cs index b55288d..a59327f 100644 --- a/Svelto.ECS/IEntityFactory.cs +++ b/Svelto.ECS/IEntityFactory.cs @@ -22,7 +22,7 @@ namespace Svelto.ECS /// /// /// - void PreallocateEntitySpace(ExclusiveGroup groupID, int size) where T : IEntityDescriptor, new(); + void PreallocateEntitySpace(ExclusiveGroup.ExclusiveGroupStruct groupStructId, int size) where T : IEntityDescriptor, new(); /// /// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity @@ -35,10 +35,10 @@ namespace Svelto.ECS /// grouped. /// /// - /// + /// /// /// - EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, object[] implementors) where T:IEntityDescriptor, new(); + EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, object[] implementors) where T:IEntityDescriptor, new(); EntityStructInitializer BuildEntity(EGID egid, object[] implementors) where T:IEntityDescriptor, new(); /// /// When the type of the entity is not known (this is a special case!) an EntityDescriptorInfo @@ -48,7 +48,7 @@ namespace Svelto.ECS /// /// /// - EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, T descriptorEntity, object[] implementors) where T:IEntityDescriptor; + EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, T descriptorEntity, object[] implementors) where T:IEntityDescriptor; EntityStructInitializer BuildEntity(EGID egid, T entityDescriptor, object[] implementors) where T:IEntityDescriptor; } } diff --git a/Svelto.ECS/IEntityFunctions.cs b/Svelto.ECS/IEntityFunctions.cs index 1dee968..f9fc3a7 100644 --- a/Svelto.ECS/IEntityFunctions.cs +++ b/Svelto.ECS/IEntityFunctions.cs @@ -8,15 +8,14 @@ namespace Svelto.ECS //an entity is removed. Not specifying the group will attempt to remove //the entity from the special standard group. void RemoveEntity(int entityID, int groupID) where T : IEntityDescriptor, new(); - void RemoveEntity(int entityID, ExclusiveGroup 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 RemoveGroupAndEntities(int groupID); - void RemoveGroupAndEntities(ExclusiveGroup groupID); + void RemoveGroupAndEntities(ExclusiveGroup.ExclusiveGroupStruct groupID); - void SwapEntityGroup(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new(); - void SwapEntityGroup(int entityID, ExclusiveGroup fromGroupID, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new(); - void SwapEntityGroup(EGID id, int toGroupID) where T : IEntityDescriptor, new(); - void SwapEntityGroup(EGID id, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new(); + void SwapEntityGroup(int entityID, int fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new(); + void SwapEntityGroup(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new(); + void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new(); } } \ No newline at end of file diff --git a/Svelto.ECS/Sequencer.cs b/Svelto.ECS/Sequencer.cs index c22a2c4..09a52ec 100644 --- a/Svelto.ECS/Sequencer.cs +++ b/Svelto.ECS/Sequencer.cs @@ -4,32 +4,100 @@ using System.Collections.Generic; namespace Svelto.ECS { - public class Steps : Dictionary + public class Steps { - public new void Add(IEngine engine, IDictionary dictionary) + internal readonly Dictionary _steps; + + public Steps(params Step[] values) + { + _steps = new Dictionary(); + + for (int i = 0; i < values.Length; i++) + _steps.Add(values[i].from, values[i].to); + } + } + + public class To + { + public To(IStep engine) + { + this.engine = engine; + } + + public To(params IStep[] engine) { - if (ContainsKey(engine)) - { - Svelto.Utilities.Console.LogError("can't hold multiple steps with the same engine as origin in a Sequencer"); - } - base.Add(engine, dictionary); + this.engines = engine; } + + public IStep engine { get; set; } + public IStep[] engines { get; set; } } - public class To : Dictionary[]> where C : struct, IConvertible + public class To:To where C : struct, IConvertible { - public new void Add(C condition, params IStep[] engines) + internal readonly Dictionary[]> _tos = new Dictionary[]>(); + + public To(C condition, params IStep[] steps) + { + _tos[condition] = steps; + } + + public To(C condition, IStep step) { - base.Add(condition, engines); + _tos[condition] = new[] { step }; } } + + public interface IStep + { + void Step(EGID id); + } public interface IStep where C:struct,IConvertible { void Step(C condition, EGID id); } - public abstract class Sequencer + public struct Step + { + public IEngine from { get; set; } + public To to { get; set; } + } + + /// + /// The sequencer has just one goal: define a complex sequence of engines to call. The sequence is not + /// just "sequential", but can create branches and loops. + /// With the time, I figure out that the majority of the times this class is useful in the rare cases where + /// order execution of the engine is necessary/ + /// Using branching is even rarer, but still useful sometimes. + /// I used loops only once. + /// There is the chance that this class will become obsolete over the time, as by design engines should + /// never rely on the order of execution + /// Using this class to let engines from different EnginesRoot communicate will also become obsolete, as + /// better solution will be implemented once I have the time + /// Trying to work out how to initialize this structure can be painful. This is by design as this class must + /// be initialized using the following pattern: + /// instancedSequence.SetSequence( + /// new Steps //list of steps + /// ( + /// new Step // first step + /// { + /// from = menuOptionEnablerEngine, //starting engine + /// to = new To //targets + /// { + /// { + /// ItemActionsPanelEnableCondition.Enable, //condition 1 + /// menuPanelEnablerEngine //targets for condition 1 + /// }, + /// { + /// ItemActionsPanelEnableCondition.Disable,//condition 2 + /// menuPanelEnablerEngine //targets for condition 2 + /// } + /// } + /// }) + /// ); + /// + public class Sequencer where S: Sequencer, new() { public void SetSequence(Steps steps) { @@ -39,14 +107,25 @@ namespace Svelto.ECS public void Next(IEngine engine, C condition, EGID id) where C:struct,IConvertible { C branch = condition; - var steps = (_steps[engine] as Dictionary[]>)[branch]; - - if (steps == null) - Svelto.Utilities.Console.LogError("selected steps not found in sequencer ".FastConcat(this.ToString())); + var to = (_steps._steps[engine] as To); + var steps = to._tos[branch]; + for (var i = 0; i < steps.Length; i++) steps[i].Step(condition, id); } + + public void Next(IEngine engine, EGID id) + { + var to = _steps._steps[engine]; + var steps = to.engines; + + if (steps != null) + for (var i = 0; i < steps.Length; i++) + steps[i].Step(id); + else + to.engine.Step(id); + } Steps _steps; }