From 5512696a0440751685d532eacec7af0ec4fc0332 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Mon, 27 Nov 2023 17:33:05 +0300 Subject: [PATCH] feat: Basic Play Area --- Game/Behaviours/MovementBallBehaviour.cs | 29 ++++++++++++++++++++++-- Game/Behaviours/PlayAreaBehaviour.cs | 26 +++++++++++++++++++++ Game/Game.csproj | 1 + Game/Game1.cs | 6 ++++- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 Game/Behaviours/PlayAreaBehaviour.cs diff --git a/Game/Behaviours/MovementBallBehaviour.cs b/Game/Behaviours/MovementBallBehaviour.cs index dd75ac1..b7cd436 100644 --- a/Game/Behaviours/MovementBallBehaviour.cs +++ b/Game/Behaviours/MovementBallBehaviour.cs @@ -6,9 +6,10 @@ using Syntriax.Engine.Input; namespace Pong.Behaviours; -public class MovementBallBehaviour(Vector2 StartDirection, float Speed) : BehaviourOverride +public class MovementBallBehaviour(Vector2 StartDirection, PlayAreaBehaviour PlayAreaBehaviour, float Speed) : BehaviourOverride { - public Vector2 StartDirection { get; } = StartDirection; + public Vector2 StartDirection { get; private set; } = StartDirection; + public PlayAreaBehaviour PlayAreaBehaviour { get; } = PlayAreaBehaviour; public float Speed { get; set; } = Speed; protected override void OnInitialize() => StartDirection.Normalize(); @@ -16,5 +17,29 @@ public class MovementBallBehaviour(Vector2 StartDirection, float Speed) : Behavi protected override void OnUpdate(GameTime time) { GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed; + + float absY = MathF.Abs(GameObject.Transform.Position.Y); + float differenceY = absY - PlayAreaBehaviour.PlayArea.Y * 0.5f; + if (differenceY > 0f) + { + if (GameObject.Transform.Position.Y > 0f) + GameObject.Transform.Position -= Vector2.UnitY * differenceY * 2f; + else + GameObject.Transform.Position += Vector2.UnitY * differenceY * 2f; + + StartDirection = new(StartDirection.X, -StartDirection.Y); + } + + float absX = MathF.Abs(GameObject.Transform.Position.X); + float differenceX = absX - PlayAreaBehaviour.PlayArea.X * 0.5f; + if (differenceX > 0f) + { + if (GameObject.Transform.Position.X > 0f) + GameObject.Transform.Position -= Vector2.UnitX * differenceX * 2f; + else + GameObject.Transform.Position += Vector2.UnitX * differenceX * 2f; + + StartDirection = new(-StartDirection.X, StartDirection.Y); + } } } diff --git a/Game/Behaviours/PlayAreaBehaviour.cs b/Game/Behaviours/PlayAreaBehaviour.cs new file mode 100644 index 0000000..0b0765e --- /dev/null +++ b/Game/Behaviours/PlayAreaBehaviour.cs @@ -0,0 +1,26 @@ +using System; +using Microsoft.Xna.Framework; +using Syntriax.Engine.Core; + +namespace Pong.Behaviours; + +public class PlayAreaBehaviour : BehaviourOverride +{ + public Action? OnPlayAreaChanged { get; set; } = null; + + private Vector2 _playArea = Vector2.Zero; + + public Vector2 PlayArea + { + get => _playArea; + set + { + if (_playArea == value) + return; + + _playArea = value; + OnPlayAreaChanged?.Invoke(this); + } + } + +} diff --git a/Game/Game.csproj b/Game/Game.csproj index 4117ad7..9b23577 100644 --- a/Game/Game.csproj +++ b/Game/Game.csproj @@ -5,6 +5,7 @@ Major false false + enable app.manifest diff --git a/Game/Game1.cs b/Game/Game1.cs index 6c110ce..ece01ab 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -51,11 +51,15 @@ public class Game1 : Game gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour(); gameManager.Camera.Viewport = GraphicsDevice.Viewport; + IGameObject gameObjectPlayArea = gameManager.InstantiateGameObject(); + PlayAreaBehaviour playAreaBehaviour = gameObjectPlayArea.BehaviourController.AddBehaviour(); + playAreaBehaviour.PlayArea = new Vector2(_graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight); + IGameObject gameObjectBall = gameManager.InstantiateGameObject(); gameObjectBall.Name = "Ball"; gameObjectBall.Transform.Position = Vector2.Zero; gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); - // gameObjectBall.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), 100f); + gameObjectBall.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), playAreaBehaviour, 100f); gameObjectBall.BehaviourController.AddBehaviour().Assign(spriteBall); IGameObject gameObjectLeft = gameManager.InstantiateGameObject();