diff --git a/Svelto.Common b/Svelto.Common index 5b756d5..a1f5f55 160000 --- a/Svelto.Common +++ b/Svelto.Common @@ -1 +1 @@ -Subproject commit 5b756d57920c83a8e31dad9eedee8b629c6cb245 +Subproject commit a1f5f55c8b28f66ed033fa02c425dfa16c3c30e6 diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index 240a62d..0015195 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -20,12 +20,13 @@ namespace Svelto.ECS.Internal void RemoveEntitiesFromEngines(Dictionary> entityViewEnginesDB); - void RemoveEntityFromEngines(EGID entityGid, - Dictionary> - entityViewEnginesDB); + void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, + ITypeSafeDictionary toGroup, + Dictionary> + entityViewEnginesDB); void FillWithIndexedEntities(ITypeSafeDictionary entities); - void AddEntityViewsToEngines(Dictionary> entityViewEnginesDB); + void AddEntitiesToEngines(Dictionary> entityViewEnginesDB); void AddCapacity(int size); bool Remove(int idGid); @@ -61,7 +62,7 @@ namespace Svelto.ECS.Internal } } - public void AddEntityViewsToEngines(Dictionary> entityViewEnginesDB) + public void AddEntitiesToEngines(Dictionary> entityViewEnginesDB) { int count; TValue[] values = GetFasterValuesBuffer(out count); @@ -83,11 +84,26 @@ namespace Svelto.ECS.Internal (entityViewsEngines[i] as IHandleEntityStructEngine).AddInternal(ref entity); } - public void RemoveEntityFromEngines(EGID entityGid, - Dictionary> - entityViewEnginesDB) + public void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, ITypeSafeDictionary toGroup, + Dictionary> + entityViewEnginesDB) { - RemoveEntityViewFromEngines(entityViewEnginesDB, ref GetFasterValuesBuffer()[GetValueIndex(entityGid.entityID)]); + var fasterValuesBuffer = GetFasterValuesBuffer(); + var valueIndex = GetValueIndex(fromEntityGid.entityID); + + if (entityViewEnginesDB != null) + RemoveEntityViewFromEngines(entityViewEnginesDB, ref fasterValuesBuffer[valueIndex]); + + if (toGroup != null) + { + var toGroupCasted = (toGroup as TypeSafeDictionary); + toGroupCasted.Add(fromEntityGid.entityID, ref fasterValuesBuffer[valueIndex]); + + if (entityViewEnginesDB != null) + AddEntityViewToEngines(entityViewEnginesDB, ref toGroupCasted.GetFasterValuesBuffer()[toGroupCasted.GetValueIndex(fromEntityGid.entityID)]); + } + + Remove(fromEntityGid.entityID); } static void RemoveEntityViewFromEngines @@ -109,7 +125,7 @@ namespace Svelto.ECS.Internal RemoveEntityViewFromEngines(entityViewEnginesDB, ref values[i]); } } - + public ITypeSafeDictionary Create() { return new TypeSafeDictionary(); diff --git a/Svelto.ECS/EnginesRoot.Engines.cs b/Svelto.ECS/EnginesRoot.Engines.cs index e12b326..b7f666a 100644 --- a/Svelto.ECS/EnginesRoot.Engines.cs +++ b/Svelto.ECS/EnginesRoot.Engines.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Svelto.DataStructures; +using Svelto.DataStructures.Experimental; using Svelto.ECS.Internal; using Svelto.ECS.Schedulers; using Svelto.WeakEvents; @@ -38,9 +39,11 @@ namespace Svelto.ECS _entityEngines = new Dictionary>(); _otherEngines = new FasterList(); - _groupEntityDB = new Dictionary>(); - _groupEntityDB[ExclusiveGroup.StandardEntitiesGroup] = new Dictionary(); - + _groupEntityDB = new Dictionary> + { + [ExclusiveGroup.StandardEntitiesGroup] = new Dictionary() + }; + _groupedGroups = new Dictionary>(); _groupedEntityToAdd = new DoubleBufferedEntityViews>>(); _DB = new entitiesDB(_groupEntityDB); diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 6d24a5f..be9d6e1 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Svelto.DataStructures.Experimental; using Svelto.ECS.Internal; #if ENGINE_PROFILER_ENABLED && UNITY_EDITOR @@ -107,38 +108,47 @@ namespace Svelto.ECS ///-------------------------------------------- /// - void RemoveEntity(EGID entityGID) + void MoveEntity(EGID entityGID, Dictionary toGroup = null) { var entityViewInfoDictionary = _groupEntityDB[entityGID.groupID][_typeEntityInfoView] as TypeSafeDictionary; - var entityInfoView = entityViewInfoDictionary[entityGID.entityID]; - var entityBuilders = entityInfoView.entityToBuild; + var entityBuilders = entityViewInfoDictionary[entityGID.entityID].entityToBuild; var entityBuildersCount = entityBuilders.Length; - var group = _groupEntityDB[entityGID.groupID]; //for each entity view generated by the entity descriptor for (var i = 0; i < entityBuildersCount; i++) { var entityType = entityBuilders[i].GetEntityType(); - - var groupedEntities = _groupEntityDB[entityGID.groupID]; - var typeSafeDictionary = groupedEntities[entityType]; - - typeSafeDictionary.RemoveEntityFromEngines(entityGID, _entityEngines); - RemoveEntityFromSafeDictionary(group, entityType, entityGID); + MoveEntity(entityGID, toGroup, @group, entityType); } - RemoveEntityFromSafeDictionary(group, _typeEntityInfoView, entityGID); + MoveEntity(entityGID, toGroup, @group, _typeEntityInfoView); } - static void RemoveEntityFromSafeDictionary(Dictionary @group, Type entityType, EGID id) + void MoveEntity(EGID entityGID, Dictionary toGroup, Dictionary @group, Type entityType) { - var typeSafeList = @group[entityType]; - typeSafeList.Remove(id.entityID); - if (typeSafeList.Count == 0)//clean up + var fromTypeSafeDictionary = @group[entityType]; + ITypeSafeDictionary safeDictionary = null; + + if (toGroup != null) + { + if (toGroup.TryGetValue(entityType, out safeDictionary) == false) + { + safeDictionary = fromTypeSafeDictionary.Create(); + toGroup.Add(entityType, safeDictionary); + } + } + + fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(entityGID, safeDictionary, _entityEngines); + + if (fromTypeSafeDictionary.Count == 0) //clean up + { @group.Remove(entityType); + } + + //it doesn't eliminate the group itself on purpose } void RemoveGroupAndEntitiesFromDB(int groupID) @@ -156,38 +166,12 @@ namespace Svelto.ECS DBC.ECS.Check.Require(fromGroupID != toGroupID, "can't move an entity to the same group where it already belongs to"); - var entityegid = new EGID(entityID, fromGroupID); - var groupedEntities = _groupEntityDB[fromGroupID]; - var entityInfoViewDictionary = (TypeSafeDictionary) groupedEntities[_typeEntityInfoView]; - var entityViewBuilders = entityInfoViewDictionary[entityegid.entityID].entityToBuild; - var entityViewBuildersCount = entityViewBuilders.Length; - - Dictionary groupedEntityViewsTyped; - if (_groupEntityDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) - { - groupedEntityViewsTyped = new Dictionary(); - - _groupEntityDB.Add(toGroupID, groupedEntityViewsTyped); - } - - ITypeSafeDictionary toSafeDic; - - for (var i = 0; i < entityViewBuildersCount; i++) - { - var entityViewBuilder = entityViewBuilders[i]; - var entityViewType = entityViewBuilder.GetEntityType(); - - var fromSafeDic = groupedEntities[entityViewType]; - if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeDic) == false) - groupedEntityViewsTyped[entityViewType] = toSafeDic = fromSafeDic.Create(); - - entityViewBuilder.MoveEntityView(entityegid, toGroupID, fromSafeDic, toSafeDic); - } + Dictionary toGroup; - if (groupedEntityViewsTyped.TryGetValue(_typeEntityInfoView, out toSafeDic) == false) - groupedEntityViewsTyped[_typeEntityInfoView] = toSafeDic = entityInfoViewDictionary.Create(); + if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false) + toGroup = _groupEntityDB[toGroupID] = new Dictionary(); - EntityBuilder.MoveEntityView(entityegid, toGroupID, entityInfoViewDictionary, toSafeDic); + MoveEntity(new EGID(entityID, fromGroupID), toGroup); } EGID SwapFirstEntityGroup(int fromGroupID, int toGroupId) @@ -202,6 +186,7 @@ namespace Svelto.ECS readonly entitiesDB _DB; readonly Dictionary> _groupEntityDB; + readonly Dictionary> _groupedGroups; //yes I am being sarcastic static readonly Type _typeEntityInfoView = typeof(EntityInfoView); } diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs index 23f3c70..9904927 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs @@ -19,17 +19,17 @@ namespace Svelto.ECS public void RemoveEntity(int entityID) { - _weakReference.Target.RemoveEntity(new EGID(entityID)); + _weakReference.Target.MoveEntity(new EGID(entityID)); } public void RemoveEntity(int entityID, int groupID) { - _weakReference.Target.RemoveEntity(new EGID(entityID, groupID)); + _weakReference.Target.MoveEntity(new EGID(entityID, groupID)); } public void RemoveEntity(EGID entityEGID) { - _weakReference.Target.RemoveEntity(entityEGID); + _weakReference.Target.MoveEntity(entityEGID); } public void RemoveGroupAndEntities(int groupID) diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index c167c3d..f0c510f 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -76,7 +76,7 @@ namespace Svelto.ECS { foreach (var entityViewsPerType in groupToSubmit.Value) { - entityViewsPerType.Value.AddEntityViewsToEngines(_entityEngines); + entityViewsPerType.Value.AddEntitiesToEngines(_entityEngines); } } } diff --git a/Svelto.ECS/EntityViewBuilder.cs b/Svelto.ECS/EntityViewBuilder.cs index abe154f..bdaddb0 100644 --- a/Svelto.ECS/EntityViewBuilder.cs +++ b/Svelto.ECS/EntityViewBuilder.cs @@ -89,22 +89,6 @@ namespace Svelto.ECS return ENTITY_VIEW_TYPE; } - void IEntityBuilder.MoveEntityView(EGID entityID, int toGroupID, ITypeSafeDictionary fromSafeDic, ITypeSafeDictionary toSafeDic) - { - MoveEntityView(entityID, toGroupID, fromSafeDic, toSafeDic); - } - - public static void MoveEntityView(EGID entityID, int toGroupID, ITypeSafeDictionary fromSafeDic, ITypeSafeDictionary toSafeDic) - { - var fromCastedDic = fromSafeDic as TypeSafeDictionary; - var toCastedDic = toSafeDic as TypeSafeDictionary; - - var entity = fromCastedDic[entityID.entityID]; - fromCastedDic.Remove(entityID.entityID); - entity.ID = new EGID(entityID.entityID, toGroupID); - toCastedDic.Add(entityID.entityID, entity); - } - static FasterList>> entityViewBlazingFastReflection { get { return EntityView.cachedFields; } diff --git a/Svelto.ECS/IEntityBuilder.cs b/Svelto.ECS/IEntityBuilder.cs index db73201..d53f21b 100644 --- a/Svelto.ECS/IEntityBuilder.cs +++ b/Svelto.ECS/IEntityBuilder.cs @@ -9,6 +9,5 @@ namespace Svelto.ECS ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary dictionary, int size); Type GetEntityType(); - void MoveEntityView(EGID entityID, int toGroupID, ITypeSafeDictionary fromSafeDic, ITypeSafeDictionary toSafeDic); } } \ No newline at end of file