2023-01-10 00:41:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace MoonTools.ECS
|
2022-03-05 02:01:44 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public class World
|
2022-03-05 02:01:44 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
internal readonly TypeIndices ComponentTypeIndices = new TypeIndices();
|
|
|
|
|
internal readonly TypeIndices RelationTypeIndices = new TypeIndices();
|
2022-05-03 04:51:11 +00:00
|
|
|
|
internal readonly EntityStorage EntityStorage = new EntityStorage();
|
2023-01-10 00:41:00 +00:00
|
|
|
|
internal readonly ComponentDepot ComponentDepot;
|
2022-05-03 04:51:11 +00:00
|
|
|
|
internal readonly MessageDepot MessageDepot = new MessageDepot();
|
2023-01-10 00:41:00 +00:00
|
|
|
|
internal readonly RelationDepot RelationDepot;
|
|
|
|
|
internal readonly FilterStorage FilterStorage;
|
|
|
|
|
public FilterBuilder FilterBuilder => new FilterBuilder(FilterStorage, ComponentTypeIndices);
|
|
|
|
|
|
|
|
|
|
internal readonly ComponentDepot TemplateComponentDepot;
|
|
|
|
|
|
|
|
|
|
public World()
|
|
|
|
|
{
|
|
|
|
|
ComponentDepot = new ComponentDepot(ComponentTypeIndices);
|
|
|
|
|
RelationDepot = new RelationDepot(RelationTypeIndices);
|
|
|
|
|
FilterStorage = new FilterStorage(EntityStorage, ComponentTypeIndices);
|
|
|
|
|
TemplateComponentDepot = new ComponentDepot(ComponentTypeIndices);
|
|
|
|
|
}
|
2022-04-08 05:52:03 +00:00
|
|
|
|
|
|
|
|
|
public Entity CreateEntity()
|
|
|
|
|
{
|
|
|
|
|
return EntityStorage.Create();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
public void Set<TComponent>(Entity entity, in TComponent component) where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
// check for use after destroy
|
|
|
|
|
if (!EntityStorage.Exists(entity))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("This entity is not valid!");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-01-27 00:34:15 +00:00
|
|
|
|
ComponentDepot.Set<TComponent>(entity.ID, component);
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
if (EntityStorage.SetComponent(entity.ID, ComponentTypeIndices.GetIndex<TComponent>()))
|
|
|
|
|
{
|
|
|
|
|
FilterStorage.Check<TComponent>(entity.ID);
|
|
|
|
|
}
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 19:46:27 +00:00
|
|
|
|
public void Remove<TComponent>(in Entity entity) where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-05-24 19:46:27 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 19:46:27 +00:00
|
|
|
|
public void Relate<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-05-24 19:46:27 +00:00
|
|
|
|
RelationDepot.Set(entityA, entityB, relationData);
|
|
|
|
|
var relationTypeIndex = RelationTypeIndices.GetIndex<TRelationKind>();
|
|
|
|
|
EntityStorage.AddRelationKind(entityA.ID, relationTypeIndex);
|
|
|
|
|
EntityStorage.AddRelationKind(entityB.ID, relationTypeIndex);
|
2023-01-10 00:41:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 19:46:27 +00:00
|
|
|
|
public void Unrelate<TRelationKind>(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged
|
2023-01-10 00:41:00 +00:00
|
|
|
|
{
|
2023-05-24 19:46:27 +00:00
|
|
|
|
var (aEmpty, bEmpty) = RelationDepot.Remove<TRelationKind>(entityA, entityB);
|
2023-01-10 00:41:00 +00:00
|
|
|
|
|
2023-05-24 19:46:27 +00:00
|
|
|
|
if (aEmpty)
|
2023-01-10 00:41:00 +00:00
|
|
|
|
{
|
2023-05-24 19:46:27 +00:00
|
|
|
|
EntityStorage.RemoveRelation(entityA.ID, RelationTypeIndices.GetIndex<TRelationKind>());
|
2023-01-10 00:41:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 19:46:27 +00:00
|
|
|
|
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>());
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-05-03 04:51:11 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public void Send<TMessage>(in TMessage message) where TMessage : unmanaged
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
MessageDepot.Add(message);
|
2022-05-03 04:51:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 19:46:27 +00:00
|
|
|
|
public void Send<TMessage>(in Entity entity, in TMessage message) where TMessage : unmanaged
|
2023-02-09 18:50:25 +00:00
|
|
|
|
{
|
2023-05-24 19:46:27 +00:00
|
|
|
|
MessageDepot.Add(entity.ID, message);
|
2023-02-09 18:50:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public void Destroy(in Entity entity)
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID))
|
|
|
|
|
{
|
2023-05-19 20:24:26 +00:00
|
|
|
|
// Run filter storage update first so that the entity state is still valid in the remove callback.
|
2023-01-10 00:41:00 +00:00
|
|
|
|
FilterStorage.RemoveEntity(entity.ID, componentTypeIndex);
|
2023-05-19 20:24:26 +00:00
|
|
|
|
ComponentDepot.Remove(entity.ID, componentTypeIndex);
|
2023-01-10 00:41:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var relationTypeIndex in EntityStorage.RelationTypeIndices(entity.ID))
|
|
|
|
|
{
|
|
|
|
|
RelationDepot.UnrelateAll(entity.ID, relationTypeIndex);
|
2023-02-09 23:15:55 +00:00
|
|
|
|
EntityStorage.RemoveRelation(entity.ID, relationTypeIndex);
|
2023-01-10 00:41:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EntityStorage.Destroy(entity);
|
2022-05-03 04:51:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
|
|
|
|
|
public void FinishUpdate()
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
MessageDepot.Clear();
|
2022-05-03 04:51:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public Snapshot CreateSnapshot()
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
return new Snapshot(this);
|
2022-05-03 04:51:11 +00:00
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|