Browse Source

Swap and Remove entity now needs the entity descriptor

tags/2.6a
sebas77 6 years ago
parent
commit
0358c48974
9 changed files with 57 additions and 86 deletions
  1. +22
    -44
      Svelto.ECS/EnginesRoot.Entities.cs
  2. +6
    -6
      Svelto.ECS/EnginesRoot.GenericEntityFactory.cs
  3. +14
    -14
      Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs
  4. +0
    -7
      Svelto.ECS/EntityFactory.cs
  5. +2
    -2
      Svelto.ECS/Extensions/Unity/GenericEntityDescriptorHolder.cs
  6. +1
    -1
      Svelto.ECS/IEntityDescriptorHolder.cs
  7. +3
    -3
      Svelto.ECS/IEntityFactory.cs
  8. +7
    -7
      Svelto.ECS/IEntityFunctions.cs
  9. +2
    -2
      Svelto.ECS/StaticEntityDescriptorInfo.cs

+ 22
- 44
Svelto.ECS/EnginesRoot.Entities.cs View File

@@ -41,30 +41,21 @@ namespace Svelto.ECS
EntityStructInitializer BuildEntity<T>(EGID entityID, object[] implementors)
where T : IEntityDescriptor, new()
{
var descriptorEntitiesToBuild = EntityDescriptorTemplate<T>.descriptor.entitiesToBuild;
#if DEBUG && !PROFILER
CheckEntityID(entityID, descriptorEntitiesToBuild);
#endif
var dic = EntityFactory.BuildGroupedEntityViews(entityID,
_groupedEntityToAdd.current,
descriptorEntitiesToBuild,
implementors);

_newEntitiesBuiltToProcess++;
return new EntityStructInitializer(entityID, dic);
return BuildEntity(entityID, EntityDescriptorTemplate<T>.descriptor, implementors);
}

EntityStructInitializer BuildEntity(EGID entityID,
IEntityBuilder[] entityToBuild,
IEntityDescriptor entityDescriptor,
object[] implementors)
{
var descriptorEntitiesToBuild = entityDescriptor.entitiesToBuild;
#if DEBUG && !PROFILER
CheckEntityID(entityID, entityToBuild);
CheckEntityID(entityID, entityDescriptor);
#endif
var dic = EntityFactory.BuildGroupedEntityViews(entityID,
_groupedEntityToAdd.current,
entityToBuild,
descriptorEntitiesToBuild,
implementors);
_newEntitiesBuiltToProcess++;
@@ -72,27 +63,28 @@ namespace Svelto.ECS
return new EntityStructInitializer(entityID, dic);
}
void CheckEntityID(EGID entityID, IEntityBuilder[] descriptorEntitiesToBuild)
void CheckEntityID(EGID entityID, IEntityDescriptor descriptorEntity)
{
Dictionary<Type, ITypeSafeDictionary> @group;
var descriptorEntitiesToBuild = descriptorEntity.entitiesToBuild;
if (_groupEntityDB.TryGetValue(entityID.groupID, out @group) == true)
{
for (int i = 0; i < descriptorEntitiesToBuild.Length; i++)
{
CheckEntityID(entityID, descriptorEntitiesToBuild[i].GetEntityType(), @group);
CheckEntityID(entityID, descriptorEntitiesToBuild[i].GetEntityType(), @group, descriptorEntity.ToString());
}
CheckEntityID(entityID, _typeEntityInfoView, @group);
}
}

