From b7d49e41c38c6421610eb1b652669580b6d6a40d Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 30 Jan 2024 12:43:30 +0300 Subject: [PATCH] refactor: General --- Engine | 2 +- Game/Behaviours/PongBall.cs | 72 +++++++++++++++++++ .../{PongScoreboard.cs => PongManager.cs} | 21 +++--- Game/Behaviours/PongTextBehaviour.cs | 27 +++++++ Game/Behaviours/TextBehaviour.cs | 1 + Game/Game1.cs | 36 ++-------- 6 files changed, 119 insertions(+), 40 deletions(-) create mode 100644 Game/Behaviours/PongBall.cs rename Game/Behaviours/{PongScoreboard.cs => PongManager.cs} (67%) create mode 100644 Game/Behaviours/PongTextBehaviour.cs diff --git a/Engine b/Engine index 4000e76..0766635 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit 4000e761a7827bb76d657b10a3341248b90b0aa5 +Subproject commit 07666359f26d4761914aac05d73fdbe0020b0b6a diff --git a/Game/Behaviours/PongBall.cs b/Game/Behaviours/PongBall.cs new file mode 100644 index 0000000..736b214 --- /dev/null +++ b/Game/Behaviours/PongBall.cs @@ -0,0 +1,72 @@ +using System; + +using Syntriax.Engine.Core; +using Syntriax.Engine.Physics2D; +using Syntriax.Engine.Physics2D.Abstract; + +namespace Pong.Behaviours; + +public class PongBall : BehaviourOverride +{ + public Vector2D StartDirection { get; private set; } = Vector2D.Zero; + public float Speed { get; set; } = 500f; + public float SpeedUpMultiplier { get; set; } = .0125f; + + private IRigidBody2D rigidBody = null!; + + public PongBall(Vector2D startDirection, float speed) + { + StartDirection = Vector2D.Normalize(startDirection); + Speed = speed; + } + public PongBall() { } + + protected override void OnFirstActiveFrame() + { + if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody)) + throw new Exception($"{nameof(IRigidBody2D)} is missing on {GameObject.Name}."); + if (!BehaviourController.TryGetBehaviour(out ICollider2D? foundCollider)) + throw new Exception($"{nameof(ICollider2D)} is missing on {GameObject.Name}."); + + foundCollider.OnCollisionDetected += OnCollisionDetected; + + rigidBody = foundRigidBody; + + if (GameObject.GameManager.TryFindBehaviour(out PongManager? pongManager)) + { + pongManager.OnReset += ResetBall; + pongManager.OnScored += ResetBall; + pongManager.OnFinished += DisableBall; + } + } + + private void DisableBall(PongManager pongManager) + { + BehaviourController.GameObject.Transform.Position = Vector2D.Zero; + rigidBody.Velocity = Vector2D.Zero; + } + + private void ResetBall(PongManager pongManager) + { + StateEnable.Enabled = true; + BehaviourController.GameObject.Transform.Position = Vector2D.Zero; + rigidBody.Velocity = Vector2D.One.Normalized * Speed; + } + + protected override void OnUpdate() + { + if (rigidBody.Velocity.MagnitudeSquared <= 0.01f) + return; + + Vector2D speedUp = rigidBody.Velocity.Normalized * (float)Time.Elapsed.TotalMilliseconds; + rigidBody.Velocity += speedUp * SpeedUpMultiplier; + } + + private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information) + { + if (Syntriax.Engine.Core.Math.Abs(information.Normal.Dot(Vector2D.Right)) > .25) + rigidBody.Velocity = information.Left.Transform.Position.FromTo(information.Right.Transform.Position).Normalized * rigidBody.Velocity.Magnitude; + else + rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal); + } +} diff --git a/Game/Behaviours/PongScoreboard.cs b/Game/Behaviours/PongManager.cs similarity index 67% rename from Game/Behaviours/PongScoreboard.cs rename to Game/Behaviours/PongManager.cs index 5322ba4..54b1cd4 100644 --- a/Game/Behaviours/PongScoreboard.cs +++ b/Game/Behaviours/PongManager.cs @@ -3,22 +3,23 @@ using System; using Microsoft.Xna.Framework.Input; using Syntriax.Engine.Core; -using Syntriax.Engine.Input; namespace Pong.Behaviours; -public class PongScoreboard(int WinScore) : BehaviourOverride +public class PongManager : BehaviourOverride { - public Action? OnReset { get; set; } = null; - public Action? OnFinished { get; set; } = null; - public Action? OnScored { get; set; } = null; + public Action? OnReset { get; set; } = null; + public Action? OnFinished { get; set; } = null; + public Action? OnScored { get; set; } = null; public int ScoreLeft { get; private set; } = 0; public int ScoreRight { get; private set; } = 0; public int ScoreSum => ScoreLeft + ScoreRight; - public int WinScore { get; } = WinScore; + public int WinScore { get; } = 5; + public PongManager() => WinScore = 5; + public PongManager(int winScore) => WinScore = winScore; protected override void OnFirstActiveFrame() { @@ -34,7 +35,7 @@ public class PongScoreboard(int WinScore) : BehaviourOverride public void ScoreToLeft() { ScoreLeft++; - OnScored?.Invoke(); + OnScored?.Invoke(this); CheckFinish(); } @@ -42,7 +43,7 @@ public class PongScoreboard(int WinScore) : BehaviourOverride public void ScoreToRight() { ScoreRight++; - OnScored?.Invoke(); + OnScored?.Invoke(this); CheckFinish(); } @@ -50,7 +51,7 @@ public class PongScoreboard(int WinScore) : BehaviourOverride public void Reset() { ScoreLeft = ScoreRight = 0; - OnReset?.Invoke(); + OnReset?.Invoke(this); } private void CheckFinish() @@ -58,6 +59,6 @@ public class PongScoreboard(int WinScore) : BehaviourOverride int halfwayScore = (int)(WinScore * .5f); if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore) - OnFinished?.Invoke(); + OnFinished?.Invoke(this); } } diff --git a/Game/Behaviours/PongTextBehaviour.cs b/Game/Behaviours/PongTextBehaviour.cs new file mode 100644 index 0000000..666a65b --- /dev/null +++ b/Game/Behaviours/PongTextBehaviour.cs @@ -0,0 +1,27 @@ +using Syntriax.Engine.Core; + +namespace Pong.Behaviours; + +public class PongTextBehaviour(bool IsLeft) : TextBehaviour +{ + public bool IsLeft { get; } = IsLeft; + + private PongManager? pongManager = null; + + protected override void OnFirstActiveFrame() + { + if (!GameObject.GameManager.TryFindBehaviour(out pongManager)) + return; + + pongManager.OnScored += UpdateScores; + pongManager.OnReset += UpdateScores; + } + + private void UpdateScores(PongManager pongManager) + { + if (IsLeft) + Text = pongManager.ScoreLeft.ToString(); + else + Text = pongManager.ScoreRight.ToString(); + } +} diff --git a/Game/Behaviours/TextBehaviour.cs b/Game/Behaviours/TextBehaviour.cs index 80a9030..52a51c9 100644 --- a/Game/Behaviours/TextBehaviour.cs +++ b/Game/Behaviours/TextBehaviour.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; + using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; diff --git a/Game/Game1.cs b/Game/Game1.cs index f26b6de..4d84ac7 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -23,7 +23,7 @@ public class Game1 : Game public static GameManager gameManager = null!; private MonoGameCameraBehaviour cameraBehaviour = null!; - private PongScoreboard pongScoreboard; + private PongManager pongManager; public Game1() { @@ -72,7 +72,7 @@ public class Game1 : Game IGameObject gameObjectPongManager = gameManager.InstantiateGameObject(); gameObjectPongManager.Name = "Pong Game Manager"; - pongScoreboard = gameObjectPongManager.BehaviourController.AddBehaviour(5); + pongManager = gameObjectPongManager.BehaviourController.AddBehaviour(5); //////////////////////////////////////////////////////////////////////////////////// @@ -81,7 +81,7 @@ public class Game1 : Game gameObjectBall.Transform.Position = new Vector2D(0, 0f); gameObjectBall.Transform.Scale = new Vector2D(10f, 10f); gameObjectBall.BehaviourController.AddBehaviour(new Circle(Vector2D.Zero, 1f)); - MovementBallBehaviour movementBallBehaviour = gameObjectBall.BehaviourController.AddBehaviour(); + PongBall ballBehaviour = gameObjectBall.BehaviourController.AddBehaviour(); RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour(); engine.AddRigidBody(rigidBodyBall); @@ -131,7 +131,7 @@ public class Game1 : Game gameObjectWallRight.Name = "WallRight"; gameObjectWallRight.Transform.Position = new Vector2D(532f, 0f); gameObjectWallRight.Transform.Scale = new Vector2D(20f, 328f); - gameObjectWallRight.BehaviourController.AddBehaviour((Action)pongScoreboard.ScoreToLeft); + gameObjectWallRight.BehaviourController.AddBehaviour((Action)pongManager.ScoreToLeft); gameObjectWallRight.BehaviourController.AddBehaviour(Shape.Box); RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour(); rigidBodyWallRight.IsStatic = true; @@ -141,7 +141,7 @@ public class Game1 : Game gameObjectWallLeft.Name = "WallLeft"; gameObjectWallLeft.Transform.Position = new Vector2D(-532f, 0f); gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 328f); - gameObjectWallLeft.BehaviourController.AddBehaviour((Action)pongScoreboard.ScoreToRight); + gameObjectWallLeft.BehaviourController.AddBehaviour((Action)pongManager.ScoreToRight); gameObjectWallLeft.BehaviourController.AddBehaviour(Shape.Box); RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour(); rigidBodyWallLeft.IsStatic = true; @@ -153,38 +153,16 @@ public class Game1 : Game gameObjectLeftScoreText.Name = "Score Left"; gameObjectLeftScoreText.Transform.Position = new Vector2D(-250f, 250f); gameObjectLeftScoreText.Transform.Scale = Vector2D.One * .25f; - TextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour(); + PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour(true); textBehaviourLeft.Font = spriteFont; textBehaviourLeft.Text = "0"; IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject(); gameObjectRightScoreText.Name = "Score Right"; gameObjectRightScoreText.Transform.Position = new Vector2D(250f, 250f); gameObjectRightScoreText.Transform.Scale = Vector2D.One * .25f; - TextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour(); + PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour(false); textBehaviourRight.Font = spriteFont; textBehaviourRight.Text = "0"; - - pongScoreboard.OnScored += () => - { - gameObjectBall.Transform.Position = Vector2D.Zero; - rigidBodyBall.Velocity = rigidBodyBall.Velocity.Normalized * movementBallBehaviour.Speed; - textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString(); - textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString(); - }; - pongScoreboard.OnFinished += () => - { - rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero; - rigidBodyBall.Velocity = Vector2D.Zero; - }; - - pongScoreboard.OnReset += () => - { - gameObjectBall.StateEnable.Enabled = true; - rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero; - rigidBodyBall.Velocity = Vector2D.One.Normalized * movementBallBehaviour.Speed; - textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString(); - textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString(); - }; } protected override void Update(GameTime gameTime)