PongFE/PongFE/Engines/GameStateEngine.cs

187 lines
5.6 KiB
C#
Raw Normal View History

2020-07-18 21:41:32 +00:00
using Encompass;
using Microsoft.Xna.Framework.Input;
2020-07-20 03:16:15 +00:00
using MoonTools.Structs;
2020-07-18 21:41:32 +00:00
using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages;
2020-07-20 03:16:15 +00:00
using SpriteFontPlus;
2020-07-18 21:41:32 +00:00
namespace PongFE.Engines
{
2020-07-20 03:16:15 +00:00
[Reads(
typeof(PositionComponent),
typeof(GameStateComponent),
typeof(PlayAreaComponent),
typeof(UITextComponent)
)]
2020-07-18 21:41:32 +00:00
[Receives(typeof(ChangeGameStateMessage))]
[Sends(
typeof(BallSpawnMessage),
typeof(PaddleSpawnMessage),
typeof(BoundarySpawnMessage),
2020-07-20 03:16:15 +00:00
typeof(GoalBoundarySpawnMessage),
typeof(UITextSpawnMessage)
2020-07-18 21:41:32 +00:00
)]
[Writes(typeof(GameStateComponent))]
public class GameStateEngine : Engine
{
2020-07-20 03:16:15 +00:00
private DynamicSpriteFont TitleFont { get; }
private DynamicSpriteFont InstructionFont { get; }
public GameStateEngine(DynamicSpriteFont titleFont, DynamicSpriteFont instructionFont)
{
TitleFont = titleFont;
InstructionFont = instructionFont;
}
2020-07-18 21:41:32 +00:00
public override void Update(double dt)
{
ref readonly var gameStateEntity = ref ReadEntity<GameStateComponent>();
ref readonly var gameStateComponent = ref GetComponent<GameStateComponent>(gameStateEntity);
if (gameStateComponent.GameState == GameState.Title)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
EndTitle();
StartGame();
SetComponent(gameStateEntity, new GameStateComponent(GameState.Game));
}
}
if (SomeMessage<ChangeGameStateMessage>())
{
ref readonly var changeGameStateMessage = ref ReadMessage<ChangeGameStateMessage>();
if (changeGameStateMessage.GameState == gameStateComponent.GameState)
{
return;
}
2020-07-20 03:16:15 +00:00
if (changeGameStateMessage.GameState == GameState.Title)
2020-07-18 21:41:32 +00:00
{
2020-07-20 03:16:15 +00:00
EndGame();
StartTitle();
2020-07-18 21:41:32 +00:00
2020-07-20 03:16:15 +00:00
SetComponent(gameStateEntity, new GameStateComponent(GameState.Title));
2020-07-18 21:41:32 +00:00
}
}
}
private void StartGame()
{
2020-07-20 02:00:24 +00:00
ref readonly var playAreaComponent = ref ReadComponent<PlayAreaComponent>();
var playAreaWidth = playAreaComponent.Width;
var playAreaHeight = playAreaComponent.Height;
2020-07-18 21:41:32 +00:00
SendMessage(
new PaddleSpawnMessage(
2020-07-20 02:00:24 +00:00
new MoonTools.Structs.Position2D(20, playAreaHeight / 2 - 40),
2020-07-18 21:41:32 +00:00
Enums.PlayerIndex.One,
PaddleControl.Player,
20,
80
)
);
SendMessage(
new PaddleSpawnMessage(
2020-07-20 02:00:24 +00:00
new MoonTools.Structs.Position2D(playAreaWidth - 45, playAreaHeight / 2 - 40),
2020-07-18 21:41:32 +00:00
Enums.PlayerIndex.Two,
PaddleControl.Computer,
20,
80
)
);
SendMessage(
new BallSpawnMessage(
2020-07-20 02:00:24 +00:00
new MoonTools.Structs.Position2D(playAreaWidth / 2, playAreaHeight / 2),
2020-07-18 21:41:32 +00:00
500,
16,
16
),
0.5
);
// top boundary
SendMessage(
new BoundarySpawnMessage(
new MoonTools.Structs.Position2D(0, -6),
2020-07-20 02:00:24 +00:00
playAreaWidth,
2020-07-18 21:41:32 +00:00
6
)
);
// bottom boundary
SendMessage(
new BoundarySpawnMessage(
2020-07-20 02:00:24 +00:00
new MoonTools.Structs.Position2D(0, playAreaHeight),
playAreaWidth,
2020-07-18 21:41:32 +00:00
6
)
);
// right boundary
SendMessage(
new GoalBoundarySpawnMessage(
Enums.PlayerIndex.One,
2020-07-20 02:00:24 +00:00
new MoonTools.Structs.Position2D(playAreaWidth, 0),
2020-07-18 21:41:32 +00:00
6,
2020-07-20 02:00:24 +00:00
playAreaHeight
2020-07-18 21:41:32 +00:00
)
);
// left boundary
SendMessage(
new GoalBoundarySpawnMessage(
Enums.PlayerIndex.Two,
new MoonTools.Structs.Position2D(-6, 0),
6,
2020-07-20 02:00:24 +00:00
playAreaHeight
2020-07-18 21:41:32 +00:00
)
);
}
private void EndGame()
{
DestroyAllWith<PositionComponent>();
}
private void StartTitle()
{
2020-07-20 03:16:15 +00:00
ref readonly var playAreaComponent = ref ReadComponent<PlayAreaComponent>();
2020-07-18 21:41:32 +00:00
2020-07-20 03:16:15 +00:00
var titleDimensions = TitleFont.MeasureString("PongFE");
var titlePosition = new Position2D(
(playAreaComponent.Width - titleDimensions.X) / 2,
(playAreaComponent.Height - titleDimensions.Y) / 4
);
SendMessage(new UITextSpawnMessage(
titlePosition,
TitleFont,
"PongFE"
));
var instructionDimensions = InstructionFont.MeasureString("Press Enter to begin");
var instructionPosition = new Position2D(
(playAreaComponent.Width - instructionDimensions.X) / 2,
playAreaComponent.Height * 2 / 3
);
SendMessage(new UITextSpawnMessage(
instructionPosition,
InstructionFont,
"Press Enter to play"
));
2020-07-18 21:41:32 +00:00
}
private void EndTitle()
{
2020-07-20 03:16:15 +00:00
DestroyAllWith<UITextComponent>();
2020-07-18 21:41:32 +00:00
}
}
}