Skip to content

Commit

Permalink
Fixed #173
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Nov 23, 2023
1 parent cae1026 commit 15fb43e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,36 @@ public void DestroyAll()
That(_world.Archetypes[1].Size, Is.EqualTo(1));
}

/// <summary>
/// Tests an edge case where entities are being bulk moved between archetypes and destroyed at the same time.
/// </summary>
[Test]
public void DestroyEdgeCase()
{
var entitiesToChangeColor = new QueryDescription().WithAll<Transform>();
var entities = new List<Entity>();
for (var i = 0; i < 1000; i++)
{
var ent = _world.Create(_entityGroup);
entities.Add(ent);
}

for (var i = 8; i < entities.Count; i++)
{
var ent = entities[i];
if (i % 3 != 0)
{
continue;
}

// A demonstration of bulk adding and removing components.
_world.Add(in entitiesToChangeColor, 1);
_world.Remove<int>(in entitiesToChangeColor);

_world.Destroy(ent);
}
}

/// <summary>
/// Checks if the <see cref="World"/> recycles destroyed <see cref="Entity"/> correctly.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions src/Arch/Core/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ private void EnsureCapacity(int newCapacity)
internal void EnsureEntityCapacity(int newCapacity)
{
// Calculate amount of required chunks.
ref var lastChunk = ref LastChunk;
var freeSpots = lastChunk.Capacity - lastChunk.Size;
var freeSpots = (Capacity * EntitiesPerChunk) - Entities;
var neededSpots = newCapacity - freeSpots;
var neededChunks = (int)Math.Ceiling((float)neededSpots / EntitiesPerChunk);

Expand Down

0 comments on commit 15fb43e

Please sign in to comment.