Compare commits

..

7 Commits

Author SHA1 Message Date
cosmonaut 3e60d2dff2 reset entity tag on Destroy 2024-02-27 15:48:00 -08:00
cosmonaut 3b460cf326 fix crash in Debug_GetEntities 2024-02-27 15:47:26 -08:00
cosmonaut 079765d009 fix off by one error on LinearCongruentialEnumerator 2024-01-30 17:10:55 -08:00
cosmonaut 0d567f1854 allow Manipulator to send messages 2024-01-18 15:58:28 -08:00
cosmonaut 9b90d47d01 remove Platforms directive from csproj 2024-01-18 15:58:17 -08:00
cosmonaut 842398b237 mark type assigners as internal 2023-12-20 15:16:49 -08:00
cosmonaut b31120310c Eliminating dictionary lookups (#7)
Did some storage restructuring to avoid dictionary lookups. This should be a fairly significant speedup, particularly on the type lookups which now use a clever static generic lookup.

Reviewed-on: #7
2023-12-20 23:12:05 +00:00
12 changed files with 208 additions and 214 deletions

View File

@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

View File

@ -7,8 +7,8 @@ namespace MoonTools.ECS.Collections;
public unsafe class NativeArray<T> : IDisposable where T : unmanaged
{
private T* Elements;
public int Count { get; private set; }
public int Capacity { get; private set; }
public int Count { get; private set;}
private int Capacity;
private int ElementSize;
public Span<T> ToSpan() => new Span<T>(Elements, Count);
@ -61,7 +61,7 @@ public unsafe class NativeArray<T> : IDisposable where T : unmanaged
Count = 0;
}
public void ResizeTo(int size)
private void ResizeTo(int size)
{
Capacity = size;
Elements = (T*) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));

View File

