Engine-Pong/Game/Behaviours/TextScoreBehaviour.cs

34 lines
901 B
C#
Raw Normal View History

using Microsoft.Xna.Framework.Graphics;
2024-01-30 12:43:30 +03:00
using Syntriax.Engine.Core;
namespace Pong.Behaviours;
2024-01-31 12:17:43 +03:00
public class TextScoreBehaviour : TextBehaviour
2024-01-30 12:43:30 +03:00
{
public bool IsLeft { get; }
2024-01-30 12:43:30 +03:00
2024-01-31 12:17:43 +03:00
private PongManagerBehaviour? pongManager = null;
2024-01-30 12:43:30 +03:00
protected override void OnFirstActiveFrame()
{
if (!GameObject.GameManager.TryFindBehaviour(out pongManager))
return;
pongManager.OnScored += UpdateScores;
pongManager.OnReset += UpdateScores;
UpdateScores(pongManager);
2024-01-30 12:43:30 +03:00
}
2024-01-31 12:17:43 +03:00
private void UpdateScores(PongManagerBehaviour pongManager)
2024-01-30 12:43:30 +03:00
{
if (IsLeft)
Text = pongManager.ScoreLeft.ToString();
else
Text = pongManager.ScoreRight.ToString();
}
2024-01-31 12:51:23 +03:00
public TextScoreBehaviour(bool IsLeft) => this.IsLeft = IsLeft;
public TextScoreBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft;
2024-01-30 12:43:30 +03:00
}