Skip to content

Commit

Permalink
IndexedComponents: update indexes on CreateEntityBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Nov 8, 2024
1 parent 6964f48 commit 48f81e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
19 changes: 18 additions & 1 deletion src/ECS/Batch/CreateEntityBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,35 @@ private Entity CreateEntityInternal(EntityStore entityStore, int id)
foreach (var heap in archetype.structHeaps) {
heap.SetBatchComponent(components, compIndex);
}
var entity = new Entity(entityStore, id, revision);

// -- add indexed components to indexes
var indexTypes = archetype.componentTypes.bitSet.l0 & indexTypesMask;
if (indexTypes != 0) {
AddIndexedComponents(entity, indexTypes);
}
if (autoReturn) {
isReturned = true;
Clear();
entityStore.ReturnCreateBatch(this);
}
var entity = new Entity(entityStore, id, revision);

// Send event. See: SEND_EVENT notes
entityStore.CreateEntityEvent(entity);
return entity;
}

private readonly long indexTypesMask = EntityStoreBase.Static.EntitySchema.indexTypes.bitSet.l0;

private void AddIndexedComponents(Entity entity, long indexTypes)
{
var types = new ComponentTypes();
types.bitSet.l0 = indexTypes;
foreach (var type in types) {
archetype.heapMap[type.StructIndex].AddIndex(entity);
}
}

/// <summary>
/// Return the batch instance to its <see cref="EntityStore"/> to prevent memory allocations for future
/// <see cref="EntityStoreBase.Batch"/> calls.
Expand Down
6 changes: 3 additions & 3 deletions src/ECS/Batch/EntityStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class EntityStoreBase
[Browse(Never)]
internal int PooledEntityBatchCount => internBase.entityBatches.Count;

private readonly long indexTypesMak = Static.EntitySchema.indexTypes.bitSet.l0;
private readonly long indexTypesMask = Static.EntitySchema.indexTypes.bitSet.l0;

internal EntityBatch GetBatch(int entityId)
{
Expand Down Expand Up @@ -51,7 +51,7 @@ internal void ApplyBatchTo(EntityBatch batch, int entityId)

// --- stash old component values only if an event handler is set or an indexed component changes
var oldHeapMap = archetype.heapMap;
var indexChanges = ((batch.componentsAdd.bitSet.l0 | batch.componentsRemove.bitSet.l0) & indexTypesMak) != 0;
var indexChanges = ((batch.componentsAdd.bitSet.l0 | batch.componentsRemove.bitSet.l0) & indexTypesMask) != 0;
var sendRemoveEvents = internBase.componentRemoved != null;
var sendAddEvents = internBase.componentAdded != null;
if (sendRemoveEvents || sendAddEvents || indexChanges) {
Expand Down Expand Up @@ -81,7 +81,7 @@ internal void ApplyBatchTo(EntityBatch batch, int entityId)
}
}
// --- update indexes of removed indexed components
var removedIndexTypes = batch.componentsRemove.bitSet.l0 & indexTypesMak;
var removedIndexTypes = batch.componentsRemove.bitSet.l0 & indexTypesMask;
if (removedIndexTypes != 0) {
RemoveComponentIndexes(removedIndexTypes, new Entity((EntityStore)this, entityId, node.revision), oldHeapMap);
}
Expand Down
29 changes: 21 additions & 8 deletions src/Tests/ECS/Serialize/Test_SerializeIndexedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ReSharper disable InconsistentNaming
namespace Tests.ECS.Serialize {

public static class Test_SerializeIndexedComponent
public static class Test_IndexedComponent
{
private const string JSON_withIndexedComponent =
@"[{
Expand All @@ -29,7 +29,7 @@ public static class Test_SerializeIndexedComponent
}";

[Test]
public static void Test_SerializeIndexedComponent_add()
public static void Test_IndexedComponent_serialize_add()
{
var store = new EntityStore();
store.CreateEntity(1);
Expand All @@ -42,7 +42,7 @@ public static void Test_SerializeIndexedComponent_add()
}

[Test]
public static void Test_SerializeIndexedComponent_update()
public static void Test_IndexedComponent_serialize_update()
{
var store = new EntityStore();
Entity entity = store.CreateEntity(1);
Expand All @@ -58,7 +58,7 @@ public static void Test_SerializeIndexedComponent_update()
}

[Test]
public static void Test_SerializeIndexedComponent_remove()
public static void Test_IndexedComponent_serialize_remove()
{
var store = new EntityStore();
var entity = store.CreateEntity(1);
Expand All @@ -85,7 +85,7 @@ public static void Test_IndexedComponent_CreateEntity()
}

[Test]
public static void Test_IndexedComponent_Add()
public static void Test_IndexedComponent_EntityExtension_Add()
{
var store = new EntityStore();
var entity = store.CreateEntity();
Expand All @@ -101,7 +101,7 @@ public static void Test_IndexedComponent_Add()
}

[Test]
public static void Test_IndexedComponent_Set()
public static void Test_IndexedComponent_EntityExtension_Set()
{
var store = new EntityStore();
var entity = store.CreateEntity(new IndexedInt { value = 40 });
Expand All @@ -117,7 +117,7 @@ public static void Test_IndexedComponent_Set()
}

[Test]
public static void Test_IndexedComponent_Remove()
public static void Test_IndexedComponent_EntityExtension_Remove()
{
var store = new EntityStore();
var entity = store.CreateEntity();
Expand All @@ -135,7 +135,7 @@ public static void Test_IndexedComponent_Remove()
}

[Test]
public static void Test_IndexedComponent_ApplyTo()
public static void Test_IndexedComponent_EntityBatch_ApplyTo()
{
var store = new EntityStore(PidType.RandomPids);
var batch = new EntityBatch();
Expand All @@ -161,6 +161,19 @@ public static void Test_IndexedComponent_ApplyTo()

batch.ApplyTo(entity1);
}

[Test]
public static void Test_IndexedComponent_CreateEntityBatch()
{
var store = new EntityStore(PidType.UsePidAsId);
var batch = new CreateEntityBatch(store);
batch.Add(new Position())
.Add(new IndexedInt { value = 60 });

batch.CreateEntity();
AreEqual(1, store.GetEntitiesWithComponentValue<IndexedInt,int>(60).Count);
AreEqual(1, store.GetAllIndexedComponentValues<IndexedInt,int>().Count);
}
}

}

0 comments on commit 48f81e6

Please sign in to comment.