Skip to content

Commit

Permalink
Add ArchEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Sep 1, 2024
1 parent 7a34b40 commit 6fc2f42
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
22 changes: 3 additions & 19 deletions Robust.Shared/GameObjects/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,26 @@

namespace Robust.Shared.GameObjects;

public record struct ArchEntity<T> : IFluentEntityUid
where T : IComponent?
public record struct ArchEntity : IFluentEntityUid
{
public EntityUid Owner;
public T Comp;
EntityUid IFluentEntityUid.FluentOwner => Owner;

internal Chunk Chunk;
internal int ChunkIndex;

public ArchEntity(EntityUid owner, T comp, Chunk chunk, int chunkIndex)
public ArchEntity(EntityUid owner, Chunk chunk, int chunkIndex)
{
DebugTools.AssertOwner(owner, comp);

Owner = owner;
Comp = comp;
Chunk = chunk;
ChunkIndex = chunkIndex;
}

public static implicit operator EntityUid(ArchEntity<T> ent)
public static implicit operator EntityUid(ArchEntity ent)
{
return ent.Owner;
}

public static implicit operator T(ArchEntity<T> ent)
{
return ent.Comp;
}

public readonly void Deconstruct(out EntityUid owner, out T comp)
{
owner = Owner;
comp = Comp;
}

public override int GetHashCode() => Owner.GetHashCode();
}

Expand Down
45 changes: 28 additions & 17 deletions Robust.Shared/GameObjects/EntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,27 +664,20 @@ private void DeleteComponent(EntityUid entityUid, IComponent component, CompIdx

#region Entity<T>

public ArchEntity<T?> GetArchEntity<T>(EntityUid uid) where T : IComponent?
public ArchEntity GetArchEntity(EntityUid uid)
{
// TODO: Could be faster
var archetype = _world.GetArchetype(uid);
var slot = _world.GetSlot(uid);
var entRef = (EntityReference)uid;
var (slot, archetype) = _world.GetSlotArchetype(entRef.Entity);
var chunk = archetype.GetChunk(slot.Item2);
var index = slot.Item1;
T? comp = default;

if (chunk.Has<T>())
{
comp = chunk.Get<T>(index);
}

return new ArchEntity<T?>(uid, comp, chunk, index);
return new ArchEntity(uid, chunk, index);
}

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

if (!chunk.Has<TComp>())
Expand All @@ -693,13 +686,31 @@ public bool TryComp<TComp, T>(ref ArchEntity<T> entity, [NotNullWhen(true)] out
return false;
}

ref var index = ref entity.ChunkIndex;
comp = chunk.Get<TComp>(index)!;
return true;
}

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

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

var comps = chunk.Get<TComp1, TComp2>(index);
comp1 = comps.t0!;
comp2 = comps.t1!;
return true;
}

public bool HasComp<TComp>(ArchEntity entity) where TComp : IComponent?
{
ref var chunk = ref entity.Chunk;
return chunk.Has<TComp>();
Expand Down Expand Up @@ -923,7 +934,7 @@ public bool TryGetComponent(EntityUid uid, Type type, [NotNullWhen(true)] out IC

public bool TryGetComponent(EntityUid uid, CompIdx type, [NotNullWhen(true)] out IComponent? component)
{
if (_world.TryGetAlive(uid, type.Type, out var comp))
if (_world.TryGetAlive(uid, type.Type.Id, out var comp))
{
component = (IComponent)comp!;
if (component != null! && !component.Deleted)
Expand Down

0 comments on commit 6fc2f42

Please sign in to comment.