From a2281628a0a15c0d410e3e80d3bb90db0642130e Mon Sep 17 00:00:00 2001 From: sebas77 Date: Mon, 6 Aug 2018 17:25:46 +0100 Subject: [PATCH] There were some references to EntityInfoView still in place, they are all removed now Split ExecuteOnEntities functions to another class Added the index element in ExecuteOnEntities so that is possible to know the current index of the iteration --- .../DataStructures/TypeSafeDictionary.cs | 4 +- Svelto.ECS/EntitiesDB.cs | 201 +---------------- ...{EntityViewBuilder.cs => EntityBuilder.cs} | 2 +- Svelto.ECS/EntityInfoView.cs | 9 - Svelto.ECS/ExecuteOnEntitiesDB.cs | 212 ++++++++++++++++++ Svelto.ECS/IEntitiesDB.cs | 41 ++-- 6 files changed, 246 insertions(+), 223 deletions(-) rename Svelto.ECS/{EntityViewBuilder.cs => EntityBuilder.cs} (98%) delete mode 100644 Svelto.ECS/EntityInfoView.cs create mode 100644 Svelto.ECS/ExecuteOnEntitiesDB.cs diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index d852218..540d77f 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -144,7 +144,7 @@ namespace Svelto.ECS.Internal return new TypeSafeDictionary(); } - public bool ExecuteOnEntityView(int entityGidEntityId, ref W value, ActionRef action) + public bool ExecuteOnEntityView(int entityGidEntityId, ref W value, EntityAction action) { uint findIndex; if (FindIndex(entityGidEntityId, out findIndex)) @@ -157,7 +157,7 @@ namespace Svelto.ECS.Internal return false; } - public bool ExecuteOnEntityView(int entityGidEntityId, ActionRef action) + public bool ExecuteOnEntityView(int entityGidEntityId, EntityAction action) { uint findIndex; if (FindIndex(entityGidEntityId, out findIndex)) diff --git a/Svelto.ECS/EntitiesDB.cs b/Svelto.ECS/EntitiesDB.cs index 175e84a..aa9fa6d 100644 --- a/Svelto.ECS/EntitiesDB.cs +++ b/Svelto.ECS/EntitiesDB.cs @@ -6,7 +6,7 @@ using Svelto.Utilities; namespace Svelto.ECS.Internal { - class entitiesDB : IEntitiesDB + partial class entitiesDB : IEntitiesDB { internal entitiesDB(Dictionary> groupEntityViewsDB, Dictionary> groupedGroups) @@ -90,197 +90,6 @@ namespace Svelto.ECS.Internal return entityView; } - public void ExecuteOnEntity(EGID entityGID, ref W value, ActionRef action) where T : IEntityStruct - { - TypeSafeDictionary casted; - if (QueryEntitySafeDictionary(entityGID.groupID, out casted)) - { - if (casted != null) - if (casted.ExecuteOnEntityView(entityGID.entityID, ref value, action) == true) - return; - } - - throw new EntitiesDBException("Entity not found id: ".FastConcat(entityGID.entityID).FastConcat(" groupID: ").FastConcat(entityGID.groupID)); - } - - public void ExecuteOnEntity(EGID entityGID, ActionRef action) where T : IEntityStruct - { - TypeSafeDictionary casted; - if (QueryEntitySafeDictionary(entityGID.groupID, out casted)) - { - if (casted != null) - if (casted.ExecuteOnEntityView(entityGID.entityID, action) == true) - return; - } - - throw new EntitiesDBException("Entity not found id: ".FastConcat(entityGID.entityID).FastConcat(" groupID: ").FastConcat(entityGID.groupID)); - } - - public void ExecuteOnEntity(int id, ActionRef action) where T : IEntityStruct - { - ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), action); - } - - public void ExecuteOnEntity(int id, int groupid, ActionRef action) where T : IEntityStruct - { - ExecuteOnEntity(new EGID(id, groupid), action); - } - - public void ExecuteOnEntity(int id, ref W value, ActionRef action) where T : IEntityStruct - { - ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), ref value, action); - } - - public void ExecuteOnEntity(int id, int groupid, ref W value, ActionRef action) where T : IEntityStruct - { - ExecuteOnEntity(new EGID(id, groupid), ref value, action); - } - - public void ExecuteOnEntities(int groupID, ActionRef action) where T : IEntityStruct - { - int count; - TypeSafeDictionary typeSafeDictionary; - if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return; - - var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); - - for (var i = 0; i < count; i++) - action(ref entities[i]); - - SafetyChecks(typeSafeDictionary, count); - } - - static void SafetyChecks(TypeSafeDictionary typeSafeDictionary, int count) where T : IEntityStruct - { - if (typeSafeDictionary.Count != count) - throw new EntitiesDBException("Entities cannot be swapped or removed during an iteration"); - } - - public void ExecuteOnEntities(ActionRef action) where T : IEntityStruct - { - ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action); - } - - public void ExecuteOnEntities(int groupID, ref W value, ActionRef action) where T : IEntityStruct - { - int count; - TypeSafeDictionary typeSafeDictionary; - if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return; - - var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); - - for (var i = 0; i < count; i++) - action(ref entities[i], ref value); - - SafetyChecks(typeSafeDictionary, count); - } - - public void ExecuteOnEntities(ref W value, ActionRef action) where T : IEntityStruct - { - ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action); - } - - public void ExecuteOnEntities(W value, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct - { - ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action); - } - - public void ExecuteOnAllEntities(ActionRef action) where T : IEntityStruct - { - var type = typeof(T); - FasterDictionary dic; - if (_groupedGroups.TryGetValue(type, out dic)) - { - int count; - var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count); - for (int j = 0; j < count; j++) - { - int innerCount; - var typeSafeDictionary = typeSafeDictionaries[j]; - var casted = typeSafeDictionary as TypeSafeDictionary; - - var entities = casted.GetFasterValuesBuffer(out innerCount); - - for (int i = 0; i < innerCount; i++) - action(ref entities[i]); - - SafetyChecks(casted, count); - } - } - } - - public void ExecuteOnAllEntities(ref W value, ActionRef action) where T : IEntityStruct - { - var type = typeof(T); - FasterDictionary dic; - if (_groupedGroups.TryGetValue(type, out dic)) - { - int count; - var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count); - for (int j = 0; j < count; j++) - { - int innerCount; - var typeSafeDictionary = typeSafeDictionaries[j]; - var casted = typeSafeDictionary as TypeSafeDictionary; - - var entities = casted.GetFasterValuesBuffer(out innerCount); - - for (int i = 0; i < innerCount; i++) - action(ref entities[i], ref value); - - SafetyChecks(casted, count); - } - } - } - - public void ExecuteOnEntities(int group, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct - { - int count; - TypeSafeDictionary typeSafeDictionary; - if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return; - - var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); - - EGIDMapper map = QueryMappedEntities(group); - - for (var i = 0; i < count; i++) - { - uint index; - action(ref entities[i], ref map.entities(entities[i].ID, out index)[index]); - } - - SafetyChecks(typeSafeDictionary, count); - } - - public void ExecuteOnEntities(int group, ref W value, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct - { - int count; - TypeSafeDictionary typeSafeDictionary; - if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return; - - var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); - - EGIDMapper map = QueryMappedEntities(group); - - for (var i = 0; i < count; i++) - { - uint index; - action(ref entities[i], ref map.entities(entities[i].ID, out index)[index], ref value); - } - - SafetyChecks(typeSafeDictionary, count); - } - - public void ExecuteOnEntities(ActionRef action) where T : IEntityStruct where T1 : IEntityStruct - { - ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action); - } - - public void ExecuteOnEntities(ref W value, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct - { - ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action); - } - public bool Exists(EGID entityGID) where T : IEntityStruct { TypeSafeDictionary casted; @@ -347,6 +156,14 @@ namespace Svelto.ECS.Internal return true; } + + static void SafetyChecks(TypeSafeDictionary typeSafeDictionary, int count) where T : IEntityStruct + { +#if DEBUG + if (typeSafeDictionary.Count != count) + throw new EntitiesDBException("Entities cannot be swapped or removed during an iteration"); +#endif + } static ReadOnlyCollectionStruct RetrieveEmptyEntityViewList() { diff --git a/Svelto.ECS/EntityViewBuilder.cs b/Svelto.ECS/EntityBuilder.cs similarity index 98% rename from Svelto.ECS/EntityViewBuilder.cs rename to Svelto.ECS/EntityBuilder.cs index 0b91ea7..18f0168 100644 --- a/Svelto.ECS/EntityViewBuilder.cs +++ b/Svelto.ECS/EntityBuilder.cs @@ -16,7 +16,7 @@ namespace Svelto.ECS _initializer = default(T); #if DEBUG && !PROFILER - if (needsReflection == false && typeof(T) != typeof(EntityInfoView)) + if (needsReflection == false) { CheckFields(typeof(T)); } diff --git a/Svelto.ECS/EntityInfoView.cs b/Svelto.ECS/EntityInfoView.cs deleted file mode 100644 index 419d531..0000000 --- a/Svelto.ECS/EntityInfoView.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Svelto.ECS -{ - public struct EntityInfoView : IEntityStruct - { - public EGID ID { get; set; } - - public IEntityBuilder[] entityToBuild; - } -} \ No newline at end of file diff --git a/Svelto.ECS/ExecuteOnEntitiesDB.cs b/Svelto.ECS/ExecuteOnEntitiesDB.cs new file mode 100644 index 0000000..5b35f46 --- /dev/null +++ b/Svelto.ECS/ExecuteOnEntitiesDB.cs @@ -0,0 +1,212 @@ +using Svelto.DataStructures.Experimental; +using Svelto.Utilities; + +namespace Svelto.ECS.Internal +{ + partial class entitiesDB + { + public void ExecuteOnEntity(EGID entityGID, ref W value, EntityAction action) where T : IEntityStruct + { + TypeSafeDictionary casted; + if (QueryEntitySafeDictionary(entityGID.groupID, out casted)) + { + if (casted != null) + if (casted.ExecuteOnEntityView(entityGID.entityID, ref value, action) == true) + return; + } + + throw new EntitiesDBException("Entity not found id: " + .FastConcat(entityGID.entityID).FastConcat(" groupID: ") + .FastConcat(entityGID.groupID)); + } + + public void ExecuteOnEntity(EGID entityGID, EntityAction action) where T : IEntityStruct + { + TypeSafeDictionary casted; + if (QueryEntitySafeDictionary(entityGID.groupID, out casted)) + { + if (casted != null) + if (casted.ExecuteOnEntityView(entityGID.entityID, action) == true) + return; + } + + throw new EntitiesDBException("Entity not found id: " + .FastConcat(entityGID.entityID).FastConcat(" groupID: ") + .FastConcat(entityGID.groupID)); + } + + public void ExecuteOnEntity(int id, EntityAction action) where T : IEntityStruct + { + ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), action); + } + + public void ExecuteOnEntity(int id, int groupid, EntityAction action) where T : IEntityStruct + { + ExecuteOnEntity(new EGID(id, groupid), action); + } + + public void ExecuteOnEntity(int id, ref W value, EntityAction action) where T : IEntityStruct + { + ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), ref value, action); + } + + public void ExecuteOnEntity(int id, int groupid, ref W value, EntityAction action) + where T : IEntityStruct + { + ExecuteOnEntity(new EGID(id, groupid), ref value, action); + } + + //---------------------------------------------------------------------------------------------------------- + + public void ExecuteOnEntities(int groupID, EntitiesAction action) where T : IEntityStruct + { + int count; + TypeSafeDictionary typeSafeDictionary; + if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return; + + var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); + + for (var i = 0; i < count; i++) + action(ref entities[i], i); + + SafetyChecks(typeSafeDictionary, count); + } + + public void ExecuteOnEntities(EntitiesAction action) where T : IEntityStruct + { + ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action); + } + + public void ExecuteOnEntities(int groupID, ref W value, EntitiesAction action) where T : IEntityStruct + { + int count; + TypeSafeDictionary typeSafeDictionary; + if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return; + + var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); + + for (var i = 0; i < count; i++) + action(ref entities[i], ref value, i); + + SafetyChecks(typeSafeDictionary, count); + } + + public void ExecuteOnEntities(ref W value, EntitiesAction action) where T : IEntityStruct + { + ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action); + } + + public void ExecuteOnEntities(W value, EntitiesAction action) + where T : IEntityStruct where T1 : IEntityStruct + { + ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action); + } + + public void ExecuteOnEntities(int group, EntitiesAction action) + where T : IEntityStruct where T1 : IEntityStruct + { + int count; + TypeSafeDictionary typeSafeDictionary; + if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return; + + var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); + + EGIDMapper map = QueryMappedEntities(@group); + + for (var i = 0; i < count; i++) + { + uint index; + action(ref entities[i], ref map.entities(entities[i].ID, out index)[index], i); + } + + SafetyChecks(typeSafeDictionary, count); + } + + public void ExecuteOnEntities(int group, ref W value, EntitiesAction action) + where T : IEntityStruct where T1 : IEntityStruct + { + int count; + TypeSafeDictionary typeSafeDictionary; + if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return; + + var entities = typeSafeDictionary.GetFasterValuesBuffer(out count); + + EGIDMapper map = QueryMappedEntities(@group); + + for (var i = 0; i < count; i++) + { + uint index; + action(ref entities[i], ref map.entities(entities[i].ID, out index)[index], ref value, i); + } + + SafetyChecks(typeSafeDictionary, count); + } + + public void ExecuteOnEntities(EntitiesAction action) where T : IEntityStruct where T1 : IEntityStruct + { + ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action); + } + + public void ExecuteOnEntities(ref W value, EntitiesAction action) + where T : IEntityStruct where T1 : IEntityStruct + { + ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action); + } + + //----------------------------------------------------------------------------------------------------------- + + public void ExecuteOnAllEntities(EntityAction action) where T : IEntityStruct + { + var type = typeof(T); + FasterDictionary dic; + + if (_groupedGroups.TryGetValue(type, out dic)) + { + int count; + var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count); + + for (int j = 0; j < count; j++) + { + int innerCount; + var typeSafeDictionary = typeSafeDictionaries[j]; + var casted = typeSafeDictionary as TypeSafeDictionary; + + var entities = casted.GetFasterValuesBuffer(out innerCount); + + for (int i = 0; i < innerCount; i++) + action(ref entities[i]); + + SafetyChecks(casted, count); + } + } + } + + public void ExecuteOnAllEntities(ref W value, EntityAction action) where T : IEntityStruct + { + var type = typeof(T); + FasterDictionary dic; + + if (_groupedGroups.TryGetValue(type, out dic)) + { + int count; + var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count); + + for (int j = 0; j < count; j++) + { + int innerCount; + var typeSafeDictionary = typeSafeDictionaries[j]; + var casted = typeSafeDictionary as TypeSafeDictionary; + + var entities = casted.GetFasterValuesBuffer(out innerCount); + + for (int i = 0; i < innerCount; i++) + action(ref entities[i], ref value); + + SafetyChecks(casted, count); + } + } + } + + + } +} \ No newline at end of file diff --git a/Svelto.ECS/IEntitiesDB.cs b/Svelto.ECS/IEntitiesDB.cs index a8f7940..c5571da 100644 --- a/Svelto.ECS/IEntitiesDB.cs +++ b/Svelto.ECS/IEntitiesDB.cs @@ -1,5 +1,4 @@ using Svelto.DataStructures; -using Svelto.Utilities; namespace Svelto.ECS { @@ -60,10 +59,10 @@ namespace Svelto.ECS /// /// /// - void ExecuteOnEntities(int groupID, ActionRef action) where T : IEntityStruct; - void ExecuteOnEntities(ActionRef action) where T : IEntityStruct; - void ExecuteOnEntities(int groupID, ref W value, ActionRef action) where T : IEntityStruct; - void ExecuteOnEntities(ref W value, ActionRef action) where T : IEntityStruct; + void ExecuteOnEntities(int groupID, EntitiesAction action) where T : IEntityStruct; + void ExecuteOnEntities(EntitiesAction action) where T : IEntityStruct; + void ExecuteOnEntities(int groupID, ref W value, EntitiesAction action) where T : IEntityStruct; + void ExecuteOnEntities(ref W value, EntitiesAction action) where T : IEntityStruct; /// /// This specialized version allows to execute actions on multiple entity views or entity structs /// Safety checks are in place. This function doesn't guarantee cache @@ -73,12 +72,10 @@ namespace Svelto.ECS /// /// /// - void ExecuteOnEntities(int groupID, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct; - void ExecuteOnEntities(ActionRef action) where T : IEntityStruct where T1 : IEntityStruct; - - void ExecuteOnEntities(int groupID, ref W value, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct; - void ExecuteOnEntities(ref W value, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct; - void ExecuteOnEntities(W value, ActionRef action) where T : IEntityStruct where T1 : IEntityStruct; + void ExecuteOnEntities(int groupID, EntitiesAction action) where T : IEntityStruct where T1 : IEntityStruct; + void ExecuteOnEntities(EntitiesAction action) where T : IEntityStruct where T1 : IEntityStruct; + void ExecuteOnEntities(int groupID, ref W value, EntitiesAction action) where T : IEntityStruct where T1 : IEntityStruct; + void ExecuteOnEntities(ref W value, EntitiesAction action) where T : IEntityStruct where T1 : IEntityStruct; /// /// Execute an action on ALL the entities regardless the group. This function doesn't guarantee cache /// friendliness even if just EntityStructs are used. @@ -86,8 +83,8 @@ namespace Svelto.ECS /// /// /// - void ExecuteOnAllEntities(ActionRef action) where T : IEntityStruct; - void ExecuteOnAllEntities(ref W value, ActionRef action) where T : IEntityStruct; + void ExecuteOnAllEntities(EntityAction action) where T : IEntityStruct; + void ExecuteOnAllEntities(ref W value, EntityAction action) where T : IEntityStruct; /// /// ECS is meant to work on a set of Entities. Working on a single entity is sometime necessary, but using /// the following functions inside a loop would be a mistake as performance can be significantly impacted @@ -108,16 +105,22 @@ namespace Svelto.ECS /// /// /// - void ExecuteOnEntity(EGID egid, ActionRef action) where T : IEntityStruct; - void ExecuteOnEntity(int id, ActionRef action) where T : IEntityStruct; - void ExecuteOnEntity(int id, int groupid, ActionRef action) where T : IEntityStruct; - void ExecuteOnEntity(EGID egid, ref W value, ActionRef action) where T : IEntityStruct; - void ExecuteOnEntity(int id, ref W value, ActionRef action) where T : IEntityStruct; - void ExecuteOnEntity(int id, int groupid, ref W value, ActionRef action) where T : IEntityStruct; + void ExecuteOnEntity(EGID egid, EntityAction action) where T : IEntityStruct; + void ExecuteOnEntity(int id, EntityAction action) where T : IEntityStruct; + void ExecuteOnEntity(int id, int groupid, EntityAction action) where T : IEntityStruct; + void ExecuteOnEntity(EGID egid, ref W value, EntityAction action) where T : IEntityStruct; + void ExecuteOnEntity(int id, ref W value, EntityAction action) where T : IEntityStruct; + void ExecuteOnEntity(int id, int groupid, ref W value, EntityAction action) where T : IEntityStruct; bool Exists(EGID egid) where T : IEntityStruct; bool HasAny() where T:IEntityStruct; bool HasAny(int group) where T:IEntityStruct; } + + public delegate void EntityAction(ref T target, ref W value); + public delegate void EntityAction(ref T target); + public delegate void EntitiesAction(ref T target, ref T1 target1, ref W value, int index); + public delegate void EntitiesAction(ref T target, ref W value, int index); + public delegate void EntitiesAction(ref T target, int index); } \ No newline at end of file