MoonTools.ECS/src/World.cs

109 lines
3.1 KiB
C#
Raw Normal View History

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
{
internal readonly TypeIndices ComponentTypeIndices = new TypeIndices();
internal readonly 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);
internal readonly TemplateStorage TemplateStorage = new TemplateStorage();
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();
}
public void Set<TComponent>(Entity entity, in TComponent component) where TComponent : unmanaged
2022-04-08 05:52:03 +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);
if (EntityStorage.SetComponent(entity.ID, ComponentTypeIndices.GetIndex<TComponent>()))
{
FilterStorage.Check<TComponent>(entity.ID);
}
2022-04-08 05:52:03 +00:00
}
public Template CreateTemplate()
2022-04-08 05:52:03 +00:00
{
return TemplateStorage.Create();
2022-04-08 05:52:03 +00:00
}
public void Set<TComponent>(in Template template, in TComponent component) where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
var componentTypeIndex = ComponentTypeIndices.GetIndex<TComponent>();
TemplateStorage.SetComponent(template.ID, componentTypeIndex);
TemplateComponentDepot.Set(template.ID, component);
ComponentDepot.Register<TComponent>(componentTypeIndex);
}
public unsafe Entity Instantiate(in Template template)
{
var entity = EntityStorage.Create();
foreach (var componentTypeIndex in TemplateStorage.ComponentTypeIndices(template.ID))
{
EntityStorage.SetComponent(entity.ID, componentTypeIndex);
FilterStorage.Check(entity.ID, componentTypeIndex);
ComponentDepot.Set(entity.ID, componentTypeIndex, TemplateComponentDepot.UntypedGet(template.ID, componentTypeIndex));
}
return entity;
2022-04-08 05:52:03 +00:00
}
public void Send<TMessage>(in TMessage message) where TMessage : unmanaged
{
MessageDepot.Add(message);
}
public void Destroy(in Entity entity)
{
foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID))
{
ComponentDepot.Remove(entity.ID, componentTypeIndex);
FilterStorage.RemoveEntity(entity.ID, componentTypeIndex);
}
foreach (var relationTypeIndex in EntityStorage.RelationTypeIndices(entity.ID))
{
RelationDepot.UnrelateAll(entity.ID, relationTypeIndex);
}
EntityStorage.Destroy(entity);
}
public void FinishUpdate()
{
MessageDepot.Clear();
}
public Snapshot CreateSnapshot()
{
return new Snapshot(this);
}
2022-03-05 02:01:44 +00:00
}
}