From c8db244932acf4460dbb93fbabc38acd34418ebc Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Wed, 27 Jun 2018 21:41:05 +1000 Subject: [PATCH] cleaning the api --- Source/Demos/Platformer/EntityFactory.cs | 5 ++- Source/Demos/Platformer/GameMain.cs | 19 +++++---- .../Demos/Platformer/Systems/WorldSystem.cs | 1 + Source/Demos/Sandbox/GameMain.cs | 9 ++-- .../Demos/Sandbox/Systems/RainfallSystem.cs | 6 +-- .../ComponentManager.cs | 2 +- Source/MonoGame.Extended.Entities/Entity.cs | 2 +- .../EntityManager.cs | 2 +- .../Systems/EntitySystem.cs | 41 ++++++++++--------- .../Systems/ISystem.cs | 2 +- .../{EntityWorld.cs => World.cs} | 7 ++-- .../WorldBuilder.cs | 26 ++++++++++++ .../KeyboardExtended.cs | 1 + .../MonoGame.Extended.Input/MouseExtended.cs | 5 ++- 14 files changed, 81 insertions(+), 47 deletions(-) rename Source/MonoGame.Extended.Entities/{EntityWorld.cs => World.cs} (91%) create mode 100644 Source/MonoGame.Extended.Entities/WorldBuilder.cs diff --git a/Source/Demos/Platformer/EntityFactory.cs b/Source/Demos/Platformer/EntityFactory.cs index 00178550..c578b74d 100644 --- a/Source/Demos/Platformer/EntityFactory.cs +++ b/Source/Demos/Platformer/EntityFactory.cs @@ -8,15 +8,16 @@ using MonoGame.Extended.Entities; using MonoGame.Extended.TextureAtlases; using Platformer.Collisions; using Platformer.Components; +using World = MonoGame.Extended.Entities.World; namespace Platformer { public class EntityFactory { - private readonly EntityWorld _world; + private readonly World _world; private readonly ContentManager _contentManager; - public EntityFactory(EntityWorld world, ContentManager contentManager) + public EntityFactory(World world, ContentManager contentManager) { _world = world; _contentManager = contentManager; diff --git a/Source/Demos/Platformer/GameMain.cs b/Source/Demos/Platformer/GameMain.cs index 3763bf9b..35de56be 100644 --- a/Source/Demos/Platformer/GameMain.cs +++ b/Source/Demos/Platformer/GameMain.cs @@ -15,7 +15,7 @@ namespace Platformer private TiledMapRenderer _renderer; private EntityFactory _entityFactory; private OrthographicCamera _camera; - private EntityWorld _world; + private World _world; public GameMain() { @@ -31,11 +31,14 @@ namespace Platformer protected override void LoadContent() { - _world = new EntityWorld(); - _world.RegisterSystem(new WorldSystem()); - _world.RegisterSystem(new PlayerSystem()); - _world.RegisterSystem(new EnemySystem()); - _world.RegisterSystem(new RenderSystem(new SpriteBatch(GraphicsDevice), _camera)); + _world = new WorldBuilder() + .AddSystem(new WorldSystem()) + .AddSystem(new PlayerSystem()) + .AddSystem(new EnemySystem()) + .AddSystem(new RenderSystem(new SpriteBatch(GraphicsDevice), _camera)) + .Build(); + + Components.Add(_world); _entityFactory = new EntityFactory(_world, Content); @@ -78,7 +81,7 @@ namespace Platformer _renderer.Update(gameTime); //_camera.LookAt(_playerEntity.Get().Position); - _world.Update(gameTime); + //_world.Update(gameTime); base.Update(gameTime); } @@ -88,7 +91,7 @@ namespace Platformer GraphicsDevice.Clear(Color.Black); _renderer.Draw(_camera.GetViewMatrix()); - _world.Draw(gameTime); + //_world.Draw(gameTime); base.Draw(gameTime); } diff --git a/Source/Demos/Platformer/Systems/WorldSystem.cs b/Source/Demos/Platformer/Systems/WorldSystem.cs index 68925d1d..3cf57136 100644 --- a/Source/Demos/Platformer/Systems/WorldSystem.cs +++ b/Source/Demos/Platformer/Systems/WorldSystem.cs @@ -3,6 +3,7 @@ using MonoGame.Extended; using MonoGame.Extended.Entities; using MonoGame.Extended.Entities.Systems; using Platformer.Collisions; +using World = Platformer.Collisions.World; namespace Platformer.Systems { diff --git a/Source/Demos/Sandbox/GameMain.cs b/Source/Demos/Sandbox/GameMain.cs index 3148c175..b0228b88 100644 --- a/Source/Demos/Sandbox/GameMain.cs +++ b/Source/Demos/Sandbox/GameMain.cs @@ -13,7 +13,7 @@ namespace Sandbox // ReSharper disable once NotAccessedField.Local private GraphicsDeviceManager _graphicsDeviceManager; private SpriteBatch _spriteBatch; - private EntityWorld _world; + private World _world; public MainGame() { @@ -25,9 +25,10 @@ namespace Sandbox protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); - _world = new EntityWorld(); - _world.RegisterSystem(new RenderSystem(GraphicsDevice)); - _world.RegisterSystem(new RainfallSystem(_world)); + _world = new WorldBuilder() + .AddSystem(new RenderSystem(GraphicsDevice)) + .AddSystem(new RainfallSystem()) + .Build(); var random = new FastRandom(); diff --git a/Source/Demos/Sandbox/Systems/RainfallSystem.cs b/Source/Demos/Sandbox/Systems/RainfallSystem.cs index a7bbfd95..6121d1b4 100644 --- a/Source/Demos/Sandbox/Systems/RainfallSystem.cs +++ b/Source/Demos/Sandbox/Systems/RainfallSystem.cs @@ -8,14 +8,12 @@ namespace Sandbox.Systems { public class RainfallSystem : EntityUpdateSystem { - private readonly EntityWorld _world; private ComponentMapper _transformMapper; private ComponentMapper _raindropMapper; - public RainfallSystem(EntityWorld world) + public RainfallSystem() : base(Aspect.All(typeof(Transform2), typeof(Raindrop))) { - _world = world; } public override void Initialize(IComponentMapperService mapperService) @@ -37,7 +35,7 @@ namespace Sandbox.Systems transform.Position += raindrop.Velocity * elapsedSeconds; if (transform.Position.Y >= 480) - _world.DestroyEntity(entity); + DestroyEntity(entity); } } } diff --git a/Source/MonoGame.Extended.Entities/ComponentManager.cs b/Source/MonoGame.Extended.Entities/ComponentManager.cs index 1764258c..ef8a107c 100644 --- a/Source/MonoGame.Extended.Entities/ComponentManager.cs +++ b/Source/MonoGame.Extended.Entities/ComponentManager.cs @@ -24,7 +24,7 @@ namespace MonoGame.Extended.Entities { } - public void Initialize(EntityWorld world) + public void Initialize(World world) { } diff --git a/Source/MonoGame.Extended.Entities/Entity.cs b/Source/MonoGame.Extended.Entities/Entity.cs index 117b8a72..39d244c6 100644 --- a/Source/MonoGame.Extended.Entities/Entity.cs +++ b/Source/MonoGame.Extended.Entities/Entity.cs @@ -16,7 +16,7 @@ namespace MonoGame.Extended.Entities _componentManager = componentManager; } - public int Id { get; internal set; } + public int Id { get; } public BitVector32 ComponentBits => _entityManager.GetComponentBits(Id); diff --git a/Source/MonoGame.Extended.Entities/EntityManager.cs b/Source/MonoGame.Extended.Entities/EntityManager.cs index ac033abc..b217782c 100644 --- a/Source/MonoGame.Extended.Entities/EntityManager.cs +++ b/Source/MonoGame.Extended.Entities/EntityManager.cs @@ -28,7 +28,7 @@ namespace MonoGame.Extended.Entities { } - public void Initialize(EntityWorld world) + public void Initialize(World world) { } diff --git a/Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs b/Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs index b2bd16dc..4afcaeaf 100644 --- a/Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs +++ b/Source/MonoGame.Extended.Entities/Systems/EntitySystem.cs @@ -11,37 +11,40 @@ namespace MonoGame.Extended.Entities.Systems public void Dispose() { - if (World != null) + if (_world != null) { - World.EntityManager.EntityAdded -= OnEntityAdded; - World.EntityManager.EntityRemoved -= OnEntityRemoved; + _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; } + private World _world; protected virtual void OnEntityChanged(int entityId) { } protected virtual void OnEntityAdded(int entityId) { } protected virtual void OnEntityRemoved(int entityId) { } public Bag ActiveEntities => _subscription.ActiveEntities; + + public virtual void Initialize(World 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); + + protected void DestroyEntity(int entityId) => _world.DestroyEntity(entityId); + protected Entity CreateEntity() => _world.CreateEntity(); } } \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/Systems/ISystem.cs b/Source/MonoGame.Extended.Entities/Systems/ISystem.cs index ac15761d..d4511fcb 100644 --- a/Source/MonoGame.Extended.Entities/Systems/ISystem.cs +++ b/Source/MonoGame.Extended.Entities/Systems/ISystem.cs @@ -4,6 +4,6 @@ namespace MonoGame.Extended.Entities.Systems { public interface ISystem : IDisposable { - void Initialize(EntityWorld world); + void Initialize(World world); } } \ No newline at end of file diff --git a/Source/MonoGame.Extended.Entities/EntityWorld.cs b/Source/MonoGame.Extended.Entities/World.cs similarity index 91% rename from Source/MonoGame.Extended.Entities/EntityWorld.cs rename to Source/MonoGame.Extended.Entities/World.cs index 3a63213b..0ac8f3da 100644 --- a/Source/MonoGame.Extended.Entities/EntityWorld.cs +++ b/Source/MonoGame.Extended.Entities/World.cs @@ -4,12 +4,12 @@ using MonoGame.Extended.Entities.Systems; namespace MonoGame.Extended.Entities { - public class EntityWorld : SimpleDrawableGameComponent + public class World : SimpleDrawableGameComponent { private readonly Bag _updateSystems; private readonly Bag _drawSystems; - public EntityWorld() + internal World() { _updateSystems = new Bag(); _drawSystems = new Bag(); @@ -34,8 +34,7 @@ namespace MonoGame.Extended.Entities public Bag AllEntities => EntityManager.Entities; - // TODO: Move this to world configuration - public void RegisterSystem(ISystem system) + internal void RegisterSystem(ISystem system) { // ReSharper disable once ConvertIfStatementToSwitchStatement if (system is IUpdateSystem updateSystem) diff --git a/Source/MonoGame.Extended.Entities/WorldBuilder.cs b/Source/MonoGame.Extended.Entities/WorldBuilder.cs new file mode 100644 index 00000000..7f033233 --- /dev/null +++ b/Source/MonoGame.Extended.Entities/WorldBuilder.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using MonoGame.Extended.Entities.Systems; + +namespace MonoGame.Extended.Entities +{ + public class WorldBuilder + { + private readonly List _systems = new List(); + + public WorldBuilder AddSystem(ISystem system) + { + _systems.Add(system); + return this; + } + + public World Build() + { + var world = new World(); + + foreach (var system in _systems) + world.RegisterSystem(system); + + return world; + } + } +} \ No newline at end of file diff --git a/Source/MonoGame.Extended.Input/KeyboardExtended.cs b/Source/MonoGame.Extended.Input/KeyboardExtended.cs index 79f2f9db..0262e456 100644 --- a/Source/MonoGame.Extended.Input/KeyboardExtended.cs +++ b/Source/MonoGame.Extended.Input/KeyboardExtended.cs @@ -4,6 +4,7 @@ namespace MonoGame.Extended.Input { public static class KeyboardExtended { + // TODO: This global static state was a horrible idea. private static KeyboardState _currentKeyboardState; private static KeyboardState _previousKeyboardState; diff --git a/Source/MonoGame.Extended.Input/MouseExtended.cs b/Source/MonoGame.Extended.Input/MouseExtended.cs index 18ac1358..f4a0b644 100644 --- a/Source/MonoGame.Extended.Input/MouseExtended.cs +++ b/Source/MonoGame.Extended.Input/MouseExtended.cs @@ -6,6 +6,7 @@ namespace MonoGame.Extended.Input { public static class MouseExtended { + // TODO: This global static state was a horrible idea. private static MouseState _currentMouseState; private static MouseState _previousMouseState; @@ -22,8 +23,8 @@ namespace MonoGame.Extended.Input public static IntPtr WindowHandle { - get { return Mouse.WindowHandle; } - set { Mouse.WindowHandle = value; } + get => Mouse.WindowHandle; + set => Mouse.WindowHandle = value; } } } \ No newline at end of file