@ -7,8 +7,8 @@ namespace MoonTools.ECS.Collections;
internal unsafe class NativeArray : IDisposable
{
private nint Elements;
public int Count { get; private set; }
public int Capacity { get; private set; }
public int Count { get; private set;}
private int Capacity;
public readonly int ElementSize;
private bool IsDisposed;
@ -44,7 +44,7 @@ internal unsafe class NativeArray : IDisposable
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
}
public void ResizeTo(int capacity)
private void ResizeTo(int capacity)
{
Capacity = capacity;
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
@ -65,11 +65,6 @@ internal unsafe class NativeArray : IDisposable
Count -= 1;
}
public void RemoveLastElement()
{
Count -= 1;
}
public void Append<T>(T component) where T : unmanaged
{
if (Count >= Capacity)

View File

@ -1,105 +1,90 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using MoonTools.ECS.Collections;
namespace MoonTools.ECS;
public class ComponentStorage : IDisposable
internal class ComponentStorage : IDisposable
{
internal readonly NativeArray<Entity> DenseArray = new NativeArray<Entity>();
internal readonly NativeArray<int> SparseArray = new NativeArray<int>();
internal nint ElementArray;
internal int ElementArrayCapacity;
internal readonly Dictionary<Entity, int> EntityIDToStorageIndex = new Dictionary<Entity, int>(16);
internal readonly NativeArray Components;
internal readonly NativeArray<Entity> EntityIDs;
internal readonly TypeId TypeId;
internal readonly int ElementSize;
private bool IsDisposed;
public int ElementSize { get; private set; }
public int Count => DenseArray.Count;
public unsafe ComponentStorage(int elementSize)
public ComponentStorage(TypeId typeId, int elementSize)
{
for (var i = 0; i < 16; i += 1)
{
SparseArray.Append(Entity.Null.ID); // sentinel value
}
ElementArrayCapacity = 16;
ElementArray = (nint) NativeMemory.Alloc((nuint) (elementSize * ElementArrayCapacity));
ElementSize = elementSize;
Components = new NativeArray(elementSize);
EntityIDs = new NativeArray<Entity>();
TypeId = typeId;
}
public bool Any() => DenseArray.Count > 0;
public unsafe ref T Get<T>(Entity entity) where T : unmanaged
public bool Any()
{
return ref ((T*) ElementArray)[SparseArray[entity.ID]];
}
public unsafe ref T GetFirst<T>() where T : unmanaged
{
#if DEBUG
if (DenseArray.Count == 0)
{
throw new IndexOutOfRangeException("Component storage is empty!");
}
#endif
return ref ((T*) ElementArray)[0];
}
public unsafe bool Set<T>(Entity entity, T component) where T : unmanaged
{
var newEntity = entity.ID >= SparseArray.Count || SparseArray[entity.ID] == Entity.Null.ID;
if (newEntity)
{
// the entity is being added! let's do some record keeping
var index = DenseArray.Count;
DenseArray.Append(entity);
if (entity.ID >= SparseArray.Count)
{
var oldCount = SparseArray.Count;
SparseArray.ResizeTo(entity.ID + 1);
for (var i = oldCount; i < SparseArray.Capacity; i += 1)
{
SparseArray.Append(Entity.Null.ID); // sentinel value
}
}
SparseArray[entity.ID] = index;
if (entity.ID >= ElementArrayCapacity)
{
ElementArrayCapacity = entity.ID + 1;
ElementArray = (nint) NativeMemory.Realloc((void*) ElementArray, (nuint) (ElementArrayCapacity * ElementSize));
}
}
Unsafe.Write((void*) (ElementArray + ElementSize * SparseArray[entity.ID]), component);
return !newEntity;
return Components.Count > 0;
}
public bool Has(Entity entity)
{
return entity.ID < SparseArray.Count && SparseArray[entity.ID] != Entity.Null.ID;
return EntityIDToStorageIndex.ContainsKey(entity);
}
public unsafe bool Remove(Entity entity)
public ref T Get<T>(in Entity entity) where T : unmanaged
{
if (Has(entity))
return ref Components.Get<T>(EntityIDToStorageIndex[entity]);
}
public ref T GetFirst<T>() where T : unmanaged
{
#if DEBUG
if (Components.Count == 0)
{
var denseIndex = SparseArray[entity.ID];
var lastItem = DenseArray[DenseArray.Count - 1];
DenseArray[denseIndex] = lastItem;
SparseArray[lastItem.ID] = denseIndex;
SparseArray[entity.ID] = Entity.Null.ID; // sentinel value
throw new IndexOutOfRangeException("Component storage is empty!");
}
#endif
return ref Components.Get<T>(0);
}
if (denseIndex != DenseArray.Count - 1)
// Returns true if the entity had this component.
public bool Set<T>(in Entity entity, in T component) where T : unmanaged
{
if (EntityIDToStorageIndex.TryGetValue(entity, out var index))
{
Components.Set(index, component);
return true;
}
else
{
EntityIDToStorageIndex[entity] = Components.Count;
EntityIDs.Append(entity);
Components.Append(component);
return false;
}
}
// Returns true if the entity had this component.
public bool Remove(in Entity entity)
{
if (EntityIDToStorageIndex.TryGetValue(entity, out int index))
{
var lastElementIndex = Components.Count - 1;
var lastEntity = EntityIDs[lastElementIndex];
// move a component into the hole to maintain contiguous memory
Components.Delete(index);
EntityIDs.Delete(index);
EntityIDToStorageIndex.Remove(entity);
// update the index if it changed
if (lastElementIndex != index)
{
NativeMemory.Copy((void*) (ElementArray + ElementSize * (DenseArray.Count - 1)), (void*) (ElementArray + ElementSize * denseIndex), (nuint) ElementSize);
EntityIDToStorageIndex[lastEntity] = index;
}
DenseArray.RemoveLastElement();
return true;
}
@ -108,47 +93,46 @@ public class ComponentStorage : IDisposable
public void Clear()
{
DenseArray.Clear();
for (var i = 0; i < SparseArray.Capacity; i += 1)
{
SparseArray[i] = Entity.Null.ID;
}
Components.Clear();
EntityIDs.Clear();
EntityIDToStorageIndex.Clear();
}
public Entity FirstEntity()
{
#if DEBUG
if (DenseArray.Count == 0)
if (EntityIDs.Count == 0)
{
throw new IndexOutOfRangeException("Component storage is empty!");
}
#endif
return DenseArray[0];
return EntityIDs[0];
}
#if DEBUG
internal Span<Entity> Debug_GetEntities()
internal IEnumerable<Entity> Debug_GetEntities()
{
return DenseArray.ToSpan();
return EntityIDToStorageIndex.Keys;
}
#endif
protected unsafe virtual void Dispose(bool disposing)
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
DenseArray.Dispose();
SparseArray.Dispose();
}
NativeMemory.Free((void*) ElementArray);
Components.Dispose();
EntityIDs.Dispose();
IsDisposed = true;
}
}
// ~ComponentStorage()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method

