From a2eda72620fa2eeb00b4dcd5e2df4a8955fdd4b7 Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Wed, 27 Jun 2018 21:19:20 +1000 Subject: [PATCH] rejigged the base systems classes and interfaces --- .../ComponentManager.cs | 12 ++++- .../EntityManager.cs | 12 ++++- .../MonoGame.Extended.Entities/EntityWorld.cs | 39 ++++++--------- .../Systems/EntityDrawSystem.cs | 7 +-- .../Systems/EntityProcessingSystem.cs | 2 +- .../Systems/EntitySystem.cs | 47 +++++++++++++++++++ .../Systems/EntityUpdateSystem.cs | 42 ++--------------- .../Systems/IDrawSystem.cs | 9 ++++ .../Systems/ISystem.cs | 9 ++++ .../Systems/IUpdateSystem.cs | 9 ++++ .../Systems/UpdateSystem.cs | 16 ------- 11 files changed, 115 insertions(+), 89 deletions(-) create mode 100644 Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs create mode 100644 Source/MonoGame.Extended.Entities/Systems/IDrawSystem.cs create mode 100644 Source/MonoGame.Extended.Entities/Systems/ISystem.cs create mode 100644 Source/MonoGame.Extended.Entities/Systems/IUpdateSystem.cs delete mode 100644 Source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs diff --git a/Source/MonoGame.Extended.Entities/ComponentManager.cs b/Source/MonoGame.Extended.Entities/ComponentManager.cs index 4d099b3d..1764258c 100644 --- a/Source/MonoGame.Extended.Entities/ComponentManager.cs +++ b/Source/MonoGame.Extended.Entities/ComponentManager.cs @@ -12,13 +12,21 @@ namespace MonoGame.Extended.Entities ComponentMapper GetMapper() where T : class; } - public class ComponentManager : UpdateSystem, IComponentMapperService + public class ComponentManager : IUpdateSystem, IComponentMapperService { public ComponentManager() { _componentMappers = new Bag(); _componentTypes = new Dictionary(); } + + public void Dispose() + { + } + + public void Initialize(EntityWorld world) + { + } private readonly Bag _componentMappers; private readonly Dictionary _componentTypes; @@ -76,7 +84,7 @@ namespace MonoGame.Extended.Entities return componentBits; } - public override void Update(GameTime gameTime) + public void Update(GameTime gameTime) { } } diff --git a/Source/MonoGame.Extended.Entities/EntityManager.cs b/Source/MonoGame.Extended.Entities/EntityManager.cs index 401809c2..ac033abc 100644 --- a/Source/MonoGame.Extended.Entities/EntityManager.cs +++ b/Source/MonoGame.Extended.Entities/EntityManager.cs @@ -6,7 +6,7 @@ using MonoGame.Extended.Entities.Systems; namespace MonoGame.Extended.Entities { - public class EntityManager : UpdateSystem + public class EntityManager : IUpdateSystem { private const int _defaultBagSize = 128; @@ -24,6 +24,14 @@ namespace MonoGame.Extended.Entities _entityPool = new Pool(() => new Entity(_nextId++, this, _componentManager), _defaultBagSize); } + public void Dispose() + { + } + + public void Initialize(EntityWorld world) + { + } + private readonly ComponentManager _componentManager; private int _nextId; @@ -72,7 +80,7 @@ namespace MonoGame.Extended.Entities _changedEntities.Add(entityId); } - public override void Update(GameTime gameTime) + public void Update(GameTime gameTime) { foreach (var entity in _addedEntities) { diff --git a/Source/MonoGame.Extended.Entities/EntityWorld.cs b/Source/MonoGame.Extended.Entities/EntityWorld.cs index a87b8ec0..3a63213b 100644 --- a/Source/MonoGame.Extended.Entities/EntityWorld.cs +++ b/Source/MonoGame.Extended.Entities/EntityWorld.cs @@ -6,20 +6,16 @@ namespace MonoGame.Extended.Entities { public class EntityWorld : SimpleDrawableGameComponent { - private readonly Bag _updateSystems; - private readonly Bag _drawSystems; + private readonly Bag _updateSystems; + private readonly Bag _drawSystems; public EntityWorld() { - ComponentManager = new ComponentManager(); - EntityManager = new EntityManager(ComponentManager); + _updateSystems = new Bag(); + _drawSystems = new Bag(); - _updateSystems = new Bag - { - ComponentManager, - EntityManager - }; - _drawSystems = new Bag(); + RegisterSystem(ComponentManager = new ComponentManager()); + RegisterSystem(EntityManager = new EntityManager(ComponentManager)); } public override void Dispose() @@ -39,23 +35,16 @@ namespace MonoGame.Extended.Entities public Bag AllEntities => EntityManager.Entities; // TODO: Move this to world configuration - public void RegisterSystem(UpdateSystem system) + public void RegisterSystem(ISystem system) { - switch (system) - { - case EntityDrawSystem drawSystem: - _drawSystems.Add(drawSystem); - break; - case UpdateSystem updateSystem: - _updateSystems.Add(updateSystem); - break; - } + // ReSharper disable once ConvertIfStatementToSwitchStatement + if (system is IUpdateSystem updateSystem) + _updateSystems.Add(updateSystem); - if (system is EntityUpdateSystem entitySystem) - { - entitySystem.World = this; - entitySystem.Initialize(ComponentManager); - } + if (system is IDrawSystem drawSystem) + _drawSystems.Add(drawSystem); + + system.Initialize(this); } public Entity GetEntity(int entityId) diff --git a/Source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs b/Source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs index 362a46c7..5a0d6b92 100644 --- a/Source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs +++ b/Source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs @@ -2,18 +2,13 @@ namespace MonoGame.Extended.Entities.Systems { - public abstract class EntityDrawSystem : EntityUpdateSystem + public abstract class EntityDrawSystem : EntitySystem, IDrawSystem { protected EntityDrawSystem(AspectBuilder aspect) : base(aspect) { } - public sealed override void Update(GameTime gameTime) - { - Draw(gameTime); - } - public abstract void Draw(GameTime gameTime); } } \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs b/Source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs index 5cad55b3..c4d93393 100644 --- a/Source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs +++ b/Source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs @@ -4,7 +4,7 @@ namespace MonoGame.Extended.Entities.Systems { public abstract class EntityProcessingSystem : EntityUpdateSystem { - protected EntityProcessingSystem(AspectBuilder aspectBuilder) + protected EntityProcessingSystem(AspectBuilder aspectBuilder) : base(aspectBuilder) { } diff --git a/Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs b/Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs new file mode 100644 index 00000000..b2bd16dc --- /dev/null +++ b/Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs @@ -0,0 +1,47 @@ +using MonoGame.Extended.Collections; + +namespace MonoGame.Extended.Entities.Systems +{ + public abstract class EntitySystem : ISystem + { + protected EntitySystem(AspectBuilder aspectBuilder) + { + _aspectBuilder = aspectBuilder; + } + + public void Dispose() + { + if (World != null) + { + World.EntityManager.EntityAdded -= OnEntityAdded; + World.EntityManager.EntityRemoved -= OnEntityRemoved; + } + } + + public virtual void Initialize(EntityWorld world) + { + World = world; + + var aspect = _aspectBuilder.Build(World.ComponentManager); + _subscription = new EntitySubscription(World.EntityManager, aspect); + World.EntityManager.EntityAdded += OnEntityAdded; + World.EntityManager.EntityRemoved += OnEntityRemoved; + World.EntityManager.EntityChanged += OnEntityChanged; + + Initialize(world.ComponentManager); + } + + public abstract void Initialize(IComponentMapperService mapperService); + + private readonly AspectBuilder _aspectBuilder; + private EntitySubscription _subscription; + + public EntityWorld World { get; private set; } + + protected virtual void OnEntityChanged(int entityId) { } + protected virtual void OnEntityAdded(int entityId) { } + protected virtual void OnEntityRemoved(int entityId) { } + + public Bag ActiveEntities => _subscription.ActiveEntities; + } +} \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs b/Source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs index e08cd738..97a61d52 100644 --- a/Source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs +++ b/Source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs @@ -1,46 +1,14 @@ -using MonoGame.Extended.Collections; +using Microsoft.Xna.Framework; namespace MonoGame.Extended.Entities.Systems { - public abstract class EntityUpdateSystem : UpdateSystem + public abstract class EntityUpdateSystem : EntitySystem, IUpdateSystem { - protected EntityUpdateSystem(AspectBuilder aspectBuilder) + protected EntityUpdateSystem(AspectBuilder aspectBuilder) + : base(aspectBuilder) { - _aspectBuilder = aspectBuilder; } - public override void Dispose() - { - if (_world != null) - { - _world.EntityManager.EntityAdded -= OnEntityAdded; - _world.EntityManager.EntityRemoved -= OnEntityRemoved; - } - - base.Dispose(); - } - - private readonly AspectBuilder _aspectBuilder; - private EntitySubscription _subscription; - - private EntityWorld _world; - public EntityWorld World - { - get => _world; - internal set - { - _world = value; - _subscription = new EntitySubscription(_world.EntityManager, _aspectBuilder.Build(_world.ComponentManager)); - _world.EntityManager.EntityAdded += OnEntityAdded; - _world.EntityManager.EntityRemoved += OnEntityRemoved; - } - } - - protected virtual void OnEntityAdded(int entityId) { } - protected virtual void OnEntityRemoved(int entityId) { } - - public Bag ActiveEntities => _subscription.ActiveEntities; - - public abstract void Initialize(IComponentMapperService mapperService); + public abstract void Update(GameTime gameTime); } } \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/Systems/IDrawSystem.cs b/Source/MonoGame.Extended.Entities/Systems/IDrawSystem.cs new file mode 100644 index 00000000..17ca59ce --- /dev/null +++ b/Source/MonoGame.Extended.Entities/Systems/IDrawSystem.cs @@ -0,0 +1,9 @@ +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Entities.Systems +{ + public interface IDrawSystem : ISystem + { + void Draw(GameTime gameTime); + } +} \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/Systems/ISystem.cs b/Source/MonoGame.Extended.Entities/Systems/ISystem.cs new file mode 100644 index 00000000..ac15761d --- /dev/null +++ b/Source/MonoGame.Extended.Entities/Systems/ISystem.cs @@ -0,0 +1,9 @@ +using System; + +namespace MonoGame.Extended.Entities.Systems +{ + public interface ISystem : IDisposable + { + void Initialize(EntityWorld world); + } +} \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/Systems/IUpdateSystem.cs b/Source/MonoGame.Extended.Entities/Systems/IUpdateSystem.cs new file mode 100644 index 00000000..30eee640 --- /dev/null +++ b/Source/MonoGame.Extended.Entities/Systems/IUpdateSystem.cs @@ -0,0 +1,9 @@ +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Entities.Systems +{ + public interface IUpdateSystem : ISystem + { + void Update(GameTime gameTime); + } +} \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs b/Source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs deleted file mode 100644 index cf47cad1..00000000 --- a/Source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using Microsoft.Xna.Framework; - -namespace MonoGame.Extended.Entities.Systems -{ - public abstract class UpdateSystem : IDisposable - { - protected UpdateSystem() - { - } - - public virtual void Dispose() { } - - public abstract void Update(GameTime gameTime); - } -} \ No newline at end of file