Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
cosmonaut | 1dfa6ba4ef |
|
@ -1,8 +1,9 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<Platforms>x64</Platforms>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
|
@ -1,106 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace MoonTools.ECS.Collections;
|
|
||||||
|
|
||||||
public unsafe class IndexableSet<T> : IDisposable where T : unmanaged
|
|
||||||
{
|
|
||||||
private Dictionary<T, int> Indices;
|
|
||||||
private T* Array;
|
|
||||||
public int Count { get; private set; }
|
|
||||||
private int Capacity;
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public Span<T> AsSpan() => new Span<T>(Array, Count);
|
|
||||||
public ReverseSpanEnumerator<T> GetEnumerator() => new ReverseSpanEnumerator<T>(new Span<T>(Array, Count));
|
|
||||||
|
|
||||||
public IndexableSet(int capacity = 32)
|
|
||||||
{
|
|
||||||
this.Capacity = capacity;
|
|
||||||
Count = 0;
|
|
||||||
|
|
||||||
Indices = new Dictionary<T, int>(capacity);
|
|
||||||
Array = (T*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<T>()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public T this[int i] => Array[i];
|
|
||||||
|
|
||||||
public bool Contains(T element)
|
|
||||||
{
|
|
||||||
return Indices.ContainsKey(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Add(T element)
|
|
||||||
{
|
|
||||||
if (!Contains(element))
|
|
||||||
{
|
|
||||||
Indices.Add(element, Count);
|
|
||||||
|
|
||||||
if (Count >= Capacity)
|
|
||||||
{
|
|
||||||
Capacity *= 2;
|
|
||||||
Array = (T*) NativeMemory.Realloc(Array, (nuint) (Capacity * Unsafe.SizeOf<T>()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Array[Count] = element;
|
|
||||||
Count += 1;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Remove(T element)
|
|
||||||
{
|
|
||||||
if (!Contains(element))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var index = Indices[element];
|
|
||||||
|
|
||||||
for (var i = index; i < Count - 1; i += 1)
|
|
||||||
{
|
|
||||||
Array[i] = Array[i + 1];
|
|
||||||
Indices[Array[i]] = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
Indices.Remove(element);
|
|
||||||
Count -= 1;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
Indices.Clear();
|
|
||||||
Count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
NativeMemory.Free(Array);
|
|
||||||
Array = null;
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~IndexableSet()
|
|
||||||
{
|
|
||||||
// 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
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,120 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace MoonTools.ECS.Collections;
|
|
||||||
|
|
||||||
public unsafe class NativeArray<T> : IDisposable where T : unmanaged
|
|
||||||
{
|
|
||||||
private T* Elements;
|
|
||||||
public int Count { get; private set;}
|
|
||||||
private int Capacity;
|
|
||||||
private int ElementSize;
|
|
||||||
|
|
||||||
public Span<T> ToSpan() => new Span<T>(Elements, Count);
|
|
||||||
public Span<T>.Enumerator GetEnumerator() => new Span<T>(Elements, Count).GetEnumerator();
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public NativeArray(int capacity = 16)
|
|
||||||
{
|
|
||||||
this.Capacity = capacity;
|
|
||||||
ElementSize = Unsafe.SizeOf<T>();
|
|
||||||
Elements = (T*) NativeMemory.Alloc((nuint) (capacity * ElementSize));
|
|
||||||
Count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref T this[int i] => ref Elements[i];
|
|
||||||
|
|
||||||
public void Append(T item)
|
|
||||||
{
|
|
||||||
if (Count >= Capacity)
|
|
||||||
{
|
|
||||||
Capacity *= 2;
|
|
||||||
Elements = (T*) NativeMemory.Realloc(Elements, (nuint) (Capacity * Unsafe.SizeOf<T>()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Elements[Count] = item;
|
|
||||||
Count += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveLastElement()
|
|
||||||
{
|
|
||||||
Count -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryPop(out T element)
|
|
||||||
{
|
|
||||||
if (Count > 0)
|
|
||||||
{
|
|
||||||
element = Elements[Count - 1];
|
|
||||||
Count -= 1;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
element = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
Count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ResizeTo(int size)
|
|
||||||
{
|
|
||||||
Capacity = size;
|
|
||||||
Elements = (T*) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fills gap by copying final element to the deleted index
|
|
||||||
public void Delete(int index)
|
|
||||||
{
|
|
||||||
if (index != Count - 1)
|
|
||||||
{
|
|
||||||
this[index] = this[Count - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
Count -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CopyTo(NativeArray<T> other)
|
|
||||||
{
|
|
||||||
if (Count >= other.Capacity)
|
|
||||||
{
|
|
||||||
other.ResizeTo(Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
NativeMemory.Copy(
|
|
||||||
(void*) Elements,
|
|
||||||
(void*) other.Elements,
|
|
||||||
(nuint) (ElementSize * Count)
|
|
||||||
);
|
|
||||||
|
|
||||||
other.Count = Count;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
NativeMemory.Free(Elements);
|
|
||||||
Elements = null;
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~NativeArray()
|
|
||||||
{
|
|
||||||
// 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
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,121 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace MoonTools.ECS.Collections;
|
|
||||||
|
|
||||||
internal unsafe class NativeArray : IDisposable
|
|
||||||
{
|
|
||||||
private nint Elements;
|
|
||||||
public int Count { get; private set;}
|
|
||||||
private int Capacity;
|
|
||||||
public readonly int ElementSize;
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public NativeArray(int elementSize)
|
|
||||||
{
|
|
||||||
Capacity = 16;
|
|
||||||
Count = 0;
|
|
||||||
ElementSize = elementSize;
|
|
||||||
|
|
||||||
Elements = (nint) NativeMemory.Alloc((nuint) (ElementSize * Capacity));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Span<T> ToSpan<T>()
|
|
||||||
{
|
|
||||||
return new Span<T>((void*) Elements, Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public ref T Get<T>(int i) where T : unmanaged
|
|
||||||
{
|
|
||||||
return ref ((T*) Elements)[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Set<T>(int i, in T element) where T : unmanaged
|
|
||||||
{
|
|
||||||
Unsafe.Write((void*) (Elements + ElementSize * i), element);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Resize()
|
|
||||||
{
|
|
||||||
Capacity *= 2;
|
|
||||||
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ResizeTo(int capacity)
|
|
||||||
{
|
|
||||||
Capacity = capacity;
|
|
||||||
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fills gap by copying final element to the deleted index
|
|
||||||
public void Delete(int index)
|
|
||||||
{
|
|
||||||
if (index != Count - 1)
|
|
||||||
{
|
|
||||||
NativeMemory.Copy(
|
|
||||||
(void*) (Elements + ((Count - 1) * ElementSize)),
|
|
||||||
(void*) (Elements + (index * ElementSize)),
|
|
||||||
(nuint) ElementSize
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Count -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Append<T>(T component) where T : unmanaged
|
|
||||||
{
|
|
||||||
if (Count >= Capacity)
|
|
||||||
{
|
|
||||||
Resize();
|
|
||||||
}
|
|
||||||
|
|
||||||
((T*) Elements)[Count] = component;
|
|
||||||
Count += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CopyAllTo(NativeArray other)
|
|
||||||
{
|
|
||||||
if (Count >= other.Capacity)
|
|
||||||
{
|
|
||||||
other.ResizeTo(Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
NativeMemory.Copy(
|
|
||||||
(void*) Elements,
|
|
||||||
(void*) other.Elements,
|
|
||||||
(nuint) (ElementSize * Count)
|
|
||||||
);
|
|
||||||
|
|
||||||
other.Count = Count;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
Count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
NativeMemory.Free((void*) Elements);
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~NativeArray()
|
|
||||||
{
|
|
||||||
// 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
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,139 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS
|
||||||
|
{
|
||||||
|
internal class ComponentDepot
|
||||||
|
{
|
||||||
|
private TypeIndices ComponentTypeIndices;
|
||||||
|
|
||||||
|
private ComponentStorage[] storages = new ComponentStorage[256];
|
||||||
|
|
||||||
|
public ComponentDepot(TypeIndices componentTypeIndices)
|
||||||
|
{
|
||||||
|
ComponentTypeIndices = componentTypeIndices;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal void Register<TComponent>(int index) where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
if (index >= storages.Length)
|
||||||
|
{
|
||||||
|
Array.Resize(ref storages, storages.Length * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
storages[index] = new ComponentStorage<TComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private ComponentStorage<TComponent> Lookup<TComponent>() where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
var storageIndex = ComponentTypeIndices.GetIndex<TComponent>();
|
||||||
|
// TODO: is there some way to avoid this null check?
|
||||||
|
if (storageIndex >= storages.Length || storages[storageIndex] == null)
|
||||||
|
{
|
||||||
|
Register<TComponent>(storageIndex);
|
||||||
|
}
|
||||||
|
return (ComponentStorage<TComponent>) storages[storageIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Some<TComponent>() where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TComponent>().Any();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
return ref Lookup<TComponent>().Get(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref readonly TComponent GetFirst<TComponent>() where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
return ref Lookup<TComponent>().GetFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
Lookup<TComponent>().Set(entityID, component);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity GetSingletonEntity<TComponent>() where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TComponent>().FirstEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TComponent>().AllComponents();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(int entityID, int storageIndex)
|
||||||
|
{
|
||||||
|
storages[storageIndex].Remove(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove<TComponent>(int entityID) where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
Lookup<TComponent>().Remove(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
for (var i = 0; i < storages.Length; i += 1)
|
||||||
|
{
|
||||||
|
if (storages[i] != null)
|
||||||
|
{
|
||||||
|
storages[i].Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// these methods used to implement transfers and debugging
|
||||||
|
|
||||||
|
internal unsafe void* UntypedGet(int entityID, int componentTypeIndex)
|
||||||
|
{
|
||||||
|
return storages[componentTypeIndex].UntypedGet(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal unsafe void Set(int entityID, int componentTypeIndex, void* component)
|
||||||
|
{
|
||||||
|
storages[componentTypeIndex].Set(entityID, component);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateMissingStorages(ComponentDepot other)
|
||||||
|
{
|
||||||
|
while (other.ComponentTypeIndices.Count >= storages.Length)
|
||||||
|
{
|
||||||
|
Array.Resize(ref storages, storages.Length * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (other.ComponentTypeIndices.Count >= other.storages.Length)
|
||||||
|
{
|
||||||
|
Array.Resize(ref other.storages, other.storages.Length * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < other.ComponentTypeIndices.Count; i += 1)
|
||||||
|
{
|
||||||
|
if (storages[i] == null && other.storages[i] != null)
|
||||||
|
{
|
||||||
|
storages[i] = other.storages[i].CreateStorage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this method is used to iterate components of an entity, only for use with a debug inspector
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
public object Debug_Get(int entityID, int componentTypeIndex)
|
||||||
|
{
|
||||||
|
return storages[componentTypeIndex].Debug_Get(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<int> Debug_GetEntityIDs(int componentTypeIndex)
|
||||||
|
{
|
||||||
|
return storages[componentTypeIndex].Debug_GetEntityIDs();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,137 +1,175 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using MoonTools.ECS.Collections;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
internal class ComponentStorage : IDisposable
|
|
||||||
{
|
{
|
||||||
internal readonly Dictionary<Entity, int> EntityIDToStorageIndex = new Dictionary<Entity, int>(16);
|
internal abstract class ComponentStorage
|
||||||
internal readonly NativeArray Components;
|
|
||||||
internal readonly NativeArray<Entity> EntityIDs;
|
|
||||||
internal readonly TypeId TypeId;
|
|
||||||
internal readonly int ElementSize;
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public ComponentStorage(TypeId typeId, int elementSize)
|
|
||||||
{
|
{
|
||||||
ElementSize = elementSize;
|
internal abstract unsafe void Set(int entityID, void* component);
|
||||||
Components = new NativeArray(elementSize);
|
public abstract bool Remove(int entityID);
|
||||||
EntityIDs = new NativeArray<Entity>();
|
public abstract void Clear();
|
||||||
TypeId = typeId;
|
|
||||||
|
// used for debugging and template instantiation
|
||||||
|
internal abstract unsafe void* UntypedGet(int entityID);
|
||||||
|
// used to create correctly typed storage on snapshot
|
||||||
|
public abstract ComponentStorage CreateStorage();
|
||||||
|
#if DEBUG
|
||||||
|
internal abstract object Debug_Get(int entityID);
|
||||||
|
internal abstract IEnumerable<int> Debug_GetEntityIDs();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
internal unsafe class ComponentStorage<TComponent> : ComponentStorage, IDisposable where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
private readonly Dictionary<int, int> entityIDToStorageIndex = new Dictionary<int, int>(16);
|
||||||
|
private TComponent* components;
|
||||||
|
private int* entityIDs;
|
||||||
|
private int count = 0;
|
||||||
|
private int capacity = 16;
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
public ComponentStorage()
|
||||||
|
{
|
||||||
|
components = (TComponent*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<TComponent>()));
|
||||||
|
entityIDs = (int*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<int>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Any()
|
public bool Any()
|
||||||
{
|
{
|
||||||
return Components.Count > 0;
|
return count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Has(Entity entity)
|
public ref readonly TComponent Get(int entityID)
|
||||||
{
|
{
|
||||||
return EntityIDToStorageIndex.ContainsKey(entity);
|
return ref components[entityIDToStorageIndex[entityID]];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref T Get<T>(in Entity entity) where T : unmanaged
|
internal override unsafe void* UntypedGet(int entityID)
|
||||||
{
|
{
|
||||||
return ref Components.Get<T>(EntityIDToStorageIndex[entity]);
|
return &components[entityIDToStorageIndex[entityID]];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref T GetFirst<T>() where T : unmanaged
|
public ref readonly TComponent GetFirst()
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (Components.Count == 0)
|
if (count == 0)
|
||||||
{
|
{
|
||||||
throw new IndexOutOfRangeException("Component storage is empty!");
|
throw new IndexOutOfRangeException("Component storage is empty!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return ref Components.Get<T>(0);
|
return ref components[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set(int entityID, in TComponent component)
|
||||||
|
{
|
||||||
|
if (!entityIDToStorageIndex.ContainsKey(entityID))
|
||||||
|
{
|
||||||
|
var index = count;
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
if (index >= capacity)
|
||||||
|
{
|
||||||
|
capacity *= 2;
|
||||||
|
components = (TComponent*) NativeMemory.Realloc(components, (nuint) (capacity * Unsafe.SizeOf<TComponent>()));
|
||||||
|
entityIDs = (int*) NativeMemory.Realloc(entityIDs, (nuint) (capacity * Unsafe.SizeOf<int>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
entityIDToStorageIndex[entityID] = index;
|
||||||
|
entityIDs[index] = entityID;
|
||||||
|
}
|
||||||
|
|
||||||
|
components[entityIDToStorageIndex[entityID]] = component;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override unsafe void Set(int entityID, void* component)
|
||||||
|
{
|
||||||
|
Set(entityID, *(TComponent*) component);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the entity had this component.
|
// Returns true if the entity had this component.
|
||||||
public bool Set<T>(in Entity entity, in T component) where T : unmanaged
|
public override bool Remove(int entityID)
|
||||||
{
|
{
|
||||||
if (EntityIDToStorageIndex.TryGetValue(entity, out var index))
|
if (entityIDToStorageIndex.TryGetValue(entityID, out int storageIndex))
|
||||||
{
|
{
|
||||||
Components.Set(index, component);
|
entityIDToStorageIndex.Remove(entityID);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
EntityIDToStorageIndex[entity] = Components.Count;
|
|
||||||
EntityIDs.Append(entity);
|
|
||||||
Components.Append(component);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns true if the entity had this component.
|
var lastElementIndex = count - 1;
|
||||||
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
|
// move a component into the hole to maintain contiguous memory
|
||||||
Components.Delete(index);
|
if (lastElementIndex != storageIndex)
|
||||||
EntityIDs.Delete(index);
|
|
||||||
EntityIDToStorageIndex.Remove(entity);
|
|
||||||
|
|
||||||
// update the index if it changed
|
|
||||||
if (lastElementIndex != index)
|
|
||||||
{
|
{
|
||||||
EntityIDToStorageIndex[lastEntity] = index;
|
var lastEntityID = entityIDs[lastElementIndex];
|
||||||
|
entityIDToStorageIndex[lastEntityID] = storageIndex;
|
||||||
|
components[storageIndex] = components[lastElementIndex];
|
||||||
|
entityIDs[storageIndex] = lastEntityID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count -= 1;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public override void Clear()
|
||||||
{
|
{
|
||||||
Components.Clear();
|
count = 0;
|
||||||
EntityIDs.Clear();
|
entityIDToStorageIndex.Clear();
|
||||||
EntityIDToStorageIndex.Clear();
|
}
|
||||||
|
|
||||||
|
public ReadOnlySpan<TComponent> AllComponents()
|
||||||
|
{
|
||||||
|
return new ReadOnlySpan<TComponent>(components, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity FirstEntity()
|
public Entity FirstEntity()
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (EntityIDs.Count == 0)
|
if (count == 0)
|
||||||
{
|
{
|
||||||
throw new IndexOutOfRangeException("Component storage is empty!");
|
throw new IndexOutOfRangeException("Component storage is empty!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return EntityIDs[0];
|
return new Entity(entityIDs[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ComponentStorage<TComponent> CreateStorage()
|
||||||
|
{
|
||||||
|
return new ComponentStorage<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
internal IEnumerable<Entity> Debug_GetEntities()
|
internal override object Debug_Get(int entityID)
|
||||||
{
|
{
|
||||||
return EntityIDToStorageIndex.Keys;
|
return components[entityIDToStorageIndex[entityID]];
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override IEnumerable<int> Debug_GetEntityIDs()
|
||||||
|
{
|
||||||
|
return entityIDToStorageIndex.Keys;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
protected virtual void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (!IsDisposed)
|
if (!disposed)
|
||||||
{
|
{
|
||||||
Components.Dispose();
|
NativeMemory.Free(components);
|
||||||
EntityIDs.Dispose();
|
NativeMemory.Free(entityIDs);
|
||||||
|
components = null;
|
||||||
|
entityIDs = null;
|
||||||
|
|
||||||
IsDisposed = true;
|
disposed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~ComponentStorage()
|
~ComponentStorage()
|
||||||
// {
|
{
|
||||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||||
// Dispose(disposing: false);
|
Dispose(disposing: false);
|
||||||
// }
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
@ -139,4 +177,6 @@ internal class ComponentStorage : IDisposable
|
||||||
Dispose(disposing: true);
|
Dispose(disposing: true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,63 @@
|
||||||
// NOTE: these methods are very inefficient
|
// NOTE: these methods are very inefficient
|
||||||
// this class should only be used in debugging contexts!!
|
// this class should only be used in debugging contexts!!
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public abstract class DebugSystem : System
|
|
||||||
{
|
{
|
||||||
protected DebugSystem(World world) : base(world) { }
|
public abstract class DebugSystem : System
|
||||||
|
{
|
||||||
|
protected DebugSystem(World world) : base(world)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
protected World.ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity) => World.Debug_GetAllComponentTypes(entity);
|
protected ComponentEnumerator Debug_GetAllComponents(Entity entity)
|
||||||
protected IEnumerable<Entity> Debug_GetEntities(Type componentType) => World.Debug_GetEntities(componentType);
|
{
|
||||||
protected IEnumerable<Type> Debug_SearchComponentType(string typeString) => World.Debug_SearchComponentType(typeString);
|
return new ComponentEnumerator(ComponentDepot, entity, EntityStorage.ComponentTypeIndices(entity.ID));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IEnumerable<Entity> Debug_GetEntities(Type componentType)
|
||||||
|
{
|
||||||
|
foreach (var entityID in ComponentDepot.Debug_GetEntityIDs(ComponentTypeIndices.GetIndex(componentType)))
|
||||||
|
{
|
||||||
|
yield return new Entity(entityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IEnumerable<Type> Debug_SearchComponentType(string typeString)
|
||||||
|
{
|
||||||
|
foreach (var type in ComponentTypeIndices.Types)
|
||||||
|
{
|
||||||
|
if (type.ToString().ToLower().Contains(typeString.ToLower()))
|
||||||
|
{
|
||||||
|
yield return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref struct ComponentEnumerator
|
||||||
|
{
|
||||||
|
private ComponentDepot ComponentDepot;
|
||||||
|
private Entity Entity;
|
||||||
|
private ReverseSpanEnumerator<int> ComponentTypeIndices;
|
||||||
|
|
||||||
|
public ComponentEnumerator GetEnumerator() => this;
|
||||||
|
|
||||||
|
internal ComponentEnumerator(
|
||||||
|
ComponentDepot componentDepot,
|
||||||
|
Entity entity,
|
||||||
|
Collections.IndexableSet<int> componentTypeIndices
|
||||||
|
) {
|
||||||
|
ComponentDepot = componentDepot;
|
||||||
|
Entity = entity;
|
||||||
|
ComponentTypeIndices = componentTypeIndices.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext() => ComponentTypeIndices.MoveNext();
|
||||||
|
public object Current => ComponentDepot.Debug_Get(Entity.ID, ComponentTypeIndices.Current);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS.Collections
|
||||||
|
{
|
||||||
|
public unsafe class NativeArray<T> : IDisposable where T : unmanaged
|
||||||
|
{
|
||||||
|
private T* Array;
|
||||||
|
private int count;
|
||||||
|
private int capacity;
|
||||||
|
|
||||||
|
public int Count => count;
|
||||||
|
|
||||||
|
public ReverseSpanEnumerator<T> GetEnumerator() => new ReverseSpanEnumerator<T>(new Span<T>(Array, Count));
|
||||||
|
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
public NativeArray(int capacity = 16)
|
||||||
|
{
|
||||||
|
this.capacity = capacity;
|
||||||
|
Array = (T*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<T>()));
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref T this[int i] => ref Array[i];
|
||||||
|
|
||||||
|
public void Add(T item)
|
||||||
|
{
|
||||||
|
if (count >= capacity)
|
||||||
|
{
|
||||||
|
capacity *= 2;
|
||||||
|
Array = (T*) NativeMemory.Realloc(Array, (nuint) (capacity * Unsafe.SizeOf<T>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Array[count] = item;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
NativeMemory.Free(Array);
|
||||||
|
Array = null;
|
||||||
|
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~NativeArray()
|
||||||
|
{
|
||||||
|
// 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
|
||||||
|
Dispose(disposing: true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,49 @@
|
||||||
namespace MoonTools.ECS;
|
using System;
|
||||||
|
|
||||||
public readonly record struct Entity(uint ID);
|
namespace MoonTools.ECS
|
||||||
|
{
|
||||||
|
public struct Entity : IEquatable<Entity>
|
||||||
|
{
|
||||||
|
public int ID { get; }
|
||||||
|
|
||||||
|
internal Entity(int id)
|
||||||
|
{
|
||||||
|
ID = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object? obj)
|
||||||
|
{
|
||||||
|
return obj is Entity entity && Equals(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return HashCode.Combine(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(Entity other)
|
||||||
|
{
|
||||||
|
return ID == other.ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(Entity a, Entity b)
|
||||||
|
{
|
||||||
|
return a.Equals(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(Entity a, Entity b)
|
||||||
|
{
|
||||||
|
return !a.Equals(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator int(Entity e)
|
||||||
|
{
|
||||||
|
return e.ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator Entity(int i)
|
||||||
|
{
|
||||||
|
return new Entity(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,36 +1,127 @@
|
||||||
namespace MoonTools.ECS;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
public abstract class EntityComponentReader
|
namespace MoonTools.ECS
|
||||||
{
|
{
|
||||||
protected readonly World World;
|
public abstract class EntityComponentReader
|
||||||
public FilterBuilder FilterBuilder => World.FilterBuilder;
|
{
|
||||||
|
internal readonly World World;
|
||||||
|
internal EntityStorage EntityStorage => World.EntityStorage;
|
||||||
|
internal ComponentDepot ComponentDepot => World.ComponentDepot;
|
||||||
|
internal RelationDepot RelationDepot => World.RelationDepot;
|
||||||
|
protected FilterBuilder FilterBuilder => new FilterBuilder(FilterStorage, ComponentTypeIndices);
|
||||||
|
internal FilterStorage FilterStorage => World.FilterStorage;
|
||||||
|
internal TypeIndices ComponentTypeIndices => World.ComponentTypeIndices;
|
||||||
|
internal TypeIndices RelationTypeIndices => World.RelationTypeIndices;
|
||||||
|
|
||||||
protected EntityComponentReader(World world)
|
public EntityComponentReader(World world)
|
||||||
{
|
{
|
||||||
World = world;
|
World = world;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected string GetTag(in Entity entity) => World.GetTag(entity);
|
protected string GetTag(in Entity entity)
|
||||||
|
{
|
||||||
|
return World.GetTag(entity);
|
||||||
|
}
|
||||||
|
|
||||||
protected bool Has<T>(in Entity Entity) where T : unmanaged => World.Has<T>(Entity);
|
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : unmanaged
|
||||||
protected bool Some<T>() where T : unmanaged => World.Some<T>();
|
{
|
||||||
protected ref T Get<T>(in Entity Entity) where T : unmanaged => ref World.Get<T>(Entity);
|
return ComponentDepot.ReadComponents<TComponent>();
|
||||||
protected ref T GetSingleton<T>() where T : unmanaged => ref World.GetSingleton<T>();
|
}
|
||||||
protected Entity GetSingletonEntity<T>() where T : unmanaged => World.GetSingletonEntity<T>();
|
|
||||||
|
|
||||||
protected ReverseSpanEnumerator<(Entity, Entity)> Relations<T>() where T : unmanaged => World.Relations<T>();
|
protected bool Has<TComponent>(in Entity entity) where TComponent : unmanaged
|
||||||
protected bool Related<T>(in Entity entityA, in Entity entityB) where T : unmanaged => World.Related<T>(entityA, entityB);
|
{
|
||||||
protected T GetRelationData<T>(in Entity entityA, in Entity entityB) where T : unmanaged => World.GetRelationData<T>(entityA, entityB);
|
var storageIndex = ComponentTypeIndices.GetIndex<TComponent>();
|
||||||
|
return EntityStorage.HasComponent(entity.ID, storageIndex);
|
||||||
|
}
|
||||||
|
|
||||||
protected ReverseSpanEnumerator<Entity> OutRelations<T>(in Entity entity) where T : unmanaged => World.OutRelations<T>(entity);
|
protected bool Some<TComponent>() where TComponent : unmanaged
|
||||||
protected Entity OutRelationSingleton<T>(in Entity entity) where T : unmanaged => World.OutRelationSingleton<T>(entity);
|
{
|
||||||
protected bool HasOutRelation<T>(in Entity entity) where T : unmanaged => World.HasOutRelation<T>(entity);
|
return ComponentDepot.Some<TComponent>();
|
||||||
protected int OutRelationCount<T>(in Entity entity) where T : unmanaged => World.OutRelationCount<T>(entity);
|
}
|
||||||
protected Entity NthOutRelation<T>(in Entity entity, int n) where T : unmanaged => World.NthOutRelation<T>(entity, n);
|
|
||||||
|
|
||||||
protected ReverseSpanEnumerator<Entity> InRelations<T>(in Entity entity) where T : unmanaged => World.InRelations<T>(entity);
|
protected ref readonly TComponent Get<TComponent>(in Entity entity) where TComponent : unmanaged
|
||||||
protected Entity InRelationSingleton<T>(in Entity entity) where T : unmanaged => World.InRelationSingleton<T>(entity);
|
{
|
||||||
protected bool HasInRelation<T>(in Entity entity) where T : unmanaged => World.HasInRelation<T>(entity);
|
return ref ComponentDepot.Get<TComponent>(entity.ID);
|
||||||
protected int InRelationCount<T>(in Entity entity) where T : unmanaged => World.InRelationCount<T>(entity);
|
}
|
||||||
protected Entity NthInRelation<T>(in Entity entity, int n) where T : unmanaged => World.NthInRelation<T>(entity, n);
|
|
||||||
|
protected ref readonly TComponent GetSingleton<TComponent>() where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
return ref ComponentDepot.GetFirst<TComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Entity GetSingletonEntity<TComponent>() where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
return ComponentDepot.GetSingletonEntity<TComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ReverseSpanEnumerator<(Entity, Entity)> Relations<TRelationKind>() where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.Relations<TRelationKind>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool Related<TRelationKind>(in Entity a, in Entity b) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.Related<TRelationKind>(a.ID, b.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected TRelationKind GetRelationData<TRelationKind>(in Entity a, in Entity b) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.Get<TRelationKind>(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// relations go A->B, so given A, will give all entities in outgoing relations of this kind.
|
||||||
|
protected ReverseSpanEnumerator<Entity> OutRelations<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.OutRelations<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Entity OutRelationSingleton<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.OutRelationSingleton<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: this WILL crash if at least n + 1 relations do not exist!
|
||||||
|
protected Entity NthOutRelation<TRelationKind>(in Entity entity, int n) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.NthOutRelation<TRelationKind>(entity.ID, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool HasOutRelation<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.HasOutRelation<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int OutRelationCount<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.OutRelationCount<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Relations go A->B, so given B, will give all entities in incoming A relations of this kind.
|
||||||
|
protected ReverseSpanEnumerator<Entity> InRelations<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.InRelations<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Entity InRelationSingleton<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.InRelationSingleton<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: this WILL crash if at least n + 1 relations do not exist!
|
||||||
|
protected Entity NthInRelation<TRelationKind>(in Entity entity, int n) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.NthInRelation<TRelationKind>(entity.ID, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool HasInRelation<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.HasInRelation<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int InRelationCount<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return RelationDepot.InRelationCount<TRelationKind>(entity.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,142 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using MoonTools.ECS.Collections;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS
|
||||||
|
{
|
||||||
|
internal class EntityStorage
|
||||||
|
{
|
||||||
|
private int nextID = 0;
|
||||||
|
// FIXME: why is this duplicated?
|
||||||
|
private readonly Stack<int> availableIDs = new Stack<int>();
|
||||||
|
// FIXME: this is only needed in debug mode
|
||||||
|
private readonly HashSet<int> availableIDHash = new HashSet<int>();
|
||||||
|
|
||||||
|
private Dictionary<int, IndexableSet<int>> EntityToComponentTypeIndices = new Dictionary<int, IndexableSet<int>>();
|
||||||
|
private Dictionary<int, IndexableSet<int>> EntityToRelationTypeIndices = new Dictionary<int, IndexableSet<int>>();
|
||||||
|
|
||||||
|
public int Count => nextID - availableIDs.Count;
|
||||||
|
|
||||||
|
public Dictionary<int, string> Tags = new Dictionary<int, string>();
|
||||||
|
|
||||||
|
public Entity Create(string tag)
|
||||||
|
{
|
||||||
|
var entity = new Entity(NextID());
|
||||||
|
|
||||||
|
if (!EntityToComponentTypeIndices.ContainsKey(entity.ID))
|
||||||
|
{
|
||||||
|
EntityToComponentTypeIndices.Add(entity.ID, new IndexableSet<int>());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!EntityToRelationTypeIndices.ContainsKey(entity.ID))
|
||||||
|
{
|
||||||
|
EntityToRelationTypeIndices.Add(entity.ID, new IndexableSet<int>());
|
||||||
|
}
|
||||||
|
|
||||||
|
Tags[entity.ID] = tag;
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Exists(in Entity entity)
|
||||||
|
{
|
||||||
|
return Taken(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Tag(in Entity entity, string tag)
|
||||||
|
{
|
||||||
|
Tags[entity.ID] = tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Destroy(in Entity entity)
|
||||||
|
{
|
||||||
|
EntityToComponentTypeIndices[entity.ID].Clear();
|
||||||
|
EntityToRelationTypeIndices[entity.ID].Clear();
|
||||||
|
Tags.Remove(entity.ID);
|
||||||
|
Release(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns true if the component is new.
|
||||||
|
public bool SetComponent(int entityID, int componentTypeIndex)
|
||||||
|
{
|
||||||
|
return EntityToComponentTypeIndices[entityID].Add(componentTypeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasComponent(int entityID, int componentTypeIndex)
|
||||||
|
{
|
||||||
|
return EntityToComponentTypeIndices[entityID].Contains(componentTypeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns true if the component existed.
|
||||||
|
public bool RemoveComponent(int entityID, int componentTypeIndex)
|
||||||
|
{
|
||||||
|
return EntityToComponentTypeIndices[entityID].Remove(componentTypeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddRelationKind(int entityID, int relationIndex)
|
||||||
|
{
|
||||||
|
EntityToRelationTypeIndices[entityID].Add(relationIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveRelation(int entityId, int relationIndex)
|
||||||
|
{
|
||||||
|
EntityToRelationTypeIndices[entityId].Remove(relationIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Tag(int entityID)
|
||||||
|
{
|
||||||
|
return Tags[entityID];
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexableSet<int> ComponentTypeIndices(int entityID)
|
||||||
|
{
|
||||||
|
return EntityToComponentTypeIndices[entityID];
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexableSet<int> RelationTypeIndices(int entityID)
|
||||||
|
{
|
||||||
|
return EntityToRelationTypeIndices[entityID];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
nextID = 0;
|
||||||
|
foreach (var componentSet in EntityToComponentTypeIndices.Values)
|
||||||
|
{
|
||||||
|
componentSet.Clear();
|
||||||
|
}
|
||||||
|
foreach (var relationSet in EntityToRelationTypeIndices.Values)
|
||||||
|
{
|
||||||
|
relationSet.Clear();
|
||||||
|
}
|
||||||
|
availableIDs.Clear();
|
||||||
|
availableIDHash.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int NextID()
|
||||||
|
{
|
||||||
|
if (availableIDs.Count > 0)
|
||||||
|
{
|
||||||
|
var id = availableIDs.Pop();
|
||||||
|
availableIDHash.Remove(id);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var id = nextID;
|
||||||
|
nextID += 1;
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool Taken(int id)
|
||||||
|
{
|
||||||
|
return !availableIDHash.Contains(id) && id < nextID;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Release(int id)
|
||||||
|
{
|
||||||
|
availableIDs.Push(id);
|
||||||
|
availableIDHash.Add(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,23 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public ref struct ReverseSpanEnumerator<T>
|
|
||||||
{
|
{
|
||||||
|
public ref struct ReverseSpanEnumerator<T>
|
||||||
|
{
|
||||||
private ReadOnlySpan<T> Span;
|
private ReadOnlySpan<T> Span;
|
||||||
private int Index;
|
private int index;
|
||||||
|
|
||||||
public ReverseSpanEnumerator<T> GetEnumerator() => this;
|
public ReverseSpanEnumerator<T> GetEnumerator() => this;
|
||||||
|
|
||||||
public T Current => Span[Index];
|
public T Current => Span[index];
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
if (Index > 0)
|
if (index > 0)
|
||||||
{
|
{
|
||||||
Index -= 1;
|
index -= 1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,8 +28,9 @@ public ref struct ReverseSpanEnumerator<T>
|
||||||
public ReverseSpanEnumerator(Span<T> span)
|
public ReverseSpanEnumerator(Span<T> span)
|
||||||
{
|
{
|
||||||
Span = span;
|
Span = span;
|
||||||
Index = span.Length;
|
index = span.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReverseSpanEnumerator<T> Empty => new ReverseSpanEnumerator<T>();
|
public static ReverseSpanEnumerator<T> Empty => new ReverseSpanEnumerator<T>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
125
src/Filter.cs
125
src/Filter.cs
|
@ -1,123 +1,38 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using MoonTools.ECS.Collections;
|
using MoonTools.ECS.Collections;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public class Filter
|
|
||||||
{
|
{
|
||||||
private World World;
|
public class Filter
|
||||||
|
{
|
||||||
internal FilterSignature Signature;
|
internal FilterSignature Signature;
|
||||||
|
private FilterStorage FilterStorage;
|
||||||
|
|
||||||
internal IndexableSet<Entity> EntitySet = new IndexableSet<Entity>();
|
internal Filter(FilterStorage filterStorage, IndexableSet<int> included, IndexableSet<int> excluded)
|
||||||
|
{
|
||||||
|
FilterStorage = filterStorage;
|
||||||
|
Signature = new FilterSignature(included, excluded);
|
||||||
|
}
|
||||||
|
|
||||||
private bool IsDisposed;
|
public ReverseSpanEnumerator<Entity> Entities => FilterStorage.FilterEntities(Signature);
|
||||||
|
public RandomEntityEnumerator EntitiesInRandomOrder => FilterStorage.FilterEntitiesRandom(Signature);
|
||||||
|
public Entity RandomEntity => FilterStorage.FilterRandomEntity(Signature);
|
||||||
|
|
||||||
public ReverseSpanEnumerator<Entity> Entities => EntitySet.GetEnumerator();
|
public int Count => FilterStorage.FilterCount(Signature);
|
||||||
|
public bool Empty => Count == 0;
|
||||||
public bool Empty => EntitySet.Count == 0;
|
|
||||||
public int Count => EntitySet.Count;
|
|
||||||
|
|
||||||
// WARNING: this WILL crash if the index is out of range!
|
// WARNING: this WILL crash if the index is out of range!
|
||||||
public Entity NthEntity(int index) => EntitySet[index];
|
public Entity NthEntity(int index) => FilterStorage.FilterNthEntity(Signature, index);
|
||||||
|
|
||||||
// WARNING: this WILL crash if the filter is empty!
|
public void RegisterAddCallback(Action<Entity> callback)
|
||||||
public Entity RandomEntity => EntitySet[RandomManager.Next(EntitySet.Count)];
|
|
||||||
public RandomEntityEnumerator EntitiesInRandomOrder => new RandomEntityEnumerator(this);
|
|
||||||
|
|
||||||
internal Filter(World world, FilterSignature signature)
|
|
||||||
{
|
{
|
||||||
World = world;
|
FilterStorage.RegisterAddCallback(Signature, callback);
|
||||||
Signature = signature;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DestroyAllEntities()
|
public void RegisterRemoveCallback(Action<Entity> callback)
|
||||||
{
|
{
|
||||||
foreach (var entity in EntitySet)
|
FilterStorage.RegisterRemoveCallback(Signature, callback);
|
||||||
{
|
|
||||||
World.Destroy(entity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Check(Entity entity)
|
|
||||||
{
|
|
||||||
foreach (var type in Signature.Included)
|
|
||||||
{
|
|
||||||
if (!World.Has(entity, type))
|
|
||||||
{
|
|
||||||
EntitySet.Remove(entity);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var type in Signature.Excluded)
|
|
||||||
{
|
|
||||||
if (World.Has(entity, type))
|
|
||||||
{
|
|
||||||
EntitySet.Remove(entity);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
EntitySet.Add(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void AddEntity(in Entity entity)
|
|
||||||
{
|
|
||||||
EntitySet.Add(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void RemoveEntity(in Entity entity)
|
|
||||||
{
|
|
||||||
EntitySet.Remove(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void Clear()
|
|
||||||
{
|
|
||||||
EntitySet.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref struct RandomEntityEnumerator
|
|
||||||
{
|
|
||||||
private Filter Filter;
|
|
||||||
private LinearCongruentialEnumerator LinearCongruentialEnumerator;
|
|
||||||
|
|
||||||
public RandomEntityEnumerator GetEnumerator() => this;
|
|
||||||
|
|
||||||
internal RandomEntityEnumerator(Filter filter)
|
|
||||||
{
|
|
||||||
Filter = filter;
|
|
||||||
LinearCongruentialEnumerator =
|
|
||||||
RandomManager.LinearCongruentialSequence(filter.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool MoveNext() => LinearCongruentialEnumerator.MoveNext();
|
|
||||||
public Entity Current => Filter.NthEntity(LinearCongruentialEnumerator.Current);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
EntitySet.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
||||||
// ~Filter()
|
|
||||||
// {
|
|
||||||
// // 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
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,40 +4,42 @@ namespace MoonTools.ECS
|
||||||
{
|
{
|
||||||
public struct FilterBuilder
|
public struct FilterBuilder
|
||||||
{
|
{
|
||||||
World World;
|
private TypeIndices ComponentTypeIndices;
|
||||||
IndexableSet<TypeId> Included;
|
private FilterStorage FilterStorage;
|
||||||
IndexableSet<TypeId> Excluded;
|
private IndexableSet<int> Included;
|
||||||
|
private IndexableSet<int> Excluded;
|
||||||
|
|
||||||
internal FilterBuilder(World world)
|
internal FilterBuilder(FilterStorage filterStorage, TypeIndices componentTypeIndices)
|
||||||
{
|
{
|
||||||
World = world;
|
FilterStorage = filterStorage;
|
||||||
Included = new IndexableSet<TypeId>();
|
ComponentTypeIndices = componentTypeIndices;
|
||||||
Excluded = new IndexableSet<TypeId>();
|
Included = new IndexableSet<int>();
|
||||||
|
Excluded = new IndexableSet<int>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private FilterBuilder(World world, IndexableSet<TypeId> included, IndexableSet<TypeId> excluded)
|
private FilterBuilder(FilterStorage filterStorage, TypeIndices componentTypeIndices, IndexableSet<int> included, IndexableSet<int> excluded)
|
||||||
{
|
{
|
||||||
World = world;
|
FilterStorage = filterStorage;
|
||||||
|
ComponentTypeIndices = componentTypeIndices;
|
||||||
Included = included;
|
Included = included;
|
||||||
Excluded = excluded;
|
Excluded = excluded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FilterBuilder Include<T>() where T : unmanaged
|
public FilterBuilder Include<TComponent>() where TComponent : unmanaged
|
||||||
{
|
{
|
||||||
Included.Add(World.GetComponentTypeId<T>());
|
Included.Add(ComponentTypeIndices.GetIndex<TComponent>());
|
||||||
return new FilterBuilder(World, Included, Excluded);
|
return new FilterBuilder(FilterStorage, ComponentTypeIndices, Included, Excluded);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FilterBuilder Exclude<T>() where T : unmanaged
|
public FilterBuilder Exclude<TComponent>() where TComponent : unmanaged
|
||||||
{
|
{
|
||||||
Excluded.Add(World.GetComponentTypeId<T>());
|
Excluded.Add(ComponentTypeIndices.GetIndex<TComponent>());
|
||||||
return new FilterBuilder(World, Included, Excluded);
|
return new FilterBuilder(FilterStorage, ComponentTypeIndices, Included, Excluded);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Filter Build()
|
public Filter Build()
|
||||||
{
|
{
|
||||||
var signature = new FilterSignature(Included, Excluded);
|
return FilterStorage.CreateFilter(Included, Excluded);
|
||||||
return World.GetFilter(signature);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using MoonTools.ECS.Collections;
|
using MoonTools.ECS.Collections;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public struct FilterSignature : IEquatable<FilterSignature>
|
|
||||||
{
|
{
|
||||||
public readonly IndexableSet<TypeId> Included;
|
public struct FilterSignature : IEquatable<FilterSignature>
|
||||||
public readonly IndexableSet<TypeId> Excluded;
|
{
|
||||||
|
public readonly IndexableSet<int> Included;
|
||||||
|
public readonly IndexableSet<int> Excluded;
|
||||||
|
|
||||||
public FilterSignature(IndexableSet<TypeId> included, IndexableSet<TypeId> excluded)
|
public FilterSignature(IndexableSet<int> included, IndexableSet<int> excluded)
|
||||||
{
|
{
|
||||||
Included = included;
|
Included = included;
|
||||||
Excluded = excluded;
|
Excluded = excluded;
|
||||||
|
@ -66,4 +67,5 @@ public struct FilterSignature : IEquatable<FilterSignature>
|
||||||
{
|
{
|
||||||
return !(left == right);
|
return !(left == right);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,273 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using MoonTools.ECS.Collections;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS
|
||||||
|
{
|
||||||
|
internal class FilterStorage
|
||||||
|
{
|
||||||
|
private EntityStorage EntityStorage;
|
||||||
|
private TypeIndices ComponentTypeIndices;
|
||||||
|
private Dictionary<FilterSignature, IndexableSet<Entity>> filterSignatureToEntityIDs = new Dictionary<FilterSignature, IndexableSet<Entity>>();
|
||||||
|
private Dictionary<int, List<FilterSignature>> typeToFilterSignatures = new Dictionary<int, List<FilterSignature>>();
|
||||||
|
|
||||||
|
private Dictionary<FilterSignature, Action<Entity>> addCallbacks = new Dictionary<FilterSignature, Action<Entity>>();
|
||||||
|
private Dictionary<FilterSignature, Action<Entity>> removeCallbacks = new Dictionary<FilterSignature, Action<Entity>>();
|
||||||
|
|
||||||
|
public FilterStorage(EntityStorage entityStorage, TypeIndices componentTypeIndices)
|
||||||
|
{
|
||||||
|
EntityStorage = entityStorage;
|
||||||
|
ComponentTypeIndices = componentTypeIndices;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CopyTypeCache(Dictionary<int, List<FilterSignature>> typeCache)
|
||||||
|
{
|
||||||
|
foreach (var type in typeCache.Keys)
|
||||||
|
{
|
||||||
|
if (!typeToFilterSignatures.ContainsKey(type))
|
||||||
|
{
|
||||||
|
typeToFilterSignatures.Add(type, new List<FilterSignature>());
|
||||||
|
|
||||||
|
foreach (var signature in typeCache[type])
|
||||||
|
{
|
||||||
|
typeToFilterSignatures[type].Add(signature);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateMissingStorages(FilterStorage other)
|
||||||
|
{
|
||||||
|
foreach (var filterSignature in other.filterSignatureToEntityIDs.Keys)
|
||||||
|
{
|
||||||
|
if (!filterSignatureToEntityIDs.ContainsKey(filterSignature))
|
||||||
|
{
|
||||||
|
filterSignatureToEntityIDs.Add(filterSignature, new IndexableSet<Entity>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CopyTypeCache(other.typeToFilterSignatures);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Filter CreateFilter(IndexableSet<int> included, IndexableSet<int> excluded)
|
||||||
|
{
|
||||||
|
var filterSignature = new FilterSignature(included, excluded);
|
||||||
|
if (!filterSignatureToEntityIDs.ContainsKey(filterSignature))
|
||||||
|
{
|
||||||
|
filterSignatureToEntityIDs.Add(filterSignature, new IndexableSet<Entity>());
|
||||||
|
|
||||||
|
foreach (var type in included)
|
||||||
|
{
|
||||||
|
if (!typeToFilterSignatures.ContainsKey(type))
|
||||||
|
{
|
||||||
|
typeToFilterSignatures.Add(type, new List<FilterSignature>());
|
||||||
|
}
|
||||||
|
|
||||||
|
typeToFilterSignatures[type].Add(filterSignature);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var type in excluded)
|
||||||
|
{
|
||||||
|
if (!typeToFilterSignatures.ContainsKey(type))
|
||||||
|
{
|
||||||
|
typeToFilterSignatures.Add(type, new List<FilterSignature>());
|
||||||
|
}
|
||||||
|
|
||||||
|
typeToFilterSignatures[type].Add(filterSignature);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Filter(this, included, excluded);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReverseSpanEnumerator<Entity> FilterEntities(FilterSignature filterSignature)
|
||||||
|
{
|
||||||
|
return filterSignatureToEntityIDs[filterSignature].GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RandomEntityEnumerator FilterEntitiesRandom(FilterSignature filterSignature)
|
||||||
|
{
|
||||||
|
return new RandomEntityEnumerator(
|
||||||
|
this,
|
||||||
|
filterSignature,
|
||||||
|
RandomManager.LinearCongruentialSequence(FilterCount(filterSignature)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity FilterNthEntity(FilterSignature filterSignature, int index)
|
||||||
|
{
|
||||||
|
return new Entity(filterSignatureToEntityIDs[filterSignature][index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity FilterRandomEntity(FilterSignature filterSignature)
|
||||||
|
{
|
||||||
|
var randomIndex = RandomManager.Next(FilterCount(filterSignature));
|
||||||
|
return new Entity(filterSignatureToEntityIDs[filterSignature][randomIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int FilterCount(FilterSignature filterSignature)
|
||||||
|
{
|
||||||
|
return filterSignatureToEntityIDs[filterSignature].Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Check(int entityID, int componentTypeIndex)
|
||||||
|
{
|
||||||
|
if (typeToFilterSignatures.TryGetValue(componentTypeIndex, out var filterSignatures))
|
||||||
|
{
|
||||||
|
foreach (var filterSignature in filterSignatures)
|
||||||
|
{
|
||||||
|
CheckFilter(entityID, filterSignature);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Check<TComponent>(int entityID) where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
Check(entityID, ComponentTypeIndices.GetIndex<TComponent>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CheckSatisfied(int entityID, FilterSignature filterSignature)
|
||||||
|
{
|
||||||
|
foreach (var type in filterSignature.Included)
|
||||||
|
{
|
||||||
|
if (!EntityStorage.HasComponent(entityID, type))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var type in filterSignature.Excluded)
|
||||||
|
{
|
||||||
|
if (EntityStorage.HasComponent(entityID, type))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckFilter(int entityID, FilterSignature filterSignature)
|
||||||
|
{
|
||||||
|
foreach (var type in filterSignature.Included)
|
||||||
|
{
|
||||||
|
if (!EntityStorage.HasComponent(entityID, type))
|
||||||
|
{
|
||||||
|
if (filterSignatureToEntityIDs[filterSignature].Remove(entityID))
|
||||||
|
{
|
||||||
|
if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback))
|
||||||
|
{
|
||||||
|
removeCallback(entityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var type in filterSignature.Excluded)
|
||||||
|
{
|
||||||
|
if (EntityStorage.HasComponent(entityID, type))
|
||||||
|
{
|
||||||
|
if (filterSignatureToEntityIDs[filterSignature].Remove(entityID))
|
||||||
|
{
|
||||||
|
if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback))
|
||||||
|
{
|
||||||
|
removeCallback(entityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filterSignatureToEntityIDs[filterSignature].Add(entityID))
|
||||||
|
{
|
||||||
|
if (addCallbacks.TryGetValue(filterSignature, out var addCallback))
|
||||||
|
{
|
||||||
|
addCallback(entityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveEntity(int entityID, int componentTypeIndex)
|
||||||
|
{
|
||||||
|
if (typeToFilterSignatures.TryGetValue(componentTypeIndex, out var filterSignatures))
|
||||||
|
{
|
||||||
|
foreach (var filterSignature in filterSignatures)
|
||||||
|
{
|
||||||
|
if (filterSignatureToEntityIDs[filterSignature].Remove(entityID))
|
||||||
|
{
|
||||||
|
if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback))
|
||||||
|
{
|
||||||
|
removeCallback(entityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Used by TransferEntity
|
||||||
|
public void AddEntity(FilterSignature signature, int entityID)
|
||||||
|
{
|
||||||
|
filterSignatureToEntityIDs[signature].Add(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void TransferStorage(Dictionary<int, int> worldToTransferID, FilterStorage other)
|
||||||
|
{
|
||||||
|
foreach (var (filterSignature, entityIDs) in filterSignatureToEntityIDs)
|
||||||
|
{
|
||||||
|
foreach (var entity in entityIDs)
|
||||||
|
{
|
||||||
|
if (worldToTransferID.ContainsKey(entity))
|
||||||
|
{
|
||||||
|
var otherEntityID = worldToTransferID[entity];
|
||||||
|
other.AddEntity(filterSignature, otherEntityID);
|
||||||
|
|
||||||
|
if (other.addCallbacks.TryGetValue(filterSignature, out var addCallback))
|
||||||
|
{
|
||||||
|
addCallback(otherEntityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// used by World.Clear, ignores callbacks
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
foreach (var (filterSignature, entityIDs) in filterSignatureToEntityIDs)
|
||||||
|
{
|
||||||
|
entityIDs.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterAddCallback(FilterSignature filterSignature, Action<Entity> callback)
|
||||||
|
{
|
||||||
|
addCallbacks.Add(filterSignature, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterRemoveCallback(FilterSignature filterSignature, Action<Entity> callback)
|
||||||
|
{
|
||||||
|
removeCallbacks.Add(filterSignature, callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref struct RandomEntityEnumerator
|
||||||
|
{
|
||||||
|
public RandomEntityEnumerator GetEnumerator() => this;
|
||||||
|
|
||||||
|
private FilterStorage FilterStorage;
|
||||||
|
private FilterSignature FilterSignature;
|
||||||
|
private LinearCongruentialEnumerator LinearCongruentialEnumerator;
|
||||||
|
|
||||||
|
internal RandomEntityEnumerator(
|
||||||
|
FilterStorage filterStorage,
|
||||||
|
FilterSignature filterSignature,
|
||||||
|
LinearCongruentialEnumerator linearCongruentialEnumerator)
|
||||||
|
{
|
||||||
|
FilterStorage = filterStorage;
|
||||||
|
FilterSignature = filterSignature;
|
||||||
|
LinearCongruentialEnumerator = linearCongruentialEnumerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext() => LinearCongruentialEnumerator.MoveNext();
|
||||||
|
public Entity Current => FilterStorage.FilterNthEntity(FilterSignature, LinearCongruentialEnumerator.Current);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,62 +0,0 @@
|
||||||
using System;
|
|
||||||
using MoonTools.ECS.Collections;
|
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
|
||||||
|
|
||||||
internal class IdAssigner : IDisposable
|
|
||||||
{
|
|
||||||
uint Next;
|
|
||||||
NativeArray<uint> AvailableIds = new NativeArray<uint>();
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public uint Assign()
|
|
||||||
{
|
|
||||||
if (AvailableIds.TryPop(out var id))
|
|
||||||
{
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
id = Next;
|
|
||||||
Next += 1;
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Unassign(uint id)
|
|
||||||
{
|
|
||||||
AvailableIds.Append(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CopyTo(IdAssigner other)
|
|
||||||
{
|
|
||||||
AvailableIds.CopyTo(other.AvailableIds);
|
|
||||||
other.Next = Next;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
AvailableIds.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// // 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
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS.Collections
|
||||||
|
{
|
||||||
|
public unsafe class IndexableSet<T> : IDisposable where T : unmanaged
|
||||||
|
{
|
||||||
|
private Dictionary<T, int> indices;
|
||||||
|
private T* array;
|
||||||
|
private int count;
|
||||||
|
private int capacity;
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
public int Count => count;
|
||||||
|
public ReverseSpanEnumerator<T> GetEnumerator() => new ReverseSpanEnumerator<T>(new Span<T>(array, count));
|
||||||
|
|
||||||
|
public IndexableSet(int capacity = 32)
|
||||||
|
{
|
||||||
|
this.capacity = capacity;
|
||||||
|
count = 0;
|
||||||
|
|
||||||
|
indices = new Dictionary<T, int>(capacity);
|
||||||
|
array = (T*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<T>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public T this[int i] => array[i];
|
||||||
|
|
||||||
|
public bool Contains(T element)
|
||||||
|
{
|
||||||
|
return indices.ContainsKey(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Add(T element)
|
||||||
|
{
|
||||||
|
if (!Contains(element))
|
||||||
|
{
|
||||||
|
indices.Add(element, count);
|
||||||
|
|
||||||
|
if (count >= capacity)
|
||||||
|
{
|
||||||
|
capacity *= 2;
|
||||||
|
array = (T*) NativeMemory.Realloc(array, (nuint) (capacity * Unsafe.SizeOf<T>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
array[count] = element;
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(T element)
|
||||||
|
{
|
||||||
|
if (!Contains(element))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
var lastElement = array[Count - 1];
|
||||||
|
var index = indices[element];
|
||||||
|
array[index] = lastElement;
|
||||||
|
indices[lastElement] = index;
|
||||||
|
count -= 1;
|
||||||
|
indices.Remove(element);
|
||||||
|
*/
|
||||||
|
|
||||||
|
var index = indices[element];
|
||||||
|
|
||||||
|
for (var i = index; i < Count - 1; i += 1)
|
||||||
|
{
|
||||||
|
array[i] = array[i + 1];
|
||||||
|
indices[array[i]] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
indices.Remove(element);
|
||||||
|
count -= 1;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
indices.Clear();
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
NativeMemory.Free(array);
|
||||||
|
array = null;
|
||||||
|
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~IndexableSet()
|
||||||
|
{
|
||||||
|
// 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
|
||||||
|
Dispose(disposing: true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public abstract class Manipulator : EntityComponentReader
|
|
||||||
{
|
{
|
||||||
|
public abstract class Manipulator : EntityComponentReader
|
||||||
|
{
|
||||||
public Manipulator(World world) : base(world)
|
public Manipulator(World world) : base(world)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,9 @@ public abstract class Manipulator : EntityComponentReader
|
||||||
protected void Tag(Entity entity, string tag) => World.Tag(entity, tag);
|
protected void Tag(Entity entity, string tag) => World.Tag(entity, tag);
|
||||||
protected void Set<TComponent>(in Entity entity, in TComponent component) where TComponent : unmanaged => World.Set<TComponent>(entity, component);
|
protected void Set<TComponent>(in Entity entity, in TComponent component) where TComponent : unmanaged => World.Set<TComponent>(entity, component);
|
||||||
protected void Remove<TComponent>(in Entity entity) where TComponent : unmanaged => World.Remove<TComponent>(entity);
|
protected void Remove<TComponent>(in Entity entity) where TComponent : unmanaged => World.Remove<TComponent>(entity);
|
||||||
|
|
||||||
protected void Relate<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged => World.Relate(entityA, entityB, relationData);
|
protected void Relate<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged => World.Relate(entityA, entityB, relationData);
|
||||||
protected void Unrelate<TRelationKind>(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged => World.Unrelate<TRelationKind>(entityA, entityB);
|
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 UnrelateAll<TRelationKind>(in Entity entity) where TRelationKind : unmanaged => World.UnrelateAll<TRelationKind>(entity);
|
||||||
protected void Destroy(in Entity entity) => World.Destroy(entity);
|
protected void Destroy(in Entity entity) => World.Destroy(entity);
|
||||||
|
}
|
||||||
protected void Send<TMessage>(in TMessage message) where TMessage : unmanaged => World.Send(message);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS
|
||||||
|
{
|
||||||
|
internal class MessageDepot
|
||||||
|
{
|
||||||
|
private Dictionary<Type, MessageStorage> storages = new Dictionary<Type, MessageStorage>();
|
||||||
|
|
||||||
|
private MessageStorage<TMessage> Lookup<TMessage>() where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
if (!storages.ContainsKey(typeof(TMessage)))
|
||||||
|
{
|
||||||
|
storages.Add(typeof(TMessage), new MessageStorage<TMessage>());
|
||||||
|
}
|
||||||
|
|
||||||
|
return storages[typeof(TMessage)] as MessageStorage<TMessage>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add<TMessage>(in TMessage message) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
Lookup<TMessage>().Add(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add<TMessage>(int entityID, in TMessage message) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
Lookup<TMessage>().Add(entityID, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Some<TMessage>() where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TMessage>().Some();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TMessage>().All();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TMessage First<TMessage>() where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TMessage>().First();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReverseSpanEnumerator<TMessage> WithEntity<TMessage>(int entityID) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TMessage>().WithEntity(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return ref Lookup<TMessage>().FirstWithEntity(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TMessage>().SomeWithEntity(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
foreach (var storage in storages.Values)
|
||||||
|
{
|
||||||
|
storage.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,58 +1,125 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using MoonTools.ECS.Collections;
|
using MoonTools.ECS.Collections;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public class MessageStorage : IDisposable
|
|
||||||
{
|
{
|
||||||
private NativeArray Messages;
|
internal abstract class MessageStorage
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public MessageStorage(int elementSize)
|
|
||||||
{
|
{
|
||||||
Messages = new NativeArray(elementSize);
|
public abstract void Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add<T>(in T message) where T : unmanaged
|
internal unsafe class MessageStorage<TMessage> : MessageStorage, IDisposable where TMessage : unmanaged
|
||||||
{
|
{
|
||||||
Messages.Append(message);
|
private int count = 0;
|
||||||
|
private int capacity = 128;
|
||||||
|
private TMessage* messages;
|
||||||
|
// duplicating storage here for fast iteration
|
||||||
|
private Dictionary<int, NativeArray<TMessage>> entityToMessages = new Dictionary<int, NativeArray<TMessage>>();
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
public MessageStorage()
|
||||||
|
{
|
||||||
|
messages = (TMessage*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<TMessage>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(in TMessage message)
|
||||||
|
{
|
||||||
|
if (count == capacity)
|
||||||
|
{
|
||||||
|
capacity *= 2;
|
||||||
|
messages = (TMessage*) NativeMemory.Realloc(messages, (nuint) (capacity * Unsafe.SizeOf<TMessage>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
messages[count] = message;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(int entityID, in TMessage message)
|
||||||
|
{
|
||||||
|
if (!entityToMessages.ContainsKey(entityID))
|
||||||
|
{
|
||||||
|
entityToMessages.Add(entityID, new NativeArray<TMessage>());
|
||||||
|
}
|
||||||
|
entityToMessages[entityID].Add(message);
|
||||||
|
|
||||||
|
Add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Some()
|
public bool Some()
|
||||||
{
|
{
|
||||||
return Messages.Count > 0;
|
return count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<T> All<T>() where T : unmanaged
|
public ReadOnlySpan<TMessage> All()
|
||||||
{
|
{
|
||||||
return Messages.ToSpan<T>();
|
return new ReadOnlySpan<TMessage>(messages, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T First<T>() where T : unmanaged
|
public TMessage First()
|
||||||
{
|
{
|
||||||
return Messages.Get<T>(0);
|
return messages[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public ReverseSpanEnumerator<TMessage> WithEntity(int entityID)
|
||||||
{
|
{
|
||||||
Messages.Clear();
|
if (entityToMessages.TryGetValue(entityID, out var messages))
|
||||||
|
{
|
||||||
|
return messages.GetEnumerator();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
private void Dispose(bool disposing)
|
|
||||||
{
|
{
|
||||||
if (!IsDisposed)
|
return ReverseSpanEnumerator<TMessage>.Empty;
|
||||||
{
|
|
||||||
Messages.Dispose();
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~MessageStorage()
|
public ref readonly TMessage FirstWithEntity(int entityID)
|
||||||
// {
|
{
|
||||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
return ref entityToMessages[entityID][0];
|
||||||
// Dispose(disposing: false);
|
}
|
||||||
// }
|
|
||||||
|
public bool SomeWithEntity(int entityID)
|
||||||
|
{
|
||||||
|
return entityToMessages.ContainsKey(entityID) && entityToMessages[entityID].Count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Clear()
|
||||||
|
{
|
||||||
|
count = 0;
|
||||||
|
foreach (var set in entityToMessages.Values)
|
||||||
|
{
|
||||||
|
set.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
foreach (var array in entityToMessages.Values)
|
||||||
|
{
|
||||||
|
array.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
NativeMemory.Free(messages);
|
||||||
|
messages = null;
|
||||||
|
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~MessageStorage()
|
||||||
|
{
|
||||||
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||||
|
Dispose(disposing: false);
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
@ -60,4 +127,5 @@ public class MessageStorage : IDisposable
|
||||||
Dispose(disposing: true);
|
Dispose(disposing: true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// This class implements the well equidistributed long-period linear pseudorandom number generator.
|
|
||||||
/// Code taken from Chris Lomont: http://lomont.org/papers/2008/Lomont_PRNG_2008.pdf
|
|
||||||
/// </summary>
|
|
||||||
public class Random
|
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// This class implements the well equidistributed long-period linear pseudorandom number generator.
|
||||||
|
/// Code taken from Chris Lomont: http://lomont.org/papers/2008/Lomont_PRNG_2008.pdf
|
||||||
|
/// </summary>
|
||||||
|
public class Random
|
||||||
|
{
|
||||||
public const int STATE_BYTE_COUNT = 68; // 16 state ints + 1 index int
|
public const int STATE_BYTE_COUNT = 68; // 16 state ints + 1 index int
|
||||||
|
|
||||||
uint[] State = new uint[16];
|
uint[] State = new uint[16];
|
||||||
|
@ -67,12 +67,12 @@ public class Random
|
||||||
/// <exception cref="ArgumentException">Thrown if the byte span is too short.</exception>
|
/// <exception cref="ArgumentException">Thrown if the byte span is too short.</exception>
|
||||||
public unsafe void SaveState(Span<byte> bytes)
|
public unsafe void SaveState(Span<byte> bytes)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (bytes.Length < STATE_BYTE_COUNT)
|
if (bytes.Length < STATE_BYTE_COUNT)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Byte span too short!");
|
throw new ArgumentException("Byte span too short!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fixed (byte* ptr = bytes)
|
fixed (byte* ptr = bytes)
|
||||||
{
|
{
|
||||||
|
@ -94,12 +94,12 @@ public class Random
|
||||||
/// <exception cref="ArgumentException">Thrown if the byte span is too short.</exception>
|
/// <exception cref="ArgumentException">Thrown if the byte span is too short.</exception>
|
||||||
public unsafe void LoadState(Span<byte> bytes)
|
public unsafe void LoadState(Span<byte> bytes)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (bytes.Length < STATE_BYTE_COUNT)
|
if (bytes.Length < STATE_BYTE_COUNT)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Byte span too short!");
|
throw new ArgumentException("Byte span too short!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fixed (byte* ptr = bytes)
|
fixed (byte* ptr = bytes)
|
||||||
{
|
{
|
||||||
|
@ -205,4 +205,5 @@ public class Random
|
||||||
var n = NextInternal();
|
var n = NextInternal();
|
||||||
return ((double) n) / uint.MaxValue;
|
return ((double) n) / uint.MaxValue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public static class RandomManager
|
|
||||||
{
|
{
|
||||||
|
public static class RandomManager
|
||||||
|
{
|
||||||
private static Random Random = new Random();
|
private static Random Random = new Random();
|
||||||
|
|
||||||
private static int[] Primes =
|
private static int[] Primes =
|
||||||
|
@ -39,12 +39,12 @@ public static class RandomManager
|
||||||
x = Primes[Random.Next(Primes.Length - 1)];
|
x = Primes[Random.Next(Primes.Length - 1)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return new LinearCongruentialEnumerator(Random.Next(n + 1), x, n);
|
return new LinearCongruentialEnumerator(Random.Next(n), x, n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public struct LinearCongruentialEnumerator
|
public struct LinearCongruentialEnumerator
|
||||||
{
|
{
|
||||||
private readonly int start;
|
private readonly int start;
|
||||||
private readonly int count;
|
private readonly int count;
|
||||||
private readonly int prime;
|
private readonly int prime;
|
||||||
|
@ -65,7 +65,7 @@ public struct LinearCongruentialEnumerator
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
current += 1;
|
current += 1;
|
||||||
if (current <= start + count)
|
if (current < start + count)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -78,4 +78,5 @@ public struct LinearCongruentialEnumerator
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => (current * prime) % count;
|
get => (current * prime) % count;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,193 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS
|
||||||
|
{
|
||||||
|
internal class RelationDepot
|
||||||
|
{
|
||||||
|
private EntityStorage EntityStorage;
|
||||||
|
private TypeIndices RelationTypeIndices;
|
||||||
|
private RelationStorage[] storages = new RelationStorage[256];
|
||||||
|
|
||||||
|
public RelationDepot(EntityStorage entityStorage, TypeIndices relationTypeIndices)
|
||||||
|
{
|
||||||
|
EntityStorage = entityStorage;
|
||||||
|
RelationTypeIndices = relationTypeIndices;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Register<TRelationKind>(int index) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
if (index >= storages.Length)
|
||||||
|
{
|
||||||
|
Array.Resize(ref storages, storages.Length * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
storages[index] = new RelationStorage<TRelationKind>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private RelationStorage<TRelationKind> Lookup<TRelationKind>() where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
var storageIndex = RelationTypeIndices.GetIndex<TRelationKind>();
|
||||||
|
// TODO: is there some way to avoid this null check?
|
||||||
|
if (storages[storageIndex] == null)
|
||||||
|
{
|
||||||
|
Register<TRelationKind>(storageIndex);
|
||||||
|
}
|
||||||
|
return (RelationStorage<TRelationKind>) storages[storageIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
Lookup<TRelationKind>().Set(entityA, entityB, relationData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TRelationKind Get<TRelationKind>(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().Get(entityA, entityB);
|
||||||
|
}
|
||||||
|
|
||||||
|
public (bool, bool) Remove<TRelationKind>(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().Remove(entityA, entityB);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnrelateAll<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
Lookup<TRelationKind>().UnrelateAll(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReverseSpanEnumerator<(Entity, Entity)> Relations<TRelationKind>() where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().All();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Related<TRelationKind>(int idA, int idB) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().Has((idA, idB));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReverseSpanEnumerator<Entity> OutRelations<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().OutRelations(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity OutRelationSingleton<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().OutFirst(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity NthOutRelation<TRelationKind>(int entityID, int n) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().OutNth(entityID, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int OutRelationCount<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().OutRelationCount(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasOutRelation<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().HasOutRelation(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReverseSpanEnumerator<Entity> InRelations<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().InRelations(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity NthInRelation<TRelationKind>(int entityID, int n) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().InNth(entityID, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity InRelationSingleton<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().InFirst(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasInRelation<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().HasInRelation(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int InRelationCount<TRelationKind>(int entityID) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
return Lookup<TRelationKind>().InRelationCount(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// untyped methods used for destroying and snapshots
|
||||||
|
|
||||||
|
public unsafe void Set(int entityA, int entityB, int relationTypeIndex, void* relationData)
|
||||||
|
{
|
||||||
|
storages[relationTypeIndex].Set(entityA, entityB, relationData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnrelateAll(int entityID, int relationTypeIndex)
|
||||||
|
{
|
||||||
|
storages[relationTypeIndex].UnrelateAll(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
for (var i = 0; i < storages.Length; i += 1)
|
||||||
|
{
|
||||||
|
if (storages[i] != null)
|
||||||
|
{
|
||||||
|
storages[i].Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateMissingStorages(RelationDepot other)
|
||||||
|
{
|
||||||
|
while (other.RelationTypeIndices.Count >= storages.Length)
|
||||||
|
{
|
||||||
|
Array.Resize(ref storages, storages.Length * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (other.RelationTypeIndices.Count >= other.storages.Length)
|
||||||
|
{
|
||||||
|
Array.Resize(ref other.storages, other.storages.Length * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < other.RelationTypeIndices.Count; i += 1)
|
||||||
|
{
|
||||||
|
if (storages[i] == null && other.storages[i] != null)
|
||||||
|
{
|
||||||
|
storages[i] = other.storages[i].CreateStorage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public unsafe void TransferStorage(Dictionary<int, int> worldToTransferID, RelationDepot other)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < storages.Length; i += 1)
|
||||||
|
{
|
||||||
|
if (storages[i] != null)
|
||||||
|
{
|
||||||
|
foreach (var (a, b) in storages[i].All())
|
||||||
|
{
|
||||||
|
if (worldToTransferID.TryGetValue(a, out var otherA))
|
||||||
|
{
|
||||||
|
if (worldToTransferID.TryGetValue(b, out var otherB))
|
||||||
|
{
|
||||||
|
var storageIndex = storages[i].GetStorageIndex(a, b);
|
||||||
|
var relationData = storages[i].Get(storageIndex);
|
||||||
|
other.Set(otherA, otherB, i, relationData);
|
||||||
|
other.EntityStorage.AddRelationKind(otherA, i);
|
||||||
|
other.EntityStorage.AddRelationKind(otherB, i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Missing transfer entity! {EntityStorage.Tag(a.ID)} related to {EntityStorage.Tag(b.ID)}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,76 +1,99 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using MoonTools.ECS.Collections;
|
using MoonTools.ECS.Collections;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
// TODO: implement this entire class with NativeMemory equivalents, can just memcpy for snapshots
|
|
||||||
internal class RelationStorage
|
|
||||||
{
|
{
|
||||||
internal NativeArray Relations;
|
internal abstract class RelationStorage
|
||||||
internal NativeArray RelationDatas;
|
|
||||||
internal int ElementSize;
|
|
||||||
internal Dictionary<(Entity, Entity), int> Indices = new Dictionary<(Entity, Entity), int>(16);
|
|
||||||
internal Dictionary<Entity, IndexableSet<Entity>> OutRelationSets = new Dictionary<Entity, IndexableSet<Entity>>(16);
|
|
||||||
internal Dictionary<Entity, IndexableSet<Entity>> InRelationSets = new Dictionary<Entity, IndexableSet<Entity>>(16);
|
|
||||||
private Stack<IndexableSet<Entity>> ListPool = new Stack<IndexableSet<Entity>>();
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public RelationStorage(int relationDataSize)
|
|
||||||
{
|
{
|
||||||
ElementSize = relationDataSize;
|
public abstract unsafe void Set(int entityA, int entityB, void* relationData);
|
||||||
Relations = new NativeArray(Unsafe.SizeOf<(Entity, Entity)>());
|
public abstract int GetStorageIndex(int entityA, int entityB);
|
||||||
RelationDatas = new NativeArray(relationDataSize);
|
public abstract unsafe void* Get(int relationStorageIndex);
|
||||||
|
public abstract void UnrelateAll(int entityID);
|
||||||
|
public abstract ReverseSpanEnumerator<(Entity, Entity)> All();
|
||||||
|
public abstract RelationStorage CreateStorage();
|
||||||
|
public abstract void Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReverseSpanEnumerator<(Entity, Entity)> All()
|
// Relation is the two entities, A related to B.
|
||||||
|
// TRelation is the data attached to the relation.
|
||||||
|
internal unsafe class RelationStorage<TRelation> : RelationStorage, IDisposable where TRelation : unmanaged
|
||||||
{
|
{
|
||||||
return new ReverseSpanEnumerator<(Entity, Entity)>(Relations.ToSpan<(Entity, Entity)>());
|
private int count = 0;
|
||||||
|
private int capacity = 16;
|
||||||
|
private (Entity, Entity)* relations;
|
||||||
|
private TRelation* relationDatas;
|
||||||
|
private Dictionary<(Entity, Entity), int> indices = new Dictionary<(Entity, Entity), int>(16);
|
||||||
|
private Dictionary<int, IndexableSet<Entity>> outRelations = new Dictionary<int, IndexableSet<Entity>>(16);
|
||||||
|
private Dictionary<int, IndexableSet<Entity>> inRelations = new Dictionary<int, IndexableSet<Entity>>(16);
|
||||||
|
private Stack<IndexableSet<Entity>> listPool = new Stack<IndexableSet<Entity>>();
|
||||||
|
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
public RelationStorage()
|
||||||
|
{
|
||||||
|
relations = ((Entity, Entity)*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<(Entity, Entity)>()));
|
||||||
|
relationDatas = (TRelation*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<TRelation>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public unsafe void Set<T>(in Entity entityA, in Entity entityB, in T relationData) where T : unmanaged
|
public override ReverseSpanEnumerator<(Entity, Entity)> All()
|
||||||
|
{
|
||||||
|
return new ReverseSpanEnumerator<(Entity, Entity)>(new Span<(Entity, Entity)>(relations, count));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set(in Entity entityA, in Entity entityB, TRelation relationData)
|
||||||
{
|
{
|
||||||
var relation = (entityA, entityB);
|
var relation = (entityA, entityB);
|
||||||
|
|
||||||
if (Indices.TryGetValue(relation, out var index))
|
if (indices.TryGetValue(relation, out var index))
|
||||||
{
|
{
|
||||||
RelationDatas.Set(index, relationData);
|
relationDatas[index] = relationData;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!OutRelationSets.ContainsKey(entityA))
|
var idA = entityA.ID;
|
||||||
{
|
var idB = entityB.ID;
|
||||||
OutRelationSets[entityA] = AcquireHashSetFromPool();
|
|
||||||
}
|
|
||||||
OutRelationSets[entityA].Add(entityB);
|
|
||||||
|
|
||||||
if (!InRelationSets.ContainsKey(entityB))
|
if (!outRelations.ContainsKey(idA))
|
||||||
{
|
{
|
||||||
InRelationSets[entityB] = AcquireHashSetFromPool();
|
outRelations[idA] = AcquireHashSetFromPool();
|
||||||
}
|
}
|
||||||
InRelationSets[entityB].Add(entityA);
|
outRelations[idA].Add(idB);
|
||||||
|
|
||||||
Relations.Append(relation);
|
if (!inRelations.ContainsKey(idB))
|
||||||
RelationDatas.Append(relationData);
|
{
|
||||||
Indices.Add(relation, Relations.Count - 1);
|
inRelations[idB] = AcquireHashSetFromPool();
|
||||||
|
}
|
||||||
|
inRelations[idB].Add(idA);
|
||||||
|
|
||||||
|
if (count >= capacity)
|
||||||
|
{
|
||||||
|
capacity *= 2;
|
||||||
|
relations = ((Entity, Entity)*) NativeMemory.Realloc(relations, (nuint) (capacity * Unsafe.SizeOf<(Entity, Entity)>()));
|
||||||
|
relationDatas = (TRelation*) NativeMemory.Realloc(relationDatas, (nuint) (capacity * Unsafe.SizeOf<TRelation>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref T Get<T>(in Entity entityA, in Entity entityB) where T : unmanaged
|
relations[count] = relation;
|
||||||
{
|
relationDatas[count] = relationData;
|
||||||
var relationIndex = Indices[(entityA, entityB)];
|
indices.Add(relation, count);
|
||||||
return ref RelationDatas.Get<T>(relationIndex);
|
count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Has(Entity entityA, Entity entityB)
|
public TRelation Get(in Entity entityA, in Entity entityB)
|
||||||
{
|
{
|
||||||
return Indices.ContainsKey((entityA, entityB));
|
return relationDatas[indices[(entityA, entityB)]];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReverseSpanEnumerator<Entity> OutRelations(Entity Entity)
|
public bool Has((Entity, Entity) relation)
|
||||||
{
|
{
|
||||||
if (OutRelationSets.TryGetValue(Entity, out var entityOutRelations))
|
return indices.ContainsKey(relation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReverseSpanEnumerator<Entity> OutRelations(int entityID)
|
||||||
|
{
|
||||||
|
if (outRelations.TryGetValue(entityID, out var entityOutRelations))
|
||||||
{
|
{
|
||||||
return entityOutRelations.GetEnumerator();
|
return entityOutRelations.GetEnumerator();
|
||||||
}
|
}
|
||||||
|
@ -80,35 +103,35 @@ internal class RelationStorage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity OutFirst(Entity Entity)
|
public Entity OutFirst(int entityID)
|
||||||
{
|
{
|
||||||
return OutNth(Entity, 0);
|
return OutNth(entityID, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity OutNth(Entity Entity, int n)
|
public Entity OutNth(int entityID, int n)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (!OutRelationSets.ContainsKey(Entity) || OutRelationSets[Entity].Count == 0)
|
if (!outRelations.ContainsKey(entityID) || outRelations[entityID].Count == 0)
|
||||||
{
|
{
|
||||||
throw new KeyNotFoundException("No out relations to this entity!");
|
throw new KeyNotFoundException("No out relations to this entity!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return OutRelationSets[Entity][n];
|
return outRelations[entityID][n];
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool HasOutRelation(Entity Entity)
|
public bool HasOutRelation(int entityID)
|
||||||
{
|
{
|
||||||
return OutRelationSets.ContainsKey(Entity) && OutRelationSets[Entity].Count > 0;
|
return outRelations.ContainsKey(entityID) && outRelations[entityID].Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int OutRelationCount(Entity Entity)
|
public int OutRelationCount(int entityID)
|
||||||
{
|
{
|
||||||
return OutRelationSets.TryGetValue(Entity, out var entityOutRelations) ? entityOutRelations.Count : 0;
|
return outRelations.TryGetValue(entityID, out var entityOutRelations) ? entityOutRelations.Count : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReverseSpanEnumerator<Entity> InRelations(Entity Entity)
|
public ReverseSpanEnumerator<Entity> InRelations(int entityID)
|
||||||
{
|
{
|
||||||
if (InRelationSets.TryGetValue(Entity, out var entityInRelations))
|
if (inRelations.TryGetValue(entityID, out var entityInRelations))
|
||||||
{
|
{
|
||||||
return entityInRelations.GetEnumerator();
|
return entityInRelations.GetEnumerator();
|
||||||
}
|
}
|
||||||
|
@ -118,31 +141,31 @@ internal class RelationStorage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity InFirst(Entity Entity)
|
public Entity InFirst(int entityID)
|
||||||
{
|
{
|
||||||
return InNth(Entity, 0);
|
return InNth(entityID, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity InNth(Entity Entity, int n)
|
public Entity InNth(int entityID, int n)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (!InRelationSets.ContainsKey(Entity) || InRelationSets[Entity].Count == 0)
|
if (!inRelations.ContainsKey(entityID) || inRelations[entityID].Count == 0)
|
||||||
{
|
{
|
||||||
throw new KeyNotFoundException("No in relations to this entity!");
|
throw new KeyNotFoundException("No in relations to this entity!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return InRelationSets[Entity][n];
|
return inRelations[entityID][n];
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool HasInRelation(Entity Entity)
|
public bool HasInRelation(int entityID)
|
||||||
{
|
{
|
||||||
return InRelationSets.ContainsKey(Entity) && InRelationSets[Entity].Count > 0;
|
return inRelations.ContainsKey(entityID) && inRelations[entityID].Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int InRelationCount(Entity Entity)
|
public int InRelationCount(int entityID)
|
||||||
{
|
{
|
||||||
return InRelationSets.TryGetValue(Entity, out var entityInRelations) ? entityInRelations.Count : 0;
|
return inRelations.TryGetValue(entityID, out var entityInRelations) ? entityInRelations.Count : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public (bool, bool) Remove(in Entity entityA, in Entity entityB)
|
public (bool, bool) Remove(in Entity entityA, in Entity entityB)
|
||||||
|
@ -151,131 +174,153 @@ internal class RelationStorage
|
||||||
var bEmpty = false;
|
var bEmpty = false;
|
||||||
var relation = (entityA, entityB);
|
var relation = (entityA, entityB);
|
||||||
|
|
||||||
if (OutRelationSets.TryGetValue(entityA, out var entityOutRelations))
|
if (outRelations.TryGetValue(entityA.ID, out var entityOutRelations))
|
||||||
{
|
{
|
||||||
entityOutRelations.Remove(entityB);
|
entityOutRelations.Remove(entityB.ID);
|
||||||
if (OutRelationSets[entityA].Count == 0)
|
if (outRelations[entityA.ID].Count == 0)
|
||||||
{
|
{
|
||||||
aEmpty = true;
|
aEmpty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (InRelationSets.TryGetValue(entityB, out var entityInRelations))
|
if (inRelations.TryGetValue(entityB.ID, out var entityInRelations))
|
||||||
{
|
{
|
||||||
entityInRelations.Remove(entityA);
|
entityInRelations.Remove(entityA.ID);
|
||||||
if (InRelationSets[entityB].Count == 0)
|
if (inRelations[entityB.ID].Count == 0)
|
||||||
{
|
{
|
||||||
bEmpty = true;
|
bEmpty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Indices.TryGetValue(relation, out var index))
|
if (indices.TryGetValue(relation, out var index))
|
||||||
{
|
{
|
||||||
var lastElementIndex = Relations.Count - 1;
|
var lastElementIndex = count - 1;
|
||||||
|
|
||||||
// move an element into the hole
|
// move an element into the hole
|
||||||
if (index != lastElementIndex)
|
if (index != lastElementIndex)
|
||||||
{
|
{
|
||||||
var lastRelation = Relations.Get<(Entity, Entity)>(lastElementIndex);
|
var lastRelation = relations[lastElementIndex];
|
||||||
Indices[lastRelation] = index;
|
indices[lastRelation] = index;
|
||||||
|
relationDatas[index] = relationDatas[lastElementIndex];
|
||||||
|
relations[index] = lastRelation;
|
||||||
}
|
}
|
||||||
|
|
||||||
RelationDatas.Delete(index);
|
count -= 1;
|
||||||
Relations.Delete(index);
|
indices.Remove(relation);
|
||||||
|
|
||||||
Indices.Remove(relation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (aEmpty, bEmpty);
|
return (aEmpty, bEmpty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveEntity(in Entity entity)
|
private IndexableSet<Entity> AcquireHashSetFromPool()
|
||||||
{
|
{
|
||||||
if (OutRelationSets.TryGetValue(entity, out var entityOutRelations))
|
if (listPool.Count == 0)
|
||||||
{
|
{
|
||||||
foreach (var entityB in entityOutRelations)
|
listPool.Push(new IndexableSet<Entity>());
|
||||||
{
|
|
||||||
Remove(entity, entityB);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnHashSetToPool(entityOutRelations);
|
return listPool.Pop();
|
||||||
OutRelationSets.Remove(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (InRelationSets.TryGetValue(entity, out var entityInRelations))
|
|
||||||
{
|
|
||||||
foreach (var entityA in entityInRelations)
|
|
||||||
{
|
|
||||||
Remove(entityA, entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
ReturnHashSetToPool(entityInRelations);
|
|
||||||
InRelationSets.Remove(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal IndexableSet<Entity> AcquireHashSetFromPool()
|
|
||||||
{
|
|
||||||
if (ListPool.Count == 0)
|
|
||||||
{
|
|
||||||
ListPool.Push(new IndexableSet<Entity>());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ListPool.Pop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReturnHashSetToPool(IndexableSet<Entity> hashSet)
|
private void ReturnHashSetToPool(IndexableSet<Entity> hashSet)
|
||||||
{
|
{
|
||||||
hashSet.Clear();
|
hashSet.Clear();
|
||||||
ListPool.Push(hashSet);
|
listPool.Push(hashSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
// untyped methods used for internal implementation
|
||||||
{
|
|
||||||
Indices.Clear();
|
|
||||||
|
|
||||||
foreach (var set in InRelationSets.Values)
|
public override unsafe void Set(int entityA, int entityB, void* relationData)
|
||||||
|
{
|
||||||
|
Set(entityA, entityB, *(TRelation*) relationData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetStorageIndex(int entityA, int entityB)
|
||||||
|
{
|
||||||
|
return indices[(entityA, entityB)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public override unsafe void* Get(int relationStorageIndex)
|
||||||
|
{
|
||||||
|
return &relationDatas[relationStorageIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void UnrelateAll(int entityID)
|
||||||
|
{
|
||||||
|
if (outRelations.TryGetValue(entityID, out var entityOutRelations))
|
||||||
|
{
|
||||||
|
foreach (var entityB in entityOutRelations)
|
||||||
|
{
|
||||||
|
Remove(entityID, entityB);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnHashSetToPool(entityOutRelations);
|
||||||
|
outRelations.Remove(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inRelations.TryGetValue(entityID, out var entityInRelations))
|
||||||
|
{
|
||||||
|
foreach (var entityA in entityInRelations)
|
||||||
|
{
|
||||||
|
Remove(entityA, entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnHashSetToPool(entityInRelations);
|
||||||
|
inRelations.Remove(entityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override RelationStorage<TRelation> CreateStorage()
|
||||||
|
{
|
||||||
|
return new RelationStorage<TRelation>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Clear()
|
||||||
|
{
|
||||||
|
count = 0;
|
||||||
|
indices.Clear();
|
||||||
|
|
||||||
|
foreach (var set in inRelations.Values)
|
||||||
{
|
{
|
||||||
ReturnHashSetToPool(set);
|
ReturnHashSetToPool(set);
|
||||||
}
|
}
|
||||||
InRelationSets.Clear();
|
inRelations.Clear();
|
||||||
|
|
||||||
foreach (var set in OutRelationSets.Values)
|
foreach (var set in outRelations.Values)
|
||||||
{
|
{
|
||||||
ReturnHashSetToPool(set);
|
ReturnHashSetToPool(set);
|
||||||
}
|
}
|
||||||
OutRelationSets.Clear();
|
outRelations.Clear();
|
||||||
|
|
||||||
Relations.Clear();
|
|
||||||
RelationDatas.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
protected virtual void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (!IsDisposed)
|
if (!disposed)
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
|
|
||||||
if (disposing)
|
if (disposing)
|
||||||
{
|
{
|
||||||
foreach (var set in ListPool)
|
foreach (var set in listPool)
|
||||||
{
|
{
|
||||||
set.Dispose();
|
set.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
Relations.Dispose();
|
|
||||||
RelationDatas.Dispose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IsDisposed = true;
|
NativeMemory.Free(relations);
|
||||||
|
NativeMemory.Free(relationDatas);
|
||||||
|
relations = null;
|
||||||
|
relationDatas = null;
|
||||||
|
|
||||||
|
disposed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~RelationStorage()
|
~RelationStorage()
|
||||||
// {
|
{
|
||||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||||
// Dispose(disposing: false);
|
Dispose(disposing: false);
|
||||||
// }
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
@ -283,4 +328,5 @@ internal class RelationStorage
|
||||||
Dispose(disposing: true);
|
Dispose(disposing: true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public abstract class Renderer : EntityComponentReader
|
|
||||||
{
|
{
|
||||||
|
public abstract class Renderer : EntityComponentReader
|
||||||
|
{
|
||||||
public Renderer(World world) : base(world) { }
|
public Renderer(World world) : base(world) { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
378
src/Snapshot.cs
378
src/Snapshot.cs
|
@ -1,378 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using MoonTools.ECS.Collections;
|
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
|
||||||
|
|
||||||
// TODO: we should implement a NativeDictionary that can be memcopied
|
|
||||||
public class Snapshot : IDisposable
|
|
||||||
{
|
|
||||||
private List<ComponentSnapshot> ComponentSnapshots = new List<ComponentSnapshot>();
|
|
||||||
|
|
||||||
// FIXME: we could just have a filter ID
|
|
||||||
private Dictionary<FilterSignature, List<Entity>> Filters = new Dictionary<FilterSignature, List<Entity>>();
|
|
||||||
|
|
||||||
private List<RelationSnapshot> RelationSnapshots = new List<RelationSnapshot>();
|
|
||||||
|
|
||||||
private List<IndexableSet<TypeId>> EntityRelationIndex =
|
|
||||||
new List<IndexableSet<TypeId>>();
|
|
||||||
|
|
||||||
private List<IndexableSet<TypeId>> EntityComponentIndex =
|
|
||||||
new List<IndexableSet<TypeId>>();
|
|
||||||
|
|
||||||
private List<string> EntityTags = new List<string>();
|
|
||||||
|
|
||||||
private IdAssigner EntityIdAssigner = new IdAssigner();
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public void Restore(World world)
|
|
||||||
{
|
|
||||||
// restore id assigner state
|
|
||||||
EntityIdAssigner.CopyTo(world.EntityIdAssigner);
|
|
||||||
|
|
||||||
// restore filter states
|
|
||||||
// this could be sped up if we figured out a direct IndexableSet copy
|
|
||||||
foreach (var (signature, entityList) in Filters)
|
|
||||||
{
|
|
||||||
var filter = world.FilterIndex[signature];
|
|
||||||
|
|
||||||
filter.Clear();
|
|
||||||
|
|
||||||
foreach (var entity in entityList)
|
|
||||||
{
|
|
||||||
filter.AddEntity(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// clear all component storages in case any were created after snapshot
|
|
||||||
// FIXME: this can be eliminated via component discovery
|
|
||||||
foreach (var componentStorage in world.ComponentIndex)
|
|
||||||
{
|
|
||||||
componentStorage.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
// clear all relation storages in case any were created after snapshot
|
|
||||||
// FIXME: this can be eliminated via component discovery
|
|
||||||
foreach (var relationStorage in world.RelationIndex)
|
|
||||||
{
|
|
||||||
relationStorage.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < ComponentSnapshots.Count; i += 1)
|
|
||||||
{
|
|
||||||
var componentStorage = world.ComponentIndex[i];
|
|
||||||
ComponentSnapshots[i].Restore(componentStorage);
|
|
||||||
}
|
|
||||||
|
|
||||||
// restore relation state
|
|
||||||
for (var i = 0; i < RelationSnapshots.Count; i += 1)
|
|
||||||
{
|
|
||||||
var relationStorage = world.RelationIndex[i];
|
|
||||||
RelationSnapshots[i].Restore(relationStorage);
|
|
||||||
}
|
|
||||||
|
|
||||||
// restore entity relation index state
|
|
||||||
// FIXME: arghhhh this is so slow
|
|
||||||
|
|
||||||
foreach (var relationTypeSet in world.EntityRelationIndex)
|
|
||||||
{
|
|
||||||
relationTypeSet.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < EntityRelationIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
var relationTypeSet = EntityRelationIndex[i];
|
|
||||||
|
|
||||||
foreach (var typeId in relationTypeSet)
|
|
||||||
{
|
|
||||||
world.EntityRelationIndex[i].Add(typeId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// restore entity component index state
|
|
||||||
// FIXME: arrghghhh this is so slow
|
|
||||||
|
|
||||||
foreach (var componentTypeSet in world.EntityComponentIndex)
|
|
||||||
{
|
|
||||||
componentTypeSet.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < EntityComponentIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
var componentTypeSet = EntityComponentIndex[i];
|
|
||||||
|
|
||||||
foreach (var typeId in componentTypeSet)
|
|
||||||
{
|
|
||||||
world.EntityComponentIndex[i].Add(typeId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// restore entity tags
|
|
||||||
for (var i = 0; i < EntityTags.Count; i += 1)
|
|
||||||
{
|
|
||||||
world.EntityTags[i] = EntityTags[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Take(World world)
|
|
||||||
{
|
|
||||||
// copy id assigner state
|
|
||||||
world.EntityIdAssigner.CopyTo(EntityIdAssigner);
|
|
||||||
|
|
||||||
// copy filter states
|
|
||||||
foreach (var (_, filter) in world.FilterIndex)
|
|
||||||
{
|
|
||||||
TakeFilterSnapshot(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy components
|
|
||||||
for (var i = ComponentSnapshots.Count; i < world.ComponentIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
ComponentSnapshots.Add(new ComponentSnapshot(world.ComponentIndex[i].ElementSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < world.ComponentIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
ComponentSnapshots[i].Take(world.ComponentIndex[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy relations
|
|
||||||
for (var i = RelationSnapshots.Count; i < world.RelationIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
RelationSnapshots.Add(new RelationSnapshot(world.RelationIndex[i].ElementSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < world.RelationIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
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
|
|
||||||
for (var i = 0; i < world.EntityRelationIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
EntityRelationIndex[i].Clear();
|
|
||||||
|
|
||||||
foreach (var typeId in world.EntityRelationIndex[i])
|
|
||||||
{
|
|
||||||
EntityRelationIndex[i].Add(typeId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy entity component index
|
|
||||||
// FIXME: arghhhh this is so slow
|
|
||||||
for (var i = 0; i < world.EntityComponentIndex.Count; i += 1)
|
|
||||||
{
|
|
||||||
EntityComponentIndex[i].Clear();
|
|
||||||
|
|
||||||
foreach (var typeId in world.EntityComponentIndex[i])
|
|
||||||
{
|
|
||||||
EntityComponentIndex[i].Add(typeId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy entity tags
|
|
||||||
EntityTags.Clear();
|
|
||||||
foreach (var s in world.EntityTags)
|
|
||||||
{
|
|
||||||
EntityTags.Add(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void TakeFilterSnapshot(Filter filter)
|
|
||||||
{
|
|
||||||
if (!Filters.TryGetValue(filter.Signature, out var entities))
|
|
||||||
{
|
|
||||||
entities = new List<Entity>();
|
|
||||||
Filters.Add(filter.Signature, entities);
|
|
||||||
}
|
|
||||||
|
|
||||||
entities.Clear();
|
|
||||||
|
|
||||||
foreach (var entity in filter.EntitySet.AsSpan())
|
|
||||||
{
|
|
||||||
entities.Add(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ComponentSnapshot : IDisposable
|
|
||||||
{
|
|
||||||
private readonly NativeArray Components;
|
|
||||||
private readonly NativeArray<Entity> EntityIDs;
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public ComponentSnapshot(int elementSize)
|
|
||||||
{
|
|
||||||
Components = new NativeArray(elementSize);
|
|
||||||
EntityIDs = new NativeArray<Entity>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Take(ComponentStorage componentStorage)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
var entityID = EntityIDs[i];
|
|
||||||
componentStorage.EntityIDToStorageIndex[entityID] = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
Components.Dispose();
|
|
||||||
EntityIDs.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class RelationSnapshot : IDisposable
|
|
||||||
{
|
|
||||||
private NativeArray Relations;
|
|
||||||
private NativeArray RelationDatas;
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
public RelationSnapshot(int elementSize)
|
|
||||||
{
|
|
||||||
Relations = new NativeArray(Unsafe.SizeOf<(Entity, Entity)>());
|
|
||||||
RelationDatas = new NativeArray(elementSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Take(RelationStorage relationStorage)
|
|
||||||
{
|
|
||||||
relationStorage.Relations.CopyAllTo(Relations);
|
|
||||||
relationStorage.RelationDatas.CopyAllTo(RelationDatas);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Restore(RelationStorage relationStorage)
|
|
||||||
{
|
|
||||||
relationStorage.Clear();
|
|
||||||
|
|
||||||
Relations.CopyAllTo(relationStorage.Relations);
|
|
||||||
RelationDatas.CopyAllTo(relationStorage.RelationDatas);
|
|
||||||
|
|
||||||
for (int index = 0; index < Relations.Count; index += 1)
|
|
||||||
{
|
|
||||||
var relation = Relations.Get<(Entity, Entity)>(index);
|
|
||||||
relationStorage.Indices[relation] = index;
|
|
||||||
|
|
||||||
relationStorage.Indices[relation] = index;
|
|
||||||
|
|
||||||
if (!relationStorage.OutRelationSets.ContainsKey(relation.Item1))
|
|
||||||
{
|
|
||||||
relationStorage.OutRelationSets[relation.Item1] =
|
|
||||||
relationStorage.AcquireHashSetFromPool();
|
|
||||||
}
|
|
||||||
|
|
||||||
relationStorage.OutRelationSets[relation.Item1].Add(relation.Item2);
|
|
||||||
|
|
||||||
if (!relationStorage.InRelationSets.ContainsKey(relation.Item2))
|
|
||||||
{
|
|
||||||
relationStorage.InRelationSets[relation.Item2] =
|
|
||||||
relationStorage.AcquireHashSetFromPool();
|
|
||||||
}
|
|
||||||
|
|
||||||
relationStorage.InRelationSets[relation.Item2].Add(relation.Item1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
Relations.Dispose();
|
|
||||||
RelationDatas.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
foreach (var componentSnapshot in ComponentSnapshots)
|
|
||||||
{
|
|
||||||
componentSnapshot.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var relationSnapshot in RelationSnapshots)
|
|
||||||
{
|
|
||||||
relationSnapshot.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentSet in EntityComponentIndex)
|
|
||||||
{
|
|
||||||
componentSet.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var relationSet in EntityRelationIndex)
|
|
||||||
{
|
|
||||||
relationSet.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityIdAssigner.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,15 +1,47 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS
|
||||||
|
|
||||||
public abstract class System : Manipulator
|
|
||||||
{
|
{
|
||||||
protected System(World world) : base(world) { }
|
public abstract class System : Manipulator
|
||||||
|
{
|
||||||
|
internal MessageDepot MessageDepot => World.MessageDepot;
|
||||||
|
|
||||||
|
public System(World world) : base(world) { }
|
||||||
|
|
||||||
public abstract void Update(TimeSpan delta);
|
public abstract void Update(TimeSpan delta);
|
||||||
|
|
||||||
protected ReadOnlySpan<T> ReadMessages<T>() where T : unmanaged => World.ReadMessages<T>();
|
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : unmanaged
|
||||||
protected T ReadMessage<T>() where T : unmanaged => World.ReadMessage<T>();
|
{
|
||||||
protected bool SomeMessage<T>() where T : unmanaged => World.SomeMessage<T>();
|
return MessageDepot.All<TMessage>();
|
||||||
protected void Send<T>(T message) where T : unmanaged => World.Send(message);
|
}
|
||||||
|
|
||||||
|
protected TMessage ReadMessage<TMessage>() where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return MessageDepot.First<TMessage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool SomeMessage<TMessage>() where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return MessageDepot.Some<TMessage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ReverseSpanEnumerator<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return MessageDepot.WithEntity<TMessage>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return ref MessageDepot.FirstWithEntity<TMessage>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
return MessageDepot.SomeWithEntity<TMessage>(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void Send<TMessage>(in TMessage message) where TMessage : unmanaged => World.Send(message);
|
||||||
|
|
||||||
|
protected void Send<TMessage>(in Entity entity, in TMessage message) where TMessage : unmanaged => World.Send(entity, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,82 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
|
||||||
|
|
||||||
public readonly record struct TypeId(uint Value) : IComparable<TypeId>
|
|
||||||
{
|
|
||||||
public int CompareTo(TypeId other)
|
|
||||||
{
|
|
||||||
return Value.CompareTo(other.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator int(TypeId typeId)
|
|
||||||
{
|
|
||||||
return (int) typeId.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class ComponentTypeIdAssigner
|
|
||||||
{
|
|
||||||
protected static ushort Counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class ComponentTypeIdAssigner<T> : ComponentTypeIdAssigner
|
|
||||||
{
|
|
||||||
public static readonly ushort Id;
|
|
||||||
public static readonly int Size;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
static ComponentTypeIdAssigner()
|
|
||||||
{
|
|
||||||
Id = Counter++;
|
|
||||||
Size = Unsafe.SizeOf<T>();
|
|
||||||
|
|
||||||
World.ComponentTypeElementSizes.Add(Size);
|
|
||||||
|
|
||||||
#if DEBUG
|
|
||||||
World.ComponentTypeToId[typeof(T)] = new TypeId(Id);
|
|
||||||
World.ComponentTypeIdToType.Add(typeof(T));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class RelationTypeIdAssigner
|
|
||||||
{
|
|
||||||
protected static ushort Counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class RelationTypeIdAssigner<T> : RelationTypeIdAssigner
|
|
||||||
{
|
|
||||||
public static readonly ushort Id;
|
|
||||||
public static readonly int Size;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
static RelationTypeIdAssigner()
|
|
||||||
{
|
|
||||||
Id = Counter++;
|
|
||||||
Size = Unsafe.SizeOf<T>();
|
|
||||||
|
|
||||||
World.RelationTypeElementSizes.Add(Size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class MessageTypeIdAssigner
|
|
||||||
{
|
|
||||||
protected static ushort Counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class MessageTypeIdAssigner<T> : MessageTypeIdAssigner
|
|
||||||
{
|
|
||||||
public static readonly ushort Id;
|
|
||||||
public static readonly int Size;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
static MessageTypeIdAssigner()
|
|
||||||
{
|
|
||||||
Id = Counter++;
|
|
||||||
Size = Unsafe.SizeOf<T>();
|
|
||||||
|
|
||||||
World.MessageTypeElementSizes.Add(Size);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS
|
||||||
|
{
|
||||||
|
public class TypeIndices
|
||||||
|
{
|
||||||
|
Dictionary<Type, int> TypeToIndex = new Dictionary<Type, int>();
|
||||||
|
int nextID = 0;
|
||||||
|
public int Count => TypeToIndex.Count;
|
||||||
|
|
||||||
|
public int GetIndex<T>() where T : unmanaged
|
||||||
|
{
|
||||||
|
if (!TypeToIndex.ContainsKey(typeof(T)))
|
||||||
|
{
|
||||||
|
TypeToIndex.Add(typeof(T), nextID);
|
||||||
|
nextID += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TypeToIndex[typeof(T)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetIndex(Type type)
|
||||||
|
{
|
||||||
|
return TypeToIndex[type];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
public Dictionary<Type, int>.KeyCollection Types => TypeToIndex.Keys;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
622
src/World.cs
622
src/World.cs
|
@ -1,506 +1,194 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using MoonTools.ECS.Collections;
|
|
||||||
|
|
||||||
#if DEBUG
|
namespace MoonTools.ECS
|
||||||
using System.Reflection;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
|
||||||
|
|
||||||
public class World : IDisposable
|
|
||||||
{
|
{
|
||||||
#if DEBUG
|
public class World
|
||||||
// TODO: is there a smarter way to do this?
|
|
||||||
internal static Dictionary<Type, TypeId> ComponentTypeToId = new Dictionary<Type, TypeId>();
|
|
||||||
internal static List<Type> ComponentTypeIdToType = new List<Type>();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
internal static List<int> ComponentTypeElementSizes = new List<int>();
|
|
||||||
internal static List<int> RelationTypeElementSizes = new List<int>();
|
|
||||||
internal static List<int> MessageTypeElementSizes = new List<int>();
|
|
||||||
|
|
||||||
// Filters
|
|
||||||
internal readonly Dictionary<FilterSignature, Filter> FilterIndex = new Dictionary<FilterSignature, Filter>();
|
|
||||||
private readonly List<List<Filter>> ComponentTypeToFilter = new List<List<Filter>>();
|
|
||||||
|
|
||||||
// TODO: can we make the tag an native array of chars at some point?
|
|
||||||
internal List<string> EntityTags = new List<string>();
|
|
||||||
|
|
||||||
// Relation Storages
|
|
||||||
internal List<RelationStorage> RelationIndex = new List<RelationStorage>();
|
|
||||||
internal List<IndexableSet<TypeId>> EntityRelationIndex = new List<IndexableSet<TypeId>>();
|
|
||||||
|
|
||||||
// Message Storages
|
|
||||||
private List<MessageStorage> MessageIndex = new List<MessageStorage>();
|
|
||||||
|
|
||||||
public FilterBuilder FilterBuilder => new FilterBuilder(this);
|
|
||||||
|
|
||||||
internal readonly List<ComponentStorage> ComponentIndex = new List<ComponentStorage>();
|
|
||||||
internal List<IndexableSet<TypeId>> EntityComponentIndex = new List<IndexableSet<TypeId>>();
|
|
||||||
|
|
||||||
internal IdAssigner EntityIdAssigner = new IdAssigner();
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
|
||||||
|
|
||||||
internal TypeId GetComponentTypeId<T>() where T : unmanaged
|
|
||||||
{
|
{
|
||||||
var typeId = new TypeId(ComponentTypeIdAssigner<T>.Id);
|
internal readonly static TypeIndices ComponentTypeIndices = new TypeIndices();
|
||||||
if (typeId < ComponentIndex.Count)
|
internal readonly static TypeIndices RelationTypeIndices = new TypeIndices();
|
||||||
|
internal readonly EntityStorage EntityStorage = new EntityStorage();
|
||||||
|
internal readonly ComponentDepot ComponentDepot;
|
||||||
|
internal readonly MessageDepot MessageDepot = new MessageDepot();
|
||||||
|
internal readonly RelationDepot RelationDepot;
|
||||||
|
internal readonly FilterStorage FilterStorage;
|
||||||
|
public FilterBuilder FilterBuilder => new FilterBuilder(FilterStorage, ComponentTypeIndices);
|
||||||
|
|
||||||
|
public World()
|
||||||
{
|
{
|
||||||
return typeId;
|
ComponentDepot = new ComponentDepot(ComponentTypeIndices);
|
||||||
|
RelationDepot = new RelationDepot(EntityStorage, RelationTypeIndices);
|
||||||
|
FilterStorage = new FilterStorage(EntityStorage, ComponentTypeIndices);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 missingTypeId = new TypeId((uint) i);
|
|
||||||
var componentStorage = new ComponentStorage(missingTypeId, ComponentTypeElementSizes[i]);
|
|
||||||
ComponentIndex.Add(componentStorage);
|
|
||||||
ComponentTypeToFilter.Add(new List<Filter>());
|
|
||||||
}
|
|
||||||
|
|
||||||
return typeId;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private ComponentStorage GetComponentStorage<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
var typeId = GetComponentTypeId<T>();
|
|
||||||
return ComponentIndex[typeId];
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILTERS
|
|
||||||
|
|
||||||
internal Filter GetFilter(FilterSignature signature)
|
|
||||||
{
|
|
||||||
if (!FilterIndex.TryGetValue(signature, out var filter))
|
|
||||||
{
|
|
||||||
filter = new Filter(this, signature);
|
|
||||||
|
|
||||||
foreach (var typeId in signature.Included)
|
|
||||||
{
|
|
||||||
ComponentTypeToFilter[(int) typeId.Value].Add(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var typeId in signature.Excluded)
|
|
||||||
{
|
|
||||||
ComponentTypeToFilter[(int) typeId.Value].Add(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
FilterIndex.Add(signature, filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
return filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ENTITIES
|
|
||||||
|
|
||||||
public Entity CreateEntity(string tag = "")
|
public Entity CreateEntity(string tag = "")
|
||||||
{
|
{
|
||||||
var entity = new Entity(EntityIdAssigner.Assign());
|
return EntityStorage.Create(tag);
|
||||||
|
|
||||||
if (entity.ID == EntityComponentIndex.Count)
|
|
||||||
{
|
|
||||||
EntityRelationIndex.Add(new IndexableSet<TypeId>());
|
|
||||||
EntityComponentIndex.Add(new IndexableSet<TypeId>());
|
|
||||||
EntityTags.Add(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
return entity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Tag(Entity entity, string tag)
|
public void Tag(Entity entity, string tag)
|
||||||
{
|
{
|
||||||
EntityTags[(int) entity.ID] = tag;
|
EntityStorage.Tag(entity, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetTag(Entity entity)
|
public string GetTag(Entity entity)
|
||||||
{
|
{
|
||||||
return EntityTags[(int) entity.ID];
|
return EntityStorage.Tag(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set<TComponent>(Entity entity, in TComponent component) where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
// check for use after destroy
|
||||||
|
if (!EntityStorage.Exists(entity))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("This entity is not valid!");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
ComponentDepot.Set<TComponent>(entity.ID, component);
|
||||||
|
|
||||||
|
if (EntityStorage.SetComponent(entity.ID, ComponentTypeIndices.GetIndex<TComponent>()))
|
||||||
|
{
|
||||||
|
FilterStorage.Check<TComponent>(entity.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// untyped version for Transfer
|
||||||
|
// no filter check because filter state is copied directly
|
||||||
|
internal unsafe void Set(Entity entity, int componentTypeIndex, void* component)
|
||||||
|
{
|
||||||
|
ComponentDepot.Set(entity.ID, componentTypeIndex, component);
|
||||||
|
EntityStorage.SetComponent(entity.ID, componentTypeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove<TComponent>(in Entity entity) where TComponent : unmanaged
|
||||||
|
{
|
||||||
|
if (EntityStorage.RemoveComponent(entity.ID, ComponentTypeIndices.GetIndex<TComponent>()))
|
||||||
|
{
|
||||||
|
// Run filter storage update first so that the entity state is still valid in the remove callback.
|
||||||
|
FilterStorage.Check<TComponent>(entity.ID);
|
||||||
|
ComponentDepot.Remove<TComponent>(entity.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Relate<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
RelationDepot.Set(entityA, entityB, relationData);
|
||||||
|
var relationTypeIndex = RelationTypeIndices.GetIndex<TRelationKind>();
|
||||||
|
EntityStorage.AddRelationKind(entityA.ID, relationTypeIndex);
|
||||||
|
EntityStorage.AddRelationKind(entityB.ID, relationTypeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Unrelate<TRelationKind>(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
var (aEmpty, bEmpty) = RelationDepot.Remove<TRelationKind>(entityA, entityB);
|
||||||
|
|
||||||
|
if (aEmpty)
|
||||||
|
{
|
||||||
|
EntityStorage.RemoveRelation(entityA.ID, RelationTypeIndices.GetIndex<TRelationKind>());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bEmpty)
|
||||||
|
{
|
||||||
|
EntityStorage.RemoveRelation(entityB.ID, RelationTypeIndices.GetIndex<TRelationKind>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnrelateAll<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
||||||
|
{
|
||||||
|
RelationDepot.UnrelateAll<TRelationKind>(entity.ID);
|
||||||
|
EntityStorage.RemoveRelation(entity.ID, RelationTypeIndices.GetIndex<TRelationKind>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send<TMessage>(in TMessage message) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
MessageDepot.Add(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send<TMessage>(in Entity entity, in TMessage message) where TMessage : unmanaged
|
||||||
|
{
|
||||||
|
MessageDepot.Add(entity.ID, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Destroy(in Entity entity)
|
public void Destroy(in Entity entity)
|
||||||
{
|
{
|
||||||
var componentSet = EntityComponentIndex[(int) entity.ID];
|
foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID))
|
||||||
var relationSet = EntityRelationIndex[(int) entity.ID];
|
|
||||||
|
|
||||||
// remove all components from storages
|
|
||||||
foreach (var componentTypeIndex in componentSet)
|
|
||||||
{
|
{
|
||||||
var componentStorage = ComponentIndex[componentTypeIndex];
|
// Run filter storage update first so that the entity state is still valid in the remove callback.
|
||||||
componentStorage.Remove(entity);
|
FilterStorage.RemoveEntity(entity.ID, componentTypeIndex);
|
||||||
|
ComponentDepot.Remove(entity.ID, componentTypeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var filter in ComponentTypeToFilter[componentTypeIndex])
|
foreach (var relationTypeIndex in EntityStorage.RelationTypeIndices(entity.ID))
|
||||||
{
|
{
|
||||||
filter.RemoveEntity(entity);
|
RelationDepot.UnrelateAll(entity.ID, relationTypeIndex);
|
||||||
}
|
EntityStorage.RemoveRelation(entity.ID, relationTypeIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove all relations from storage
|
EntityStorage.Destroy(entity);
|
||||||
foreach (var relationTypeIndex in relationSet)
|
|
||||||
{
|
|
||||||
var relationStorage = RelationIndex[relationTypeIndex];
|
|
||||||
relationStorage.RemoveEntity(entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
var storage = GetComponentStorage<T>();
|
|
||||||
return storage.Has(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal bool Has(in Entity entity, in TypeId typeId)
|
|
||||||
{
|
|
||||||
return EntityComponentIndex[(int) entity.ID].Contains(typeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Some<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
var storage = GetComponentStorage<T>();
|
|
||||||
return storage.Any();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref T Get<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var storage = GetComponentStorage<T>();
|
|
||||||
return ref storage.Get<T>(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref T GetSingleton<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
var storage = GetComponentStorage<T>();
|
|
||||||
return ref storage.GetFirst<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity GetSingletonEntity<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
var storage = GetComponentStorage<T>();
|
|
||||||
return storage.FirstEntity();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Set<T>(in Entity entity, in T component) where T : unmanaged
|
|
||||||
{
|
|
||||||
var componentStorage = GetComponentStorage<T>();
|
|
||||||
|
|
||||||
if (!componentStorage.Set(entity, component))
|
|
||||||
{
|
|
||||||
EntityComponentIndex[(int) entity.ID].Add(componentStorage.TypeId);
|
|
||||||
|
|
||||||
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
|
|
||||||
{
|
|
||||||
filter.Check(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var componentStorage = GetComponentStorage<T>();
|
|
||||||
|
|
||||||
if (componentStorage.Remove(entity))
|
|
||||||
{
|
|
||||||
EntityComponentIndex[(int) entity.ID].Remove(componentStorage.TypeId);
|
|
||||||
|
|
||||||
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
|
|
||||||
{
|
|
||||||
filter.Check(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RELATIONS
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private RelationStorage GetRelationStorage<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
var typeId = new TypeId(RelationTypeIdAssigner<T>.Id);
|
|
||||||
if (typeId.Value < RelationIndex.Count)
|
|
||||||
{
|
|
||||||
return RelationIndex[typeId];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = RelationIndex.Count; i <= typeId; i += 1)
|
|
||||||
{
|
|
||||||
RelationIndex.Add(new RelationStorage(RelationTypeElementSizes[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
return RelationIndex[typeId];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Relate<T>(in Entity entityA, in Entity entityB, in T relation) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
relationStorage.Set(entityA, entityB, relation);
|
|
||||||
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
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
relationStorage.Remove(entityA, entityB);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UnrelateAll<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
relationStorage.RemoveEntity(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Related<T>(in Entity entityA, in Entity entityB) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.Has(entityA, entityB);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T GetRelationData<T>(in Entity entityA, in Entity entityB) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.Get<T>(entityA, entityB);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ReverseSpanEnumerator<(Entity, Entity)> Relations<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.All();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ReverseSpanEnumerator<Entity> OutRelations<T>(Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.OutRelations(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity OutRelationSingleton<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.OutFirst(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool HasOutRelation<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.HasOutRelation(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int OutRelationCount<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.OutRelationCount(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity NthOutRelation<T>(in Entity entity, int n) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.OutNth(entity, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ReverseSpanEnumerator<Entity> InRelations<T>(Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.InRelations(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity InRelationSingleton<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.InFirst(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool HasInRelation<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.HasInRelation(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int InRelationCount<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.InRelationCount(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity NthInRelation<T>(in Entity entity, int n) where T : unmanaged
|
|
||||||
{
|
|
||||||
var relationStorage = GetRelationStorage<T>();
|
|
||||||
return relationStorage.InNth(entity, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
// MESSAGES
|
|
||||||
|
|
||||||
private MessageStorage GetMessageStorage<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
var typeId = new TypeId(MessageTypeIdAssigner<T>.Id);
|
|
||||||
|
|
||||||
if (typeId < MessageIndex.Count)
|
|
||||||
{
|
|
||||||
return MessageIndex[typeId];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = MessageIndex.Count; i <= typeId; i += 1)
|
|
||||||
{
|
|
||||||
MessageIndex.Add(new MessageStorage(MessageTypeElementSizes[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
return MessageIndex[typeId];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Send<T>(in T message) where T : unmanaged
|
|
||||||
{
|
|
||||||
GetMessageStorage<T>().Add(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool SomeMessage<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
return GetMessageStorage<T>().Some();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ReadOnlySpan<T> ReadMessages<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
return GetMessageStorage<T>().All<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public T ReadMessage<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
return GetMessageStorage<T>().First<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClearMessages<T>() where T : unmanaged
|
|
||||||
{
|
|
||||||
GetMessageStorage<T>().Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: temporary component storage?
|
|
||||||
public void FinishUpdate()
|
public void FinishUpdate()
|
||||||
{
|
{
|
||||||
foreach (var messageStorage in MessageIndex)
|
MessageDepot.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
{
|
{
|
||||||
messageStorage.Clear();
|
EntityStorage.Clear();
|
||||||
|
MessageDepot.Clear();
|
||||||
|
RelationDepot.Clear();
|
||||||
|
ComponentDepot.Clear();
|
||||||
|
FilterStorage.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<int, int> WorldToTransferID = new Dictionary<int, int>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If you are using the World Transfer feature, call this once after your systems/filters have all been initialized.
|
||||||
|
/// </summary>
|
||||||
|
public void PrepareTransferTo(World other)
|
||||||
|
{
|
||||||
|
other.FilterStorage.CreateMissingStorages(FilterStorage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: there's probably a better way to handle Filters so they are not world-bound
|
||||||
|
public unsafe void Transfer(World other, Filter filter, Filter otherFilter)
|
||||||
|
{
|
||||||
|
WorldToTransferID.Clear();
|
||||||
|
other.ComponentDepot.CreateMissingStorages(ComponentDepot);
|
||||||
|
other.RelationDepot.CreateMissingStorages(RelationDepot);
|
||||||
|
|
||||||
|
// destroy all entities matching the filter
|
||||||
|
foreach (var entity in otherFilter.Entities)
|
||||||
|
{
|
||||||
|
other.Destroy(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
// create entities
|
||||||
|
foreach (var entity in filter.Entities)
|
||||||
|
{
|
||||||
|
var otherWorldEntity = other.CreateEntity(GetTag(entity));
|
||||||
|
WorldToTransferID.Add(entity.ID, otherWorldEntity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// transfer relations
|
||||||
|
RelationDepot.TransferStorage(WorldToTransferID, other.RelationDepot);
|
||||||
|
|
||||||
|
// set components
|
||||||
|
foreach (var entity in filter.Entities)
|
||||||
|
{
|
||||||
|
var otherWorldEntity = WorldToTransferID[entity.ID];
|
||||||
|
|
||||||
|
foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID))
|
||||||
|
{
|
||||||
|
other.Set(otherWorldEntity, componentTypeIndex, ComponentDepot.UntypedGet(entity.ID, componentTypeIndex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEBUG
|
// transfer filters last so callbacks trigger correctly
|
||||||
// NOTE: these methods are very inefficient
|
FilterStorage.TransferStorage(WorldToTransferID, other.FilterStorage);
|
||||||
// they should only be used in debugging contexts!!
|
|
||||||
#if DEBUG
|
|
||||||
public ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity)
|
|
||||||
{
|
|
||||||
return new ComponentTypeEnumerator(this, EntityComponentIndex[(int) entity.ID]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Entity> Debug_GetEntities(Type componentType)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
foreach (var type in ComponentTypeToId.Keys)
|
|
||||||
{
|
|
||||||
if (type.ToString().ToLower().Contains(typeString.ToLower()))
|
|
||||||
{
|
|
||||||
yield return type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref struct ComponentTypeEnumerator
|
|
||||||
{
|
|
||||||
private World World;
|
|
||||||
private IndexableSet<TypeId> Types;
|
|
||||||
private int ComponentIndex;
|
|
||||||
|
|
||||||
public ComponentTypeEnumerator GetEnumerator() => this;
|
|
||||||
|
|
||||||
internal ComponentTypeEnumerator(
|
|
||||||
World world,
|
|
||||||
IndexableSet<TypeId> types
|
|
||||||
)
|
|
||||||
{
|
|
||||||
World = world;
|
|
||||||
Types = types;
|
|
||||||
ComponentIndex = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool MoveNext()
|
|
||||||
{
|
|
||||||
ComponentIndex += 1;
|
|
||||||
return ComponentIndex < Types.Count;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Type Current => ComponentTypeIdToType[Types[ComponentIndex]];
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!IsDisposed)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
foreach (var componentStorage in ComponentIndex)
|
|
||||||
{
|
|
||||||
componentStorage.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var relationStorage in RelationIndex)
|
|
||||||
{
|
|
||||||
relationStorage.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var messageStorage in MessageIndex)
|
|
||||||
{
|
|
||||||
messageStorage.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentSet in EntityComponentIndex)
|
|
||||||
{
|
|
||||||
componentSet.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var relationSet in EntityRelationIndex)
|
|
||||||
{
|
|
||||||
relationSet.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var filter in FilterIndex.Values)
|
|
||||||
{
|
|
||||||
filter.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityIdAssigner.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
IsDisposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
||||||
// ~World()
|
|
||||||
// {
|
|
||||||
// // 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
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue