Browse Source

add the possibility to dispose engines (IDisposable engines are recognized)

improved tracking of entity groups (maybe fixed some bugs)
tags/2.7
sebas77 6 years ago
parent
commit
397f286a53
3 changed files with 34 additions and 14 deletions
  1. +5
    -0
      Svelto.ECS/EnginesRoot.Engines.cs
  2. +25
    -13
      Svelto.ECS/EnginesRoot.Entities.cs
  3. +4
    -1
      Svelto.ECS/EnginesRoot.Submission.cs

+ 5
- 0
Svelto.ECS/EnginesRoot.Engines.cs View File

@@ -39,6 +39,7 @@ namespace Svelto.ECS
_entitiesOperations = new FasterList<EntitySubmitOperation>();
_entityEngines = new Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>();
_otherEngines = new FasterList<IEngine>();
_disposableEngines = new FasterList<IDisposable>();

_groupEntityDB = new FasterDictionary<int, Dictionary<Type, ITypeSafeDictionary>>();
_groupedGroups = new Dictionary<Type, FasterDictionary<int, ITypeSafeDictionary>>();
@@ -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<Type, FasterList<IHandleEntityViewEngineAbstracted>> _entityEngines;
readonly FasterList<IEngine> _otherEngines;
readonly FasterList<IDisposable> _disposableEngines;
static readonly Type _objectType = typeof(object);
}

+ 25
- 13
Svelto.ECS/EnginesRoot.Entities.cs View File

@@ -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<int, ITypeSafeDictionary>();
dictionaryOfEntities = fromTypeSafeDictionary.Create();
toGroup.Add(entityType, dictionaryOfEntities);
}

_groupedGroups[entityType][toGroupID] = safeDictionary;
FasterDictionary<int, ITypeSafeDictionary> groupedGroup;
if (_groupedGroups.TryGetValue(entityType, out groupedGroup) == false)
groupedGroup = _groupedGroups[entityType] = new FasterDictionary<int, ITypeSafeDictionary>();
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);
}



+ 4
- 1
Svelto.ECS/EnginesRoot.Submission.cs View File

@@ -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<int, ITypeSafeDictionary>();
groupedGroup = _groupedGroups[entityViewTypeSafeDictionary.Key] =
new FasterDictionary<int, ITypeSafeDictionary>();

//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<int, Dictionary<Type, ITypeSafeDictionary>> _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<Type, FasterDictionary<int, ITypeSafeDictionary>> _groupedGroups; //yes I am being sarcastic
readonly DoubleBufferedEntitiesToAdd<FasterDictionary<int, Dictionary<Type, ITypeSafeDictionary>>> _groupedEntityToAdd;
readonly EntitySubmissionScheduler _scheduler;


Loading…
Cancel
Save