Skip to content

Commit

Permalink
arch refs
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Sep 2, 2024
1 parent 39410df commit 262aff0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
16 changes: 12 additions & 4 deletions Robust.Shared/GameObjects/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ public record struct ArchEntity : IFluentEntityUid
public EntityUid Owner;
EntityUid IFluentEntityUid.FluentOwner => Owner;

internal Chunk Chunk;
internal Archetype? _archetype;
internal Chunk? Chunk;

/// <summary>
/// Id of the chunk the entity is stored in.
/// </summary>
internal int ChunkId;

/// <summary>
/// Index of this entity into its chunk.
/// </summary>
internal int ChunkIndex;

public ArchEntity(EntityUid owner, Chunk chunk, int chunkIndex)
public ArchEntity(EntityUid owner)
{
Owner = owner;
Chunk = chunk;
ChunkIndex = chunkIndex;
}

public static implicit operator EntityUid(ArchEntity ent)
Expand Down
67 changes: 31 additions & 36 deletions Robust.Shared/GameObjects/EntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,71 +666,66 @@ private void DeleteComponent(EntityUid entityUid, IComponent component, CompIdx

public ArchEntity GetArchEntity(EntityUid uid)
{
var entRef = (EntityReference)uid;
var (slot, archetype) = _world.GetSlotArchetype(entRef.Entity);
var chunk = archetype.GetChunk(slot.Item2);
var index = slot.Item1;

return new ArchEntity(uid, chunk, index);
return new ArchEntity(uid);
}

public bool TryComp<TComp>(ref ArchEntity entity, [NotNullWhen(true)] out TComp? comp)
where TComp : IComponent?
public void EnsureArchEntity(ref ArchEntity ent)
{
ref var index = ref entity.ChunkIndex;
ref var chunk = ref entity.Chunk;
EnsureArchetype(ref ent);
EnsureChunk(ref ent);
}

if (!chunk.Has<TComp>())
private void EnsureArchetype(ref ArchEntity ent)
{
if (ent._archetype == null)
{
comp = default;
return false;
var entRef = (EntityReference)ent.Owner;
var (slot, archetype) = _world.GetSlotArchetype(entRef);
ent._archetype = archetype;
ent.ChunkId = slot.Item2;
ent.ChunkIndex = slot.Item1;
}
}

comp = chunk.Get<TComp>(index)!;
return true;
private void EnsureChunk(ref ArchEntity ent)
{
if (ent.Chunk == null)
{
ent.Chunk = ent._archetype!.GetChunk(ent.ChunkId);
}
}

public bool TryComp(ref ArchEntity entity, CompIdx idx, [NotNullWhen(true)] out IComponent? comp1)
public bool TryComp<TComp>(ref ArchEntity entity, [NotNullWhen(true)] out TComp? comp1)
where TComp : IComponent?
{
ref var index = ref entity.ChunkIndex;
ref var chunk = ref entity.Chunk;
EnsureArchetype(ref entity);

if (!chunk.Has(idx.Type))
if (!entity._archetype!.Has<TComp>())
{
comp1 = default;
return false;
}

comp1 = (IComponent) chunk.Get(index, idx.Type)!;
EnsureChunk(ref entity);
comp1 = entity.Chunk!.Value.Get<TComp>(entity.ChunkIndex)!;
return true;
}

public bool TryComp<TComp1, TComp2>(ref ArchEntity entity, [NotNullWhen(true)] out TComp1? comp1, [NotNullWhen(true)] out TComp2? comp2)
where TComp1 : IComponent?
where TComp2 : IComponent?
public bool TryComp(ref ArchEntity entity, CompIdx idx, [NotNullWhen(true)] out IComponent? comp1)
{
ref var index = ref entity.ChunkIndex;
ref var chunk = ref entity.Chunk;
EnsureArchetype(ref entity);

if (!chunk.Has<TComp1, TComp2>())
if (!entity._archetype!.Has(idx.Type))
{
comp1 = default;
comp2 = default;
return false;
}

var comps = chunk.Get<TComp1, TComp2>(index);
comp1 = comps.t0!;
comp2 = comps.t1!;
EnsureChunk(ref entity);
comp1 = (IComponent) entity.Chunk!.Value.Get(entity.ChunkIndex, idx.Type)!;
return true;
}

public bool HasComp<TComp>(ArchEntity entity) where TComp : IComponent?
{
ref var chunk = ref entity.Chunk;
return chunk.Has<TComp>();
}

#endregion

/// <inheritdoc />
Expand Down

0 comments on commit 262aff0

Please sign in to comment.