static void CheckEntityID(EGID entityID, Type entityType, Dictionary<Type, ITypeSafeDictionary> @group)
static void CheckEntityID(EGID entityID, Type entityType, Dictionary<Type, ITypeSafeDictionary> @group, string name)
{
ITypeSafeDictionary entities;
if (@group.TryGetValue(entityType, out entities))
{
if (entities.Has(entityID.entityID) == true)
{
Utility.Console.LogError("Entity with used ID is about to be built: "
Utility.Console.LogError("Entity ".FastConcat(name, " with used ID is about to be built: ")
.FastConcat(entityType)
.FastConcat(" id: ")
.FastConcat(entityID.entityID)
@@ -136,24 +128,13 @@ namespace Svelto.ECS
else
dbList.AddCapacity(size);
}

if (group.TryGetValue(_typeEntityInfoView, out dbList) == false)
group[_typeEntityInfoView] = EntityBuilder<EntityInfoView>.Preallocate(ref dbList, size);
else
dbList.AddCapacity(size);

if (@groupBuffer.TryGetValue(_typeEntityInfoView, out dbList) == false)
@groupBuffer[_typeEntityInfoView] = EntityBuilder<EntityInfoView>.Preallocate(ref dbList, size);
else
dbList.AddCapacity(size);
}
///--------------------------------------------
///
void MoveEntity(EGID entityGID, int toGroupID = -1, Dictionary<Type, ITypeSafeDictionary> toGroup = null)
void MoveEntity<T>(EGID entityGID, int toGroupID = -1, Dictionary<Type, ITypeSafeDictionary> toGroup = null) where T:IEntityDescriptor, new ()
{
var entityViewInfoDictionary = _groupEntityDB[entityGID.groupID][_typeEntityInfoView] as TypeSafeDictionary<EntityInfoView>;
var entityBuilders = entityViewInfoDictionary[entityGID.entityID].entityToBuild;
var entityBuilders = EntityDescriptorTemplate<T>.descriptor.entitiesToBuild;
var entityBuildersCount = entityBuilders.Length;
//for each entity view generated by the entity descriptor
@@ -161,13 +142,11 @@ namespace Svelto.ECS
{
var entityType = entityBuilders[i].GetEntityType();
MoveEntity(entityGID, toGroupID, toGroup, entityType);
MoveEntity<T>(entityGID, toGroupID, toGroup, entityType);
}

MoveEntity(entityGID, toGroupID, toGroup, _typeEntityInfoView);
}

void MoveEntity(EGID fromEntityGID, int toGroupID, Dictionary<Type, ITypeSafeDictionary> toGroup, Type entityType)
void MoveEntity<T>(EGID fromEntityGID, int toGroupID, Dictionary<Type, ITypeSafeDictionary> toGroup, Type entityType) where T:IEntityDescriptor, new ()
{
var fromGroup = _groupEntityDB[fromEntityGID.groupID];

@@ -210,7 +189,7 @@ namespace Svelto.ECS

///--------------------------------------------

void SwapEntityGroup(int entityID, int fromGroupID, int toGroupID)
void SwapEntityGroup<T>(int entityID, int fromGroupID, int toGroupID) where T:IEntityDescriptor, new ()
{
DBC.ECS.Check.Require(fromGroupID != toGroupID,
"can't move an entity to the same fromGroup where it already belongs to");
@@ -220,22 +199,21 @@ namespace Svelto.ECS
if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false)
toGroup = _groupEntityDB[toGroupID] = new Dictionary<Type, ITypeSafeDictionary>();

MoveEntity(new EGID(entityID, fromGroupID), toGroupID, toGroup);
MoveEntity<T>(new EGID(entityID, fromGroupID), toGroupID, toGroup);
}
EGID SwapFirstEntityInGroup(int fromGroupID, int toGroupId)
EGID SwapFirstEntityInGroup<T>(int fromGroupID, int toGroupId) where T:IEntityDescriptor, new()
{
var firstID =
((TypeSafeDictionary<EntityInfoView>) _groupEntityDB[fromGroupID][_typeEntityInfoView]).FasterValues[0].ID.entityID;
var firstID = _groupEntityDB[fromGroupID][EntityDescriptorTemplate<T>.descriptor.entitiesToBuild[0]
.GetEntityType()].GetFirstID();
SwapEntityGroup(firstID, fromGroupID, toGroupId);
SwapEntityGroup<T>(firstID, fromGroupID, toGroupId);
return new EGID(firstID, toGroupId);
}

readonly entitiesDB _DB;
static readonly Type _typeEntityInfoView = typeof(EntityInfoView);
int _newEntitiesBuiltToProcess;
}



+ 6
- 6
Svelto.ECS/EnginesRoot.GenericEntityFactory.cs View File

@@ -30,19 +30,19 @@ namespace Svelto.ECS
return _weakEngine.Target.BuildEntity<T>(egid, implementors);
}

public EntityStructInitializer BuildEntity(int entityID, IEntityBuilder[] entityToBuild, object[] implementors)
public EntityStructInitializer BuildEntity(int entityID, IEntityDescriptor descriptorEntity, object[] implementors)
{
return _weakEngine.Target.BuildEntity(new EGID(entityID), entityToBuild, implementors);
return _weakEngine.Target.BuildEntity(new EGID(entityID), descriptorEntity, implementors);
}

public EntityStructInitializer BuildEntity(EGID egid, IEntityBuilder[] entityToBuild, object[] implementors)
public EntityStructInitializer BuildEntity(EGID egid, IEntityDescriptor descriptorEntity, object[] implementors)
{
return _weakEngine.Target.BuildEntity(egid, entityToBuild, implementors);
return _weakEngine.Target.BuildEntity(egid, descriptorEntity, implementors);
}

public EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, IEntityBuilder[] entityToBuild, object[] implementors)
public EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, IEntityDescriptor descriptorEntity, object[] implementors)
{
return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupID), entityToBuild, implementors);
return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupID), descriptorEntity, implementors);
}
public void PreallocateEntitySpace<T>(int size) where T : IEntityDescriptor, new()


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

