Skip to content

Commit

Permalink
ECS - Relations: fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Aug 26, 2024
1 parent 46ca92a commit a305f19
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/ECS/Archetype/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ internal static int MoveEntityTo(Archetype sourceArch, int id, int sourceIndex,
// This is redundant for Entity.AddComponent() but other callers may not assign a component value.
targetHeap.SetComponentDefault(targetIndex);
}
MoveLastComponentsTo(sourceArch, sourceIndex);
MoveLastComponentsTo(sourceArch, sourceIndex, true);
return targetIndex;
}

internal static void MoveLastComponentsTo(Archetype arch, int newIndex)
internal static void MoveLastComponentsTo(Archetype arch, int newIndex, bool updateCompIndex)
{
var lastIndex = arch.entityCount - 1;
var store = arch.store;
Expand All @@ -292,8 +292,9 @@ internal static void MoveLastComponentsTo(Archetype arch, int newIndex)
}
var entityIds = arch.entityIds;
var lastEntityId = entityIds[lastIndex];
store.UpdateEntityCompIndex(lastEntityId, newIndex); // set entity component index for new archetype

if (updateCompIndex) {
store.UpdateEntityCompIndex(lastEntityId, newIndex); // set entity component index for new archetype
}
entityIds[newIndex] = lastEntityId;
} // ReSharper disable once RedundantIfElseBlock
else {
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ public void DeleteEntity()
store.DeleteEntityEvent(this);
}
finally {
store.DeleteNode(this);
Archetype.MoveLastComponentsTo(node.archetype, node.compIndex);
store.DeleteNode(this);
Archetype.MoveLastComponentsTo(node.archetype, node.compIndex, true);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/RawEntity/RawEntityStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public bool DeleteEntity(int id)
return false;
}
var archetype = archs[archIndex];
Archetype.MoveLastComponentsTo(archetype, entity.compIndex);
Archetype.MoveLastComponentsTo(archetype, entity.compIndex, true);
entity.archIndex = 0;
entity.compIndex = 0;
entityCount--;
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Relations/Internal/EntityRelations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected IdArray RemoveEntityRelation(int id, int position, IdArray positions,
}

// --- move last relation to position of removed relation
Archetype.MoveLastComponentsTo(type, position);
Archetype.MoveLastComponentsTo(type, position, false);
if (positions.count == 1) {
map.Remove(id);
store.nodes[id].isOwner &= ~relationBit;
Expand Down
33 changes: 33 additions & 0 deletions src/Tests/ECS/Relations/Test_Relations_GitHub_11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Friflo.Engine.ECS;
using NUnit.Framework;

// ReSharper disable InconsistentNaming
namespace Tests.ECS.Relations
{
// Fix https://github.com/friflo/Friflo.Engine.ECS/issues/11
public static class Test_Relations_GitHub_11
{
[Test]
public static void Test_Rel_Test()
{
var store = new EntityStore();
var entity1 = store.CreateEntity();
entity1.Add(new MyComponent1 { a = 1 });

var entity2 = store.CreateEntity();
entity2.Add(new MyComponent1 { a = 2 });

var entity3 = store.CreateEntity();

entity1.AddRelation(new AttackRelation { target = entity3 });
entity2.AddRelation(new AttackRelation { target = entity3 });

entity1.RemoveRelation<AttackRelation>(entity3);

Assert.AreEqual(1, entity1.GetComponent<MyComponent1>().a);
Assert.AreEqual(2, entity2.GetComponent<MyComponent1>().a);
Console.WriteLine($"{entity1.GetComponent<MyComponent1>().a} {entity2.GetComponent<MyComponent1>().a}");
}
}
}

0 comments on commit a305f19

Please sign in to comment.