Browse Source

Added SwapEntityGroup that allows to change the ID of the entity too (useful to keep tracking changes of IDs from servers)

improve logs
tags/2.7
sebas77 6 years ago
parent
commit
36341f86c9
8 changed files with 95 additions and 55 deletions
  1. +1
    -1
      Svelto.Common
  2. +3
    -3
      Svelto.ECS/DataStructures/TypeSafeDictionary.cs
  3. +12
    -11
      Svelto.ECS/EnginesRoot.Entities.cs
  4. +48
    -25
      Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs
  5. +15
    -8
      Svelto.ECS/EnginesRoot.Submission.cs
  6. +7
    -2
      Svelto.ECS/EntityBuilder.cs
  7. +5
    -2
      Svelto.ECS/EntitySubmitOperation.cs
  8. +4
    -3
      Svelto.ECS/IEntityFunctions.cs

+ 1
- 1
Svelto.Common

@@ -1 +1 @@
Subproject commit 5a7036b8d0c3a2ba7ad6046f7b284e11ae40496c
Subproject commit f1c40592636518e977111beeb8c3ce96392c9765

+ 3
- 3
Svelto.ECS/DataStructures/TypeSafeDictionary.cs View File

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

void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, int toGroupID, ITypeSafeDictionary toGroup,
void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, EGID toEntityID, ITypeSafeDictionary toGroup,
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
entityViewEnginesDB);
@@ -91,7 +91,7 @@ namespace Svelto.ECS.Internal
(entityViewsEngines[i] as IHandleEntityStructEngine<TValue>).AddInternal(ref entity);
}

public void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, int toGroupID, ITypeSafeDictionary toGroup,
public void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, EGID toEntityID, ITypeSafeDictionary toGroup,
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
entityViewEnginesDB)
{
@@ -105,7 +105,7 @@ namespace Svelto.ECS.Internal
if (toGroup != null)
{
var toGroupCasted = toGroup as TypeSafeDictionary<TValue>;
fasterValuesBuffer[valueIndex].ID = new EGID(fromEntityGid.entityID, toGroupID);
fasterValuesBuffer[valueIndex].ID = toEntityID;
toGroupCasted.Add(fromEntityGid.entityID, ref fasterValuesBuffer[valueIndex]);
if (entityViewEnginesDB != null)


+ 12
- 11
Svelto.ECS/EnginesRoot.Entities.cs View File

@@ -120,7 +120,7 @@ namespace Svelto.ECS
///--------------------------------------------
///
void MoveEntity(IEntityBuilder[] entityBuilders, EGID entityGID, Type originalDescriptorType, int toGroupID = -1,
void MoveEntity(IEntityBuilder[] entityBuilders, EGID entityGID, Type originalDescriptorType, EGID toEntityGID,
Dictionary<Type, ITypeSafeDictionary> toGroup = null)
{
//for each entity view generated by the entity descriptor
@@ -141,7 +141,7 @@ namespace Svelto.ECS
var entitiesToMove = entityInfoView.entitiesToBuild;
for (int i = 0; i < entitiesToMove.Length; i++)
MoveEntityView(entityGID, toGroupID, toGroup, fromGroup, entitiesToMove[i].GetEntityType());
MoveEntityView(entityGID, toEntityGID, toGroup, fromGroup, entitiesToMove[i].GetEntityType());
}
//otherwise it's a normal static entity descriptor
else
@@ -153,11 +153,11 @@ namespace Svelto.ECS
originalDescriptorType.Name));
for (var i = 0; i < entityBuilders.Length; i++)
MoveEntityView(entityGID, toGroupID, toGroup, fromGroup, entityBuilders[i].GetEntityType());
MoveEntityView(entityGID, toEntityGID, toGroup, fromGroup, entityBuilders[i].GetEntityType());
}
}

