From bc7925dbabd3272990c27d7c5ffe243ea832b648 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 30 Jan 2024 20:56:43 +0300 Subject: [PATCH] refactor: Text Behaviour Font Constructor --- Game/Behaviours/PongTextBehaviour.cs | 10 ++++++++-- Game/Behaviours/TextBehaviour.cs | 3 +++ Game/GamePong.cs | 8 ++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Game/Behaviours/PongTextBehaviour.cs b/Game/Behaviours/PongTextBehaviour.cs index 666a65b..dc050d2 100644 --- a/Game/Behaviours/PongTextBehaviour.cs +++ b/Game/Behaviours/PongTextBehaviour.cs @@ -1,13 +1,17 @@ +using Microsoft.Xna.Framework.Graphics; using Syntriax.Engine.Core; namespace Pong.Behaviours; -public class PongTextBehaviour(bool IsLeft) : TextBehaviour +public class PongTextBehaviour : TextBehaviour { - public bool IsLeft { get; } = IsLeft; + public bool IsLeft { get; } private PongManager? pongManager = null; + public PongTextBehaviour(bool IsLeft) => this.IsLeft = IsLeft; + public PongTextBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft; + protected override void OnFirstActiveFrame() { if (!GameObject.GameManager.TryFindBehaviour(out pongManager)) @@ -15,6 +19,8 @@ public class PongTextBehaviour(bool IsLeft) : TextBehaviour pongManager.OnScored += UpdateScores; pongManager.OnReset += UpdateScores; + + UpdateScores(pongManager); } private void UpdateScores(PongManager pongManager) diff --git a/Game/Behaviours/TextBehaviour.cs b/Game/Behaviours/TextBehaviour.cs index 52a51c9..da97b6a 100644 --- a/Game/Behaviours/TextBehaviour.cs +++ b/Game/Behaviours/TextBehaviour.cs @@ -8,6 +8,9 @@ namespace Pong.Behaviours; public class TextBehaviour : BehaviourOverride, IDisplayable { + public TextBehaviour() { } + public TextBehaviour(SpriteFont font) => Font = font; + public SpriteFont? Font { get; set; } = null; public int Size { get; set; } = 16; public string Text { get; set; } = string.Empty; diff --git a/Game/GamePong.cs b/Game/GamePong.cs index a441407..8ce1b4b 100644 --- a/Game/GamePong.cs +++ b/Game/GamePong.cs @@ -125,15 +125,11 @@ public class GamePong : Game IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Left"); gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f); - PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour(true); - textBehaviourLeft.Font = spriteFont; - textBehaviourLeft.Text = "0"; + gameObjectLeftScoreText.BehaviourController.AddBehaviour(true, spriteFont); IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Right"); gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f); - PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour(false); - textBehaviourRight.Font = spriteFont; - textBehaviourRight.Text = "0"; + gameObjectRightScoreText.BehaviourController.AddBehaviour(false, spriteFont); } protected override void Update(GameTime gameTime)