PongFE/PongFE/Engines/InputEngine.cs

49 lines
1.7 KiB
C#
Raw Permalink Normal View History

2020-07-12 21:54:08 +00:00
using Encompass;
using Microsoft.Xna.Framework.Input;
using PongFE.Components;
2020-07-15 20:08:58 +00:00
using PongFE.Enums;
2020-07-12 21:54:08 +00:00
using PongFE.Messages;
namespace PongFE.Engines
{
2020-07-16 21:39:36 +00:00
[Reads(typeof(PlayerInputComponent), typeof(PlayerComponent))]
2020-07-12 21:54:08 +00:00
[Sends(typeof(PaddleMoveMessage))]
public class InputEngine : Engine
{
public override void Update(double dt)
{
var keyboardState = Keyboard.GetState();
foreach (ref readonly var playerInputEntity in ReadEntities<PlayerInputComponent>())
{
ref readonly var playerInputComponent = ref GetComponent<PlayerInputComponent>(playerInputEntity);
2020-07-16 21:39:36 +00:00
if (HasComponent<PlayerComponent>(playerInputEntity))
2020-07-12 21:54:08 +00:00
{
2020-07-16 21:39:36 +00:00
ref readonly var playerComponent = ref GetComponent<PlayerComponent>(playerInputEntity);
if (playerComponent.PlayerIndex == PlayerIndex.One)
2020-07-12 21:54:08 +00:00
{
2020-07-16 21:39:36 +00:00
if (keyboardState.IsKeyDown(Keys.Down))
{
SendMessage(
new PaddleMoveMessage(
playerInputEntity,
PaddleMoveDirection.Down
)
);
}
else if (keyboardState.IsKeyDown(Keys.Up))
{
SendMessage(
new PaddleMoveMessage(
playerInputEntity,
PaddleMoveDirection.Up
)
);
}
2020-07-12 21:54:08 +00:00
}
}
}
}
}
}