void MoveEntityView(EGID entityGID, int toGroupID, Dictionary<Type, ITypeSafeDictionary> toGroup,
void MoveEntityView(EGID entityGID, EGID toEntityGID, Dictionary<Type, ITypeSafeDictionary> toGroup,
Dictionary<Type, ITypeSafeDictionary> fromGroup, Type entityType)
{
ITypeSafeDictionary fromTypeSafeDictionary;
@@ -181,14 +181,14 @@ namespace Svelto.ECS
if (_groupsPerEntity.TryGetValue(entityType, out groupedGroup) == false)
groupedGroup = _groupsPerEntity[entityType] = new FasterDictionary<int, ITypeSafeDictionary>();
groupedGroup[toGroupID] = dictionaryOfEntities;
groupedGroup[toEntityGID.groupID] = dictionaryOfEntities;
}

if (fromTypeSafeDictionary.Has(entityGID.entityID) == false)
{
throw new EntityNotFoundException(entityGID.entityID, entityGID.groupID, entityType);
}
fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(entityGID, toGroupID, dictionaryOfEntities, _entityEngines);
fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(entityGID, toEntityGID, dictionaryOfEntities, _entityEngines);

if (fromTypeSafeDictionary.Count == 0) //clean up
{
@@ -217,16 +217,17 @@ namespace Svelto.ECS

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

void SwapEntityGroup(IEntityBuilder[] builders, Type originalEntityDescriptor, int entityID, int fromGroupID, int toGroupID)
void SwapEntityGroup(IEntityBuilder[] builders, Type originalEntityDescriptor, EGID fromEntityID, EGID toEntityID)
{
DBC.ECS.Check.Require(fromGroupID != toGroupID, "the entity is already in this group");
DBC.ECS.Check.Require(fromEntityID != toEntityID, "the entity destionation EGID is equal to the source EGID");

Dictionary<Type, ITypeSafeDictionary> toGroup;

if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false)
toGroup = _groupEntityDB[toGroupID] = new Dictionary<Type, ITypeSafeDictionary>();
if (_groupEntityDB.TryGetValue(toEntityID.groupID, out toGroup) == false)
toGroup = _groupEntityDB[toEntityID.groupID] = new Dictionary<Type, ITypeSafeDictionary>();

MoveEntity(builders, new EGID(entityID, fromGroupID), originalEntityDescriptor, toGroupID, toGroup);
MoveEntity(builders, fromEntityID, originalEntityDescriptor,
toEntityID, toGroup);
}
readonly Type _entityInfoView = typeof(EntityInfoView);


+ 48
- 25
Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs View File

@@ -1,4 +1,5 @@
using System;
using Svelto.ECS.Internal;

#if ENGINE_PROFILER_ENABLED && UNITY_EDITOR
using Svelto.ECS.Profiler;
@@ -19,19 +20,22 @@ namespace Svelto.ECS

public void RemoveEntity<T>(int entityID, int groupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.CheckRemoveEntityID(new EGID(entityID, groupID), EntityDescriptorTemplate<T>.descriptor);
_weakReference.Target.CheckRemoveEntityID(new EGID(entityID, groupID),
EntityDescriptorTemplate<T>.descriptor);
_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, groupID, -1,
new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, entityID, groupID, -1,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}

public void RemoveEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T : IEntityDescriptor, new()
public void RemoveEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T :
IEntityDescriptor, new()
{
_weakReference.Target.CheckRemoveEntityID(new EGID(entityID, (int) groupID), EntityDescriptorTemplate<T>.descriptor);
_weakReference.Target.CheckRemoveEntityID(new EGID(entityID, (int)groupID),
EntityDescriptorTemplate<T>.descriptor);
_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, (int)groupID, -1,
new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, entityID, groupID, -1,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}

@@ -40,53 +44,72 @@ namespace Svelto.ECS
_weakReference.Target.CheckRemoveEntityID(entityEGID, EntityDescriptorTemplate<T>.descriptor);

_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityEGID.entityID, entityEGID.groupID,
new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityEGID.entityID, entityEGID.entityID,
entityEGID.groupID,
-1, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}

public void RemoveGroupAndEntities(int groupID)
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, groupID, -1, null, null));
new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, -1, groupID, -1, null, null));
}

public void RemoveGroupAndEntities(ExclusiveGroup.ExclusiveGroupStruct groupID)
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, (int)groupID, -1, null, null));
new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, -1, groupID, -1, null, null));
}

public void SwapEntityGroup<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new()
public void SwapEntityGroup<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID,
ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Swap,
entityID,
(int) fromGroupID,
(int) toGroupID,
new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, entityID, fromGroupID, toGroupID,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}

