From 5691f224a5f0cc35efc3df532f107151880ebd25 Mon Sep 17 00:00:00 2001 From: sebas77 Date: Fri, 6 Jul 2018 18:34:22 +0100 Subject: [PATCH] Fix bug that was preventing the adding of an entity that was deleted --- .../DataStructures/TypeSafeDictionary.cs | 6 ++--- Svelto.ECS/EnginesRoot.Entities.cs | 26 +++++++++++-------- Svelto.ECS/EnginesRoot.Submission.cs | 8 +++--- Svelto.ECS/EntityFactory.cs | 20 +++++++------- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index ef83f92..240a62d 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -24,7 +24,7 @@ namespace Svelto.ECS.Internal Dictionary> entityViewEnginesDB); - void FillWithIndexedEntityViews(ITypeSafeDictionary entityViews); + void FillWithIndexedEntities(ITypeSafeDictionary entities); void AddEntityViewsToEngines(Dictionary> entityViewEnginesDB); void AddCapacity(int size); @@ -41,10 +41,10 @@ namespace Svelto.ECS.Internal public TypeSafeDictionary() {} - public void FillWithIndexedEntityViews(ITypeSafeDictionary entityViews) + public void FillWithIndexedEntities(ITypeSafeDictionary entities) { int count; - var buffer = (entityViews as TypeSafeDictionary).GetFasterValuesBuffer(out count); + var buffer = (entities as TypeSafeDictionary).GetFasterValuesBuffer(out count); try { diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 47401d9..6d24a5f 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -113,26 +113,31 @@ namespace Svelto.ECS var entityInfoView = entityViewInfoDictionary[entityGID.entityID]; var entityBuilders = entityInfoView.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 group = _groupEntityDB[entityGID.groupID]; + var groupedEntities = _groupEntityDB[entityGID.groupID]; var typeSafeDictionary = groupedEntities[entityType]; typeSafeDictionary.RemoveEntityFromEngines(entityGID, _entityEngines); - RemoveEntityFromGroup(group, entityType, entityGID); + RemoveEntityFromSafeDictionary(group, entityType, entityGID); } + + RemoveEntityFromSafeDictionary(group, _typeEntityInfoView, entityGID); } - static void RemoveEntityFromGroup(Dictionary @group, Type entityType, EGID id) + static void RemoveEntityFromSafeDictionary(Dictionary @group, Type entityType, EGID id) { var typeSafeList = @group[entityType]; - if (typeSafeList.Remove(id.entityID) == false) //clean up + typeSafeList.Remove(id.entityID); + if (typeSafeList.Count == 0)//clean up @group.Remove(entityType); } @@ -195,11 +200,10 @@ namespace Svelto.ECS return new EGID(firstID, toGroupId); } - readonly entitiesDB _DB; - - //grouped set of entity views, this is the standard way to handle entity views - readonly Dictionary> _groupEntityDB; - static readonly Type _typeEntityInfoView = typeof(EntityInfoView); + readonly entitiesDB _DB; + readonly Dictionary> _groupEntityDB; + + static readonly Type _typeEntityInfoView = typeof(EntityInfoView); } public struct EntityStructInitializer @@ -220,6 +224,6 @@ namespace Svelto.ECS } readonly Dictionary _current; - readonly EGID _id; + readonly EGID _id; } } \ No newline at end of file diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index 5a9cac1..c167c3d 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -45,7 +45,7 @@ namespace Svelto.ECS } } - //todo: groupsToSubmit can be semplified as data structure? + //todo: groupsToSubmit can be simplified as data structure? void AddEntityViewsToTheDBAndSuitableEngines(Dictionary> groupsOfEntitiesToSubmit) { //each group is indexed by entity view type. for each type there is a dictionary indexed by entityID @@ -66,7 +66,7 @@ namespace Svelto.ECS dbDic = groupDB[entityViewTypeSafeDictionary.Key] = entityViewTypeSafeDictionary.Value.Create(); //type safe copy - dbDic.FillWithIndexedEntityViews(entityViewTypeSafeDictionary.Value); + dbDic.FillWithIndexedEntities(entityViewTypeSafeDictionary.Value); } } @@ -86,7 +86,7 @@ namespace Svelto.ECS //split by type per group. It's possible to get all the entities of a give type T per group thanks //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 DoubleBufferedEntityViews>> _groupedEntityToAdd; - readonly EntitySubmissionScheduler _scheduler; + readonly DoubleBufferedEntityViews>> _groupedEntityToAdd; + readonly EntitySubmissionScheduler _scheduler; } } \ No newline at end of file diff --git a/Svelto.ECS/EntityFactory.cs b/Svelto.ECS/EntityFactory.cs index 2f498a8..679cd19 100644 --- a/Svelto.ECS/EntityFactory.cs +++ b/Svelto.ECS/EntityFactory.cs @@ -5,10 +5,11 @@ namespace Svelto.ECS.Internal { static class EntityFactory { - internal static Dictionary BuildGroupedEntityViews(EGID egid, - Dictionary> groupEntityViewsByType, - IEntityBuilder[] entityToBuild, - object[] implementors) + internal static Dictionary + BuildGroupedEntityViews(EGID egid, + Dictionary> groupEntityViewsByType, + IEntityBuilder[] entityToBuild, + object[] implementors) { var @group = FetchEntityViewGroup(egid.groupID, groupEntityViewsByType); @@ -17,7 +18,8 @@ namespace Svelto.ECS.Internal return group; } - static Dictionary FetchEntityViewGroup(int groupID, Dictionary> groupEntityViewsByType) + static Dictionary FetchEntityViewGroup(int groupID, + Dictionary> groupEntityViewsByType) { Dictionary group; @@ -45,8 +47,8 @@ namespace Svelto.ECS.Internal BuildEntityView(entityID, @group, entityViewType, entityViewBuilder, implementors); } - Builder._initializer = new EntityInfoView {entityToBuild = entityToBuild}; - BuildEntityView(entityID, @group, _viewType, Builder, null); + _builder._initializer = new EntityInfoView {entityToBuild = entityToBuild}; + BuildEntityView(entityID, @group, _viewType, _builder, null); } static void BuildEntityView(EGID entityID, Dictionary @group, @@ -66,7 +68,7 @@ namespace Svelto.ECS.Internal @group.Add(entityViewType, safeDictionary); } - static readonly EntityBuilder Builder = new EntityBuilder(); - static readonly Type _viewType = typeof(EntityInfoView); + static readonly EntityBuilder _builder = new EntityBuilder(); + static readonly Type _viewType = typeof(EntityInfoView); } } \ No newline at end of file