fix type indices not matching in world transfer

pull/4/head
cosmonaut 2023-07-10 11:39:38 -07:00
parent 227e3421cd
commit cd1df0550e
1 changed files with 14 additions and 16 deletions

View File

@ -5,8 +5,8 @@ namespace MoonTools.ECS
{ {
public class World public class World
{ {
internal readonly TypeIndices ComponentTypeIndices = new TypeIndices(); internal readonly static TypeIndices ComponentTypeIndices = new TypeIndices();
internal readonly TypeIndices RelationTypeIndices = new TypeIndices(); internal readonly static TypeIndices RelationTypeIndices = new TypeIndices();
internal readonly EntityStorage EntityStorage = new EntityStorage(); internal readonly EntityStorage EntityStorage = new EntityStorage();
internal readonly ComponentDepot ComponentDepot; internal readonly ComponentDepot ComponentDepot;
internal readonly MessageDepot MessageDepot = new MessageDepot(); internal readonly MessageDepot MessageDepot = new MessageDepot();
@ -163,14 +163,14 @@ namespace MoonTools.ECS
other.Destroy(entity); other.Destroy(entity);
} }
// create entities and set their components // create entities
foreach (var entity in filter.Entities) foreach (var entity in filter.Entities)
{ {
TransferEntity(other, entity); var otherWorldEntity = other.CreateEntity(GetTag(entity));
WorldToTransferID.Add(entity.ID, otherWorldEntity.ID);
} }
// set relations // set relations before components so filters don't freak out
// FIXME: something is going wrong here, the transfer back isn't working
foreach (var entity in filter.Entities) foreach (var entity in filter.Entities)
{ {
var otherWorldEntityA = WorldToTransferID[entity.ID]; var otherWorldEntityA = WorldToTransferID[entity.ID];
@ -194,19 +194,17 @@ namespace MoonTools.ECS
} }
} }
} }
}
private unsafe int TransferEntity(World other, Entity entity) // set components
foreach (var entity in filter.Entities)
{ {
var otherWorldEntity = other.CreateEntity(GetTag(entity)); var otherWorldEntity = WorldToTransferID[entity.ID];
WorldToTransferID.Add(entity.ID, otherWorldEntity.ID);
foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID)) foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID))
{ {
other.Set(otherWorldEntity, componentTypeIndex, ComponentDepot.UntypedGet(entity.ID, componentTypeIndex)); other.Set(otherWorldEntity, componentTypeIndex, ComponentDepot.UntypedGet(entity.ID, componentTypeIndex));
} }
}
return otherWorldEntity.ID;
} }
} }
} }