using Encompass; using PongFE.Components; using PongFE.Messages; namespace PongFE.Engines { [Reads( typeof(ScoreComponent), typeof(PlayerComponent), typeof(BallParametersComponent) )] [Receives(typeof(ScoreMessage))] [Sends(typeof(GameWinMessage), typeof(BallSpawnMessage))] [Writes(typeof(ScoreComponent))] public class ScoreEngine : Engine { public override void Update(double dt) { foreach (ref readonly var scoreMessage in ReadMessages()) { if (HasComponent(scoreMessage.Entity)) { ref readonly var scoreComponent = ref GetComponent(scoreMessage.Entity); SetComponent(scoreMessage.Entity, new ScoreComponent(scoreComponent.Score + 1)); if (scoreComponent.Score + 1 >= 2) { ref readonly var playerComponent = ref GetComponent(scoreMessage.Entity); SendMessage(new GameWinMessage(playerComponent.PlayerIndex)); } else { ref readonly var ballParametersComponent = ref ReadComponent(); SendMessage( new BallSpawnMessage( new MoonTools.Structs.Position2D(640, (int)MathHelper.RandomFloat(20, 700)), ballParametersComponent.Speed, 16, 16 ), ballParametersComponent.Delay ); } } } } } }