View File

@ -11,7 +11,7 @@ public abstract class DebugSystem : System
protected DebugSystem(World world) : base(world) { }
protected World.ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity) => World.Debug_GetAllComponentTypes(entity);
protected Span<Entity> Debug_GetEntities(Type componentType) => World.Debug_GetEntities(componentType);
protected IEnumerable<Entity> Debug_GetEntities(Type componentType) => World.Debug_GetEntities(componentType);
protected IEnumerable<Type> Debug_SearchComponentType(string typeString) => World.Debug_SearchComponentType(typeString);
}
#endif

View File

@ -1,6 +1,3 @@
namespace MoonTools.ECS;
public readonly record struct Entity(int ID)
{
public static readonly Entity Null = new Entity(int.MaxValue);
}
public readonly record struct Entity(uint ID);

View File

@ -5,12 +5,12 @@ namespace MoonTools.ECS;
internal class IdAssigner : IDisposable
{
int Next = 0;
NativeArray<int> AvailableIds = new NativeArray<int>();
uint Next;
NativeArray<uint> AvailableIds = new NativeArray<uint>();
private bool IsDisposed;
public int Assign()
public uint Assign()
{
if (AvailableIds.TryPop(out var id))
{
@ -22,7 +22,7 @@ internal class IdAssigner : IDisposable
return id;
}
public void Unassign(int id)
public void Unassign(uint id)
{
AvailableIds.Append(id);
}
@ -46,6 +46,13 @@ internal class IdAssigner : IDisposable
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~IdAssigner()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method

View File

@ -15,4 +15,6 @@ public abstract class Manipulator : EntityComponentReader
protected void Unrelate<TRelationKind>(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged => World.Unrelate<TRelationKind>(entityA, entityB);
protected void UnrelateAll<TRelationKind>(in Entity entity) where TRelationKind : unmanaged => World.UnrelateAll<TRelationKind>(entity);
protected void Destroy(in Entity entity) => World.Destroy(entity);
protected void Send<TMessage>(in TMessage message) where TMessage : unmanaged => World.Send(message);
}

View File

@ -39,7 +39,7 @@ public static class RandomManager
x = Primes[Random.Next(Primes.Length - 1)];
}
return new LinearCongruentialEnumerator(Random.Next(n), x, n);
return new LinearCongruentialEnumerator(Random.Next(n + 1), x, n);
}
}
@ -65,7 +65,7 @@ public struct LinearCongruentialEnumerator
public bool MoveNext()
{
current += 1;
if (current < start + count)
if (current <= start + count)
{
return true;
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using MoonTools.ECS.Collections;
namespace MoonTools.ECS;
@ -16,11 +15,11 @@ public class Snapshot : IDisposable
private List<RelationSnapshot> RelationSnapshots = new List<RelationSnapshot>();
private Dictionary<Entity, IndexableSet<TypeId>> EntityRelationIndex =
new Dictionary<Entity, IndexableSet<TypeId>>();
private List<IndexableSet<TypeId>> EntityRelationIndex =
new List<IndexableSet<TypeId>>();
private Dictionary<Entity, IndexableSet<TypeId>> EntityComponentIndex =
new Dictionary<Entity, IndexableSet<TypeId>>();
private List<IndexableSet<TypeId>> EntityComponentIndex =
new List<IndexableSet<TypeId>>();
private List<string> EntityTags = new List<string>();
@ -61,7 +60,7 @@ public class Snapshot : IDisposable
relationStorage.Clear();
}
for (var i = 0; i < world.ComponentIndex.Count; i += 1)
for (var i = 0; i < ComponentSnapshots.Count; i += 1)
{
var componentStorage = world.ComponentIndex[i];
ComponentSnapshots[i].Restore(componentStorage);
@ -77,40 +76,43 @@ public class Snapshot : IDisposable
// restore entity relation index state
// FIXME: arghhhh this is so slow
foreach (var (id, relationTypeSet) in world.EntityRelationIndex)
foreach (var relationTypeSet in world.EntityRelationIndex)
{
relationTypeSet.Clear();
}
foreach (var (id, relationTypeSet) in EntityRelationIndex)
for (var i = 0; i < EntityRelationIndex.Count; i += 1)
{
var relationTypeSet = EntityRelationIndex[i];
foreach (var typeId in relationTypeSet)
{
world.EntityRelationIndex[id].Add(typeId);
world.EntityRelationIndex[i].Add(typeId);
}
}
// restore entity component index state
// FIXME: arrghghhh this is so slow
foreach (var (id, componentTypeSet) in world.EntityComponentIndex)
foreach (var componentTypeSet in world.EntityComponentIndex)
{
componentTypeSet.Clear();
}
foreach (var (id, componentTypeSet) in EntityComponentIndex)
for (var i = 0; i < EntityComponentIndex.Count; i += 1)
{
var componentTypeSet = EntityComponentIndex[i];
foreach (var typeId in componentTypeSet)
{
world.EntityComponentIndex[id].Add(typeId);
world.EntityComponentIndex[i].Add(typeId);
}
}
// restore entity tags
world.EntityTags.Clear();
foreach (var s in EntityTags)
for (var i = 0; i < EntityTags.Count; i += 1)
{
world.EntityTags.Add(s);
world.EntityTags[i] = EntityTags[i];
}
}
@ -147,37 +149,39 @@ public class Snapshot : IDisposable
RelationSnapshots[i].Take(world.RelationIndex[i]);
}
// fill in missing index structures
for (var i = EntityComponentIndex.Count; i < world.EntityComponentIndex.Count; i += 1)
{
EntityComponentIndex.Add(new IndexableSet<TypeId>());
}
for (var i = EntityRelationIndex.Count; i < world.EntityRelationIndex.Count; i += 1)
{
EntityRelationIndex.Add(new IndexableSet<TypeId>());
}
// copy entity relation index
// FIXME: arghhhh this is so slow
foreach (var (id, relationTypeSet) in world.EntityRelationIndex)
for (var i = 0; i < world.EntityRelationIndex.Count; i += 1)
{
if (!EntityRelationIndex.ContainsKey(id))
{
EntityRelationIndex.Add(id, new IndexableSet<TypeId>());
}
EntityRelationIndex[i].Clear();
EntityRelationIndex[id].Clear();
foreach (var typeId in relationTypeSet)
foreach (var typeId in world.EntityRelationIndex[i])
{
EntityRelationIndex[id].Add(typeId);
EntityRelationIndex[i].Add(typeId);
}
}
// copy entity component index
// FIXME: arghhhh this is so slow
foreach (var (id, componentTypeSet) in world.EntityComponentIndex)
for (var i = 0; i < world.EntityComponentIndex.Count; i += 1)
{
if (!EntityComponentIndex.ContainsKey(id))
{
EntityComponentIndex.Add(id, new IndexableSet<TypeId>());
}
EntityComponentIndex[i].Clear();
EntityComponentIndex[id].Clear();
foreach (var typeId in componentTypeSet)
foreach (var typeId in world.EntityComponentIndex[i])
{
EntityComponentIndex[id].Add(typeId);
EntityComponentIndex[i].Add(typeId);
}
}
@ -207,51 +211,46 @@ public class Snapshot : IDisposable
private class ComponentSnapshot : IDisposable
{
private readonly NativeArray<Entity> DenseArray;
private readonly NativeArray<int> SparseArray;
private nint ElementArray;
private int ElementArrayCapacity;
private readonly NativeArray Components;
private readonly NativeArray<Entity> EntityIDs;
private bool IsDisposed;
public unsafe ComponentSnapshot(int elementSize)
public ComponentSnapshot(int elementSize)
{
ElementArray = (nint) NativeMemory.Alloc((nuint) (16 * elementSize));
DenseArray = new NativeArray<Entity>(elementSize);
SparseArray = new NativeArray<int>(elementSize);
Components = new NativeArray(elementSize);
EntityIDs = new NativeArray<Entity>();
}
public unsafe void Take(ComponentStorage componentStorage)
public void Take(ComponentStorage componentStorage)
{
componentStorage.DenseArray.CopyTo(DenseArray);
componentStorage.SparseArray.CopyTo(SparseArray);
if (componentStorage.ElementArrayCapacity > ElementArrayCapacity)
componentStorage.Components.CopyAllTo(Components);
componentStorage.EntityIDs.CopyTo(EntityIDs);
}
public void Restore(ComponentStorage componentStorage)
{
Components.CopyAllTo(componentStorage.Components);
EntityIDs.CopyTo(componentStorage.EntityIDs);
componentStorage.EntityIDToStorageIndex.Clear();
for (int i = 0; i < EntityIDs.Count; i += 1)
{
ElementArrayCapacity = componentStorage.ElementArrayCapacity;
NativeMemory.Realloc((void*) ElementArray, (nuint) (componentStorage.ElementSize * ElementArrayCapacity));
var entityID = EntityIDs[i];
componentStorage.EntityIDToStorageIndex[entityID] = i;
}
NativeMemory.Copy((void*) componentStorage.ElementArray, (void*) ElementArray, (nuint) (ElementArrayCapacity * componentStorage.ElementSize));
}
public unsafe void Restore(ComponentStorage componentStorage)
{
DenseArray.CopyTo(componentStorage.DenseArray);
SparseArray.CopyTo(componentStorage.SparseArray);
NativeMemory.Copy((void*) ElementArray, (void*) componentStorage.ElementArray, (nuint) (ElementArrayCapacity * componentStorage.ElementSize));
}
protected unsafe virtual void Dispose(bool disposing)
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
DenseArray.Dispose();
SparseArray.Dispose();
Components.Dispose();
EntityIDs.Dispose();
}
NativeMemory.Free((void*) ElementArray);
IsDisposed = true;
}
}
@ -353,12 +352,12 @@ public class Snapshot : IDisposable
relationSnapshot.Dispose();
}
foreach (var componentSet in EntityComponentIndex.Values)
foreach (var componentSet in EntityComponentIndex)
{
componentSet.Dispose();
}
foreach (var relationSet in EntityRelationIndex.Values)
foreach (var relationSet in EntityRelationIndex)
{
relationSet.Dispose();
}

