diff --git a/Svelto.Common b/Svelto.Common index 5a7036b..f1c4059 160000 --- a/Svelto.Common +++ b/Svelto.Common @@ -1 +1 @@ -Subproject commit 5a7036b8d0c3a2ba7ad6046f7b284e11ae40496c +Subproject commit f1c40592636518e977111beeb8c3ce96392c9765 diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index ebc1e81..40affa4 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -12,7 +12,7 @@ namespace Svelto.ECS.Internal void RemoveEntitiesFromEngines(Dictionary> entityViewEnginesDB); - void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, int toGroupID, ITypeSafeDictionary toGroup, + void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, EGID toEntityID, ITypeSafeDictionary toGroup, Dictionary> entityViewEnginesDB); @@ -91,7 +91,7 @@ namespace Svelto.ECS.Internal (entityViewsEngines[i] as IHandleEntityStructEngine).AddInternal(ref entity); } - public void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, int toGroupID, ITypeSafeDictionary toGroup, + public void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, EGID toEntityID, ITypeSafeDictionary toGroup, Dictionary> entityViewEnginesDB) { @@ -105,7 +105,7 @@ namespace Svelto.ECS.Internal if (toGroup != null) { var toGroupCasted = toGroup as TypeSafeDictionary; - fasterValuesBuffer[valueIndex].ID = new EGID(fromEntityGid.entityID, toGroupID); + fasterValuesBuffer[valueIndex].ID = toEntityID; toGroupCasted.Add(fromEntityGid.entityID, ref fasterValuesBuffer[valueIndex]); if (entityViewEnginesDB != null) diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 2001195..413a068 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -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 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 toGroup, + void MoveEntityView(EGID entityGID, EGID toEntityGID, Dictionary toGroup, Dictionary fromGroup, Type entityType) { ITypeSafeDictionary fromTypeSafeDictionary; @@ -181,14 +181,14 @@ namespace Svelto.ECS if (_groupsPerEntity.TryGetValue(entityType, out groupedGroup) == false) groupedGroup = _groupsPerEntity[entityType] = new FasterDictionary(); - 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 toGroup; - if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false) - toGroup = _groupEntityDB[toGroupID] = new Dictionary(); + if (_groupEntityDB.TryGetValue(toEntityID.groupID, out toGroup) == false) + toGroup = _groupEntityDB[toEntityID.groupID] = new Dictionary(); - MoveEntity(builders, new EGID(entityID, fromGroupID), originalEntityDescriptor, toGroupID, toGroup); + MoveEntity(builders, fromEntityID, originalEntityDescriptor, + toEntityID, toGroup); } readonly Type _entityInfoView = typeof(EntityInfoView); diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs index 56e74d7..b5e8a35 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs @@ -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(int entityID, int groupID) where T : IEntityDescriptor, new() { - _weakReference.Target.CheckRemoveEntityID(new EGID(entityID, groupID), EntityDescriptorTemplate.descriptor); + _weakReference.Target.CheckRemoveEntityID(new EGID(entityID, groupID), + EntityDescriptorTemplate.descriptor); _weakReference.Target.QueueEntitySubmitOperation( - new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, groupID, -1, + new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, entityID, groupID, -1, EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); } - public void RemoveEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T : IEntityDescriptor, new() + public void RemoveEntity(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T : + IEntityDescriptor, new() { - _weakReference.Target.CheckRemoveEntityID(new EGID(entityID, (int) groupID), EntityDescriptorTemplate.descriptor); + _weakReference.Target.CheckRemoveEntityID(new EGID(entityID, (int)groupID), + EntityDescriptorTemplate.descriptor); _weakReference.Target.QueueEntitySubmitOperation( - new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, (int)groupID, -1, + new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, entityID, groupID, -1, EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); } @@ -40,53 +44,72 @@ namespace Svelto.ECS _weakReference.Target.CheckRemoveEntityID(entityEGID, EntityDescriptorTemplate.descriptor); _weakReference.Target.QueueEntitySubmitOperation( - new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityEGID.entityID, entityEGID.groupID, + new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityEGID.entityID, entityEGID.entityID, + entityEGID.groupID, -1, EntityDescriptorTemplate.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(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new() + public void SwapEntityGroup(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, + ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new() { _weakReference.Target.QueueEntitySubmitOperation( - new EntitySubmitOperation(EntitySubmitOperationType.Swap, - entityID, - (int) fromGroupID, - (int) toGroupID, + new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, entityID, fromGroupID, toGroupID, EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); } - public void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new() + public void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) + where T : IEntityDescriptor, new() { _weakReference.Target.QueueEntitySubmitOperation( new EntitySubmitOperation(EntitySubmitOperationType.Swap, - id.entityID, - id.groupID, - (int) toGroupID, + id.entityID, id.entityID, id.groupID, toGroupID, EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); } - public void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new() + public void SwapEntityGroup(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( - new EntitySubmitOperation(EntitySubmitOperationType.Swap, - id.entityID, - id.groupID, - (int) toGroupID, - EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); + new EntitySubmitOperation(EntitySubmitOperationType.Swap, + id.entityID, id.entityID, id.groupID, toGroupID, + EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); } + public void SwapEntityGroup(EGID id, EGID toID) + where T : IEntityDescriptor, new() + { + _weakReference.Target.QueueEntitySubmitOperation( + new EntitySubmitOperation(EntitySubmitOperationType.Swap, + id.entityID, toID.entityID, id.groupID, toID.groupID, + EntityDescriptorTemplate.descriptor.entitiesToBuild, typeof(T))); + } + public void SwapEntityGroup(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( + new EntitySubmitOperation(EntitySubmitOperationType.Swap, + id.entityID, toID.entityID, id.groupID, toID.groupID, + EntityDescriptorTemplate.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 diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index 4132504..6af2322 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -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: ") diff --git a/Svelto.ECS/EntityBuilder.cs b/Svelto.ECS/EntityBuilder.cs index eb0dea4..0fb4667 100644 --- a/Svelto.ECS/EntityBuilder.cs +++ b/Svelto.ECS/EntityBuilder.cs @@ -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 } } diff --git a/Svelto.ECS/EntitySubmitOperation.cs b/Svelto.ECS/EntitySubmitOperation.cs index dbbb033..2a2db7d 100644 --- a/Svelto.ECS/EntitySubmitOperation.cs +++ b/Svelto.ECS/EntitySubmitOperation.cs @@ -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; diff --git a/Svelto.ECS/IEntityFunctions.cs b/Svelto.ECS/IEntityFunctions.cs index f9c2853..5b88f78 100644 --- a/Svelto.ECS/IEntityFunctions.cs +++ b/Svelto.ECS/IEntityFunctions.cs @@ -1,5 +1,3 @@ -using Svelto.ECS.Internal; - namespace Svelto.ECS { public interface IEntityFunctions @@ -16,6 +14,9 @@ namespace Svelto.ECS void SwapEntityGroup(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new(); void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new(); - void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new(); + void SwapEntityGroup(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup) where T : IEntityDescriptor, new(); + + void SwapEntityGroup(EGID id, EGID toId) where T : IEntityDescriptor, new(); + void SwapEntityGroup(EGID id, EGID toId, ExclusiveGroup.ExclusiveGroupStruct mustBeFromGroup) where T : IEntityDescriptor, new(); } } \ No newline at end of file