diff --git a/Svelto.Common b/Svelto.Common index a1f5f55..4f7d4d9 160000 --- a/Svelto.Common +++ b/Svelto.Common @@ -1 +1 @@ -Subproject commit a1f5f55c8b28f66ed033fa02c425dfa16c3c30e6 +Subproject commit 4f7d4d96407f3ce390a23b930ba235a2447729ec diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index 9149c71..5266477 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -88,7 +88,8 @@ namespace Svelto.ECS.Internal Dictionary> entityViewEnginesDB) { - var fasterValuesBuffer = GetFasterValuesBuffer(); + int count; + var fasterValuesBuffer = GetFasterValuesBuffer(out count); var valueIndex = GetValueIndex(fromEntityGid.entityID); if (entityViewEnginesDB != null) @@ -101,7 +102,7 @@ namespace Svelto.ECS.Internal toGroupCasted.Add(fromEntityGid.entityID, ref fasterValuesBuffer[valueIndex]); if (entityViewEnginesDB != null) - AddEntityViewToEngines(entityViewEnginesDB, ref toGroupCasted.GetFasterValuesBuffer()[toGroupCasted.GetValueIndex(fromEntityGid.entityID)]); + AddEntityViewToEngines(entityViewEnginesDB, ref toGroupCasted.GetFasterValuesBuffer(out count)[toGroupCasted.GetValueIndex(fromEntityGid.entityID)]); } Remove(fromEntityGid.entityID); diff --git a/Svelto.ECS/EnginesRoot.Engines.cs b/Svelto.ECS/EnginesRoot.Engines.cs index e2b9f4a..b68e9c4 100644 --- a/Svelto.ECS/EnginesRoot.Engines.cs +++ b/Svelto.ECS/EnginesRoot.Engines.cs @@ -43,10 +43,10 @@ namespace Svelto.ECS { [ExclusiveGroup.StandardEntitiesGroup] = new Dictionary() }; - //_groupedGroups = new Dictionary>(); + _groupedGroups = new Dictionary>(); _groupedEntityToAdd = new DoubleBufferedEntityViews>>(); - _DB = new entitiesDB(_groupEntityDB); + _DB = new entitiesDB(_groupEntityDB, _groupedGroups); _scheduler = entityViewScheduler; _scheduler.Schedule(new WeakAction(SubmitEntityViews)); diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 05122d2..a079afc 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -138,14 +138,18 @@ namespace Svelto.ECS { safeDictionary = fromTypeSafeDictionary.Create(); toGroup.Add(entityType, safeDictionary); - //_groupedGroups[entityType] = new FasterDictionary(); + _groupedGroups[entityType] = new FasterDictionary(); } + + _groupedGroups[entityType][toGroupID] = safeDictionary; } fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(fromEntityGID, toGroupID, safeDictionary, _entityEngines); if (fromTypeSafeDictionary.Count == 0) //clean up { + _groupedGroups[entityType].Remove(toGroupID); + fromGroup.Remove(entityType); } @@ -188,7 +192,6 @@ namespace Svelto.ECS readonly entitiesDB _DB; readonly DoubleBufferedEntityViews>> _groupedEntityToAdd; - static readonly Type _typeEntityInfoView = typeof(EntityInfoView); } @@ -205,8 +208,9 @@ namespace Svelto.ECS var typeSafeDictionary = (TypeSafeDictionary) _current[typeof(T)]; initializer.ID = _id; - - typeSafeDictionary.GetFasterValuesBuffer()[typeSafeDictionary.FindElementIndex(_id.entityID)] = initializer; + + int count; + typeSafeDictionary.GetFasterValuesBuffer(out count)[typeSafeDictionary.FindElementIndex(_id.entityID)] = initializer; } readonly Dictionary _current; diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index 52f7572..08dcc92 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -63,15 +63,18 @@ namespace Svelto.ECS foreach (var entityViewTypeSafeDictionary in groupOfEntitiesToSubmit.Value) { ITypeSafeDictionary dbDic; + FasterDictionary groupedGroup = null; if (groupDB.TryGetValue(entityViewTypeSafeDictionary.Key, out dbDic) == false) { dbDic = groupDB[entityViewTypeSafeDictionary.Key] = entityViewTypeSafeDictionary.Value.Create(); - //_groupedGroups[entityViewTypeSafeDictionary.Key] = new FasterDictionary(); + + if (_groupedGroups.TryGetValue(entityViewTypeSafeDictionary.Key, out groupedGroup) == false) + groupedGroup = _groupedGroups[entityViewTypeSafeDictionary.Key] = new FasterDictionary(); } //type safe copy dbDic.FillWithIndexedEntities(entityViewTypeSafeDictionary.Value); - // _groupedGroups[entityViewTypeSafeDictionary.Key][groupID] = dbDic; + groupedGroup[groupID] = dbDic; } } @@ -92,8 +95,8 @@ namespace Svelto.ECS //to the FasterDictionary capabilitiies OR it's possible to get a specific entityView indexed by //ID. This ID doesn't need to be the EGID, it can be just the entityID - readonly Dictionary> _groupEntityDB; -// readonly Dictionary> _groupedGroups; //yes I am being sarcastic - readonly EntitySubmissionScheduler _scheduler; + readonly Dictionary> _groupEntityDB; + readonly Dictionary> _groupedGroups; //yes I am being sarcastic + readonly EntitySubmissionScheduler _scheduler; } } \ No newline at end of file diff --git a/Svelto.ECS/EntitiesDB.cs b/Svelto.ECS/EntitiesDB.cs index 3a0712e..27f4a1c 100644 --- a/Svelto.ECS/EntitiesDB.cs +++ b/Svelto.ECS/EntitiesDB.cs @@ -1,15 +1,18 @@ using System; using System.Collections.Generic; using Svelto.DataStructures; +using Svelto.DataStructures.Experimental; using Svelto.Utilities; namespace Svelto.ECS.Internal { class entitiesDB : IEntitiesDB { - internal entitiesDB(Dictionary> groupEntityViewsDB) + internal entitiesDB(Dictionary> groupEntityViewsDB, + Dictionary> groupedGroups) { _groupEntityViewsDB = groupEntityViewsDB; + _groupedGroups = groupedGroups; } public ReadOnlyCollectionStruct QueryEntityViews() where T:class, IEntityStruct @@ -159,7 +162,7 @@ namespace Svelto.ECS.Internal public void ExecuteOnEntities(int groupID, ref W value, ActionRef action) where T : IEntityStruct { int count; - var entities = QueryEntities(out count); + var entities = QueryEntities(groupID, out count); for (int i = 0; i < count; i++) action(ref entities[i], ref value); @@ -174,6 +177,36 @@ namespace Svelto.ECS.Internal action(ref entities[i], ref value); } + public void ExecuteOnAllEntities(ActionRef action) where T : IEntityStruct + { + int count; + var typeSafeDictionaries = _groupedGroups[typeof(T)].GetFasterValuesBuffer(out count); + for (int j = 0; j < count; j++) + { + int count2; + var safedic = typeSafeDictionaries[j]; + TypeSafeDictionary casted = safedic as TypeSafeDictionary; + var entities = casted.GetFasterValuesBuffer(out count2); + for (int i = 0; i < count2; i++) + action(ref entities[i]); + } + } + + public void ExecuteOnAllEntities(ref W value, ActionRef action) where T : IEntityStruct + { + int count; + var typeSafeDictionaries = _groupedGroups[typeof(T)].GetFasterValuesBuffer(out count); + for (int j = 0; j < count; j++) + { + int count2; + var safedic = typeSafeDictionaries[j]; + TypeSafeDictionary casted = safedic as TypeSafeDictionary; + var entities = casted.GetFasterValuesBuffer(out count2); + for (int i = 0; i < count2; i++) + action(ref entities[i], ref value); + } + } + public bool Exists(EGID entityGID) where T : IEntityStruct { TypeSafeDictionary casted; @@ -257,5 +290,6 @@ namespace Svelto.ECS.Internal //grouped set of entity views, this is the standard way to handle entity views readonly Dictionary> _groupEntityViewsDB; + Dictionary> _groupedGroups; } } diff --git a/Svelto.ECS/IEntitiesDB.cs b/Svelto.ECS/IEntitiesDB.cs index 54fdcdd..433b28f 100644 --- a/Svelto.ECS/IEntitiesDB.cs +++ b/Svelto.ECS/IEntitiesDB.cs @@ -38,21 +38,26 @@ namespace Svelto.ECS bool TryQueryEntitiesAndIndex(EGID entityGid, out uint index, out T[] array) where T : IEntityStruct; //to use with EntityViews, EntityStructs and EntityViewStructs - void ExecuteOnEntity(EGID egid, ref W value, ActionRef action) where T : IEntityStruct; + 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(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 ExecuteOnEntities(int groupID, ActionRef action) where T : IEntityStruct; void ExecuteOnEntities(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 ExecuteOnEntities(int groupID, ref W value, ActionRef action) where T : IEntityStruct; void ExecuteOnEntities(ref W value, ActionRef action) where T : IEntityStruct; - + + void ExecuteOnAllEntities(ActionRef action) where T : IEntityStruct; + void ExecuteOnAllEntities(ref W value, ActionRef action) where T : IEntityStruct; + bool Exists(EGID egid) where T : IEntityStruct; bool HasAny() where T:IEntityStruct;