From 0f6282b049f2bf5b28d9a94d60c57ac5bf15282b Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Thu, 16 Jul 2020 14:39:36 -0700 Subject: [PATCH] scoring stuff --- PongFE/Components/ComputerControlComponent.cs | 11 +---- .../IncreaseScoreAfterDestroyComponent.cs | 9 ++++ PongFE/Components/PlayerComponent.cs | 15 +++++++ PongFE/Components/PlayerInputComponent.cs | 11 +---- PongFE/Components/ScoreComponent.cs | 14 +++++++ PongFE/Engines/CollisionEngine.cs | 2 +- PongFE/Engines/DestroyEngine.cs | 15 ++++++- PongFE/Engines/InputEngine.cs | 41 ++++++++++--------- PongFE/Engines/ScoreEngine.cs | 24 +++++++++++ PongFE/Engines/Spawners/BallSpawner.cs | 1 + .../Engines/Spawners/GoalBoundarySpawner.cs | 1 + PongFE/Engines/Spawners/PaddleSpawner.cs | 5 ++- PongFE/Messages/DestroyMessage.cs | 4 +- PongFE/Messages/GoalBoundarySpawnMessage.cs | 1 + PongFE/Messages/ScoreMessage.cs | 14 +++++++ PongFE/PongFEGame.cs | 1 + 16 files changed, 124 insertions(+), 45 deletions(-) create mode 100644 PongFE/Components/IncreaseScoreAfterDestroyComponent.cs create mode 100644 PongFE/Components/PlayerComponent.cs create mode 100644 PongFE/Components/ScoreComponent.cs create mode 100644 PongFE/Engines/ScoreEngine.cs create mode 100644 PongFE/Messages/ScoreMessage.cs diff --git a/PongFE/Components/ComputerControlComponent.cs b/PongFE/Components/ComputerControlComponent.cs index 1e55192..ad45885 100644 --- a/PongFE/Components/ComputerControlComponent.cs +++ b/PongFE/Components/ComputerControlComponent.cs @@ -1,15 +1,6 @@ using Encompass; -using PongFE.Enums; namespace PongFE.Components { - public struct ComputerControlComponent : IComponent - { - public PlayerIndex PlayerIndex { get; } - - public ComputerControlComponent(PlayerIndex playerIndex) - { - PlayerIndex = playerIndex; - } - } + public struct ComputerControlComponent : IComponent { } } diff --git a/PongFE/Components/IncreaseScoreAfterDestroyComponent.cs b/PongFE/Components/IncreaseScoreAfterDestroyComponent.cs new file mode 100644 index 0000000..698b986 --- /dev/null +++ b/PongFE/Components/IncreaseScoreAfterDestroyComponent.cs @@ -0,0 +1,9 @@ +using Encompass; + +namespace PongFE.Components +{ + public struct IncreaseScoreAfterDestroyComponent : IComponent + { + + } +} diff --git a/PongFE/Components/PlayerComponent.cs b/PongFE/Components/PlayerComponent.cs new file mode 100644 index 0000000..558db08 --- /dev/null +++ b/PongFE/Components/PlayerComponent.cs @@ -0,0 +1,15 @@ +using Encompass; +using PongFE.Enums; + +namespace PongFE.Components +{ + public struct PlayerComponent : IComponent + { + public PlayerIndex PlayerIndex { get; } + + public PlayerComponent(PlayerIndex playerIndex) + { + PlayerIndex = playerIndex; + } + } +} diff --git a/PongFE/Components/PlayerInputComponent.cs b/PongFE/Components/PlayerInputComponent.cs index 1b2e81b..29dec78 100644 --- a/PongFE/Components/PlayerInputComponent.cs +++ b/PongFE/Components/PlayerInputComponent.cs @@ -1,15 +1,6 @@ using Encompass; -using PongFE.Enums; namespace PongFE.Components { - public struct PlayerInputComponent : IComponent - { - public PlayerIndex PlayerIndex { get; } - - public PlayerInputComponent(PlayerIndex playerIndex) - { - PlayerIndex = playerIndex; - } - } + public struct PlayerInputComponent : IComponent { } } diff --git a/PongFE/Components/ScoreComponent.cs b/PongFE/Components/ScoreComponent.cs new file mode 100644 index 0000000..8170bc9 --- /dev/null +++ b/PongFE/Components/ScoreComponent.cs @@ -0,0 +1,14 @@ +using Encompass; + +namespace PongFE.Components +{ + public struct ScoreComponent : IComponent + { + public int Score { get; } + + public ScoreComponent(int score) + { + Score = score; + } + } +} diff --git a/PongFE/Engines/CollisionEngine.cs b/PongFE/Engines/CollisionEngine.cs index a161e41..15c1fae 100644 --- a/PongFE/Engines/CollisionEngine.cs +++ b/PongFE/Engines/CollisionEngine.cs @@ -47,7 +47,7 @@ namespace PongFE.Engines { if (HasComponent(b)) { - SendMessage(new DestroyMessage(b)); + SendMessage(new DestroyMessage(b, a)); } } } diff --git a/PongFE/Engines/DestroyEngine.cs b/PongFE/Engines/DestroyEngine.cs index 6b34131..52bcc3d 100644 --- a/PongFE/Engines/DestroyEngine.cs +++ b/PongFE/Engines/DestroyEngine.cs @@ -4,9 +4,15 @@ using PongFE.Messages; namespace PongFE.Engines { - [Reads(typeof(SpawnBallAfterDestroyComponent))] + [Reads( + typeof(SpawnBallAfterDestroyComponent), + typeof(IncreaseScoreAfterDestroyComponent) + )] [Receives(typeof(DestroyMessage))] - [Sends(typeof(BallSpawnMessage))] + [Sends( + typeof(BallSpawnMessage), + typeof(ScoreMessage) + )] public class DestroyEngine : Engine { public override void Update(double dt) @@ -28,6 +34,11 @@ namespace PongFE.Engines ); } + if (HasComponent(message.Entity)) + { + SendMessage(new ScoreMessage(message.DestroyedBy)); + } + Destroy(message.Entity); } } diff --git a/PongFE/Engines/InputEngine.cs b/PongFE/Engines/InputEngine.cs index 41a46f8..c33c236 100644 --- a/PongFE/Engines/InputEngine.cs +++ b/PongFE/Engines/InputEngine.cs @@ -6,7 +6,7 @@ using PongFE.Messages; namespace PongFE.Engines { - [Reads(typeof(PlayerInputComponent))] + [Reads(typeof(PlayerInputComponent), typeof(PlayerComponent))] [Sends(typeof(PaddleMoveMessage))] public class InputEngine : Engine { @@ -17,26 +17,29 @@ namespace PongFE.Engines foreach (ref readonly var playerInputEntity in ReadEntities()) { ref readonly var playerInputComponent = ref GetComponent(playerInputEntity); - - if (playerInputComponent.PlayerIndex == PlayerIndex.One) + if (HasComponent(playerInputEntity)) { - if (keyboardState.IsKeyDown(Keys.Down)) + ref readonly var playerComponent = ref GetComponent(playerInputEntity); + if (playerComponent.PlayerIndex == PlayerIndex.One) { - SendMessage( - new PaddleMoveMessage( - playerInputEntity, - PaddleMoveDirection.Down - ) - ); - } - else if (keyboardState.IsKeyDown(Keys.Up)) - { - SendMessage( - new PaddleMoveMessage( - playerInputEntity, - PaddleMoveDirection.Up - ) - ); + if (keyboardState.IsKeyDown(Keys.Down)) + { + SendMessage( + new PaddleMoveMessage( + playerInputEntity, + PaddleMoveDirection.Down + ) + ); + } + else if (keyboardState.IsKeyDown(Keys.Up)) + { + SendMessage( + new PaddleMoveMessage( + playerInputEntity, + PaddleMoveDirection.Up + ) + ); + } } } } diff --git a/PongFE/Engines/ScoreEngine.cs b/PongFE/Engines/ScoreEngine.cs new file mode 100644 index 0000000..8e51019 --- /dev/null +++ b/PongFE/Engines/ScoreEngine.cs @@ -0,0 +1,24 @@ +using Encompass; +using PongFE.Components; +using PongFE.Messages; + +namespace PongFE.Engines +{ + [Reads(typeof(ScoreComponent))] + [Receives(typeof(ScoreMessage))] + [Writes(typeof(ScoreComponent))] + public class ScoreEngine : Engine + { + public override void Update(double dt) + { + foreach (ref readonly var scoreMessage in ReadMessages()) + { + if (HasComponent(scoreMessage.Entity)) + { + ref readonly var scoreComponent = ref GetComponent(scoreMessage.Entity); + SetComponent(scoreMessage.Entity, new ScoreComponent(scoreComponent.Score + 1)); + } + } + } + } +} diff --git a/PongFE/Engines/Spawners/BallSpawner.cs b/PongFE/Engines/Spawners/BallSpawner.cs index bc38655..0eaa862 100644 --- a/PongFE/Engines/Spawners/BallSpawner.cs +++ b/PongFE/Engines/Spawners/BallSpawner.cs @@ -32,6 +32,7 @@ namespace PongFE.Spawners AddComponent(ball, new CanBeTrackedComponent()); AddComponent(ball, new CanBeDestroyedComponent()); AddComponent(ball, new SpawnBallAfterDestroyComponent(0.5f)); + AddComponent(ball, new IncreaseScoreAfterDestroyComponent()); } } } diff --git a/PongFE/Engines/Spawners/GoalBoundarySpawner.cs b/PongFE/Engines/Spawners/GoalBoundarySpawner.cs index 749a2b8..138a49e 100644 --- a/PongFE/Engines/Spawners/GoalBoundarySpawner.cs +++ b/PongFE/Engines/Spawners/GoalBoundarySpawner.cs @@ -13,6 +13,7 @@ namespace PongFE.Spawners AddComponent(entity, new PositionComponent(message.Position)); AddComponent(entity, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, message.Width, message.Height))); AddComponent(entity, new CanDestroyComponent()); + AddComponent(entity, new ScoreComponent(0)); } } } diff --git a/PongFE/Engines/Spawners/PaddleSpawner.cs b/PongFE/Engines/Spawners/PaddleSpawner.cs index 5d2d8db..02713db 100644 --- a/PongFE/Engines/Spawners/PaddleSpawner.cs +++ b/PongFE/Engines/Spawners/PaddleSpawner.cs @@ -20,12 +20,13 @@ namespace PongFE.Spawners var paddle = CreateEntity(); if (message.PaddleControl == PaddleControl.Player) { - AddComponent(paddle, new PlayerInputComponent(message.PlayerIndex)); + AddComponent(paddle, new PlayerInputComponent()); } else { - AddComponent(paddle, new ComputerControlComponent(message.PlayerIndex)); + AddComponent(paddle, new ComputerControlComponent()); } + AddComponent(paddle, new PlayerComponent(message.PlayerIndex)); AddComponent(paddle, new PaddleMoveSpeedComponent(400)); AddComponent(paddle, new PositionComponent(message.Position)); AddComponent(paddle, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, message.Width, message.Height))); diff --git a/PongFE/Messages/DestroyMessage.cs b/PongFE/Messages/DestroyMessage.cs index a7e9eaf..50614b1 100644 --- a/PongFE/Messages/DestroyMessage.cs +++ b/PongFE/Messages/DestroyMessage.cs @@ -5,10 +5,12 @@ namespace PongFE.Messages public struct DestroyMessage : IMessage { public Entity Entity { get; } + public Entity DestroyedBy { get; } - public DestroyMessage(Entity entity) + public DestroyMessage(Entity entity, Entity destroyedBy) { Entity = entity; + DestroyedBy = destroyedBy; } } } diff --git a/PongFE/Messages/GoalBoundarySpawnMessage.cs b/PongFE/Messages/GoalBoundarySpawnMessage.cs index ef100aa..67f47a0 100644 --- a/PongFE/Messages/GoalBoundarySpawnMessage.cs +++ b/PongFE/Messages/GoalBoundarySpawnMessage.cs @@ -1,5 +1,6 @@ using Encompass; using MoonTools.Structs; +using PongFE.Enums; namespace PongFE.Messages { diff --git a/PongFE/Messages/ScoreMessage.cs b/PongFE/Messages/ScoreMessage.cs new file mode 100644 index 0000000..5828dc0 --- /dev/null +++ b/PongFE/Messages/ScoreMessage.cs @@ -0,0 +1,14 @@ +using Encompass; + +namespace PongFE.Messages +{ + public struct ScoreMessage : IMessage + { + public Entity Entity { get; } + + public ScoreMessage(Entity entity) + { + Entity = entity; + } + } +} diff --git a/PongFE/PongFEGame.cs b/PongFE/PongFEGame.cs index acf8e0a..c55b7b3 100644 --- a/PongFE/PongFEGame.cs +++ b/PongFE/PongFEGame.cs @@ -46,6 +46,7 @@ namespace PongFE WorldBuilder.AddEngine(new CollisionEngine()); WorldBuilder.AddEngine(new BounceEngine()); WorldBuilder.AddEngine(new DestroyEngine()); + WorldBuilder.AddEngine(new ScoreEngine()); WorldBuilder.AddEngine(new UpdatePositionEngine()); WorldBuilder.AddEngine(new UpdateVelocityEngine()); WorldBuilder.AddEngine(new ComputerControlEngine());