diff --git a/PongFE/Components/CanBeDestroyedComponent.cs b/PongFE/Components/CanBeDestroyedComponent.cs new file mode 100644 index 0000000..99a9669 --- /dev/null +++ b/PongFE/Components/CanBeDestroyedComponent.cs @@ -0,0 +1,6 @@ +using Encompass; + +namespace PongFE.Components +{ + public struct CanBeDestroyedComponent : IComponent { } +} diff --git a/PongFE/Components/CanDestroyComponent.cs b/PongFE/Components/CanDestroyComponent.cs new file mode 100644 index 0000000..2e3edb9 --- /dev/null +++ b/PongFE/Components/CanDestroyComponent.cs @@ -0,0 +1,6 @@ +using Encompass; + +namespace PongFE.Components +{ + public struct CanDestroyComponent : IComponent { } +} diff --git a/PongFE/Components/DestroyResponseComponent.cs b/PongFE/Components/DestroyResponseComponent.cs new file mode 100644 index 0000000..cf67457 --- /dev/null +++ b/PongFE/Components/DestroyResponseComponent.cs @@ -0,0 +1,6 @@ +using Encompass; + +namespace PongFE.Components +{ + public struct DestroyResponseComponent : IComponent { } +} diff --git a/PongFE/Components/SpawnBallAfterDestroyComponent.cs b/PongFE/Components/SpawnBallAfterDestroyComponent.cs new file mode 100644 index 0000000..d8dcd68 --- /dev/null +++ b/PongFE/Components/SpawnBallAfterDestroyComponent.cs @@ -0,0 +1,14 @@ +using Encompass; + +namespace PongFE.Components +{ + public struct SpawnBallAfterDestroyComponent : IComponent + { + public float Seconds { get; } + + public SpawnBallAfterDestroyComponent(float seconds) + { + Seconds = seconds; + } + } +} diff --git a/PongFE/Engines/CollisionEngine.cs b/PongFE/Engines/CollisionEngine.cs index 4294584..a161e41 100644 --- a/PongFE/Engines/CollisionEngine.cs +++ b/PongFE/Engines/CollisionEngine.cs @@ -7,10 +7,15 @@ namespace PongFE.Engines { [Reads( typeof(CanCauseBounceComponent), - typeof(CanBeBouncedComponent) + typeof(CanBeBouncedComponent), + typeof(CanDestroyComponent), + typeof(CanBeDestroyedComponent) )] [Receives(typeof(CollisionMessage))] - [Sends(typeof(BounceMessage))] + [Sends( + typeof(BounceMessage), + typeof(DestroyMessage) + )] public class CollisionEngine : Engine { public override void Update(double dt) @@ -19,6 +24,9 @@ namespace PongFE.Engines { CheckBounce(message.EntityA, message.EntityB, message.HitOrientation); CheckBounce(message.EntityB, message.EntityA, message.HitOrientation); + + CheckDestroy(message.EntityA, message.EntityB); + CheckDestroy(message.EntityB, message.EntityA); } } @@ -32,5 +40,16 @@ namespace PongFE.Engines } } } + + private void CheckDestroy(Entity a, Entity b) + { + if (HasComponent(a)) + { + if (HasComponent(b)) + { + SendMessage(new DestroyMessage(b)); + } + } + } } } diff --git a/PongFE/Engines/DestroyEngine.cs b/PongFE/Engines/DestroyEngine.cs new file mode 100644 index 0000000..cd92c49 --- /dev/null +++ b/PongFE/Engines/DestroyEngine.cs @@ -0,0 +1,35 @@ +using Encompass; +using PongFE.Components; +using PongFE.Messages; + +namespace PongFE.Engines +{ + [Reads(typeof(SpawnBallAfterDestroyComponent))] + [Receives(typeof(DestroyMessage))] + [Sends(typeof(BallSpawnMessage))] + public class DestroyEngine : Engine + { + public override void Update(double dt) + { + foreach (ref readonly var message in ReadMessages()) + { + if (HasComponent(message.Entity)) + { + ref readonly var respawnComponent = ref GetComponent(message.Entity); + + SendMessage( + new BallSpawnMessage( + new MoonTools.Structs.Position2D(640, 360), + new System.Numerics.Vector2(-200, 100), + 16, + 16 + ), + respawnComponent.Seconds + ); + } + + Destroy(message.Entity); + } + } + } +} diff --git a/PongFE/Engines/Spawners/BallSpawner.cs b/PongFE/Engines/Spawners/BallSpawner.cs index 1956f46..d53d132 100644 --- a/PongFE/Engines/Spawners/BallSpawner.cs +++ b/PongFE/Engines/Spawners/BallSpawner.cs @@ -24,6 +24,8 @@ namespace PongFE.Spawners AddComponent(ball, new CanBeBouncedComponent()); AddComponent(ball, new BounceResponseComponent()); AddComponent(ball, new CanBeTrackedComponent()); + AddComponent(ball, new CanBeDestroyedComponent()); + AddComponent(ball, new SpawnBallAfterDestroyComponent(0.5f)); } } } diff --git a/PongFE/Engines/Spawners/GoalBoundarySpawner.cs b/PongFE/Engines/Spawners/GoalBoundarySpawner.cs new file mode 100644 index 0000000..749a2b8 --- /dev/null +++ b/PongFE/Engines/Spawners/GoalBoundarySpawner.cs @@ -0,0 +1,18 @@ +using Encompass; +using PongFE.Components; +using PongFE.Messages; + +namespace PongFE.Spawners +{ + public class GoalBoundarySpawner : Spawner + { + protected override void Spawn(GoalBoundarySpawnMessage message) + { + var entity = CreateEntity(); + + AddComponent(entity, new PositionComponent(message.Position)); + AddComponent(entity, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, message.Width, message.Height))); + AddComponent(entity, new CanDestroyComponent()); + } + } +} diff --git a/PongFE/Messages/DestroyMessage.cs b/PongFE/Messages/DestroyMessage.cs new file mode 100644 index 0000000..a7e9eaf --- /dev/null +++ b/PongFE/Messages/DestroyMessage.cs @@ -0,0 +1,14 @@ +using Encompass; + +namespace PongFE.Messages +{ + public struct DestroyMessage : IMessage + { + public Entity Entity { get; } + + public DestroyMessage(Entity entity) + { + Entity = entity; + } + } +} diff --git a/PongFE/Messages/GoalBoundarySpawnMessage.cs b/PongFE/Messages/GoalBoundarySpawnMessage.cs new file mode 100644 index 0000000..ef100aa --- /dev/null +++ b/PongFE/Messages/GoalBoundarySpawnMessage.cs @@ -0,0 +1,19 @@ +using Encompass; +using MoonTools.Structs; + +namespace PongFE.Messages +{ + public struct GoalBoundarySpawnMessage : IMessage + { + public Position2D Position { get; } + public int Width { get; } + public int Height { get; } + + public GoalBoundarySpawnMessage(Position2D position, int width, int height) + { + Position = position; + Width = width; + Height = height; + } + } +} diff --git a/PongFE/PongFEGame.cs b/PongFE/PongFEGame.cs index 81b505e..9dbb674 100644 --- a/PongFE/PongFEGame.cs +++ b/PongFE/PongFEGame.cs @@ -45,12 +45,14 @@ namespace PongFE WorldBuilder.AddEngine(new MotionEngine()); WorldBuilder.AddEngine(new CollisionEngine()); WorldBuilder.AddEngine(new BounceEngine()); + WorldBuilder.AddEngine(new DestroyEngine()); WorldBuilder.AddEngine(new UpdatePositionEngine()); WorldBuilder.AddEngine(new UpdateVelocityEngine()); WorldBuilder.AddEngine(new ComputerControlEngine()); WorldBuilder.AddEngine(new BallSpawner(WhitePixel)); WorldBuilder.AddEngine(new BoundarySpawner()); + WorldBuilder.AddEngine(new GoalBoundarySpawner()); WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel)); WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch)); @@ -93,15 +95,6 @@ namespace PongFE ) ); - // right boundary - WorldBuilder.SendMessage( - new BoundarySpawnMessage( - new MoonTools.Structs.Position2D(1280, 0), - 6, - 720 - ) - ); - // bottom boundary WorldBuilder.SendMessage( new BoundarySpawnMessage( @@ -111,6 +104,24 @@ namespace PongFE ) ); + // right boundary + WorldBuilder.SendMessage( + new GoalBoundarySpawnMessage( + new MoonTools.Structs.Position2D(1280, 0), + 6, + 720 + ) + ); + + // left boundary + WorldBuilder.SendMessage( + new GoalBoundarySpawnMessage( + new MoonTools.Structs.Position2D(-6, 0), + 6, + 720 + ) + ); + World = WorldBuilder.Build(); }