WorldBuilder

pull/1/head
cosmonaut 2022-03-07 10:56:53 -08:00
parent b27cbcb50a
commit 229a0651bd
3 changed files with 81 additions and 22 deletions

View File

@ -5,11 +5,6 @@ public abstract class System : EntityComponentReader
internal MessageDepot MessageDepot; internal MessageDepot MessageDepot;
public FilterBuilder FilterBuilder => new FilterBuilder(ComponentDepot); public FilterBuilder FilterBuilder => new FilterBuilder(ComponentDepot);
public System(World world)
{
world.AddSystem(this);
}
internal void RegisterMessageDepot(MessageDepot messageDepot) internal void RegisterMessageDepot(MessageDepot messageDepot)
{ {
MessageDepot = messageDepot; MessageDepot = messageDepot;
@ -42,6 +37,11 @@ public abstract class System : EntityComponentReader
return MessageDepot.Some<TMessage>(); return MessageDepot.Some<TMessage>();
} }
protected void Send<TMessage>(in TMessage message) where TMessage : struct
{
MessageDepot.Add(message);
}
protected void Destroy(in Entity entity) protected void Destroy(in Entity entity)
{ {
ComponentDepot.OnEntityDestroy(entity.ID); ComponentDepot.OnEntityDestroy(entity.ID);

View File

@ -2,25 +2,25 @@
public class World public class World
{ {
private readonly List<System> Systems = new List<System>(); private readonly List<System> Systems;
private readonly List<Renderer> Renderers = new List<Renderer>(); private readonly List<Renderer> Renderers;
private EntityStorage EntityStorage { get; } = new EntityStorage(); private readonly EntityStorage EntityStorage;
private ComponentDepot ComponentDepot { get; } = new ComponentDepot(); private readonly ComponentDepot ComponentDepot;
private MessageDepot MessageDepot { get; } = new MessageDepot(); private MessageDepot MessageDepot;
internal void AddSystem(System system) internal World(
List<System> systems,
List<Renderer> renderers,
ComponentDepot componentDepot,
EntityStorage entityStorage,
MessageDepot messageDepot
)
{ {
system.RegisterEntityStorage(EntityStorage); Systems = systems;
system.RegisterComponentDepot(ComponentDepot); Renderers = renderers;
system.RegisterMessageDepot(MessageDepot); ComponentDepot = componentDepot;
Systems.Add(system); EntityStorage = entityStorage;
} MessageDepot = messageDepot;
internal void AddRenderer(Renderer renderer)
{
renderer.RegisterEntityStorage(EntityStorage);
renderer.RegisterComponentDepot(ComponentDepot);
Renderers.Add(renderer);
} }
public void Update(TimeSpan delta) public void Update(TimeSpan delta)

59
src/WorldBuilder.cs Normal file
View File

@ -0,0 +1,59 @@
namespace MoonTools.ECS;
public class WorldBuilder
{
private ComponentDepot componentDepot;
private EntityStorage entityStorage;
private MessageDepot messageDepot;
private readonly List<System> systems = new List<System>();
private readonly List<Renderer> renderers = new List<Renderer>();
public WorldBuilder()
{
componentDepot = new ComponentDepot();
entityStorage = new EntityStorage();
messageDepot = new MessageDepot();
}
public void AddSystem(System system)
{
system.RegisterEntityStorage(entityStorage);
system.RegisterComponentDepot(componentDepot);
system.RegisterMessageDepot(messageDepot);
systems.Add(system);
}
public void AddRenderer(Renderer renderer)
{
renderer.RegisterEntityStorage(entityStorage);
renderer.RegisterComponentDepot(componentDepot);
renderers.Add(renderer);
}
public Entity CreateEntity()
{
return entityStorage.Create();
}
public void Set<TComponent>(Entity entity, in TComponent component) where TComponent : struct
{
componentDepot.Set(entity.ID, component);
}
public void Send<TMessage>(in TMessage message) where TMessage : struct
{
messageDepot.Add(message);
}
public World Build()
{
return new World(
systems,
renderers,
componentDepot,
entityStorage,
messageDepot
);
}
}