diff --git a/src/ECS/Base/Types/ComponentType.cs b/src/ECS/Base/Types/ComponentType.cs index 7f83e448..949183a3 100644 --- a/src/ECS/Base/Types/ComponentType.cs +++ b/src/ECS/Base/Types/ComponentType.cs @@ -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(entity.Id, ref node.archetype, ref node.compIndex, ref archIndex, StructIndex); } throw EntityStoreBase.EntityArgumentNullException(entity, nameof(entity)); @@ -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(entity.Id, StructIndex, ref node.archetype, ref node.compIndex, ref archIndex, default); } throw EntityStoreBase.EntityArgumentNullException(entity, nameof(entity)); @@ -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)); diff --git a/src/ECS/Entity.cs b/src/ECS/Entity.cs index a826b719..185ff986 100644 --- a/src/ECS/Entity.cs +++ b/src/ECS/Entity.cs @@ -257,7 +257,7 @@ public ref readonly Tags Tags { get { /// if entity has no [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(); @@ -267,7 +267,7 @@ public ref readonly Tags Tags { get { /// if entity has no [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(); @@ -277,7 +277,7 @@ public ref readonly Tags Tags { get { /// if entity has no [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(); @@ -287,7 +287,7 @@ public ref readonly Tags Tags { get { /// if entity has no [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(); @@ -386,7 +386,7 @@ public bool HasComponent () where T : struct, IComponent { /// Executes in O(1) public ref T GetComponent() 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)node.archetype.heapMap[StructInfo.Index]).components[node.compIndex]; } throw EntityNullException(); @@ -396,7 +396,7 @@ public ref T GetComponent() where T : struct, IComponent { public bool TryGetComponent(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.Index]; if (heap == null) { result = default; @@ -417,7 +417,7 @@ public bool TryGetComponent(out T result) where T : struct, IComponent public bool AddComponent() 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.Index, ref node.archetype, ref node.compIndex, ref archIndex, default); } throw EntityNullException(); @@ -431,7 +431,7 @@ public bool AddComponent() where T : struct, IComponent { public bool AddComponent(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.Index, ref node.archetype, ref node.compIndex, ref archIndex, in component); } throw EntityNullException(); @@ -445,7 +445,7 @@ public bool AddComponent(in T component) where T : struct, IComponent { public bool RemoveComponent() 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(Id, ref node.archetype, ref node.compIndex, ref archIndex, StructInfo.Index); } throw EntityNullException(); @@ -510,7 +510,7 @@ public bool TryGetScript(out TScript result) public bool AddTag() 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(), Id, ref node.archetype, ref node.compIndex, ref index); } throw EntityNullException(); @@ -519,7 +519,7 @@ public bool AddTag() 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(); @@ -528,7 +528,7 @@ public bool AddTags(in Tags tags) { public bool RemoveTag() 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(), Id, ref node.archetype, ref node.compIndex, ref index); } throw EntityNullException(); @@ -537,7 +537,7 @@ public bool RemoveTag() 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(); @@ -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); @@ -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.Index]; if (heap == null) { treeNode = default; diff --git a/src/ECS/Entity/EntityNode.cs b/src/ECS/Entity/EntityNode.cs index 977d8518..870ab62b 100644 --- a/src/ECS/Entity/EntityNode.cs +++ b/src/ECS/Entity/EntityNode.cs @@ -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; @@ -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();