PongFE/PongFE/Engines/ScoreEngine.cs

50 lines
1.8 KiB
C#

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<ScoreMessage>())
{
if (HasComponent<ScoreComponent>(scoreMessage.Entity))
{
ref readonly var scoreComponent = ref GetComponent<ScoreComponent>(scoreMessage.Entity);
SetComponent(scoreMessage.Entity, new ScoreComponent(scoreComponent.Score + 1));
if (scoreComponent.Score + 1 >= 2)
{
ref readonly var playerComponent = ref GetComponent<PlayerComponent>(scoreMessage.Entity);
SendMessage(new GameWinMessage(playerComponent.PlayerIndex));
}
else
{
ref readonly var ballParametersComponent = ref ReadComponent<BallParametersComponent>();
SendMessage(
new BallSpawnMessage(
new MoonTools.Structs.Position2D(640, (int)MathHelper.RandomFloat(20, 700)),
ballParametersComponent.Speed,
16,
16
),
ballParametersComponent.Delay
);
}
}
}
}
}
}