MoonTools.ECS/src/World.cs

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