View File

@ -16,12 +16,12 @@ public readonly record struct TypeId(uint Value) : IComparable<TypeId>
}
}
public class ComponentTypeIdAssigner
internal class ComponentTypeIdAssigner
{
protected static ushort Counter;
}
public class ComponentTypeIdAssigner<T> : ComponentTypeIdAssigner
internal class ComponentTypeIdAssigner<T> : ComponentTypeIdAssigner
{
public static readonly ushort Id;
public static readonly int Size;
@ -41,12 +41,12 @@ public class ComponentTypeIdAssigner<T> : ComponentTypeIdAssigner
}
}
public class RelationTypeIdAssigner
internal class RelationTypeIdAssigner
{
protected static ushort Counter;
}
public class RelationTypeIdAssigner<T> : RelationTypeIdAssigner
internal class RelationTypeIdAssigner<T> : RelationTypeIdAssigner
{
public static readonly ushort Id;
public static readonly int Size;
@ -61,12 +61,12 @@ public class RelationTypeIdAssigner<T> : RelationTypeIdAssigner
}
}
public class MessageTypeIdAssigner
internal class MessageTypeIdAssigner
{
protected static ushort Counter;
}
public class MessageTypeIdAssigner<T> : MessageTypeIdAssigner
internal class MessageTypeIdAssigner<T> : MessageTypeIdAssigner
{
public static readonly ushort Id;
public static readonly int Size;

View File

@ -3,6 +3,10 @@ using System.Collections.Generic;
using System.Runtime.CompilerServices;
using MoonTools.ECS.Collections;
#if DEBUG
using System.Reflection;
#endif
namespace MoonTools.ECS;
public class World : IDisposable
@ -26,7 +30,7 @@ public class World : IDisposable
// Relation Storages
internal List<RelationStorage> RelationIndex = new List<RelationStorage>();
internal Dictionary<Entity, IndexableSet<TypeId>> EntityRelationIndex = new Dictionary<Entity, IndexableSet<TypeId>>();
internal List<IndexableSet<TypeId>> EntityRelationIndex = new List<IndexableSet<TypeId>>();
// Message Storages
private List<MessageStorage> MessageIndex = new List<MessageStorage>();
@ -34,7 +38,7 @@ public class World : IDisposable
public FilterBuilder FilterBuilder => new FilterBuilder(this);
internal readonly List<ComponentStorage> ComponentIndex = new List<ComponentStorage>();
internal Dictionary<Entity, IndexableSet<TypeId>> EntityComponentIndex = new Dictionary<Entity, IndexableSet<TypeId>>();
internal List<IndexableSet<TypeId>> EntityComponentIndex = new List<IndexableSet<TypeId>>();
internal IdAssigner EntityIdAssigner = new IdAssigner();
@ -51,7 +55,8 @@ public class World : IDisposable
// add missing storages, it's possible for there to be multiples in multi-world scenarios
for (var i = ComponentIndex.Count; i <= typeId; i += 1)
{
var componentStorage = new ComponentStorage(ComponentTypeElementSizes[i]);
var missingTypeId = new TypeId((uint) i);
var componentStorage = new ComponentStorage(missingTypeId, ComponentTypeElementSizes[i]);
ComponentIndex.Add(componentStorage);
ComponentTypeToFilter.Add(new List<Filter>());
}
@ -96,10 +101,10 @@ public class World : IDisposable
{
var entity = new Entity(EntityIdAssigner.Assign());
if (!EntityComponentIndex.ContainsKey(entity))
if (entity.ID == EntityComponentIndex.Count)
{
EntityRelationIndex.Add(entity, new IndexableSet<TypeId>());
EntityComponentIndex.Add(entity, new IndexableSet<TypeId>());
EntityRelationIndex.Add(new IndexableSet<TypeId>());
EntityComponentIndex.Add(new IndexableSet<TypeId>());
EntityTags.Add(tag);
}
@ -118,8 +123,11 @@ public class World : IDisposable
public void Destroy(in Entity entity)
{
var componentSet = EntityComponentIndex[(int) entity.ID];
var relationSet = EntityRelationIndex[(int) entity.ID];
// remove all components from storages
foreach (var componentTypeIndex in EntityComponentIndex[entity])
foreach (var componentTypeIndex in componentSet)
{
var componentStorage = ComponentIndex[componentTypeIndex];
componentStorage.Remove(entity);
@ -131,29 +139,32 @@ public class World : IDisposable
}
// remove all relations from storage
foreach (var relationTypeIndex in EntityRelationIndex[entity])
foreach (var relationTypeIndex in relationSet)
{
var relationStorage = RelationIndex[relationTypeIndex];
relationStorage.RemoveEntity(entity);
}
EntityComponentIndex[entity].Clear();
EntityRelationIndex[entity].Clear();
componentSet.Clear();
relationSet.Clear();
// recycle ID
EntityIdAssigner.Unassign(entity.ID);
EntityTags[(int) entity.ID] = "";
}
// COMPONENTS
public bool Has<T>(in Entity entity) where T : unmanaged
{
return GetComponentStorage<T>().Has(entity);
var storage = GetComponentStorage<T>();
return storage.Has(entity);
}
internal bool Has(in Entity entity, in TypeId typeId)
{
return ComponentIndex[(int) typeId.Value].Has(entity);
return EntityComponentIndex[(int) entity.ID].Contains(typeId);
}
public bool Some<T>() where T : unmanaged
@ -183,13 +194,12 @@ public class World : IDisposable
public void Set<T>(in Entity entity, in T component) where T : unmanaged
{
var componentStorage = GetComponentStorage<T>();
var typeId = new TypeId(ComponentTypeIdAssigner<T>.Id);
if (!componentStorage.Set(entity, component))
{
EntityComponentIndex[entity].Add(typeId);
EntityComponentIndex[(int) entity.ID].Add(componentStorage.TypeId);
foreach (var filter in ComponentTypeToFilter[typeId])
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
{
filter.Check(entity);
}
@ -199,13 +209,12 @@ public class World : IDisposable
public void Remove<T>(in Entity entity) where T : unmanaged
{
var componentStorage = GetComponentStorage<T>();
var typeId = new TypeId(ComponentTypeIdAssigner<T>.Id);
if (componentStorage.Remove(entity))
{
EntityComponentIndex[entity].Remove(typeId);
EntityComponentIndex[(int) entity.ID].Remove(componentStorage.TypeId);
foreach (var filter in ComponentTypeToFilter[typeId])
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
{
filter.Check(entity);
}
@ -235,8 +244,8 @@ public class World : IDisposable
{
var relationStorage = GetRelationStorage<T>();
relationStorage.Set(entityA, entityB, relation);
EntityRelationIndex[entityA].Add(new TypeId(RelationTypeIdAssigner<T>.Id));
EntityRelationIndex[entityB].Add(new TypeId(RelationTypeIdAssigner<T>.Id));
EntityRelationIndex[(int) entityA.ID].Add(new TypeId(RelationTypeIdAssigner<T>.Id));
EntityRelationIndex[(int) entityB.ID].Add(new TypeId(RelationTypeIdAssigner<T>.Id));
}
public void Unrelate<T>(in Entity entityA, in Entity entityB) where T : unmanaged
@ -388,13 +397,15 @@ public class World : IDisposable
#if DEBUG
public ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity)
{
return new ComponentTypeEnumerator(this, EntityComponentIndex[entity]);
return new ComponentTypeEnumerator(this, EntityComponentIndex[(int) entity.ID]);
}
public Span<Entity> Debug_GetEntities(Type componentType)
public IEnumerable<Entity> Debug_GetEntities(Type componentType)
{
var storage = ComponentIndex[ComponentTypeToId[componentType]];
return storage.Debug_GetEntities();
var baseGetComponentStorageMethod = typeof(World).GetMethod(nameof(World.GetComponentStorage), BindingFlags.NonPublic | BindingFlags.Instance)!;
var genericGetComponentStorageMethod = baseGetComponentStorageMethod.MakeGenericMethod(componentType);
var storage = genericGetComponentStorageMethod.Invoke(this, null) as ComponentStorage;
return storage!.Debug_GetEntities();
}
public IEnumerable<Type> Debug_SearchComponentType(string typeString)
@ -457,14 +468,14 @@ public class World : IDisposable
messageStorage.Dispose();
}
foreach (var typeSet in EntityComponentIndex.Values)
foreach (var componentSet in EntityComponentIndex)
{
typeSet.Dispose();
componentSet.Dispose();
}
foreach (var typeSet in EntityRelationIndex.Values)
foreach (var relationSet in EntityRelationIndex)
{
typeSet.Dispose();
relationSet.Dispose();
}
foreach (var filter in FilterIndex.Values)