Skip to content

Commit

Permalink
Fixed Chunk.Has (#167)
Browse files Browse the repository at this point in the history
* Fixed Has

* Update comment

* This is the comment

* Source gen Has fixed
  • Loading branch information
RoyconSkylands authored Oct 28, 2023
1 parent 9638bad commit 56b1641
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Arch.SourceGen/Fundamentals/Has.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static StringBuilder AppendChunkHas(this StringBuilder sb, int amount)
var ifs = new StringBuilder();
for (var index = 0; index <= amount; index++)
{
ifs.AppendLine($"if (ComponentIdToArrayIndex[t{index}ComponentId] != 1) return false;");
ifs.AppendLine($"if (ComponentIdToArrayIndex[t{index}ComponentId] == -1) return false;");
}

var template =
Expand Down
24 changes: 24 additions & 0 deletions src/Arch.Tests/ChunkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,28 @@ public void ChunkRemoveAndSetAgain()
That(_chunk.Entities[0].Id, Is.EqualTo(2)); // Needs to be 1, because it will be the last one getting removed and being moved to that position
That(_chunk.Entities[1].Id, Is.EqualTo(1)); // Needs to be 1, because it will be the last one getting removed and being moved to that position
}

/// <summary>
/// Checks if chunk has a component.
/// </summary>
[Test]
public void ChunkHas()
{
_chunk = new Chunk(1000, _types);

for (var index = 0; index < _chunk.Capacity; index++)
{
var entity = new Entity(index, 0);
_chunk.Add(entity);

var t = new Transform();
var r = new Rotation();
_chunk.Set(index, t);
_chunk.Set(index, r);
}

That(_chunk.Has<Transform>(), Is.True);
That(_chunk.Has<Ai>(), Is.False);
That(_chunk.Has<Rotation>(), Is.True);
}
}
2 changes: 1 addition & 1 deletion src/Arch/Core/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void Set<T>(int index, in T cmp)
public bool Has<T>()
{
var id = Component<T>.ComponentType.Id;
return id < ComponentIdToArrayIndex.Length && ComponentIdToArrayIndex[id] != 1;
return id < ComponentIdToArrayIndex.Length && ComponentIdToArrayIndex[id] != -1;
}

/// <summary>
Expand Down

0 comments on commit 56b1641

Please sign in to comment.