From 24d86877ae371708076f2a14755b4b9cf9274770 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 18 Oct 2024 01:41:39 +0200 Subject: [PATCH] Fixed tests. --- src/Arch.Tests/WorldTest.cs | 2 +- src/Arch/Core/Archetype.cs | 6 ++++-- src/Arch/Core/Chunk.cs | 4 ++-- src/Arch/Core/World.cs | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Arch.Tests/WorldTest.cs b/src/Arch.Tests/WorldTest.cs index 5b9475c..86053f5 100644 --- a/src/Arch.Tests/WorldTest.cs +++ b/src/Arch.Tests/WorldTest.cs @@ -280,7 +280,7 @@ public void TrimExcess() var archetype = world.Archetypes[0]; That(world.Size, Is.EqualTo(1)); That(world.Capacity, Is.EqualTo(archetype.EntitiesPerChunk)); - That(archetype.ChunkCount, Is.EqualTo(1)); + That(archetype.ChunkCount, Is.EqualTo(0)); That(archetype.ChunkCapacity, Is.EqualTo(1)); // Recycled ids must be trimmed too so that the newest created entity is not out of bounds! diff --git a/src/Arch/Core/Archetype.cs b/src/Arch/Core/Archetype.cs index fa3c188..599fe86 100644 --- a/src/Arch/Core/Archetype.cs +++ b/src/Arch/Core/Archetype.cs @@ -480,7 +480,7 @@ public void AddAll(Span entities, int amount) EnsureEntityCapacity(EntityCount + amount); var created = 0; - for(var index = ChunkCount; index < ChunkCapacity && created < amount; index++) + for(var index = Count; index < ChunkCapacity && created < amount; index++) { ref var chunk = ref GetChunk(index); var fillAmount = Math.Min(chunk.Buffer, amount - created); @@ -488,10 +488,11 @@ public void AddAll(Span entities, int amount) Chunk.Copy(ref entities, created, ref chunk, chunk.Count, fillAmount); chunk.Count += fillAmount; - Count = chunk.IsFull ? ChunkCount + 1 : ChunkCount; + Count = chunk.IsFull ? Count + 1 : Count; created += fillAmount; } + Count--; EntityCount += amount; } @@ -747,6 +748,7 @@ internal void EnsureEntityCapacity(int newCapacity) /// internal void TrimExcess() { + Chunks.Count = Count; // By setting the Count we will assure that unnecessary chunks are trimmed. Chunks.TrimExcess(); } } diff --git a/src/Arch/Core/Chunk.cs b/src/Arch/Core/Chunk.cs index 78c56c3..4ddc006 100644 --- a/src/Arch/Core/Chunk.cs +++ b/src/Arch/Core/Chunk.cs @@ -24,11 +24,11 @@ public Chunks(int capacity = 1) public Array Items { get; set; } public int Count { get; set; } - public int Capacity { get; set; } + public int Capacity { get; private set; } public void Add(in Chunk chunk) { - Debug.Assert(Count+1 <= Capacity, "Capacity exceeded."); + Debug.Assert(Count + 1 <= Capacity, "Capacity exceeded."); Items[Count++] = chunk; } diff --git a/src/Arch/Core/World.cs b/src/Arch/Core/World.cs index d4310b2c..f2593c8 100644 --- a/src/Arch/Core/World.cs +++ b/src/Arch/Core/World.cs @@ -384,7 +384,7 @@ public void TrimExcess() } archetype.TrimExcess(); - Capacity += archetype.ChunkCount * archetype.EntitiesPerChunk; // Since always one chunk always exists. + Capacity += archetype.EntityCapacity; } // Traverse recycled ids and remove all that are higher than the current capacity.