@@ -17,19 +17,19 @@ namespace Svelto.ECS
_weakReference = weakReference;
}

public void RemoveEntity(int entityID)
public void RemoveEntity<T>(int entityID) where T : IEntityDescriptor, new()
{
_weakReference.Target.MoveEntity(new EGID(entityID));
_weakReference.Target.MoveEntity<T>(new EGID(entityID));
}
public void RemoveEntity(int entityID, int groupID)
public void RemoveEntity<T>(int entityID, int groupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.MoveEntity(new EGID(entityID, groupID));
_weakReference.Target.MoveEntity<T>(new EGID(entityID, groupID));
}

public void RemoveEntity(EGID entityEGID)
public void RemoveEntity<T>(EGID entityEGID) where T : IEntityDescriptor, new()
{
_weakReference.Target.MoveEntity(entityEGID);
_weakReference.Target.MoveEntity<T>(entityEGID);
}

public void RemoveGroupAndEntities(int groupID)
@@ -37,24 +37,24 @@ namespace Svelto.ECS
_weakReference.Target.RemoveGroupAndEntitiesFromDB(groupID);
}

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

public void SwapEntityGroup(EGID id, int toGroupID = ExclusiveGroup.StandardEntitiesGroup)
public void SwapEntityGroup<T>(EGID id, int toGroupID = ExclusiveGroup.StandardEntitiesGroup) where T : IEntityDescriptor, new()
{
_weakReference.Target.SwapEntityGroup(id.entityID, id.groupID, toGroupID);
_weakReference.Target.SwapEntityGroup<T>(id.entityID, id.groupID, toGroupID);
}

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

public EGID SwapFirstEntityGroup(int fromGroupID, int toGroupID)
public EGID SwapFirstEntityGroup<T>(int fromGroupID, int toGroupID) where T : IEntityDescriptor, new()
{
return _weakReference.Target.SwapFirstEntityInGroup( fromGroupID, toGroupID);
return _weakReference.Target.SwapFirstEntityInGroup<T>( fromGroupID, toGroupID);
}
}
}

+ 0
- 7
Svelto.ECS/EntityFactory.cs View File

@@ -46,10 +46,6 @@ namespace Svelto.ECS.Internal

BuildEntityView(entityID, @group, entityViewType, entityViewBuilder, implementors);
}

_builder._initializer = new EntityInfoView {entityToBuild = entityToBuild};
BuildEntityView(entityID, @group, _viewType, _builder, null);
}

static void BuildEntityView(EGID entityID, Dictionary<Type, ITypeSafeDictionary> @group,
@@ -69,8 +65,5 @@ namespace Svelto.ECS.Internal
if (entityViewsPoolWillBeCreated)
@group.Add(entityViewType, safeDictionary);
}
static readonly EntityBuilder<EntityInfoView> _builder = new EntityBuilder<EntityInfoView>();
static readonly Type _viewType = typeof(EntityInfoView);
}
}

+ 2
- 2
Svelto.ECS/Extensions/Unity/GenericEntityDescriptorHolder.cs View File

