From a86f8fd8a212bb3a154fc65a3bf921c02d08be92 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 15 Jul 2020 13:08:58 -0700 Subject: [PATCH] bounce and paddle stuff --- PongFE/Components/CanBeTrackedComponent.cs | 6 +++ PongFE/Components/ComputerControlComponent.cs | 15 ++++++ PongFE/Components/PlayerInputComponent.cs | 7 +-- PongFE/Components/Texture2DComponent.cs | 5 +- PongFE/Engines/BounceEngine.cs | 1 + PongFE/Engines/CollisionEngine.cs | 1 + PongFE/Engines/ComputerControlEngine.cs | 46 +++++++++++++++++ PongFE/Engines/InputEngine.cs | 1 + PongFE/Engines/MotionEngine.cs | 1 + PongFE/Engines/PaddleMovementEngine.cs | 1 + PongFE/Engines/Spawners/BallSpawner.cs | 9 ++-- PongFE/Engines/Spawners/PaddleSpawner.cs | 36 +++++++++++++ PongFE/Enums/Enums.cs | 26 ++++++++++ PongFE/Extensions/Vector2Extensions.cs | 12 +++++ PongFE/Messages/BallSpawnMessage.cs | 6 ++- PongFE/Messages/BounceMessage.cs | 1 + PongFE/Messages/CollisionMessage.cs | 7 +-- PongFE/Messages/PaddleMoveMessage.cs | 7 +-- PongFE/Messages/PaddleSpawnMessage.cs | 30 +++++++++++ PongFE/PongFEGame.cs | 51 ++++++++++--------- PongFE/Renderers/Texture2DRenderer.cs | 4 +- 21 files changed, 222 insertions(+), 51 deletions(-) create mode 100644 PongFE/Components/CanBeTrackedComponent.cs create mode 100644 PongFE/Components/ComputerControlComponent.cs create mode 100644 PongFE/Engines/ComputerControlEngine.cs create mode 100644 PongFE/Engines/Spawners/PaddleSpawner.cs create mode 100644 PongFE/Enums/Enums.cs create mode 100644 PongFE/Extensions/Vector2Extensions.cs create mode 100644 PongFE/Messages/PaddleSpawnMessage.cs diff --git a/PongFE/Components/CanBeTrackedComponent.cs b/PongFE/Components/CanBeTrackedComponent.cs new file mode 100644 index 0000000..661aadd --- /dev/null +++ b/PongFE/Components/CanBeTrackedComponent.cs @@ -0,0 +1,6 @@ +using Encompass; + +namespace PongFE.Components +{ + public struct CanBeTrackedComponent : IComponent { } +} diff --git a/PongFE/Components/ComputerControlComponent.cs b/PongFE/Components/ComputerControlComponent.cs new file mode 100644 index 0000000..1e55192 --- /dev/null +++ b/PongFE/Components/ComputerControlComponent.cs @@ -0,0 +1,15 @@ +using Encompass; +using PongFE.Enums; + +namespace PongFE.Components +{ + public struct ComputerControlComponent : IComponent + { + public PlayerIndex PlayerIndex { get; } + + public ComputerControlComponent(PlayerIndex playerIndex) + { + PlayerIndex = playerIndex; + } + } +} diff --git a/PongFE/Components/PlayerInputComponent.cs b/PongFE/Components/PlayerInputComponent.cs index 04e07bb..1b2e81b 100644 --- a/PongFE/Components/PlayerInputComponent.cs +++ b/PongFE/Components/PlayerInputComponent.cs @@ -1,13 +1,8 @@ using Encompass; +using PongFE.Enums; namespace PongFE.Components { - public enum PlayerIndex - { - One, - Two - } - public struct PlayerInputComponent : IComponent { public PlayerIndex PlayerIndex { get; } diff --git a/PongFE/Components/Texture2DComponent.cs b/PongFE/Components/Texture2DComponent.cs index 99f3fad..c0e596f 100644 --- a/PongFE/Components/Texture2DComponent.cs +++ b/PongFE/Components/Texture2DComponent.cs @@ -1,3 +1,4 @@ +using System.Numerics; using Encompass; using Microsoft.Xna.Framework.Graphics; @@ -6,11 +7,13 @@ namespace PongFE.Components public struct Texture2DComponent : IComponent, IDrawableComponent { public Texture2D Texture { get; } + public Vector2 Scale { get; } public int Layer { get; } - public Texture2DComponent(Texture2D texture, int layer) + public Texture2DComponent(Texture2D texture, int layer, Vector2 scale) { Texture = texture; + Scale = scale; Layer = layer; } } diff --git a/PongFE/Engines/BounceEngine.cs b/PongFE/Engines/BounceEngine.cs index 157180a..9632d9d 100644 --- a/PongFE/Engines/BounceEngine.cs +++ b/PongFE/Engines/BounceEngine.cs @@ -1,6 +1,7 @@ using System.Numerics; using Encompass; using PongFE.Components; +using PongFE.Enums; using PongFE.Messages; namespace PongFE.Engines diff --git a/PongFE/Engines/CollisionEngine.cs b/PongFE/Engines/CollisionEngine.cs index e2e5d52..4294584 100644 --- a/PongFE/Engines/CollisionEngine.cs +++ b/PongFE/Engines/CollisionEngine.cs @@ -1,5 +1,6 @@ using Encompass; using PongFE.Components; +using PongFE.Enums; using PongFE.Messages; namespace PongFE.Engines diff --git a/PongFE/Engines/ComputerControlEngine.cs b/PongFE/Engines/ComputerControlEngine.cs new file mode 100644 index 0000000..a5ce5c5 --- /dev/null +++ b/PongFE/Engines/ComputerControlEngine.cs @@ -0,0 +1,46 @@ +using Encompass; +using PongFE.Components; +using PongFE.Enums; +using PongFE.Messages; + +namespace PongFE.Engines +{ + [Reads( + typeof(ComputerControlComponent), + typeof(PositionComponent), + typeof(CanBeTrackedComponent) + )] + [Sends(typeof(PaddleMoveMessage))] + public class ComputerControlEngine : Engine + { + public override void Update(double dt) + { + foreach (ref readonly var entity in ReadEntities()) + { + if (HasComponent(entity)) + { + ref readonly var trackerPositionComponent = ref GetComponent(entity); + + if (SomeComponent()) + { + ref readonly var trackedEntity = ref ReadEntity(); + + if (HasComponent(trackedEntity)) + { + ref readonly var trackedPositionComponent = ref GetComponent(trackedEntity); + + if (trackerPositionComponent.Position.Y - trackedPositionComponent.Position.Y > 20) + { + SendMessage(new PaddleMoveMessage(entity, PaddleMoveDirection.Up)); + } + else if (trackerPositionComponent.Position.Y - trackedPositionComponent.Position.Y < -20) + { + SendMessage(new PaddleMoveMessage(entity, PaddleMoveDirection.Down)); + } + } + } + } + } + } + } +} diff --git a/PongFE/Engines/InputEngine.cs b/PongFE/Engines/InputEngine.cs index 26da613..41a46f8 100644 --- a/PongFE/Engines/InputEngine.cs +++ b/PongFE/Engines/InputEngine.cs @@ -1,6 +1,7 @@ using Encompass; using Microsoft.Xna.Framework.Input; using PongFE.Components; +using PongFE.Enums; using PongFE.Messages; namespace PongFE.Engines diff --git a/PongFE/Engines/MotionEngine.cs b/PongFE/Engines/MotionEngine.cs index 7fda088..16f8b2a 100644 --- a/PongFE/Engines/MotionEngine.cs +++ b/PongFE/Engines/MotionEngine.cs @@ -4,6 +4,7 @@ using Encompass; using MoonTools.Bonk; using MoonTools.Structs; using PongFE.Components; +using PongFE.Enums; using PongFE.Messages; namespace PongFE.Engines diff --git a/PongFE/Engines/PaddleMovementEngine.cs b/PongFE/Engines/PaddleMovementEngine.cs index 6cc78ce..7533fa1 100644 --- a/PongFE/Engines/PaddleMovementEngine.cs +++ b/PongFE/Engines/PaddleMovementEngine.cs @@ -1,5 +1,6 @@ using Encompass; using PongFE.Components; +using PongFE.Enums; using PongFE.Messages; namespace PongFE.Engines diff --git a/PongFE/Engines/Spawners/BallSpawner.cs b/PongFE/Engines/Spawners/BallSpawner.cs index 2f018d3..1956f46 100644 --- a/PongFE/Engines/Spawners/BallSpawner.cs +++ b/PongFE/Engines/Spawners/BallSpawner.cs @@ -7,11 +7,11 @@ namespace PongFE.Spawners { public class BallSpawner : Spawner { - private Texture2D BallTexture { get; } + private Texture2D WhitePixel { get; } - public BallSpawner(Texture2D ballTexture) + public BallSpawner(Texture2D whitePixel) { - BallTexture = ballTexture; + WhitePixel = whitePixel; } protected override void Spawn(BallSpawnMessage message) @@ -20,9 +20,10 @@ namespace PongFE.Spawners AddComponent(ball, new PositionComponent(message.Position)); AddComponent(ball, new VelocityComponent(message.Velocity)); AddComponent(ball, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 16, 16))); - AddComponent(ball, new Texture2DComponent(BallTexture, 0)); + AddComponent(ball, new Texture2DComponent(WhitePixel, 0, new System.Numerics.Vector2(message.Width, message.Height))); AddComponent(ball, new CanBeBouncedComponent()); AddComponent(ball, new BounceResponseComponent()); + AddComponent(ball, new CanBeTrackedComponent()); } } } diff --git a/PongFE/Engines/Spawners/PaddleSpawner.cs b/PongFE/Engines/Spawners/PaddleSpawner.cs new file mode 100644 index 0000000..5d2d8db --- /dev/null +++ b/PongFE/Engines/Spawners/PaddleSpawner.cs @@ -0,0 +1,36 @@ +using Encompass; +using Microsoft.Xna.Framework.Graphics; +using PongFE.Components; +using PongFE.Enums; +using PongFE.Messages; + +namespace PongFE.Spawners +{ + public class PaddleSpawner : Spawner + { + private Texture2D WhitePixel { get; } + + public PaddleSpawner(Texture2D whitePixel) + { + WhitePixel = whitePixel; + } + + protected override void Spawn(PaddleSpawnMessage message) + { + var paddle = CreateEntity(); + if (message.PaddleControl == PaddleControl.Player) + { + AddComponent(paddle, new PlayerInputComponent(message.PlayerIndex)); + } + else + { + AddComponent(paddle, new ComputerControlComponent(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))); + AddComponent(paddle, new CanCauseBounceComponent()); + AddComponent(paddle, new Texture2DComponent(WhitePixel, 0, new System.Numerics.Vector2(message.Width, message.Height))); + } + } +} diff --git a/PongFE/Enums/Enums.cs b/PongFE/Enums/Enums.cs new file mode 100644 index 0000000..eda8cf0 --- /dev/null +++ b/PongFE/Enums/Enums.cs @@ -0,0 +1,26 @@ +namespace PongFE.Enums +{ + public enum PlayerIndex + { + One, + Two + } + + public enum HitOrientation + { + Horizontal, + Vertical + } + + public enum PaddleMoveDirection + { + Up, + Down + } + + public enum PaddleControl + { + Player, + Computer + } +} diff --git a/PongFE/Extensions/Vector2Extensions.cs b/PongFE/Extensions/Vector2Extensions.cs new file mode 100644 index 0000000..bd4b7fa --- /dev/null +++ b/PongFE/Extensions/Vector2Extensions.cs @@ -0,0 +1,12 @@ +using MoonTools.Structs; + +namespace PongFE.Extensions +{ + public static class Vector2Extensions + { + public static Microsoft.Xna.Framework.Vector2 ToXNAVector(this System.Numerics.Vector2 vector) + { + return new Microsoft.Xna.Framework.Vector2(vector.X, vector.Y); + } + } +} diff --git a/PongFE/Messages/BallSpawnMessage.cs b/PongFE/Messages/BallSpawnMessage.cs index ecf6795..3e18eea 100644 --- a/PongFE/Messages/BallSpawnMessage.cs +++ b/PongFE/Messages/BallSpawnMessage.cs @@ -8,11 +8,15 @@ namespace PongFE.Messages { public Position2D Position { get; } public Vector2 Velocity { get; } + public int Width { get; } + public int Height { get; } - public BallSpawnMessage(Position2D position, Vector2 velocity) + public BallSpawnMessage(Position2D position, Vector2 velocity, int width, int height) { Position = position; Velocity = velocity; + Width = width; + Height = height; } } } diff --git a/PongFE/Messages/BounceMessage.cs b/PongFE/Messages/BounceMessage.cs index af41abd..d2c241b 100644 --- a/PongFE/Messages/BounceMessage.cs +++ b/PongFE/Messages/BounceMessage.cs @@ -1,4 +1,5 @@ using Encompass; +using PongFE.Enums; namespace PongFE.Messages { diff --git a/PongFE/Messages/CollisionMessage.cs b/PongFE/Messages/CollisionMessage.cs index e06b837..deb0f22 100644 --- a/PongFE/Messages/CollisionMessage.cs +++ b/PongFE/Messages/CollisionMessage.cs @@ -1,13 +1,8 @@ using Encompass; +using PongFE.Enums; namespace PongFE.Messages { - public enum HitOrientation - { - Horizontal, - Vertical - } - public struct CollisionMessage : IMessage { public Entity EntityA { get; } diff --git a/PongFE/Messages/PaddleMoveMessage.cs b/PongFE/Messages/PaddleMoveMessage.cs index f3ebe56..45c1bf8 100644 --- a/PongFE/Messages/PaddleMoveMessage.cs +++ b/PongFE/Messages/PaddleMoveMessage.cs @@ -1,13 +1,8 @@ using Encompass; +using PongFE.Enums; namespace PongFE.Messages { - public enum PaddleMoveDirection - { - Up, - Down - } - public struct PaddleMoveMessage : IMessage, IHasEntity { public Entity Entity { get; } diff --git a/PongFE/Messages/PaddleSpawnMessage.cs b/PongFE/Messages/PaddleSpawnMessage.cs new file mode 100644 index 0000000..5276be5 --- /dev/null +++ b/PongFE/Messages/PaddleSpawnMessage.cs @@ -0,0 +1,30 @@ +using Encompass; +using MoonTools.Structs; +using PongFE.Enums; + +namespace PongFE.Messages +{ + public struct PaddleSpawnMessage : IMessage + { + public Position2D Position { get; } + public PlayerIndex PlayerIndex { get; } + public PaddleControl PaddleControl { get; } + public int Width { get; } + public int Height { get; } + + public PaddleSpawnMessage( + Position2D position, + PlayerIndex playerIndex, + PaddleControl paddleControl, + int width, + int height + ) + { + Position = position; + PlayerIndex = playerIndex; + PaddleControl = paddleControl; + Width = width; + Height = height; + } + } +} diff --git a/PongFE/PongFEGame.cs b/PongFE/PongFEGame.cs index 9976f06..81b505e 100644 --- a/PongFE/PongFEGame.cs +++ b/PongFE/PongFEGame.cs @@ -1,8 +1,8 @@ using Encompass; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using PongFE.Components; using PongFE.Engines; +using PongFE.Enums; using PongFE.Messages; using PongFE.Renderers; using PongFE.Spawners; @@ -18,8 +18,6 @@ namespace PongFE SpriteBatch SpriteBatch { get; set; } Texture2D WhitePixel { get; set; } - RenderTarget2D PaddleTexture { get; set; } - RenderTarget2D BallTexture { get; set; } public PongFEGame() { @@ -41,19 +39,6 @@ namespace PongFE WhitePixel = new Texture2D(GraphicsDevice, 1, 1); WhitePixel.SetData(new Color[] { Color.White }); - PaddleTexture = new RenderTarget2D(GraphicsDevice, 20, 80); - GraphicsDevice.SetRenderTarget(PaddleTexture); - SpriteBatch.Begin(); - SpriteBatch.Draw(WhitePixel, new Rectangle(0, 0, 20, 80), Color.White); - SpriteBatch.End(); - - BallTexture = new RenderTarget2D(GraphicsDevice, 16, 16); - GraphicsDevice.SetRenderTarget(BallTexture); - SpriteBatch.Begin(); - SpriteBatch.Draw(WhitePixel, new Rectangle(0, 0, 16, 16), Color.White); - SpriteBatch.End(); - GraphicsDevice.SetRenderTarget(null); - WorldBuilder.AddEngine(new InputEngine()); WorldBuilder.AddEngine(new PaddleMovementEngine()); WorldBuilder.AddEngine(new VelocityEngine()); @@ -62,24 +47,40 @@ namespace PongFE WorldBuilder.AddEngine(new BounceEngine()); WorldBuilder.AddEngine(new UpdatePositionEngine()); WorldBuilder.AddEngine(new UpdateVelocityEngine()); + WorldBuilder.AddEngine(new ComputerControlEngine()); - WorldBuilder.AddEngine(new BallSpawner(BallTexture)); + WorldBuilder.AddEngine(new BallSpawner(WhitePixel)); WorldBuilder.AddEngine(new BoundarySpawner()); + WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel)); WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch)); - var paddle = WorldBuilder.CreateEntity(); - WorldBuilder.SetComponent(paddle, new PlayerInputComponent(PongFE.Components.PlayerIndex.One)); - WorldBuilder.SetComponent(paddle, new PaddleMoveSpeedComponent(400)); - WorldBuilder.SetComponent(paddle, new PositionComponent(new MoonTools.Structs.Position2D(5, 5))); - WorldBuilder.SetComponent(paddle, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 20, 80))); - WorldBuilder.SetComponent(paddle, new CanCauseBounceComponent()); - WorldBuilder.SetComponent(paddle, new Texture2DComponent(PaddleTexture, 0)); + WorldBuilder.SendMessage( + new PaddleSpawnMessage( + new MoonTools.Structs.Position2D(5, 5), + Enums.PlayerIndex.One, + PaddleControl.Player, + 20, + 80 + ) + ); + + WorldBuilder.SendMessage( + new PaddleSpawnMessage( + new MoonTools.Structs.Position2D(1255, 5), + Enums.PlayerIndex.Two, + PaddleControl.Computer, + 20, + 80 + ) + ); WorldBuilder.SendMessage( new BallSpawnMessage( new MoonTools.Structs.Position2D(640, 360), - new System.Numerics.Vector2(-200, -50) + new System.Numerics.Vector2(-300, 200), + 16, + 16 ) ); diff --git a/PongFE/Renderers/Texture2DRenderer.cs b/PongFE/Renderers/Texture2DRenderer.cs index 5faf5bf..24ee2d3 100644 --- a/PongFE/Renderers/Texture2DRenderer.cs +++ b/PongFE/Renderers/Texture2DRenderer.cs @@ -21,12 +21,12 @@ namespace PongFE.Renderers _spriteBatch.Draw( textureComponent.Texture, - positionComponent.Position.Truncated().ToXNAVector(), + positionComponent.Position.ToXNAVector(), null, Color.White, 0, Vector2.Zero, - Vector2.One, + textureComponent.Scale.ToXNAVector(), SpriteEffects.None, 0 );