Skip to content

Commit

Permalink
ECS: add property EntityData.Components
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jul 31, 2024
1 parent 14b2490 commit c4d763d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/ECS/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public ref readonly Tags Tags { get {
[Browse(Never)] public EntityData Data { get {
ref var node = ref store.nodes[Id];
if (node.archetype != null && Revision == node.revision) {
return new EntityData(node);
return new EntityData(node, Id);
}
return default;
} }
Expand Down
40 changes: 27 additions & 13 deletions src/ECS/Entity/EntityData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// See LICENSE file in the project root for full license information.

using System;
using static System.Diagnostics.DebuggerBrowsableState;
using Browse = System.Diagnostics.DebuggerBrowsableAttribute;

// ReSharper disable InconsistentNaming
// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS;

Expand All @@ -15,20 +18,33 @@ namespace Friflo.Engine.ECS;
/// </remarks>
public readonly ref struct EntityData
{
#region entity getter
/// <summary> Returns true is the entity is deleted. </summary>
/// <remarks>Executes in O(1)</remarks>
public bool IsNull => archetype == null;

#region entity properties
/// <summary> Return the <see cref="ECS.Tags"/> added to an entity. </summary>
/// <exception cref="NullReferenceException"> if the entity is deleted.</exception>
/// <remarks>Executes in O(1)</remarks>
public Tags Tags => archetype.tags;
public Tags Tags => archetype.tags;

/// <summary> Returns true is the entity is deleted. </summary>
/// <remarks>Executes in O(1)</remarks>
[Browse(Never)] public bool IsNull => archetype == null;

/// <summary> Returns the archetype the entity belongs to. </summary>
public Archetype Archetype => archetype;
[Browse(Never)] public Archetype Archetype => archetype;

/// <summary>Return the <see cref="IComponent"/>'s added to the entity.</summary>
public EntityComponents Components => new EntityComponents(new Entity(archetype.entityStore, Id));

public override string ToString() => $"Id: {Id}";
#endregion

#region fields
[Browse(Never)] private readonly StructHeap[] heapMap;
[Browse(Never)] private readonly Archetype archetype;
public readonly int Id;
[Browse(Never)] private readonly int compIndex;
#endregion

#region entity getter
/// <summary> Returns true if the entity contains a component of the specified type. </summary>
public bool Has<T> () where T : struct, IComponent {
return heapMap[StructInfo<T>.Index] != null;
Expand Down Expand Up @@ -58,16 +74,14 @@ public bool TryGet<T>(out T value) where T : struct, IComponent {
}
#endregion

#region fields
private readonly StructHeap[] heapMap;
private readonly Archetype archetype;
private readonly int compIndex;
#endregion

internal EntityData(in EntityNode node) {
#region methods
internal EntityData(in EntityNode node, int id) {
var type = node.archetype;
heapMap = type.heapMap;
archetype = type;
Id = id;
compIndex = node.compIndex;
}
#endregion
}
18 changes: 17 additions & 1 deletion src/Tests/ECS/Entity/Test_EntityData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Tests.ECS {

public static class Test_EntityState
{
#pragma warning disable CS0618 // Type or member is obsolete
[Test]
public static void Test_EntityData_access()
{
Expand All @@ -24,6 +25,8 @@ public static void Test_EntityData_access()
IsTrue(tags.Has<TestTag>());
IsTrue(tags.Has<TestTag2>());

AreEqual(1, data.Id);
AreEqual("Id: 1", data.ToString());
AreSame (entity.Archetype, data.Archetype);
AreEqual(new Position(1,2,3), data.Get<Position>());
IsTrue ( data.Has<Position>());
Expand All @@ -35,6 +38,19 @@ public static void Test_EntityData_access()
IsFalse( data.Has<Scale3>());
IsFalse( data.TryGet<Scale3>(out _));

var components = data.Components;
AreEqual(2, components.Count);
int count = 0;
foreach (var component in components)
{
var value = component.Value;
switch (count++) {
case 0: AreEqual("test", ((EntityName)value).value); break;
case 1: AreEqual(new Position(1,2,3), (Position)value); break;
}
}
AreEqual(2, count);

entity.DeleteEntity();

data = entity.Data;
Expand All @@ -46,7 +62,7 @@ public static void Test_EntityData_access()
GetComponent(entity);
});
}

#pragma warning restore CS0618 // Type or member is obsolete

private static void GetTags(Entity entity) {
var state = entity.Data;
Expand Down

0 comments on commit c4d763d

Please sign in to comment.