From 229a0651bdaac5918b4550866cdca9122bf0d78b Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 7 Mar 2022 10:56:53 -0800 Subject: [PATCH] WorldBuilder --- src/System.cs | 10 ++++---- src/World.cs | 34 +++++++++++++------------- src/WorldBuilder.cs | 59 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 src/WorldBuilder.cs diff --git a/src/System.cs b/src/System.cs index d9d71ec..125de07 100644 --- a/src/System.cs +++ b/src/System.cs @@ -5,11 +5,6 @@ public abstract class System : EntityComponentReader internal MessageDepot MessageDepot; public FilterBuilder FilterBuilder => new FilterBuilder(ComponentDepot); - public System(World world) - { - world.AddSystem(this); - } - internal void RegisterMessageDepot(MessageDepot messageDepot) { MessageDepot = messageDepot; @@ -42,6 +37,11 @@ public abstract class System : EntityComponentReader return MessageDepot.Some(); } + protected void Send(in TMessage message) where TMessage : struct + { + MessageDepot.Add(message); + } + protected void Destroy(in Entity entity) { ComponentDepot.OnEntityDestroy(entity.ID); diff --git a/src/World.cs b/src/World.cs index 8e3fa58..2eddb2a 100644 --- a/src/World.cs +++ b/src/World.cs @@ -2,25 +2,25 @@ public class World { - private readonly List Systems = new List(); - private readonly List Renderers = new List(); - private EntityStorage EntityStorage { get; } = new EntityStorage(); - private ComponentDepot ComponentDepot { get; } = new ComponentDepot(); - private MessageDepot MessageDepot { get; } = new MessageDepot(); + private readonly List Systems; + private readonly List Renderers; + private readonly EntityStorage EntityStorage; + private readonly ComponentDepot ComponentDepot; + private MessageDepot MessageDepot; - internal void AddSystem(System system) + internal World( + List systems, + List renderers, + ComponentDepot componentDepot, + EntityStorage entityStorage, + MessageDepot messageDepot + ) { - system.RegisterEntityStorage(EntityStorage); - system.RegisterComponentDepot(ComponentDepot); - system.RegisterMessageDepot(MessageDepot); - Systems.Add(system); - } - - internal void AddRenderer(Renderer renderer) - { - renderer.RegisterEntityStorage(EntityStorage); - renderer.RegisterComponentDepot(ComponentDepot); - Renderers.Add(renderer); + Systems = systems; + Renderers = renderers; + ComponentDepot = componentDepot; + EntityStorage = entityStorage; + MessageDepot = messageDepot; } public void Update(TimeSpan delta) diff --git a/src/WorldBuilder.cs b/src/WorldBuilder.cs new file mode 100644 index 0000000..d184f1c --- /dev/null +++ b/src/WorldBuilder.cs @@ -0,0 +1,59 @@ +namespace MoonTools.ECS; + +public class WorldBuilder +{ + private ComponentDepot componentDepot; + private EntityStorage entityStorage; + private MessageDepot messageDepot; + + private readonly List systems = new List(); + private readonly List renderers = new List(); + + 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(Entity entity, in TComponent component) where TComponent : struct + { + componentDepot.Set(entity.ID, component); + } + + public void Send(in TMessage message) where TMessage : struct + { + messageDepot.Add(message); + } + + public World Build() + { + return new World( + systems, + renderers, + componentDepot, + entityStorage, + messageDepot + ); + } +}