From acea7b2e728787a0d83bbf83c8c1f042d2c32e7e Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 3 Jun 2024 10:15:45 +0800 Subject: + plugins project --- .../Systems/DrawSystem.cs | 16 +++++++ .../Systems/EntityDrawSystem.cs | 14 ++++++ .../Systems/EntityProcessingSystem.cs | 26 +++++++++++ .../Systems/EntitySystem.cs | 51 ++++++++++++++++++++++ .../Systems/EntityUpdateSystem.cs | 14 ++++++ .../MonoGame.Extended.Entities/Systems/ISystem.cs | 9 ++++ .../Systems/UpdateSystem.cs | 16 +++++++ 7 files changed, 146 insertions(+) create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/DrawSystem.cs create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntitySystem.cs create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/ISystem.cs create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems') diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/DrawSystem.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/DrawSystem.cs new file mode 100644 index 0000000..b9e63eb --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/DrawSystem.cs @@ -0,0 +1,16 @@ +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Entities.Systems +{ + public interface IDrawSystem : ISystem + { + void Draw(GameTime gameTime); + } + + public abstract class DrawSystem : IDrawSystem + { + public virtual void Dispose() { } + public virtual void Initialize(World world) { } + public abstract void Draw(GameTime gameTime); + } +} \ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs new file mode 100644 index 0000000..5a0d6b9 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityDrawSystem.cs @@ -0,0 +1,14 @@ +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Entities.Systems +{ + public abstract class EntityDrawSystem : EntitySystem, IDrawSystem + { + protected EntityDrawSystem(AspectBuilder aspect) + : base(aspect) + { + } + + public abstract void Draw(GameTime gameTime); + } +} \ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs new file mode 100644 index 0000000..c4d9339 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityProcessingSystem.cs @@ -0,0 +1,26 @@ +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Entities.Systems +{ + public abstract class EntityProcessingSystem : EntityUpdateSystem + { + protected EntityProcessingSystem(AspectBuilder aspectBuilder) + : base(aspectBuilder) + { + } + + public override void Update(GameTime gameTime) + { + Begin(); + + foreach (var entityId in ActiveEntities) + Process(gameTime, entityId); + + End(); + } + + public virtual void Begin() { } + public abstract void Process(GameTime gameTime, int entityId); + public virtual void End() { } + } +} \ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntitySystem.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntitySystem.cs new file mode 100644 index 0000000..2c95107 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntitySystem.cs @@ -0,0 +1,51 @@ +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; + } + } + + private readonly AspectBuilder _aspectBuilder; + private EntitySubscription _subscription; + + 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(); + protected Entity GetEntity(int entityId) => _world.GetEntity(entityId); + } +} \ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs new file mode 100644 index 0000000..97a61d5 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/EntityUpdateSystem.cs @@ -0,0 +1,14 @@ +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Entities.Systems +{ + public abstract class EntityUpdateSystem : EntitySystem, IUpdateSystem + { + protected EntityUpdateSystem(AspectBuilder aspectBuilder) + : base(aspectBuilder) + { + } + + public abstract void Update(GameTime gameTime); + } +} \ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/ISystem.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/ISystem.cs new file mode 100644 index 0000000..d4511fc --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/ISystem.cs @@ -0,0 +1,9 @@ +using System; + +namespace MonoGame.Extended.Entities.Systems +{ + public interface ISystem : IDisposable + { + void Initialize(World world); + } +} \ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs new file mode 100644 index 0000000..e85c964 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/Systems/UpdateSystem.cs @@ -0,0 +1,16 @@ +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Entities.Systems +{ + public interface IUpdateSystem : ISystem + { + void Update(GameTime gameTime); + } + + public abstract class UpdateSystem : IUpdateSystem + { + public virtual void Dispose() { } + public virtual void Initialize(World world) { } + public abstract void Update(GameTime gameTime); + } +} \ No newline at end of file -- cgit v1.1-26-g67d0