49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using Encompass;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using PongFE.Components;
|
|
using PongFE.Enums;
|
|
using PongFE.Messages;
|
|
|
|
namespace PongFE.Engines
|
|
{
|
|
[Reads(typeof(PlayerInputComponent), typeof(PlayerComponent))]
|
|
[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);
|
|
if (HasComponent<PlayerComponent>(playerInputEntity))
|
|
{
|
|
ref readonly var playerComponent = ref GetComponent<PlayerComponent>(playerInputEntity);
|
|
if (playerComponent.PlayerIndex == PlayerIndex.One)
|
|
{
|
|
if (keyboardState.IsKeyDown(Keys.Down))
|
|
{
|
|
SendMessage(
|
|
new PaddleMoveMessage(
|
|
playerInputEntity,
|
|
PaddleMoveDirection.Down
|
|
)
|
|
);
|
|
}
|
|
else if (keyboardState.IsKeyDown(Keys.Up))
|
|
{
|
|
SendMessage(
|
|
new PaddleMoveMessage(
|
|
playerInputEntity,
|
|
PaddleMoveDirection.Up
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|