Skip to content

Commit

Permalink
Added test and fixed create-all.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Oct 28, 2024
1 parent dc9b9c6 commit 3874955
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
56 changes: 56 additions & 0 deletions src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,62 @@ public void Create()
True(world.IsAlive(entity));
}

/// <summary>
/// Checks if the <see cref="World"/> creates <see cref="Entity"/> correctly.
/// </summary>
[Test]
public void CreateAll()
{
var size = 1024;
using var world = World.Create();

var transform = new Transform { X = size, Y = size };
var rotation = new Rotation{ X = size, Y = size, W = size, Z = size};
world.Create(size, transform , rotation);

var queryDesc = new QueryDescription().WithAll<Transform, Rotation>();
world.Query(queryDesc, (Entity entity, ref Transform entityTransform, ref Rotation entityRotation) =>
{
That(world.IsAlive(entity));
That(world.Version(entity), Is.EqualTo(1));
That(world.HasRange(entity, _entityGroup));
That(world.Get<Transform>(entity).X, Is.EqualTo(size));
That(world.Get<Rotation>(entity).X, Is.EqualTo(size));
That(entityTransform.X, Is.EqualTo(size));
That(entityRotation.X, Is.EqualTo(size));
});

That(world.Size, Is.EqualTo(size));
That(world.Capacity, Is.EqualTo(world.Archetypes[0].EntityCapacity));
}

/// <summary>
/// Checks if the <see cref="World"/> creates <see cref="Entity"/> correctly.
/// </summary>
[Test]
public void CreateAll_By_Signature()
{
var size = 1024;
using var world = World.Create();

var createdEntities = (Span<Entity>)stackalloc Entity[size];
world.Create(createdEntities, _entityGroup, size);

var index = 0;
foreach (var entity in createdEntities)
{
That(entity.Id, Is.EqualTo(index));
That(world.IsAlive(entity));
That(world.Version(entity), Is.EqualTo(1));
That(world.HasRange(entity, _entityGroup));
index++;
}

That(world.Size, Is.EqualTo(size));
That(world.Capacity, Is.EqualTo(world.Archetypes[0].EntityCapacity));
}

/// <summary>
/// Checks if the <see cref="World"/> destroys <see cref="Entity"/> correctly.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Arch/Core/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public static int GetChunkCapacityFor(int entitiesPerChunk, int entityAmount)
internal static int GetNextSlots(Archetype archetype, Span<Slot> slots, int amount)
{
var next = 0;
for (var chunkIndex = archetype.ChunkCount-1; chunkIndex < archetype.ChunkCapacity && amount > 0; chunkIndex++)
for (var chunkIndex = archetype.Count; chunkIndex < archetype.ChunkCapacity && amount > 0; chunkIndex++)
{
ref var chunk = ref archetype.GetChunk(chunkIndex);
var chunkSize = chunk.Count;
Expand Down
2 changes: 1 addition & 1 deletion src/Arch/Templates/World.CreateBulk.tt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ for (var index = 2; index < Amount; index++)
// Fill entities
var firstSlot = entityData[0].Slot;
var lastSlot = entityData[amount - 1].Slot;
archetype.SetRange<<#= generics #>>(in firstSlot, in lastSlot, <#= inParameters #>);
archetype.SetRange<<#= generics #>>(in lastSlot, in firstSlot, <#= inParameters #>);

// Add entities to entityinfo
AddEntityData(entities, entityData, amount);
Expand Down

0 comments on commit 3874955

Please sign in to comment.