PongFE/PongFE/Engines/GameStateEngine.cs

154 lines
4.3 KiB
C#

using Encompass;
using Microsoft.Xna.Framework.Input;
using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(typeof(GameStateComponent))]
[Receives(typeof(ChangeGameStateMessage))]
[Sends(
typeof(BallSpawnMessage),
typeof(PaddleSpawnMessage),
typeof(BoundarySpawnMessage),
typeof(GoalBoundarySpawnMessage)
)]
[Writes(typeof(GameStateComponent))]
public class GameStateEngine : Engine
{
private int PlayAreaWidth { get; }
private int PlayAreaHeight { get; }
public GameStateEngine(int playAreaWidth, int playAreaHeight)
{
PlayAreaWidth = playAreaWidth;
PlayAreaHeight = playAreaHeight;
}
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 (gameStateComponent.GameState == GameState.Game)
{
if (changeGameStateMessage.GameState == GameState.Title)
{
EndGame();
StartTitle();
SetComponent(gameStateEntity, new GameStateComponent(GameState.Title));
}
}
}
}
private void StartGame()
{
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()
{
}
private void EndTitle()
{
}
}
}