PongFE/PongFE/Engines/GameWinEngine.cs

56 lines
1.7 KiB
C#

using Encompass;
using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages;
using SpriteFontPlus;
namespace PongFE.Engines
{
[Reads(typeof(PlayAreaComponent))]
[Receives(typeof(GameWinMessage))]
[Sends(typeof(UITextSpawnMessage), typeof(ChangeGameStateMessage))]
public class GameWinEngine : Engine
{
public DynamicSpriteFont Font { get; }
private readonly string _playerOneWinText = "Player 1 Wins!";
private readonly string _playerTwoWinText = "Player 2 Wins!";
public GameWinEngine(DynamicSpriteFont font)
{
Font = font;
}
public override void Update(double dt)
{
if (SomeMessage<GameWinMessage>())
{
ref readonly var gameWinMessage = ref ReadMessage<GameWinMessage>();
ref readonly var playAreaComponent = ref ReadComponent<PlayAreaComponent>();
string winText;
if (gameWinMessage.PlayerIndex == PlayerIndex.One)
{
winText = _playerOneWinText;
}
else
{
winText = _playerTwoWinText;
}
var textDimensions = Font.MeasureString(winText);
SendMessage(new UITextSpawnMessage(
new MoonTools.Structs.Position2D(
(playAreaComponent.Width - textDimensions.X) / 2,
playAreaComponent.Height / 4
),
Font,
winText
));
SendMessage(new ChangeGameStateMessage(GameState.Title), 2);
}
}
}
}