refactor: Text Behaviour Font Constructor

This commit is contained in:
Syntriax 2024-01-30 20:56:43 +03:00
parent 80ee5d760a
commit bc7925dbab
3 changed files with 13 additions and 8 deletions

View File

@ -1,13 +1,17 @@
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
namespace Pong.Behaviours; 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; 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() protected override void OnFirstActiveFrame()
{ {
if (!GameObject.GameManager.TryFindBehaviour(out pongManager)) if (!GameObject.GameManager.TryFindBehaviour(out pongManager))
@ -15,6 +19,8 @@ public class PongTextBehaviour(bool IsLeft) : TextBehaviour
pongManager.OnScored += UpdateScores; pongManager.OnScored += UpdateScores;
pongManager.OnReset += UpdateScores; pongManager.OnReset += UpdateScores;
UpdateScores(pongManager);
} }
private void UpdateScores(PongManager pongManager) private void UpdateScores(PongManager pongManager)

View File

@ -8,6 +8,9 @@ namespace Pong.Behaviours;
public class TextBehaviour : BehaviourOverride, IDisplayable public class TextBehaviour : BehaviourOverride, IDisplayable
{ {
public TextBehaviour() { }
public TextBehaviour(SpriteFont font) => Font = font;
public SpriteFont? Font { get; set; } = null; public SpriteFont? Font { get; set; } = null;
public int Size { get; set; } = 16; public int Size { get; set; } = 16;
public string Text { get; set; } = string.Empty; public string Text { get; set; } = string.Empty;

View File

@ -125,15 +125,11 @@ public class GamePong : Game
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left"); IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left");
gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f); gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f);
PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(true); gameObjectLeftScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(true, spriteFont);
textBehaviourLeft.Font = spriteFont;
textBehaviourLeft.Text = "0";
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right"); IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f); gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(false); gameObjectRightScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(false, spriteFont);
textBehaviourRight.Font = spriteFont;
textBehaviourRight.Text = "0";
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)