Skip to content

Commit

Permalink
ECS - removed redundant EntityNode.flags; archetype is sufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jul 29, 2024
1 parent a3cca7b commit 218531f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
20 changes: 9 additions & 11 deletions src/ECS/Entity/EntityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct EntityNode
public Archetype Archetype => archetype;

/// <summary>Internally used flags assigned to the entity.</summary>
public NodeFlags Flags => flags;
public NodeFlags Flags => archetype != null ? Created : default;

/// <summary>Property only used to see component names encoded by <see cref="isOwner"/>. </summary>
internal ComponentTypes IsOwner => new ComponentTypes{ bitSet = new BitSet { l0 = isOwner } };
Expand All @@ -59,8 +59,8 @@ public struct EntityNode

[Browse(Never)] internal int compIndex; // 4 index within Archetype.entityIds & StructHeap<>.components

/// <summary> Use <see cref="Is"/> or <see cref="IsNot"/> for read access. </summary>
[Browse(Never)] internal NodeFlags flags; // 1
// /// <summary> Use <see cref="Is"/> or <see cref="IsNot"/> for read access. </summary>
// [Browse(Never)] internal NodeFlags flags; // 1

/// <summary>
/// Bit mask for all <see cref="EntityRelations"/> and all <see cref="ComponentIndex"/> instances.<br/>
Expand Down Expand Up @@ -93,23 +93,21 @@ public struct EntityNode
#endregion

#region internal getter
internal readonly bool Is (NodeFlags flag) => (flags & flag) != 0;
internal readonly bool IsNot (NodeFlags flag) => (flags & flag) == 0;
// internal readonly bool Is (NodeFlags flag) => (flags & flag) != 0;
// internal readonly bool IsNot (NodeFlags flag) => (flags & flag) == 0;
#endregion

#region internal methods
private readonly string GetString()
{
var sb = new StringBuilder();
if (flags != 0) {
if (archetype != null) {
sb.Append("flags: ");
var startPos = sb.Length;
if (Is(Created)) {
if (startPos < sb.Length) {
sb.Append(" | ");
}
sb.Append("Created");
if (startPos < sb.Length) {
sb.Append(" | ");
}
sb.Append("Created");
}
return sb.ToString();
}
Expand Down
10 changes: 5 additions & 5 deletions src/ECS/Entity/Store/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal void CheckEntityId(int id)
if (id < Static.MinNodeId) {
throw InvalidEntityIdException(id, nameof(id));
}
if (id < nodes.Length && nodes[id].Is(Created)) {
if (id < nodes.Length && nodes[id].archetype != null) {
throw IdAlreadyInUseException(id, nameof(id));
}
}
Expand Down Expand Up @@ -168,13 +168,13 @@ private int CreateEntityNode(Archetype archetype, int id)
{
AssertIdInNodes(id);
ref var node = ref nodes[id];
if ((node.flags & Created) != 0) {
if (node.archetype != null) {
return node.compIndex;
}
entityCount++;
node.compIndex = Archetype.AddEntity(archetype, id);
node.archetype = archetype;
node.flags = Created;
// node.flags = Created;
return node.compIndex;
}

Expand All @@ -183,7 +183,7 @@ internal void CreateEntityNodes(Archetype archetype, int count)
archetype.EnsureCapacity(count);
int compIndexStart = archetype.entityCount;
var entityIds = archetype.entityIds;
NewIds(entityIds, compIndexStart, count);
NewIds(entityIds, compIndexStart, count, archetype);
EnsureNodesLength(intern.sequenceId + 1); // may resize nodes
var localNodes = nodes;
var maxIndex = compIndexStart + count;
Expand All @@ -193,7 +193,7 @@ internal void CreateEntityNodes(Archetype archetype, int count)
ref var node = ref localNodes[id];
node.compIndex = index;
node.archetype = archetype;
node.flags = Created;
// node.flags = Created;
}
if (intern.pidType == PidType.RandomPids) {
for (int index = compIndexStart; index < maxIndex; index++) {
Expand Down
12 changes: 6 additions & 6 deletions src/ECS/Entity/Store/NodeTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ protected internal override void UpdateEntityCompIndex(int id, int compIndex)
}

/// <summary> Note! Sync implementation with <see cref="NewId"/>. </summary>
private void NewIds(int[] ids, int start, int count)
private void NewIds(int[] ids, int start, int count, Archetype archetype)
{
var localNodes = nodes;
int n = 0;
Expand All @@ -461,16 +461,16 @@ private void NewIds(int[] ids, int start, int count)
if (!intern.recycleIds.TryPop(out int id)) {
break;
}
localNodes[id].flags = Created; // mark created. So id is not used twice by loop below
ids[n + start] = id;
localNodes[id].archetype = archetype; // mark created. So id is not used twice by loop below
ids[n + start] = id;
}
int max = localNodes.Length;
int sequenceId = intern.sequenceId;
for (; n < count; n++)
{
for (; ++sequenceId < max;)
{
if ((localNodes[sequenceId].flags & Created) != 0) {
if (localNodes[sequenceId].archetype != null) {
continue;
}
break;
Expand All @@ -487,7 +487,7 @@ internal int NewId()
int id;
while (intern.recycleIds.TryPop(out id))
{
if ((localNodes[id].flags & Created) != 0) {
if (localNodes[id].archetype != null) {
continue;
}
return id;
Expand All @@ -496,7 +496,7 @@ internal int NewId()
id = ++intern.sequenceId;
for (; id < max;)
{
if ((localNodes[id].flags & Created) != 0) {
if (localNodes[id].archetype != null) {
id = ++intern.sequenceId;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/EntityStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public bool TryGetEntityByPid(long pid, out Entity value)
}
if (0 < pid && pid < nodes.Length) {
var id = (int)pid;
if (nodes[id].Is(NodeFlags.Created)) {
if (nodes[id].archetype != null) {
value = new Entity(this, id);
return true;
}
Expand Down

0 comments on commit 218531f

Please sign in to comment.