rejigged the base systems classes and interfaces

This commit is contained in:
Dylan Wilson
2018-06-27 21:19:20 +10:00
parent 3c023d242f
commit a2eda72620
11 changed files with 115 additions and 89 deletions
@@ -12,13 +12,21 @@ namespace MonoGame.Extended.Entities
ComponentMapper<T> GetMapper<T>() where T : class;
}
public class ComponentManager : UpdateSystem, IComponentMapperService
public class ComponentManager : IUpdateSystem, IComponentMapperService
{
public ComponentManager()
{
_componentMappers = new Bag<ComponentMapper>();
_componentTypes = new Dictionary<Type, int>();
}
public void Dispose()
{
}
public void Initialize(EntityWorld world)
{
}
private readonly Bag<ComponentMapper> _componentMappers;
private readonly Dictionary<Type, int> _componentTypes;
@@ -76,7 +84,7 @@ namespace MonoGame.Extended.Entities
return componentBits;
}
public override void Update(GameTime gameTime)
public void Update(GameTime gameTime)
{
}
}
@@ -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<Entity>(() => 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)
{
@@ -6,20 +6,16 @@ namespace MonoGame.Extended.Entities
{
public class EntityWorld : SimpleDrawableGameComponent
{
private readonly Bag<UpdateSystem> _updateSystems;
private readonly Bag<EntityDrawSystem> _drawSystems;
private readonly Bag<IUpdateSystem> _updateSystems;
private readonly Bag<IDrawSystem> _drawSystems;
public EntityWorld()
{
ComponentManager = new ComponentManager();
EntityManager = new EntityManager(ComponentManager);
_updateSystems = new Bag<IUpdateSystem>();
_drawSystems = new Bag<IDrawSystem>();
_updateSystems = new Bag<UpdateSystem>
{
ComponentManager,
EntityManager
};
_drawSystems = new Bag<EntityDrawSystem>();
RegisterSystem(ComponentManager = new ComponentManager());
RegisterSystem(EntityManager = new EntityManager(ComponentManager));
}
public override void Dispose()
@@ -39,23 +35,16 @@ namespace MonoGame.Extended.Entities
public Bag<Entity> 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)
@@ -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);
}
}
@@ -4,7 +4,7 @@ namespace MonoGame.Extended.Entities.Systems
{
public abstract class EntityProcessingSystem : EntityUpdateSystem
{
protected EntityProcessingSystem(AspectBuilder aspectBuilder)
protected EntityProcessingSystem(AspectBuilder aspectBuilder)
: base(aspectBuilder)
{
}
@@ -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<int> ActiveEntities => _subscription.ActiveEntities;
}
}
@@ -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<int> ActiveEntities => _subscription.ActiveEntities;
public abstract void Initialize(IComponentMapperService mapperService);
public abstract void Update(GameTime gameTime);
}
}
@@ -0,0 +1,9 @@
using Microsoft.Xna.Framework;
namespace MonoGame.Extended.Entities.Systems
{
public interface IDrawSystem : ISystem
{
void Draw(GameTime gameTime);
}
}
@@ -0,0 +1,9 @@
using System;
namespace MonoGame.Extended.Entities.Systems
{
public interface ISystem : IDisposable
{
void Initialize(EntityWorld world);
}
}
@@ -0,0 +1,9 @@
using Microsoft.Xna.Framework;
namespace MonoGame.Extended.Entities.Systems
{
public interface IUpdateSystem : ISystem
{
void Update(GameTime gameTime);
}
}
@@ -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);
}
}