Skip to content

Commit

Permalink
entityAt
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Apr 4, 2024
1 parent ff2e622 commit a928575
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
19 changes: 12 additions & 7 deletions src/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public struct ArchetypeChunk

public int Count { get; internal set; }


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ref EntityView EntityAt(int row)
=> ref Entities[row & Archetype.CHUNK_THRESHOLD];

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ref T GetReference<T>(int column) where T : struct
{
Expand Down Expand Up @@ -158,7 +163,7 @@ public int GetComponentIndex<T>() where T : struct
internal int Add(EcsID id)
{
ref var chunk = ref GetChunk(_count);
chunk.Entities[chunk.Count++] = new(_world, id);
chunk.EntityAt(chunk.Count++) = new(_world, id);
return _count++;
}

Expand All @@ -169,13 +174,13 @@ private EcsID RemoveByRow(int row)

ref var chunk = ref GetChunk(row);
ref var lastChunk = ref GetChunk(_count);
var removed = chunk.Entities[row & CHUNK_THRESHOLD];
var removed = chunk.EntityAt(row);

if (row < _count)
{
EcsAssert.Assert(lastChunk.Entities[_count & CHUNK_THRESHOLD] != EntityView.Invalid, "Entity is invalid. This should never happen!");
EcsAssert.Assert(lastChunk.EntityAt(_count) != EntityView.Invalid, "Entity is invalid. This should never happen!");

chunk.Entities[row & CHUNK_THRESHOLD] = lastChunk.Entities[_count & CHUNK_THRESHOLD];
chunk.EntityAt(row) = lastChunk.EntityAt(_count);

for (var i = 0; i < Components.Length; ++i)
{
Expand All @@ -188,10 +193,10 @@ private EcsID RemoveByRow(int row)
Array.Copy(lastValidArray, _count & CHUNK_THRESHOLD, arrayToBeRemoved, row & CHUNK_THRESHOLD, 1);
}

_world.GetRecord(chunk.Entities[row & CHUNK_THRESHOLD]).Row = row;
_world.GetRecord(chunk.EntityAt(row)).Row = row;
}

lastChunk.Entities[_count & CHUNK_THRESHOLD] = EntityView.Invalid;
lastChunk.EntityAt(_count) = EntityView.Invalid;

for (var i = 0; i < Components.Length; ++i)
{
Expand Down Expand Up @@ -232,7 +237,7 @@ ref readonly ComponentInfo component
internal int MoveEntity(Archetype newArch, int oldRow)
{
ref var fromChunk = ref GetChunk(oldRow);
var newRow = newArch.Add(fromChunk.Entities[oldRow & CHUNK_THRESHOLD]);
var newRow = newArch.Add(fromChunk.EntityAt(oldRow));

var isLeft = newArch.Components.Length < Components.Length;
int i = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void Each(QueryFilterDelegateWithEntity fn)
{
foreach (ref readonly var chunk in arch)
{
ref var entity = ref chunk.Entities[0];
ref var entity = ref chunk.EntityAt(0);
ref var last = ref Unsafe.Add(ref entity, chunk.Count);
while (Unsafe.IsAddressLessThan(ref entity, ref last))
{
Expand Down
10 changes: 5 additions & 5 deletions src/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void DetachComponent(ref EcsRecord record, ref readonly ComponentInfo cm
if (oldArch.GetComponentIndex(in cmp) < 0)
return;

OnComponentUnset?.Invoke(record.GetChunk().Entities[record.Row & Archetype.CHUNK_THRESHOLD], cmp);
OnComponentUnset?.Invoke(record.GetChunk().EntityAt(record.Row), cmp);

var newSign = oldArch.Components.Remove(cmp);
EcsAssert.Assert(newSign.Length < oldArch.Components.Length, "bad");
Expand All @@ -219,14 +219,14 @@ private void DetachComponent(ref EcsRecord record, ref readonly ComponentInfo cm
private int AttachComponent(ref EcsRecord record, ref readonly ComponentInfo cmp)
{
var oldArch = record.Archetype;

var index = oldArch.GetComponentIndex(in cmp);
if (index >= 0)
return index;

var newSign = oldArch.Components.Add(cmp).Sort(_comparer);
EcsAssert.Assert(newSign.Length > oldArch.Components.Length, "bad");

ref var newArch = ref GetArchetype(newSign, true);
if (newArch == null)
{
Expand All @@ -240,7 +240,7 @@ private int AttachComponent(ref EcsRecord record, ref readonly ComponentInfo cmp
record.Row = record.Archetype.MoveEntity(newArch, record.Row);
record.Archetype = newArch!;

OnComponentSet?.Invoke(record.GetChunk().Entities[record.Row & Archetype.CHUNK_THRESHOLD], cmp);
OnComponentSet?.Invoke(record.GetChunk().EntityAt(record.Row), cmp);

return newArch.GetComponentIndex(cmp.ID);
}
Expand Down Expand Up @@ -321,7 +321,7 @@ public void Each(QueryFilterDelegateWithEntity fn)
{
foreach (ref readonly var chunk in arch)
{
ref var entity = ref chunk.Entities[0];
ref var entity = ref chunk.EntityAt(0);
ref var last = ref Unsafe.Add(ref entity, chunk.Count);
while (Unsafe.IsAddressLessThan(ref entity, ref last))
{
Expand Down
4 changes: 2 additions & 2 deletions tools/TinyEcs.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public partial {className}
var typeParams = GenerateSequence(i + 1, ", ", j => $"T{j}");
var whereParams = GenerateSequence(i + 1, " ",j => $"where T{j} : struct");
var columnIndices = GenerateSequence(i + 1, "\n" , j => $"var column{j} = arch.GetComponentIndex<T{j}>();");
var fieldList = (withEntityView ? "ref var entityA = ref chunk.Entities[0];\n" : "") +
var fieldList = (withEntityView ? "ref var entityA = ref chunk.EntityAt(0);\n" : "") +
GenerateSequence(i + 1, "\n" , j => $"ref var t{j}A = ref chunk.GetReference<T{j}>(column{j});");
var signCallback = (withEntityView ? "entityA, " : "") +
GenerateSequence(i + 1, ", " , j => $"ref t{j}A");
Expand Down Expand Up @@ -237,7 +237,7 @@ public partial {className}
var typeParams = GenerateSequence(i + 1, ", ", j => $"T{j}");
var whereParams = GenerateSequence(i + 1, " ",j => $"where T{j} : struct");
var columnIndices = GenerateSequence(i + 1, "\n" , j => $"var column{j} = arch.GetComponentIndex<T{j}>();");
var fieldList = (withEntityView ? "ref var entityA = ref chunk.Entities[0];\n" : "") +
var fieldList = (withEntityView ? "ref var entityA = ref chunk.EntityAt(0);\n" : "") +
GenerateSequence(i + 1, "\n" , j => $"ref var t{j}A = ref chunk.GetReference<T{j}>(column{j});");
var signCallback = (withEntityView ? "entityA, " : "") +
GenerateSequence(i + 1, ", " , j => $"ref t{j}A");
Expand Down

0 comments on commit a928575

Please sign in to comment.