MoonTools.ECS/src/World.cs

82 lines
2.3 KiB
C#
Raw Normal View History

2022-04-08 05:52:03 +00:00
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
{
2022-12-03 07:43:54 +00:00
internal readonly TypeIndices ComponentTypeIndices = new TypeIndices();
internal readonly TypeIndices RelationTypeIndices = new TypeIndices();
internal readonly EntityStorage EntityStorage = new EntityStorage();
2022-12-03 07:43:54 +00:00
internal readonly ComponentDepot ComponentDepot;
internal readonly MessageDepot MessageDepot = new MessageDepot();
2022-12-03 07:43:54 +00:00
internal readonly RelationDepot RelationDepot;
internal readonly FilterStorage FilterStorage;
internal readonly TemplateStorage TemplateStorage = new TemplateStorage();
2022-12-06 01:46:18 +00:00
internal readonly ComponentDepot TemplateComponentDepot;
2022-12-03 07:43:54 +00:00
public World()
{
ComponentDepot = new ComponentDepot(ComponentTypeIndices);
RelationDepot = new RelationDepot(RelationTypeIndices);
FilterStorage = new FilterStorage(EntityStorage, ComponentTypeIndices);
2022-12-06 01:46:18 +00:00
TemplateComponentDepot = new ComponentDepot(ComponentTypeIndices);
2022-12-03 07:43:54 +00:00
}
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
{
2022-12-03 07:43:54 +00:00
if (EntityStorage.SetComponent(entity.ID, ComponentTypeIndices.GetIndex<TComponent>()))
{
FilterStorage.Check<TComponent>(entity.ID);
}
ComponentDepot.Set<TComponent>(entity.ID, component);
2022-04-08 05:52:03 +00:00
}
2022-12-03 07:43:54 +00:00
public Template CreateTemplate()
2022-04-08 05:52:03 +00:00
{
2022-12-03 07:43:54 +00:00
return TemplateStorage.Create();
2022-04-08 05:52:03 +00:00
}
2022-12-06 01:46:18 +00:00
public void Set<TComponent>(in Template template, in TComponent component) where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
2022-12-06 01:46:18 +00:00
TemplateStorage.SetComponent(template.ID, ComponentTypeIndices.GetIndex<TComponent>());
2022-12-03 07:43:54 +00:00
TemplateComponentDepot.Set(template.ID, component);
2022-04-08 05:52:03 +00:00
}
2022-12-06 01:46:18 +00:00
// TODO: TEST ME!!!
public Entity Instantiate(in Template template)
{
2022-12-03 07:43:54 +00:00
var entity = EntityStorage.Create();
2022-12-06 01:46:18 +00:00
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));
}
2022-12-03 07:43:54 +00:00
return entity;
}
2022-12-03 07:43:54 +00:00
public void Send<TMessage>(in TMessage message) where TMessage : unmanaged
{
2022-12-03 07:43:54 +00:00
MessageDepot.Add(message);
}
2022-12-03 07:43:54 +00:00
public void FinishUpdate()
{
2022-12-03 07:43:54 +00:00
MessageDepot.Clear();
}
2022-12-03 07:43:54 +00:00
public WorldState CreateState()
{
2022-12-03 07:43:54 +00:00
return new WorldState();
}
2022-03-05 02:01:44 +00:00
}
}