Browse Source

improve code and fix a bug with the SwapEntityGroup function

tags/Rel2b
sebas77 6 years ago
parent
commit
a9e20edd1b
4 changed files with 43 additions and 50 deletions
  1. +6
    -0
      ECS/DataStructures/TypeSafeFasterListForECS.cs
  2. +16
    -15
      ECS/EnginesRootEntities.cs
  3. +19
    -33
      ECS/EntityDescriptor.cs
  4. +2
    -2
      ECS/EntityViewBuilder.cs

+ 6
- 0
ECS/DataStructures/TypeSafeFasterListForECS.cs View File

@@ -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<T>: FasterList<T> where T:IEntityView
@@ -69,6 +70,11 @@ namespace Svelto.ECS.Internal
Resize(capacity);
}

public int GetIndexFromID(int entityID)
{
return _mappedIndices[entityID];
}

readonly Dictionary<int, int> _mappedIndices;
}



+ 16
- 15
ECS/EnginesRootEntities.cs View File

@@ -171,22 +171,22 @@ namespace Svelto.ECS
var entityViewBuilders = EntityDescriptorTemplate<T>.Default.entityViewsToBuild;
int entityViewBuildersCount = entityViewBuilders.Length;

Dictionary<Type, ITypeSafeList> dictionary = _groupEntityViewsDB[fromGroupID];

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

_groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped);
}

for (int i = 0; i < entityViewBuildersCount; i++)
{
IEntityViewBuilder entityViewBuilder = entityViewBuilders[i];
Type entityViewType = entityViewBuilder.GetEntityViewType();
Dictionary<Type, ITypeSafeList> dictionary = _groupEntityViewsDB[fromGroupID];

ITypeSafeList fromSafeList = dictionary[entityViewType];

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

_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<Type, ITypeSafeList> dictionary = _groupEntityViewsDB[groupID];

for (int i = 0; i < entityViewBuildersCount; i++)
{
Type entityViewType = entityViewBuilders[i].GetEntityViewType();
Dictionary<Type, ITypeSafeList> 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<Type, FasterList<IHandleEntityViewEngine>> entityViewEngines,


+ 19
- 33
ECS/EntityDescriptor.cs View File

@@ -39,51 +39,37 @@ namespace Svelto.ECS.Internal
EntityDescriptorInfo entityViewsToBuildDescriptor,
object[] implementors)
{
var entityViewsToBuild = entityViewsToBuildDescriptor.entityViewsToBuild;
int count = entityViewsToBuild.Length;
Dictionary<Type, ITypeSafeList> 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<Type, ITypeSafeList> groupedEntityViewsTyped;

if (groupEntityViewsByType.TryGetValue(groupID, out groupedEntityViewsTyped) == false)
{
groupedEntityViewsTyped = new Dictionary<Type, ITypeSafeList>();
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<Type, ITypeSafeList>();
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<Type, ITypeSafeList> entityViewsByType,
EntityDescriptorInfo entityViewsToBuildDescriptor,
object[] implementors)
{
var removeEntityImplementor = entityViewsToBuildDescriptor.removeEntityImplementor;

InternalBuildEntityViews(entityID, entityViewsByType, entityViewsToBuildDescriptor, implementors, removeEntityImplementor);
}

private static void InternalBuildEntityViews(int entityID,
Dictionary<Type, ITypeSafeList> 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);
}
}


+ 2
- 2
ECS/EntityViewBuilder.cs View File

@@ -48,7 +48,7 @@ namespace Svelto.ECS
var fromCastedList = fromSafeList as TypeSafeFasterListForECSForClasses<EntityViewType>;
var toCastedList = toSafeList as TypeSafeFasterListForECSForClasses<EntityViewType>;

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<EntityViewType>;
var toCastedList = toSafeList as TypeSafeFasterListForECSForStructs<EntityViewType>;

toCastedList.Add(fromCastedList[entityID]);
toCastedList.Add(fromCastedList[fromCastedList.GetIndexFromID(entityID)]);
}

readonly Type _entityViewType = typeof(EntityViewType);


Loading…
Cancel
Save