From d3c4c1260515058b2a47130d6e518d771d0f7930 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 2 Nov 2023 11:03:51 -0700 Subject: [PATCH] entity tag system --- src/Rev2/Compatibility/EntityComponentReader.cs | 4 +++- src/Rev2/Compatibility/Manipulator.cs | 1 + src/Rev2/Snapshot.cs | 13 +++++++++++++ src/Rev2/World.cs | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Rev2/Compatibility/EntityComponentReader.cs b/src/Rev2/Compatibility/EntityComponentReader.cs index 3b72177..14c944c 100644 --- a/src/Rev2/Compatibility/EntityComponentReader.cs +++ b/src/Rev2/Compatibility/EntityComponentReader.cs @@ -2,7 +2,7 @@ public abstract class EntityComponentReader { - protected World World; + protected readonly World World; public FilterBuilder FilterBuilder => World.FilterBuilder; protected EntityComponentReader(World world) @@ -10,6 +10,8 @@ public abstract class EntityComponentReader World = world; } + protected string GetTag(in EntityId entity) => World.GetTag(entity); + protected bool Has(in EntityId entityId) where T : unmanaged => World.Has(entityId); protected bool Some() where T : unmanaged => World.Some(); protected ref T Get(in EntityId entityId) where T : unmanaged => ref World.Get(entityId); diff --git a/src/Rev2/Compatibility/Manipulator.cs b/src/Rev2/Compatibility/Manipulator.cs index 2538952..cc2cee5 100644 --- a/src/Rev2/Compatibility/Manipulator.cs +++ b/src/Rev2/Compatibility/Manipulator.cs @@ -5,6 +5,7 @@ public class Manipulator : EntityComponentReader public Manipulator(World world) : base(world) { } protected EntityId CreateEntity(string tag = "") => World.CreateEntity(tag); + protected void Tag(in EntityId entity, string tag) => World.Tag(entity, tag); protected void Set(in EntityId entity, in TComponent component) where TComponent : unmanaged => World.Set(entity, component); protected void Remove(in EntityId entity) where TComponent : unmanaged => World.Remove(entity); diff --git a/src/Rev2/Snapshot.cs b/src/Rev2/Snapshot.cs index 57e1313..8433dde 100644 --- a/src/Rev2/Snapshot.cs +++ b/src/Rev2/Snapshot.cs @@ -4,6 +4,7 @@ using MoonTools.ECS.Collections; namespace MoonTools.ECS.Rev2; +// TODO: we should implement a NativeDictionary that can be memcopied public class Snapshot { private Dictionary ArchetypeSnapshots = @@ -17,6 +18,8 @@ public class Snapshot private Dictionary> EntityRelationIndex = new Dictionary>(); + private Dictionary EntityTags = new Dictionary(); + private IdAssigner EntityIdAssigner = new IdAssigner(); public int Count @@ -71,6 +74,11 @@ public class Snapshot world.EntityRelationIndex[id].Add(typeId); } } + + foreach (var (id, s) in EntityTags) + { + world.EntityTags[id] = s; + } } public void Take(World world) @@ -113,6 +121,11 @@ public class Snapshot EntityRelationIndex[id].Add(typeId); } } + + foreach (var (id, s) in world.EntityTags) + { + EntityTags[id] = s; + } } private void TakeArchetypeSnapshot(Archetype archetype) diff --git a/src/Rev2/World.cs b/src/Rev2/World.cs index de6bd64..6dbe7e0 100644 --- a/src/Rev2/World.cs +++ b/src/Rev2/World.cs @@ -27,6 +27,9 @@ public class World : IDisposable // Going from EntityId to Archetype and storage row internal Dictionary EntityIndex = new Dictionary(); + // TODO: can we make the tag an inline array of chars at some point? + internal Dictionary EntityTags = new Dictionary(); + // Relation Storages internal Dictionary RelationIndex = new Dictionary(); @@ -87,9 +90,21 @@ public class World : IDisposable EntityRelationIndex.Add(entityId, new IndexableSet()); } + EntityTags[entityId] = tag; + return entityId; } + public void Tag(in EntityId entityId, string tag) + { + EntityTags[entityId] = tag; + } + + public string GetTag(in EntityId entityId) + { + return EntityTags[entityId]; + } + internal TypeId GetTypeId() where T : unmanaged { if (TypeToId.ContainsKey(typeof(T)))