187 lines
5.6 KiB
C#
187 lines
5.6 KiB
C#
using Encompass;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using MoonTools.Structs;
|
|
using PongFE.Components;
|
|
using PongFE.Enums;
|
|
using PongFE.Messages;
|
|
using SpriteFontPlus;
|
|
|
|
namespace PongFE.Engines
|
|
{
|
|
[Reads(
|
|
typeof(PositionComponent),
|
|
typeof(GameStateComponent),
|
|
typeof(PlayAreaComponent),
|
|
typeof(UITextComponent)
|
|
)]
|
|
[Receives(typeof(ChangeGameStateMessage))]
|
|
[Sends(
|
|
typeof(BallSpawnMessage),
|
|
typeof(PaddleSpawnMessage),
|
|
typeof(BoundarySpawnMessage),
|
|
typeof(GoalBoundarySpawnMessage),
|
|
typeof(UITextSpawnMessage)
|
|
)]
|
|
[Writes(typeof(GameStateComponent))]
|
|
public class GameStateEngine : Engine
|
|
{
|
|
private DynamicSpriteFont TitleFont { get; }
|
|
private DynamicSpriteFont InstructionFont { get; }
|
|
|
|
public GameStateEngine(DynamicSpriteFont titleFont, DynamicSpriteFont instructionFont)
|
|
{
|
|
TitleFont = titleFont;
|
|
InstructionFont = instructionFont;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
if (changeGameStateMessage.GameState == GameState.Title)
|
|
{
|
|
EndGame();
|
|
StartTitle();
|
|
|
|
SetComponent(gameStateEntity, new GameStateComponent(GameState.Title));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void StartGame()
|
|
{
|
|
ref readonly var playAreaComponent = ref ReadComponent<PlayAreaComponent>();
|
|
var playAreaWidth = playAreaComponent.Width;
|
|
var playAreaHeight = playAreaComponent.Height;
|
|
|
|
SendMessage(
|
|
new PaddleSpawnMessage(
|
|
new MoonTools.Structs.Position2D(20, playAreaHeight / 2 - 40),
|
|
Enums.PlayerIndex.One,
|
|
PaddleControl.Player,
|
|
20,
|
|
80
|
|
)
|
|
);
|
|
|
|
SendMessage(
|
|
new PaddleSpawnMessage(
|
|
new MoonTools.Structs.Position2D(playAreaWidth - 45, playAreaHeight / 2 - 40),
|
|
Enums.PlayerIndex.Two,
|
|
PaddleControl.Computer,
|
|
20,
|
|
80
|
|
)
|
|
);
|
|
|
|
SendMessage(
|
|
new BallSpawnMessage(
|
|
new MoonTools.Structs.Position2D(playAreaWidth / 2, playAreaHeight / 2),
|
|
500,
|
|
16,
|
|
16
|
|
),
|
|
0.5
|
|
);
|
|
|
|
// top boundary
|
|
SendMessage(
|
|
new BoundarySpawnMessage(
|
|
new MoonTools.Structs.Position2D(0, -6),
|
|
playAreaWidth,
|
|
6
|
|
)
|
|
);
|
|
|
|
// bottom boundary
|
|
SendMessage(
|
|
new BoundarySpawnMessage(
|
|
new MoonTools.Structs.Position2D(0, playAreaHeight),
|
|
playAreaWidth,
|
|
6
|
|
)
|
|
);
|
|
|
|
// right boundary
|
|
SendMessage(
|
|
new GoalBoundarySpawnMessage(
|
|
Enums.PlayerIndex.One,
|
|
new MoonTools.Structs.Position2D(playAreaWidth, 0),
|
|
6,
|
|
playAreaHeight
|
|
)
|
|
);
|
|
|
|
// left boundary
|
|
SendMessage(
|
|
new GoalBoundarySpawnMessage(
|
|
Enums.PlayerIndex.Two,
|
|
new MoonTools.Structs.Position2D(-6, 0),
|
|
6,
|
|
playAreaHeight
|
|
)
|
|
);
|
|
}
|
|
|
|
private void EndGame()
|
|
{
|
|
DestroyAllWith<PositionComponent>();
|
|
}
|
|
|
|
private void StartTitle()
|
|
{
|
|
ref readonly var playAreaComponent = ref ReadComponent<PlayAreaComponent>();
|
|
|
|
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"
|
|
));
|
|
}
|
|
|
|
private void EndTitle()
|
|
{
|
|
DestroyAllWith<UITextComponent>();
|
|
}
|
|
}
|
|
}
|