diff --git a/Svelto.ECS/EnginesRoot.Engines.cs b/Svelto.ECS/EnginesRoot.Engines.cs index dc0adab..1438ee0 100644 --- a/Svelto.ECS/EnginesRoot.Engines.cs +++ b/Svelto.ECS/EnginesRoot.Engines.cs @@ -39,6 +39,7 @@ namespace Svelto.ECS _entitiesOperations = new FasterList(); _entityEngines = new Dictionary>(); _otherEngines = new FasterList(); + _disposableEngines = new FasterList(); _groupEntityDB = new FasterDictionary>(); _groupedGroups = new Dictionary>(); @@ -62,6 +63,9 @@ namespace Svelto.ECS else _otherEngines.Add(engine); + if (engine is IDisposable) + _disposableEngines.Add(engine as IDisposable); + var queryableEntityViewEngine = engine as IQueryingEntitiesEngine; if (queryableEntityViewEngine != null) { @@ -119,6 +123,7 @@ namespace Svelto.ECS readonly Dictionary> _entityEngines; readonly FasterList _otherEngines; + readonly FasterList _disposableEngines; static readonly Type _objectType = typeof(object); } diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index c2d8b49..77c9713 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -22,6 +22,9 @@ namespace Svelto.ECS foreach (var groups in _groupEntityDB) foreach (var entityList in groups.Value) entityList.Value.RemoveEntitiesFromEngines(_entityEngines); + + foreach (var engine in _disposableEngines) + engine.Dispose(); } ///-------------------------------------------- @@ -166,40 +169,49 @@ namespace Svelto.ECS { DBC.ECS.Check.Require(fromGroup.ContainsKey(entityType) == true, "from group not found"); var fromTypeSafeDictionary = fromGroup[entityType]; - ITypeSafeDictionary safeDictionary = null; + ITypeSafeDictionary dictionaryOfEntities = null; + //in case we want to move to a new group, otherwise is just a remove if (toGroup != null) { - if (toGroup.TryGetValue(entityType, out safeDictionary) == false) + if (toGroup.TryGetValue(entityType, out dictionaryOfEntities) == false) { - safeDictionary = fromTypeSafeDictionary.Create(); - toGroup.Add(entityType, safeDictionary); - _groupedGroups[entityType] = new FasterDictionary(); + dictionaryOfEntities = fromTypeSafeDictionary.Create(); + toGroup.Add(entityType, dictionaryOfEntities); } - _groupedGroups[entityType][toGroupID] = safeDictionary; + FasterDictionary groupedGroup; + if (_groupedGroups.TryGetValue(entityType, out groupedGroup) == false) + groupedGroup = _groupedGroups[entityType] = new FasterDictionary(); + + groupedGroup[toGroupID] = dictionaryOfEntities; } DBC.ECS.Check.Assert(fromTypeSafeDictionary.Has(entityGID.entityID), "entity not found"); - fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(entityGID, toGroupID, safeDictionary, _entityEngines); + fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(entityGID, toGroupID, dictionaryOfEntities, _entityEngines); if (fromTypeSafeDictionary.Count == 0) //clean up { _groupedGroups[entityType].Remove(entityGID.groupID); - //it's probably better to not remove this, but the dictionary should be trimmed? - //fromGroup.Remove(entityType); + //I don't remove the group if empty on purpose, in case it needs to be reused + //however I trim it to save memory fromTypeSafeDictionary.Trim(); } - - //it doesn't eliminate the fromGroup itself on purpose } void RemoveGroupAndEntitiesFromDB(int groupID) { - foreach (var entiTypeSafeList in _groupEntityDB[groupID]) - entiTypeSafeList.Value.RemoveEntitiesFromEngines(_entityEngines); + var dictionariesOfEntities = _groupEntityDB[groupID]; + foreach (var dictionaryOfEntities in dictionariesOfEntities) + { + dictionaryOfEntities.Value.RemoveEntitiesFromEngines(_entityEngines); + var groupedGroupOfEntities = _groupedGroups[dictionaryOfEntities.Key]; + groupedGroupOfEntities.Remove(groupID); + } + //careful, in this case I assume you really don't want to use this group anymore + //so I remove it from the database _groupEntityDB.Remove(groupID); } diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index 7aaa2c1..578bf88 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -86,7 +86,8 @@ namespace Svelto.ECS dbDic = groupDB[entityViewTypeSafeDictionary.Key] = entityViewTypeSafeDictionary.Value.Create(); if (_groupedGroups.TryGetValue(entityViewTypeSafeDictionary.Key, out groupedGroup) == false) - groupedGroup = _groupedGroups[entityViewTypeSafeDictionary.Key] = new FasterDictionary(); + groupedGroup = _groupedGroups[entityViewTypeSafeDictionary.Key] = + new FasterDictionary(); //Fill the DB with the entity views generate this frame. dbDic.FillWithIndexedEntities(entityViewTypeSafeDictionary.Value); @@ -111,7 +112,9 @@ namespace Svelto.ECS //to the FasterDictionary capabilities 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 + //for each group id, save a dictionary indexed by entity type of entities indexed by id readonly FasterDictionary> _groupEntityDB; + //for each entity view type, return the groups (dictionary of entities indexed by entity id) where they are found indexed by group id readonly Dictionary> _groupedGroups; //yes I am being sarcastic readonly DoubleBufferedEntitiesToAdd>> _groupedEntityToAdd; readonly EntitySubmissionScheduler _scheduler;