@@ -5,9 +5,9 @@ namespace Svelto.ECS.Unity
UnityEngine.MonoBehaviour , IEntityDescriptorHolder
where T: IEntityDescriptor, new()
{
public IEntityBuilder[] GetEntitiesToBuild()
public IEntityDescriptor GetDescriptor()
{
return EntityDescriptorTemplate<T>.descriptor.entitiesToBuild;
return EntityDescriptorTemplate<T>.descriptor;
}
}
}

+ 1
- 1
Svelto.ECS/IEntityDescriptorHolder.cs View File

@@ -2,6 +2,6 @@ namespace Svelto.ECS
{
public interface IEntityDescriptorHolder
{
IEntityBuilder[] GetEntitiesToBuild();
IEntityDescriptor GetDescriptor();
}
}

+ 3
- 3
Svelto.ECS/IEntityFactory.cs View File

@@ -59,8 +59,8 @@ namespace Svelto.ECS
/// <param name="entityDescriptor"></param>
/// <param name="implementors"></param>
///
EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, IEntityBuilder[] entityToBuild, object[] implementors);
EntityStructInitializer BuildEntity(int entityID, IEntityBuilder[] entityToBuild, object[] implementors);
EntityStructInitializer BuildEntity(EGID egid, IEntityBuilder[] entityToBuild, object[] implementors);
EntityStructInitializer BuildEntity(int entityID, ExclusiveGroup groupID, IEntityDescriptor descriptorEntity, object[] implementors);
EntityStructInitializer BuildEntity(int entityID, IEntityDescriptor descriptorEntity, object[] implementors);
EntityStructInitializer BuildEntity(EGID egid, IEntityDescriptor descriptorEntity, object[] implementors);
}
}

+ 7
- 7
Svelto.ECS/IEntityFunctions.cs View File

@@ -7,15 +7,15 @@ namespace Svelto.ECS
//being entity ID globally not unique, the group must be specified when
//an entity is removed. Not specificing the group will attempt to remove
//the entity from the special standard group.
void RemoveEntity(int entityID);
void RemoveEntity(int entityID, int groupID);
void RemoveEntity(EGID entityegid);
void RemoveEntity<T>(int entityID) where T : IEntityDescriptor, new();
void RemoveEntity<T>(int entityID, int groupID) where T : IEntityDescriptor, new();
void RemoveEntity<T>(EGID entityegid) where T : IEntityDescriptor, new();

void RemoveGroupAndEntities(int groupID);
void SwapEntityGroup(int entityID, int fromGroupID, int toGroupID = ExclusiveGroup.StandardEntitiesGroup);
void SwapEntityGroup(EGID id, int toGroupID = ExclusiveGroup.StandardEntitiesGroup);
void SwapEntityGroup(int entityID, int toGroupID);
EGID SwapFirstEntityGroup(int fromGroupID = ExclusiveGroup.StandardEntitiesGroup, int toGroupID = ExclusiveGroup.StandardEntitiesGroup);
void SwapEntityGroup<T>(int entityID, int fromGroupID, int toGroupID = ExclusiveGroup.StandardEntitiesGroup) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, int toGroupID = ExclusiveGroup.StandardEntitiesGroup) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(int entityID, int toGroupID) where T : IEntityDescriptor, new();
EGID SwapFirstEntityGroup<T>(int fromGroupID = ExclusiveGroup.StandardEntitiesGroup, int toGroupID = ExclusiveGroup.StandardEntitiesGroup) where T : IEntityDescriptor, new();
}
}

+ 2
- 2
Svelto.ECS/StaticEntityDescriptorInfo.cs View File

@@ -42,9 +42,9 @@ namespace Svelto.ECS
public IEntityBuilder[] entitiesToBuild { get; private set; }
}

public struct StaticEntityDescriptorInfo<TType>: IEntityDescriptor where TType : IEntityDescriptor
public class StaticEntityDescriptorInfo<TType>: IEntityDescriptor where TType : IEntityDescriptor
{
internal StaticEntityDescriptorInfo(TType descriptor) : this()
internal StaticEntityDescriptorInfo(TType descriptor)
{
entitiesToBuild = descriptor.entitiesToBuild;
}


Loading…
Cancel
Save