diff --git a/ECS/DataStructures/TypeSafeFasterListForECS.cs b/ECS/DataStructures/TypeSafeFasterListForECS.cs index 4bca085..4f15c15 100644 --- a/ECS/DataStructures/TypeSafeFasterListForECS.cs +++ b/ECS/DataStructures/TypeSafeFasterListForECS.cs @@ -16,6 +16,7 @@ namespace Svelto.ECS.Internal ITypeSafeDictionary CreateIndexedDictionary(); IEntityView[] ToArrayFast(out int count); void ReserveCapacity(int capacity); + int GetIndexFromID(int entityID); } class TypeSafeFasterListForECS: FasterList where T:IEntityView @@ -69,6 +70,11 @@ namespace Svelto.ECS.Internal Resize(capacity); } + public int GetIndexFromID(int entityID) + { + return _mappedIndices[entityID]; + } + readonly Dictionary _mappedIndices; } diff --git a/ECS/EnginesRootEntities.cs b/ECS/EnginesRootEntities.cs index f9c3896..6b58966 100644 --- a/ECS/EnginesRootEntities.cs +++ b/ECS/EnginesRootEntities.cs @@ -171,22 +171,22 @@ namespace Svelto.ECS var entityViewBuilders = EntityDescriptorTemplate.Default.entityViewsToBuild; int entityViewBuildersCount = entityViewBuilders.Length; + Dictionary dictionary = _groupEntityViewsDB[fromGroupID]; + + Dictionary groupedEntityViewsTyped; + if (_groupEntityViewsDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) + { + groupedEntityViewsTyped = new Dictionary(); + + _groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped); + } + for (int i = 0; i < entityViewBuildersCount; i++) { IEntityViewBuilder entityViewBuilder = entityViewBuilders[i]; Type entityViewType = entityViewBuilder.GetEntityViewType(); - Dictionary dictionary = _groupEntityViewsDB[fromGroupID]; ITypeSafeList fromSafeList = dictionary[entityViewType]; - - Dictionary groupedEntityViewsTyped; - if (_groupEntityViewsDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) - { - groupedEntityViewsTyped = new Dictionary(); - - _groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped); - } - ITypeSafeList toSafeList; if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeList) == false) @@ -198,9 +198,9 @@ namespace Svelto.ECS if (fromSafeList.UnorderedRemove(entityID) == false) dictionary.Remove(entityViewType); - - if (dictionary.Count == 0) _groupEntityViewsDB.Remove(fromGroupID); } + + if (dictionary.Count == 0) _groupEntityViewsDB.Remove(fromGroupID); } void InternalRemove(IEntityViewBuilder[] entityViewBuilders, int entityID, @@ -241,16 +241,17 @@ namespace Svelto.ECS { int entityViewBuildersCount = entityViewBuilders.Length; + Dictionary dictionary = _groupEntityViewsDB[groupID]; + for (int i = 0; i < entityViewBuildersCount; i++) { Type entityViewType = entityViewBuilders[i].GetEntityViewType(); - Dictionary dictionary = _groupEntityViewsDB[groupID]; if (dictionary[entityViewType].UnorderedRemove(entityID) == false) dictionary.Remove(entityViewType); - - if (dictionary.Count == 0) _groupEntityViewsDB.Remove(groupID); } + + if (dictionary.Count == 0) _groupEntityViewsDB.Remove(groupID); } static void RemoveEntityViewFromEngines(Dictionary> entityViewEngines, diff --git a/ECS/EntityDescriptor.cs b/ECS/EntityDescriptor.cs index 91e09aa..9f3a46f 100644 --- a/ECS/EntityDescriptor.cs +++ b/ECS/EntityDescriptor.cs @@ -39,51 +39,37 @@ namespace Svelto.ECS.Internal EntityDescriptorInfo entityViewsToBuildDescriptor, object[] implementors) { - var entityViewsToBuild = entityViewsToBuildDescriptor.entityViewsToBuild; - int count = entityViewsToBuild.Length; + Dictionary groupedEntityViewsTyped; - RemoveEntityImplementor removeEntityImplementor = null; - - for (int index = 0; index < count; index++) + if (groupEntityViewsByType.TryGetValue(groupID, out groupedEntityViewsTyped) == false) { - var entityViewBuilder = entityViewsToBuild[index]; - var entityViewType = entityViewBuilder.GetEntityViewType(); - - Dictionary groupedEntityViewsTyped; - - if (groupEntityViewsByType.TryGetValue(groupID, out groupedEntityViewsTyped) == false) - { - groupedEntityViewsTyped = new Dictionary(); - groupEntityViewsByType.Add(groupID, groupedEntityViewsTyped); - } - - //only class EntityView will be returned - //struct EntityView cannot be filled so it will be null. - var entityViewObjectToFill = - BuildEntityView(entityID, groupedEntityViewsTyped, entityViewType, entityViewBuilder); + groupedEntityViewsTyped = new Dictionary(); + groupEntityViewsByType.Add(groupID, groupedEntityViewsTyped); + } - //the semantic of this code must still be improved - //but only classes can be filled, so I am aware - //it's a EntityViewWithID - if (entityViewObjectToFill != null) - { - if (removeEntityImplementor == null) - removeEntityImplementor = new RemoveEntityImplementor(entityViewsToBuildDescriptor.entityViewsToBuild, groupID); + var removeEntityImplementor = new RemoveEntityImplementor(entityViewsToBuildDescriptor.entityViewsToBuild, groupID); - FillEntityView(entityViewObjectToFill as EntityView, implementors, removeEntityImplementor, - entityViewsToBuildDescriptor.name); - } - } + InternalBuildEntityViews(entityID, groupedEntityViewsTyped, entityViewsToBuildDescriptor, implementors, removeEntityImplementor); } internal static void BuildEntityViews(int entityID, Dictionary entityViewsByType, EntityDescriptorInfo entityViewsToBuildDescriptor, object[] implementors) + { + var removeEntityImplementor = entityViewsToBuildDescriptor.removeEntityImplementor; + + InternalBuildEntityViews(entityID, entityViewsByType, entityViewsToBuildDescriptor, implementors, removeEntityImplementor); + } + + private static void InternalBuildEntityViews(int entityID, + Dictionary entityViewsByType, + EntityDescriptorInfo entityViewsToBuildDescriptor, + object[] implementors, RemoveEntityImplementor removeEntityImplementor) { var entityViewsToBuild = entityViewsToBuildDescriptor.entityViewsToBuild; int count = entityViewsToBuild.Length; - + for (int index = 0; index < count; index++) { var entityViewBuilder = entityViewsToBuild[index]; @@ -99,7 +85,7 @@ namespace Svelto.ECS.Internal //it's a EntityView if (entityViewObjectToFill != null) { - FillEntityView(entityViewObjectToFill as EntityView, implementors, entityViewsToBuildDescriptor.removeEntityImplementor, + FillEntityView(entityViewObjectToFill as EntityView, implementors, removeEntityImplementor, entityViewsToBuildDescriptor.name); } } diff --git a/ECS/EntityViewBuilder.cs b/ECS/EntityViewBuilder.cs index b512cf9..73606af 100644 --- a/ECS/EntityViewBuilder.cs +++ b/ECS/EntityViewBuilder.cs @@ -48,7 +48,7 @@ namespace Svelto.ECS var fromCastedList = fromSafeList as TypeSafeFasterListForECSForClasses; var toCastedList = toSafeList as TypeSafeFasterListForECSForClasses; - toCastedList.Add(fromCastedList[entityID]); + toCastedList.Add(fromCastedList[fromCastedList.GetIndexFromID(entityID)]); } readonly Type _entityViewType = typeof(EntityViewType); @@ -91,7 +91,7 @@ namespace Svelto.ECS var fromCastedList = fromSafeList as TypeSafeFasterListForECSForStructs; var toCastedList = toSafeList as TypeSafeFasterListForECSForStructs; - toCastedList.Add(fromCastedList[entityID]); + toCastedList.Add(fromCastedList[fromCastedList.GetIndexFromID(entityID)]); } readonly Type _entityViewType = typeof(EntityViewType);