Browse Source

rename file

added the new function SwapEntityGroup to be able to easily move entities between groups
tags/Rel2b
sebas77 6 years ago
parent
commit
d180597458
6 changed files with 84 additions and 8 deletions
  1. +10
    -1
      ECS/DataStructures/TypeSafeFasterListForECS.cs
  2. +51
    -2
      ECS/EnginesRootEntities.cs
  3. +2
    -2
      ECS/EnginesRootSubmission.cs
  4. +1
    -1
      ECS/EntityDescriptor.cs
  5. +17
    -0
      ECS/EntityViewBuilder.cs
  6. +3
    -2
      ECS/IEnginesInterfaces.cs

+ 10
- 1
ECS/DataStructures/TypeSafeFasterListForECS.cs View File

@@ -48,12 +48,21 @@ namespace Svelto.ECS.Internal
{
var index = this.Count;
AddRange(entityViewListValue as FasterList<T>);
base.AddRange(entityViewListValue as FasterList<T>);
for (int i = index; i < Count; ++i)
_mappedIndices[this[i].ID] = i;
}

new public void Add(T entityView)
{
var index = this.Count;

base.Add(entityView);

_mappedIndices[entityView.ID] = index;
}

public void ReserveCapacity(int capacity)
{
if (this.ToArrayFast().Length < capacity)


+ 51
- 2
ECS/EnginesRootEntities.cs View File

@@ -164,6 +164,45 @@ namespace Svelto.ECS
InternalRemove(EntityDescriptorTemplate<T>.Default.entityViewsToBuild, entityID, _groupEntityViewsDB[groupID]);
}

void SwapEntityGroup<T>(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new()
{
DesignByContract.Check.Require(fromGroupID != toGroupID, "can't move an entity to the same group where it already belongs to");

var entityViewBuilders = EntityDescriptorTemplate<T>.Default.entityViewsToBuild;
int entityViewBuildersCount = entityViewBuilders.Length;

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)
{
toSafeList = fromSafeList.Create();
}

entityViewBuilder.MoveEntityView(entityID, fromSafeList, toSafeList);

if (fromSafeList.UnorderedRemove(entityID) == false)
dictionary.Remove(entityViewType);

if (dictionary.Count == 0) _groupEntityViewsDB.Remove(fromGroupID);
}
}

void InternalRemove(IEntityViewBuilder[] entityViewBuilders, int entityID,
Dictionary<Type, ITypeSafeList> entityViewsDB)
{
@@ -192,6 +231,13 @@ namespace Svelto.ECS

void InternalRemove(IEntityViewBuilder[] entityViewBuilders, int entityID, int groupID,
Dictionary<Type, ITypeSafeList> entityViewsDB)
{
InternalRemoveFromDB(entityViewBuilders, entityID, groupID);

InternalRemove(entityViewBuilders, entityID, entityViewsDB);
}

void InternalRemoveFromDB(IEntityViewBuilder[] entityViewBuilders, int entityID, int groupID)
{
int entityViewBuildersCount = entityViewBuilders.Length;

@@ -205,8 +251,6 @@ namespace Svelto.ECS

if (dictionary.Count == 0) _groupEntityViewsDB.Remove(groupID);
}

InternalRemove(entityViewBuilders, entityID, entityViewsDB);
}

static void RemoveEntityViewFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngine>> entityViewEngines,
@@ -297,6 +341,11 @@ namespace Svelto.ECS
_weakReference.Target.RemoveEntity<T>(entityID);
}

public void SwapEntityGroup<T>(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.SwapEntityGroup<T>(entityID, fromGroupID, toGroupID);
}

readonly DataStructures.WeakReference<EnginesRoot> _weakReference;
}



+ 2
- 2
ECS/EnginesRootSubmission.cs View File

@@ -89,9 +89,9 @@ namespace Svelto.ECS
{
foreach (var group in groupedEntityViewsToAdd)
{
AddEntityViewsToTheDBAndSuitableEngines(group.Value, entityViewsDB);

AddEntityViewsToGroupDB(groupEntityViewsDB, @group);

AddEntityViewsToTheDBAndSuitableEngines(group.Value, entityViewsDB);
}
}



ECS/EntityDescriptorTemplate.cs → ECS/EntityDescriptor.cs View File

@@ -105,7 +105,7 @@ namespace Svelto.ECS.Internal
}
}

static IEntityView BuildEntityView(int entityID, Dictionary<Type, ITypeSafeList> entityViewsByType,
internal static IEntityView BuildEntityView(int entityID, Dictionary<Type, ITypeSafeList> entityViewsByType,
Type entityViewType, IEntityViewBuilder entityViewBuilder)
{
ITypeSafeList entityViewsList;

+ 17
- 0
ECS/EntityViewBuilder.cs View File

@@ -9,6 +9,7 @@ namespace Svelto.ECS
ITypeSafeList Preallocate(ref ITypeSafeList list, int size);

Type GetEntityViewType();
void MoveEntityView(int entityID, ITypeSafeList fromSafeList, ITypeSafeList toSafeList);
}

public class EntityViewBuilder<EntityViewType> : IEntityViewBuilder where EntityViewType : EntityView, new()
@@ -42,6 +43,14 @@ namespace Svelto.ECS
return _entityViewType;
}

public void MoveEntityView(int entityID, ITypeSafeList fromSafeList, ITypeSafeList toSafeList)
{
var fromCastedList = fromSafeList as TypeSafeFasterListForECSForClasses<EntityViewType>;
var toCastedList = toSafeList as TypeSafeFasterListForECSForClasses<EntityViewType>;

toCastedList.Add(fromCastedList[entityID]);
}

readonly Type _entityViewType = typeof(EntityViewType);
}

@@ -77,6 +86,14 @@ namespace Svelto.ECS
return _entityViewType;
}

public void MoveEntityView(int entityID, ITypeSafeList fromSafeList, ITypeSafeList toSafeList)
{
var fromCastedList = fromSafeList as TypeSafeFasterListForECSForStructs<EntityViewType>;
var toCastedList = toSafeList as TypeSafeFasterListForECSForStructs<EntityViewType>;

toCastedList.Add(fromCastedList[entityID]);
}

readonly Type _entityViewType = typeof(EntityViewType);
}
}

+ 3
- 2
ECS/IEnginesInterfaces.cs View File

@@ -16,11 +16,12 @@ namespace Svelto.ECS
public interface IEntityFunctions
{
void RemoveEntity(int entityID, IRemoveEntityComponent removeInfo);

void RemoveEntity<T>(int entityID) where T:IEntityDescriptor, new();
void RemoveMetaEntity<T>(int metaEntityID) where T:IEntityDescriptor, new();

void RemoveEntityFromGroup<T>(int entityID, int groupID) where T:IEntityDescriptor, new();

void SwapEntityGroup<T>(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new();
}
}

Loading…
Cancel
Save