using Encompass; using Microsoft.Xna.Framework.Input; using PongFE.Components; using PongFE.Enums; using PongFE.Messages; namespace PongFE.Engines { [Reads(typeof(GameStateComponent), typeof(PlayAreaComponent))] [Receives(typeof(ChangeGameStateMessage))] [Sends( typeof(BallSpawnMessage), typeof(PaddleSpawnMessage), typeof(BoundarySpawnMessage), typeof(GoalBoundarySpawnMessage) )] [Writes(typeof(GameStateComponent))] public class GameStateEngine : Engine { public override void Update(double dt) { ref readonly var gameStateEntity = ref ReadEntity(); ref readonly var gameStateComponent = ref GetComponent(gameStateEntity); if (gameStateComponent.GameState == GameState.Title) { if (Keyboard.GetState().IsKeyDown(Keys.Enter)) { EndTitle(); StartGame(); SetComponent(gameStateEntity, new GameStateComponent(GameState.Game)); } } if (SomeMessage()) { ref readonly var changeGameStateMessage = ref ReadMessage(); 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() { ref readonly var playAreaComponent = ref ReadComponent(); 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(); } private void StartTitle() { } private void EndTitle() { } } }