Browse Source

simplify the MoveEntity code to maintain less code and unify behaviours

tags/Rel25b
sebas77 6 years ago
parent
commit
c6549bed1f
8 changed files with 67 additions and 80 deletions
  1. +1
    -1
      Svelto.Common
  2. +26
    -10
      Svelto.ECS/DataStructures/TypeSafeDictionary.cs
  3. +6
    -3
      Svelto.ECS/EnginesRoot.Engines.cs
  4. +30
    -45
      Svelto.ECS/EnginesRoot.Entities.cs
  5. +3
    -3
      Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs
  6. +1
    -1
      Svelto.ECS/EnginesRoot.Submission.cs
  7. +0
    -16
      Svelto.ECS/EntityViewBuilder.cs
  8. +0
    -1
      Svelto.ECS/IEntityBuilder.cs

+ 1
- 1
Svelto.Common

@@ -1 +1 @@
Subproject commit 5b756d57920c83a8e31dad9eedee8b629c6cb245
Subproject commit a1f5f55c8b28f66ed033fa02c425dfa16c3c30e6

+ 26
- 10
Svelto.ECS/DataStructures/TypeSafeDictionary.cs View File

@@ -20,12 +20,13 @@ namespace Svelto.ECS.Internal
void RemoveEntitiesFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
entityViewEnginesDB);

void RemoveEntityFromEngines(EGID entityGid,
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
entityViewEnginesDB);
void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid,
ITypeSafeDictionary toGroup,
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
entityViewEnginesDB);
void FillWithIndexedEntities(ITypeSafeDictionary entities);
void AddEntityViewsToEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB);
void AddEntitiesToEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB);
void AddCapacity(int size);
bool Remove(int idGid);
@@ -61,7 +62,7 @@ namespace Svelto.ECS.Internal
}
}

public void AddEntityViewsToEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB)
public void AddEntitiesToEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB)
{
int count;
TValue[] values = GetFasterValuesBuffer(out count);
@@ -83,11 +84,26 @@ namespace Svelto.ECS.Internal
(entityViewsEngines[i] as IHandleEntityStructEngine<TValue>).AddInternal(ref entity);
}

public void RemoveEntityFromEngines(EGID entityGid,
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
entityViewEnginesDB)
public void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, ITypeSafeDictionary toGroup,
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
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<TValue>);
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<TValue>();


+ 6
- 3
Svelto.ECS/EnginesRoot.Engines.cs View File

@@ -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<Type, FasterList<IHandleEntityViewEngineAbstracted>>();
_otherEngines = new FasterList<IEngine>();

_groupEntityDB = new Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>();
_groupEntityDB[ExclusiveGroup.StandardEntitiesGroup] = new Dictionary<Type, ITypeSafeDictionary>();
_groupEntityDB = new Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>
{
[ExclusiveGroup.StandardEntitiesGroup] = new Dictionary<Type, ITypeSafeDictionary>()
};
_groupedGroups = new Dictionary<Type, FasterDictionary<int, int>>();
_groupedEntityToAdd = new DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>>();

_DB = new entitiesDB(_groupEntityDB);


+ 30
- 45
Svelto.ECS/EnginesRoot.Entities.cs View File

@@ -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<Type, ITypeSafeDictionary> toGroup = null)
{
var entityViewInfoDictionary = _groupEntityDB[entityGID.groupID][_typeEntityInfoView] as TypeSafeDictionary<EntityInfoView>;
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<Type, ITypeSafeDictionary> @group, Type entityType, EGID id)
void MoveEntity(EGID entityGID, Dictionary<Type, ITypeSafeDictionary> toGroup, Dictionary<Type, ITypeSafeDictionary> @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<EntityInfoView>) groupedEntities[_typeEntityInfoView];
var entityViewBuilders = entityInfoViewDictionary[entityegid.entityID].entityToBuild;
var entityViewBuildersCount = entityViewBuilders.Length;

Dictionary<Type, ITypeSafeDictionary> groupedEntityViewsTyped;
if (_groupEntityDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false)
{
groupedEntityViewsTyped = new Dictionary<Type, ITypeSafeDictionary>();

_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<Type, ITypeSafeDictionary> 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<Type, ITypeSafeDictionary>();

EntityBuilder<EntityInfoView>.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<int, Dictionary<Type, ITypeSafeDictionary>> _groupEntityDB;
readonly Dictionary<Type, FasterDictionary<int, int>> _groupedGroups; //yes I am being sarcastic

static readonly Type _typeEntityInfoView = typeof(EntityInfoView);
}


+ 3
- 3
Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs View File

@@ -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)


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

@@ -76,7 +76,7 @@ namespace Svelto.ECS
{
foreach (var entityViewsPerType in groupToSubmit.Value)
{
entityViewsPerType.Value.AddEntityViewsToEngines(_entityEngines);
entityViewsPerType.Value.AddEntitiesToEngines(_entityEngines);
}
}
}


+ 0
- 16
Svelto.ECS/EntityViewBuilder.cs View File

@@ -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<T>;
var toCastedDic = toSafeDic as TypeSafeDictionary<T>;

var entity = fromCastedDic[entityID.entityID];
fromCastedDic.Remove(entityID.entityID);
entity.ID = new EGID(entityID.entityID, toGroupID);
toCastedDic.Add(entityID.entityID, entity);
}

static FasterList<KeyValuePair<Type, ActionCast<T>>> entityViewBlazingFastReflection
{
get { return EntityView<T>.cachedFields; }


+ 0
- 1
Svelto.ECS/IEntityBuilder.cs View File

@@ -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);
}
}

Loading…
Cancel
Save