public void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new()
public void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID)
where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Swap,
id.entityID,
id.groupID,
(int) toGroupID,
id.entityID, id.entityID, id.groupID, toGroupID,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}
public void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new()
public void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID
, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup) where T : IEntityDescriptor, new()
{
if (id.groupID != mustBeFromGroup)
throw new ECSException("Entity is not coming from the expected group");
_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Swap,
id.entityID,
id.groupID,
(int) toGroupID,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
new EntitySubmitOperation(EntitySubmitOperationType.Swap,
id.entityID, id.entityID, id.groupID, toGroupID,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}
public void SwapEntityGroup<T>(EGID id, EGID toID)
where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Swap,
id.entityID, toID.entityID, id.groupID, toID.groupID,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}
public void SwapEntityGroup<T>(EGID id, EGID toID
, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup) where T : IEntityDescriptor, new()
{
if (id.groupID != mustBeFromGroup)
throw new ECSException("Entity is not coming from the expected group");
_weakReference.Target.QueueEntitySubmitOperation<T>(
new EntitySubmitOperation(EntitySubmitOperationType.Swap,
id.entityID, toID.entityID, id.groupID, toID.groupID,
EntityDescriptorTemplate<T>.descriptor.entitiesToBuild, typeof(T)));
}
}
void QueueEntitySubmitOperation(EntitySubmitOperation entitySubmitOperation)
@@ -101,12 +124,12 @@ namespace Svelto.ECS
{
#if DEBUG && !PROFILER
entitySubmitOperation.trace = Environment.StackTrace;
var egid = new EGID(entitySubmitOperation.id, entitySubmitOperation.fromGroupID);
var egid = new EGID(entitySubmitOperation.ID, entitySubmitOperation.fromGroupID);
if (_entitiesOperationsDebug.ContainsKey(egid) == true)
Utilities.Console.LogError("Only one entity operation per submission is allowed. Entity "
.FastConcat(" with not found ID is about to be removed: ")
.FastConcat(" id: ")
.FastConcat(entitySubmitOperation.id)
.FastConcat(entitySubmitOperation.ID)
.FastConcat(" groupid: ")
.FastConcat(entitySubmitOperation.fromGroupID)
//.FastConcat(entitySubmitOperation.fromGroupID.GetType().Name)); do this later


+ 15
- 8
Svelto.ECS/EnginesRoot.Submission.cs View File

@@ -39,15 +39,17 @@ namespace Svelto.ECS
case EntitySubmitOperationType.Swap:
SwapEntityGroup(entitiesOperations[i].builders,
entitiesOperations[i].entityDescriptor,
entitiesOperations[i].id,
entitiesOperations[i].fromGroupID,
entitiesOperations[i].toGroupID);
new EGID(entitiesOperations[i].ID,
entitiesOperations[i].fromGroupID),
new EGID(
entitiesOperations[i].toID,
entitiesOperations[i].toGroupID));
break;
case EntitySubmitOperationType.Remove:
MoveEntity(entitiesOperations[i].builders,
new EGID(entitiesOperations[i].id,
new EGID(entitiesOperations[i].ID,
entitiesOperations[i].fromGroupID),
entitiesOperations[i].entityDescriptor);
entitiesOperations[i].entityDescriptor, new EGID());
break;
case EntitySubmitOperationType.RemoveGroup:
RemoveGroupAndEntitiesFromDB(entitiesOperations[i].fromGroupID);
@@ -59,19 +61,24 @@ namespace Svelto.ECS
#if DEBUG && !PROFILER
var str = "Entity Operation is ".FastConcat(entitiesOperations[i].type.ToString())
.FastConcat(" id: ")
.FastConcat(entitiesOperations[i].id)
.FastConcat(entitiesOperations[i].ID)
.FastConcat(" to id: ")
.FastConcat(entitiesOperations[i].toID)
.FastConcat(" from groupid: ")
.FastConcat(entitiesOperations[i].fromGroupID)
.FastConcat(" to groupid: ")
.FastConcat(entitiesOperations[i].toGroupID);

Console.LogError(e.Message.FastConcat(" ", str, " ", entitiesOperations[i].trace));
#if STRICT_ECS
throw;
#endif
#else
var str = "Entity Operation is ".FastConcat(entitiesOperations[i].type.ToString())
.FastConcat(" id: ")
.FastConcat(entitiesOperations[i].id)
.FastConcat(entitiesOperations[i].ID)
.FastConcat(" to id: ")
.FastConcat(entitiesOperations[i].toID)
.FastConcat(" from groupid: ")
.FastConcat(entitiesOperations[i].fromGroupID)
.FastConcat(" to groupid: ")


+ 7
- 2
Svelto.ECS/EntityBuilder.cs View File

@@ -33,7 +33,7 @@ namespace Svelto.ECS
if (needsReflection == false )
{
if (type.IsClass)
throw new ECSException("IEntityStructs must be structs");
throw new ECSException("IEntityStructs must be structs - EntityView:".FastConcat(typeof(T)));

var fields = type.GetFields(BindingFlags.Public |
BindingFlags.Instance);
@@ -72,8 +72,13 @@ namespace Svelto.ECS
var fields = type.GetFields(BindingFlags.Public |
BindingFlags.Instance);

if (fields.Length < 1)
throw new ECSException("Invalid entity view struct detected");
#if STRICT_ECS
throw new ECSException("Invalid entity view struct detected - EntityView:".FastConcat(typeof(T)));
#else
Svelto.Utilities.Console.LogError("Invalid entity view struct detected - EntityView:".FastConcat(typeof(T)));
#endif
}
}



+ 5
- 2
Svelto.ECS/EntitySubmitOperation.cs View File

@@ -6,7 +6,8 @@ namespace Svelto.ECS
{
public readonly EntitySubmitOperationType type;
public readonly IEntityBuilder[] builders;
public readonly int id;
public readonly int ID;
public readonly int toID;
public readonly int toGroupID;
public readonly int fromGroupID;
public readonly Type entityDescriptor;
@@ -16,6 +17,7 @@ namespace Svelto.ECS

public EntitySubmitOperation(EntitySubmitOperationType operation,
int entityId,
int toId,
int fromGroupId,
int toGroupId,
IEntityBuilder[] builders,
@@ -23,7 +25,8 @@ namespace Svelto.ECS
{
type = operation;
this.builders = builders;
id = entityId;
ID = entityId;
toID = toId;
toGroupID = toGroupId;
fromGroupID = fromGroupId;


+ 4
- 3
Svelto.ECS/IEntityFunctions.cs View File

@@ -1,5 +1,3 @@
using Svelto.ECS.Internal;

namespace Svelto.ECS
{
public interface IEntityFunctions
@@ -16,6 +14,9 @@ namespace Svelto.ECS
void SwapEntityGroup<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, EGID toId) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, EGID toId, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup) where T : IEntityDescriptor, new();
}
}

Loading…
Cancel
Save