entity tag system

rev2
cosmonaut 2023-11-02 11:03:51 -07:00
parent 2a2d8efc5f
commit d3c4c12605
4 changed files with 32 additions and 1 deletions

View File

@ -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<T>(in EntityId entityId) where T : unmanaged => World.Has<T>(entityId);
protected bool Some<T>() where T : unmanaged => World.Some<T>();
protected ref T Get<T>(in EntityId entityId) where T : unmanaged => ref World.Get<T>(entityId);

View File

@ -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<TComponent>(in EntityId entity, in TComponent component) where TComponent : unmanaged => World.Set<TComponent>(entity, component);
protected void Remove<TComponent>(in EntityId entity) where TComponent : unmanaged => World.Remove<TComponent>(entity);

View File

@ -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<ArchetypeSignature, ArchetypeSnapshot> ArchetypeSnapshots =
@ -17,6 +18,8 @@ public class Snapshot
private Dictionary<EntityId, IndexableSet<TypeId>> EntityRelationIndex =
new Dictionary<EntityId, IndexableSet<TypeId>>();
private Dictionary<EntityId, string> EntityTags = new Dictionary<EntityId, string>();
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)

View File

@ -27,6 +27,9 @@ public class World : IDisposable
// Going from EntityId to Archetype and storage row
internal Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
// TODO: can we make the tag an inline array of chars at some point?
internal Dictionary<EntityId, string> EntityTags = new Dictionary<EntityId, string>();
// Relation Storages
internal Dictionary<TypeId, RelationStorage> RelationIndex =
new Dictionary<TypeId, RelationStorage>();
@ -87,9 +90,21 @@ public class World : IDisposable
EntityRelationIndex.Add(entityId, new IndexableSet<TypeId>());
}
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<T>() where T : unmanaged
{
if (TypeToId.ContainsKey(typeof(T)))