From cd1df0550e093047603e406e4a66fa4101afe484 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 10 Jul 2023 11:39:38 -0700 Subject: [PATCH] fix type indices not matching in world transfer --- src/World.cs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/World.cs b/src/World.cs index 38ef0d3..d674f0f 100644 --- a/src/World.cs +++ b/src/World.cs @@ -5,8 +5,8 @@ namespace MoonTools.ECS { public class World { - internal readonly TypeIndices ComponentTypeIndices = new TypeIndices(); - internal readonly TypeIndices RelationTypeIndices = new TypeIndices(); + internal readonly static TypeIndices ComponentTypeIndices = new TypeIndices(); + internal readonly static TypeIndices RelationTypeIndices = new TypeIndices(); internal readonly EntityStorage EntityStorage = new EntityStorage(); internal readonly ComponentDepot ComponentDepot; internal readonly MessageDepot MessageDepot = new MessageDepot(); @@ -163,14 +163,14 @@ namespace MoonTools.ECS other.Destroy(entity); } - // create entities and set their components + // create entities foreach (var entity in filter.Entities) { - TransferEntity(other, entity); + var otherWorldEntity = other.CreateEntity(GetTag(entity)); + WorldToTransferID.Add(entity.ID, otherWorldEntity.ID); } - // set relations - // FIXME: something is going wrong here, the transfer back isn't working + // set relations before components so filters don't freak out foreach (var entity in filter.Entities) { var otherWorldEntityA = WorldToTransferID[entity.ID]; @@ -194,19 +194,17 @@ namespace MoonTools.ECS } } } - } - private unsafe int TransferEntity(World other, Entity entity) - { - var otherWorldEntity = other.CreateEntity(GetTag(entity)); - WorldToTransferID.Add(entity.ID, otherWorldEntity.ID); - - foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID)) + // set components + foreach (var entity in filter.Entities) { - other.Set(otherWorldEntity, componentTypeIndex, ComponentDepot.UntypedGet(entity.ID, componentTypeIndex)); - } + var otherWorldEntity = WorldToTransferID[entity.ID]; - return otherWorldEntity.ID; + foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID)) + { + other.Set(otherWorldEntity, componentTypeIndex, ComponentDepot.UntypedGet(entity.ID, componentTypeIndex)); + } + } } } }