Skip to content

Commit

Permalink
ECS - extract EntityNode.IsAlive()
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jul 30, 2024
1 parent b5f9014 commit 265c306
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/ECS/Base/Types/ComponentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal ComponentType(string componentKey, int structIndex, Type indexType, Typ
internal override bool RemoveEntityComponent(Entity entity) {
int archIndex = 0;
ref var node = ref entity.store.nodes[entity.Id];
if (node.archetype != null && node.revision == entity.Revision) {
if (node.IsAlive(entity.Revision)) {
return EntityStoreBase.RemoveComponent<T>(entity.Id, ref node.archetype, ref node.compIndex, ref archIndex, StructIndex);
}
throw EntityStoreBase.EntityArgumentNullException(entity, nameof(entity));
Expand All @@ -105,7 +105,7 @@ internal override bool RemoveEntityComponent(Entity entity) {
internal override bool AddEntityComponent(Entity entity) {
int archIndex = 0;
ref var node = ref entity.store.nodes[entity.Id];
if (node.archetype != null && node.revision == entity.Revision) {
if (node.IsAlive(entity.Revision)) {
return EntityStoreBase.AddComponent<T>(entity.Id, StructIndex, ref node.archetype, ref node.compIndex, ref archIndex, default);
}
throw EntityStoreBase.EntityArgumentNullException(entity, nameof(entity));
Expand All @@ -115,7 +115,7 @@ internal override bool AddEntityComponentValue(Entity entity, object value) {
int archIndex = 0;
var componentValue = (T)value;
ref var node = ref entity.store.nodes[entity.Id];
if (node.archetype != null && node.revision == entity.Revision) {
if (node.IsAlive(entity.Revision)) {
return EntityStoreBase.AddComponent(entity.Id, StructIndex, ref node.archetype, ref node.compIndex, ref archIndex, componentValue);
}
throw EntityStoreBase.EntityArgumentNullException(entity, nameof(entity));
Expand Down
31 changes: 15 additions & 16 deletions 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 {
/// <exception cref="NullReferenceException"> if entity has no <see cref="EntityName"/></exception>
[Browse(Never)] public ref EntityName Name { get {
var node = store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return ref node.archetype.std.name.components[node.compIndex];
}
throw EntityNullException();
Expand All @@ -267,7 +267,7 @@ public ref readonly Tags Tags { get {
/// <exception cref="NullReferenceException"> if entity has no <see cref="Position"/></exception>
[Browse(Never)] public ref Position Position { get {
var node = store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return ref node.archetype.std.position.components[node.compIndex];
}
throw EntityNullException();
Expand All @@ -277,7 +277,7 @@ public ref readonly Tags Tags { get {
/// <exception cref="NullReferenceException"> if entity has no <see cref="Rotation"/></exception>
[Browse(Never)] public ref Rotation Rotation { get {
var node = store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return ref node.archetype.std.rotation.components[node.compIndex];
}
throw EntityNullException();
Expand All @@ -287,7 +287,7 @@ public ref readonly Tags Tags { get {
/// <exception cref="NullReferenceException"> if entity has no <see cref="Scale3"/></exception>
[Browse(Never)] public ref Scale3 Scale3 { get {
var node = store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return ref node.archetype.std.scale3.components[node.compIndex];
}
throw EntityNullException();
Expand Down Expand Up @@ -386,7 +386,7 @@ public bool HasComponent<T> () where T : struct, IComponent {
/// <remarks>Executes in O(1)</remarks>
public ref T GetComponent<T>() where T : struct, IComponent {
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return ref ((StructHeap<T>)node.archetype.heapMap[StructInfo<T>.Index]).components[node.compIndex];
}
throw EntityNullException();
Expand All @@ -396,7 +396,7 @@ public ref T GetComponent<T>() where T : struct, IComponent {
public bool TryGetComponent<T>(out T result) where T : struct, IComponent
{
var node = store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
var heap = node.archetype.heapMap[StructInfo<T>.Index];
if (heap == null) {
result = default;
Expand All @@ -417,7 +417,7 @@ public bool TryGetComponent<T>(out T result) where T : struct, IComponent
public bool AddComponent<T>() where T : struct, IComponent {
int archIndex = 0;
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return EntityStoreBase.AddComponent<T>(Id, StructInfo<T>.Index, ref node.archetype, ref node.compIndex, ref archIndex, default);
}
throw EntityNullException();
Expand All @@ -431,7 +431,7 @@ public bool AddComponent<T>() where T : struct, IComponent {
public bool AddComponent<T>(in T component) where T : struct, IComponent {
int archIndex = 0;
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return EntityStoreBase.AddComponent (Id, StructInfo<T>.Index, ref node.archetype, ref node.compIndex, ref archIndex, in component);
}
throw EntityNullException();
Expand All @@ -445,7 +445,7 @@ public bool AddComponent<T>(in T component) where T : struct, IComponent {
public bool RemoveComponent<T>() where T : struct, IComponent {
int archIndex = 0;
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return EntityStoreBase.RemoveComponent<T>(Id, ref node.archetype, ref node.compIndex, ref archIndex, StructInfo<T>.Index);
}
throw EntityNullException();
Expand Down Expand Up @@ -510,7 +510,7 @@ public bool TryGetScript<TScript>(out TScript result)
public bool AddTag<TTag>() where TTag : struct, ITag {
int index = 0;
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return EntityStoreBase.AddTags (store, Tags.Get<TTag>(), Id, ref node.archetype, ref node.compIndex, ref index);
}
throw EntityNullException();
Expand All @@ -519,7 +519,7 @@ public bool AddTag<TTag>() where TTag : struct, ITag {
public bool AddTags(in Tags tags) {
int index = 0;
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return EntityStoreBase.AddTags (store, tags, Id, ref node.archetype, ref node.compIndex, ref index);
}
throw EntityNullException();
Expand All @@ -528,7 +528,7 @@ public bool AddTags(in Tags tags) {
public bool RemoveTag<TTag>() where TTag : struct, ITag {
int index = 0;
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return EntityStoreBase.RemoveTags(store, Tags.Get<TTag>(), Id, ref node.archetype, ref node.compIndex, ref index);
}
throw EntityNullException();
Expand All @@ -537,7 +537,7 @@ public bool RemoveTag<TTag>() where TTag : struct, ITag {
public bool RemoveTags(in Tags tags) {
int index = 0;
ref var node = ref store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
return EntityStoreBase.RemoveTags(store, tags, Id, ref node.archetype, ref node.compIndex, ref index);
}
throw EntityNullException();
Expand Down Expand Up @@ -610,8 +610,7 @@ public bool RemoveChild(Entity entity) {
public void DeleteEntity()
{
var node = store.nodes[Id];
if (node.archetype != null && node.revision == Revision)
{
if (node.IsAlive(Revision)) {
try {
// Send event. See: SEND_EVENT notes. Note - Specific characteristic: event is send before deleting the entity.
store.DeleteEntityEvent(this);
Expand All @@ -632,7 +631,7 @@ public void DeleteEntity()
internal bool TryGetTreeNode(out TreeNode treeNode)
{
var node = store.nodes[Id];
if (node.archetype != null && node.revision == Revision) {
if (node.IsAlive(Revision)) {
var heap = node.archetype.heapMap[StructInfo<TreeNode>.Index];
if (heap == null) {
treeNode = default;
Expand Down
4 changes: 4 additions & 0 deletions src/ECS/Entity/EntityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See LICENSE file in the project root for full license information.

using System;
using System.Runtime.CompilerServices;
using System.Text;
using Friflo.Engine.ECS.Index;
using Friflo.Engine.ECS.Relations;
Expand Down Expand Up @@ -100,6 +101,9 @@ public struct EntityNode
#endregion

#region internal methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool IsAlive(int revision) => archetype != null && this.revision == revision;

private readonly string GetString()
{
var sb = new StringBuilder();
Expand Down

0 comments on commit 265c306

Please sign in to comment.