MoonTools.ECS/src/World.cs

521 lines
13 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2023-11-03 19:40:26 +00:00
using System.Runtime.CompilerServices;
using MoonTools.ECS.Collections;
2023-11-21 07:13:04 +00:00
namespace MoonTools.ECS;
public class World : IDisposable
2022-03-05 02:01:44 +00:00
{
2023-11-21 07:13:04 +00:00
// Get TypeId from a Type
private readonly Dictionary<Type, TypeId> TypeToId = new Dictionary<Type, TypeId>();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
#if DEBUG
private Dictionary<TypeId, Type> IdToType = new Dictionary<TypeId, Type>();
#endif
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// Get element size from a TypeId
private readonly Dictionary<TypeId, int> ElementSizes = new Dictionary<TypeId, int>();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// Filters
internal readonly Dictionary<FilterSignature, Filter> FilterIndex = new Dictionary<FilterSignature, Filter>();
private readonly Dictionary<TypeId, List<Filter>> TypeToFilter = new Dictionary<TypeId, List<Filter>>();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// TODO: can we make the tag an native array of chars at some point?
internal Dictionary<Entity, string> EntityTags = new Dictionary<Entity, string>();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// Relation Storages
internal Dictionary<TypeId, RelationStorage> RelationIndex = new Dictionary<TypeId, RelationStorage>();
internal Dictionary<Entity, IndexableSet<TypeId>> EntityRelationIndex = new Dictionary<Entity, IndexableSet<TypeId>>();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// Message Storages
private Dictionary<TypeId, MessageStorage> MessageIndex = new Dictionary<TypeId, MessageStorage>();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public FilterBuilder FilterBuilder => new FilterBuilder(this);
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
internal readonly Dictionary<TypeId, ComponentStorage> ComponentIndex = new Dictionary<TypeId, ComponentStorage>();
internal Dictionary<Entity, IndexableSet<TypeId>> EntityComponentIndex = new Dictionary<Entity, IndexableSet<TypeId>>();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
internal IdAssigner EntityIdAssigner = new IdAssigner();
private IdAssigner TypeIdAssigner = new IdAssigner();
2023-11-21 07:13:04 +00:00
private bool IsDisposed;
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
internal TypeId GetTypeId<T>() where T : unmanaged
{
if (TypeToId.ContainsKey(typeof(T)))
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
return TypeToId[typeof(T)];
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
var typeId = new TypeId(TypeIdAssigner.Assign());
TypeToId.Add(typeof(T), typeId);
ElementSizes.Add(typeId, Unsafe.SizeOf<T>());
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
#if DEBUG
IdToType.Add(typeId, typeof(T));
#endif
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
return typeId;
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
internal TypeId GetComponentTypeId<T>() where T : unmanaged
{
var typeId = GetTypeId<T>();
if (ComponentIndex.TryGetValue(typeId, out var componentStorage))
2023-11-08 01:46:44 +00:00
{
return typeId;
}
2023-11-21 07:13:04 +00:00
componentStorage = new ComponentStorage(typeId, ElementSizes[typeId]);
ComponentIndex.Add(typeId, componentStorage);
TypeToFilter.Add(typeId, new List<Filter>());
return typeId;
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ComponentStorage GetComponentStorage<T>() where T : unmanaged
{
var typeId = GetTypeId<T>();
if (ComponentIndex.TryGetValue(typeId, out var componentStorage))
{
2023-11-03 19:40:26 +00:00
return componentStorage;
}
2023-11-21 07:13:04 +00:00
componentStorage = new ComponentStorage(typeId, ElementSizes[typeId]);
ComponentIndex.Add(typeId, componentStorage);
TypeToFilter.Add(typeId, new List<Filter>());
return componentStorage;
}
2023-11-08 01:46:44 +00:00
2023-11-21 07:13:04 +00:00
// FILTERS
2023-11-08 01:46:44 +00:00
2023-11-21 07:13:04 +00:00
internal Filter GetFilter(FilterSignature signature)
{
if (!FilterIndex.TryGetValue(signature, out var filter))
{
filter = new Filter(this, signature);
2023-11-08 01:46:44 +00:00
2023-11-21 07:13:04 +00:00
foreach (var typeId in signature.Included)
{
TypeToFilter[typeId].Add(filter);
}
2023-11-08 18:17:22 +00:00
2023-11-21 07:13:04 +00:00
foreach (var typeId in signature.Excluded)
{
TypeToFilter[typeId].Add(filter);
2023-11-08 01:46:44 +00:00
}
2023-11-21 07:13:04 +00:00
FilterIndex.Add(signature, filter);
}
2022-04-08 05:52:03 +00:00
2023-11-21 07:13:04 +00:00
return filter;
}
// ENTITIES
public Entity CreateEntity(string tag = "")
{
var entity = new Entity(EntityIdAssigner.Assign());
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
if (!EntityComponentIndex.ContainsKey(entity))
2022-04-08 05:52:03 +00:00
{
2023-11-21 07:13:04 +00:00
EntityRelationIndex.Add(entity, new IndexableSet<TypeId>());
EntityComponentIndex.Add(entity, new IndexableSet<TypeId>());
}
2023-11-03 22:39:30 +00:00
2023-11-21 07:13:04 +00:00
EntityTags[entity] = tag;
2023-11-03 22:39:30 +00:00
2023-11-21 07:13:04 +00:00
return entity;
}
2023-11-03 22:39:30 +00:00
2023-11-21 07:13:04 +00:00
public void Tag(Entity entity, string tag)
{
EntityTags[entity] = tag;
}
2023-11-21 07:13:04 +00:00
public string GetTag(Entity entity)
{
return EntityTags[entity];
}
2023-11-21 07:13:04 +00:00
public void Destroy(in Entity entity)
{
// remove all components from storages
foreach (var componentTypeIndex in EntityComponentIndex[entity])
{
2023-11-21 07:13:04 +00:00
var componentStorage = ComponentIndex[componentTypeIndex];
componentStorage.Remove(entity);
2022-04-08 05:52:03 +00:00
2023-11-21 07:13:04 +00:00
foreach (var filter in TypeToFilter[componentTypeIndex])
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
filter.RemoveEntity(entity);
2023-11-03 19:40:26 +00:00
}
2023-11-21 07:13:04 +00:00
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// remove all relations from storage
foreach (var relationTypeIndex in EntityRelationIndex[entity])
{
var relationStorage = RelationIndex[relationTypeIndex];
relationStorage.RemoveEntity(entity);
}
2023-01-27 00:34:15 +00:00
2023-11-21 07:13:04 +00:00
EntityComponentIndex[entity].Clear();
EntityRelationIndex[entity].Clear();
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// recycle ID
EntityIdAssigner.Unassign(entity.ID);
}
2022-04-08 05:52:03 +00:00
2023-11-21 07:13:04 +00:00
// COMPONENTS
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public bool Has<T>(in Entity entity) where T : unmanaged
{
var storage = GetComponentStorage<T>();
return storage.Has(entity);
}
2023-11-21 07:13:04 +00:00
internal bool Has(in Entity entity, in TypeId typeId)
{
return EntityComponentIndex[entity].Contains(typeId);
}
2023-11-08 01:46:44 +00:00
2023-11-21 07:13:04 +00:00
public bool Some<T>() where T : unmanaged
{
var storage = GetComponentStorage<T>();
return storage.Any();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public ref T Get<T>(in Entity entity) where T : unmanaged
{
var storage = GetComponentStorage<T>();
return ref storage.Get<T>(entity);
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public ref T GetSingleton<T>() where T : unmanaged
{
var storage = GetComponentStorage<T>();
return ref storage.GetFirst<T>();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public Entity GetSingletonEntity<T>() where T : unmanaged
{
var storage = GetComponentStorage<T>();
return storage.FirstEntity();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public void Set<T>(in Entity entity, in T component) where T : unmanaged
{
var componentStorage = GetComponentStorage<T>();
if (!componentStorage.Set(entity, component))
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
EntityComponentIndex[entity].Add(componentStorage.TypeId);
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
foreach (var filter in TypeToFilter[componentStorage.TypeId])
{
2023-11-21 07:13:04 +00:00
filter.Check(entity);
}
2022-04-08 05:52:03 +00:00
}
2023-11-21 07:13:04 +00:00
}
public void Remove<T>(in Entity entity) where T : unmanaged
{
var componentStorage = GetComponentStorage<T>();
2022-04-08 05:52:03 +00:00
2023-11-21 07:13:04 +00:00
if (componentStorage.Remove(entity))
2022-04-08 05:52:03 +00:00
{
2023-11-21 07:13:04 +00:00
EntityComponentIndex[entity].Remove(componentStorage.TypeId);
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
foreach (var filter in TypeToFilter[componentStorage.TypeId])
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
filter.Check(entity);
2023-11-03 22:39:30 +00:00
}
2023-11-03 19:40:26 +00:00
}
2023-11-21 07:13:04 +00:00
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// RELATIONS
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
private RelationStorage RegisterRelationType(TypeId typeId)
{
var relationStorage = new RelationStorage(ElementSizes[typeId]);
RelationIndex.Add(typeId, relationStorage);
return relationStorage;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private RelationStorage GetRelationStorage<T>() where T : unmanaged
{
var typeId = GetTypeId<T>();
if (RelationIndex.TryGetValue(typeId, out var relationStorage))
2023-11-03 19:40:26 +00:00
{
return relationStorage;
}
2023-11-21 07:13:04 +00:00
return RegisterRelationType(typeId);
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
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[entityA].Add(TypeToId[typeof(T)]);
EntityRelationIndex[entityB].Add(TypeToId[typeof(T)]);
}
2023-11-21 07:13:04 +00:00
public void Unrelate<T>(in Entity entityA, in Entity entityB) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
relationStorage.Remove(entityA, entityB);
}
2023-11-21 07:13:04 +00:00
public void UnrelateAll<T>(in Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
relationStorage.RemoveEntity(entity);
}
2023-11-21 07:13:04 +00:00
public bool Related<T>(in Entity entityA, in Entity entityB) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.Has(entityA, entityB);
}
2023-02-09 18:50:25 +00:00
2023-11-21 07:13:04 +00:00
public T GetRelationData<T>(in Entity entityA, in Entity entityB) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.Get<T>(entityA, entityB);
}
2023-11-21 07:13:04 +00:00
public ReverseSpanEnumerator<(Entity, Entity)> Relations<T>() where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.All();
}
2023-11-21 07:13:04 +00:00
public ReverseSpanEnumerator<Entity> OutRelations<T>(Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.OutRelations(entity);
}
2023-11-21 07:13:04 +00:00
public Entity OutRelationSingleton<T>(in Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.OutFirst(entity);
}
2023-11-21 07:13:04 +00:00
public bool HasOutRelation<T>(in Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.HasOutRelation(entity);
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public int OutRelationCount<T>(in Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.OutRelationCount(entity);
}
2023-11-21 07:13:04 +00:00
public Entity NthOutRelation<T>(in Entity entity, int n) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.OutNth(entity, n);
}
2023-07-21 21:41:53 +00:00
2023-11-21 07:13:04 +00:00
public ReverseSpanEnumerator<Entity> InRelations<T>(Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.InRelations(entity);
}
2023-11-21 07:13:04 +00:00
public Entity InRelationSingleton<T>(in Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.InFirst(entity);
}
2023-11-21 07:13:04 +00:00
public bool HasInRelation<T>(in Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.HasInRelation(entity);
}
2023-11-21 07:13:04 +00:00
public int InRelationCount<T>(in Entity entity) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.InRelationCount(entity);
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public Entity NthInRelation<T>(in Entity entity, int n) where T : unmanaged
{
var relationStorage = GetRelationStorage<T>();
return relationStorage.InNth(entity, n);
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// MESSAGES
private TypeId GetMessageTypeId<T>() where T : unmanaged
{
var typeId = GetTypeId<T>();
if (!MessageIndex.ContainsKey(typeId))
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
MessageIndex.Add(typeId, new MessageStorage(Unsafe.SizeOf<T>()));
2023-11-03 19:40:26 +00:00
}
2023-11-21 07:13:04 +00:00
return typeId;
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public void Send<T>(in T message) where T : unmanaged
{
var typeId = GetMessageTypeId<T>();
MessageIndex[typeId].Add(message);
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public bool SomeMessage<T>() where T : unmanaged
{
var typeId = GetMessageTypeId<T>();
return MessageIndex[typeId].Some();
}
2023-11-21 07:13:04 +00:00
public ReadOnlySpan<T> ReadMessages<T>() where T : unmanaged
{
var typeId = GetMessageTypeId<T>();
return MessageIndex[typeId].All<T>();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public T ReadMessage<T>() where T : unmanaged
{
var typeId = GetMessageTypeId<T>();
return MessageIndex[typeId].First<T>();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public void ClearMessages<T>() where T : unmanaged
{
var typeId = GetMessageTypeId<T>();
MessageIndex[typeId].Clear();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// TODO: temporary component storage?
public void FinishUpdate()
{
foreach (var (_, messageStorage) in MessageIndex)
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
messageStorage.Clear();
2023-11-03 19:40:26 +00:00
}
2023-11-21 07:13:04 +00:00
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
// DEBUG
// NOTE: these methods are very inefficient
// they should only be used in debugging contexts!!
#if DEBUG
public ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity)
{
return new ComponentTypeEnumerator(this, EntityComponentIndex[entity]);
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
public IEnumerable<Entity> Debug_GetEntities(Type componentType)
{
var storage = ComponentIndex[TypeToId[componentType]];
return storage.Debug_GetEntities();
}
2023-11-15 23:10:56 +00:00
2023-11-21 07:13:04 +00:00
public IEnumerable<Type> Debug_SearchComponentType(string typeString)
{
foreach (var type in TypeToId.Keys)
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
if (type.ToString().ToLower().Contains(typeString.ToLower()))
{
2023-11-21 07:13:04 +00:00
yield return type;
}
2023-11-03 19:40:26 +00:00
}
2023-11-21 07:13:04 +00:00
}
public ref struct ComponentTypeEnumerator
{
private World World;
private IndexableSet<TypeId> Types;
private int ComponentIndex;
public ComponentTypeEnumerator GetEnumerator() => this;
2023-11-21 07:13:04 +00:00
internal ComponentTypeEnumerator(
World world,
IndexableSet<TypeId> types
)
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
World = world;
Types = types;
ComponentIndex = -1;
2023-11-03 19:40:26 +00:00
}
2023-11-21 07:13:04 +00:00
public bool MoveNext()
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
ComponentIndex += 1;
return ComponentIndex < Types.Count;
2023-11-03 19:40:26 +00:00
}
2023-11-21 07:13:04 +00:00
public Type Current => World.IdToType[Types[ComponentIndex]];
}
#endif
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
if (disposing)
2023-11-03 19:40:26 +00:00
{
2023-11-21 07:13:04 +00:00
foreach (var componentStorage in ComponentIndex.Values)
{
2023-11-21 07:13:04 +00:00
componentStorage.Dispose();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
foreach (var relationStorage in RelationIndex.Values)
{
relationStorage.Dispose();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
foreach (var messageStorage in MessageIndex.Values)
{
messageStorage.Dispose();
}
2023-11-03 19:40:26 +00:00
2023-11-21 07:13:04 +00:00
foreach (var typeSet in EntityComponentIndex.Values)
{
typeSet.Dispose();
}
2023-11-21 07:13:04 +00:00
foreach (var typeSet in EntityRelationIndex.Values)
{
typeSet.Dispose();
}
2023-11-08 01:46:44 +00:00
2023-11-21 07:13:04 +00:00
foreach (var filter in FilterIndex.Values)
2023-11-08 01:46:44 +00:00
{
2023-11-21 07:13:04 +00:00
filter.Dispose();
2023-11-08 01:46:44 +00:00
}
2023-11-21 07:13:04 +00:00
EntityIdAssigner.Dispose();
TypeIdAssigner.Dispose();
2023-11-08 01:46:44 +00:00
}
2023-11-21 07:13:04 +00:00
IsDisposed = true;
2023-11-08 01:46:44 +00:00
}
2023-11-21 07:13:04 +00:00
}
2023-11-08 01:46:44 +00:00
2023-11-21 07:13:04 +00:00
// // 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);
// }
2023-11-08 01:46:44 +00:00
2023-11-21 07:13:04 +00:00
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
2022-03-05 02:01:44 +00:00
}
}