110 lines
4.0 KiB
C#
110 lines
4.0 KiB
C#
using System;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using Syntriax.Engine.Core;
|
|
using Syntriax.Engine.Network;
|
|
using Syntriax.Engine.Systems.Input;
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : Behaviour2D,
|
|
IPacketListenerServer<PaddleBehaviour.EntityPaddlePositionPacket>, IPacketListenerClient<PaddleBehaviour.EntityPaddlePositionPacket>
|
|
{
|
|
private Keys Up { get; } = Up;
|
|
private Keys Down { get; } = Down;
|
|
public float High { get; } = High;
|
|
public float Low { get; } = Low;
|
|
public float Speed { get; set; } = Speed;
|
|
|
|
private bool isUpPressed = false;
|
|
private bool isDownPressed = false;
|
|
|
|
private bool isMovingUp = false;
|
|
private bool isMovingDown = false;
|
|
|
|
private IButtonInputs<Keys> inputs = null!;
|
|
private INetworkCommunicatorClient networkClient = null!;
|
|
private INetworkCommunicatorServer? networkServer = null;
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
if (isMovingUp && isMovingDown)
|
|
return;
|
|
|
|
if (isMovingUp)
|
|
Transform.Position = Transform.Position + Vector2D.Up * Universe.Time.DeltaTime * Speed;
|
|
else if (isMovingDown)
|
|
Transform.Position = Transform.Position + -Vector2D.Up * Universe.Time.DeltaTime * Speed;
|
|
|
|
Transform.Position = new Vector2D(Transform.Position.X, MathF.Max(MathF.Min(Transform.Position.Y, High), Low));
|
|
}
|
|
|
|
protected override void OnFirstActiveFrame()
|
|
{
|
|
inputs = Universe.FindRequiredBehaviour<IButtonInputs<Keys>>();
|
|
networkClient = Universe.FindRequiredBehaviour<INetworkCommunicatorClient>();
|
|
networkServer = Universe.FindBehaviour<INetworkCommunicatorServer>();
|
|
|
|
inputs.RegisterOnPress(Up, OnUpPressed);
|
|
inputs.RegisterOnRelease(Up, OnUpReleased);
|
|
|
|
inputs.RegisterOnPress(Down, OnDownPressed);
|
|
inputs.RegisterOnRelease(Down, OnDownReleased);
|
|
}
|
|
|
|
protected override void OnFinalize()
|
|
{
|
|
inputs.UnregisterOnPress(Up, OnUpPressed);
|
|
inputs.UnregisterOnRelease(Up, OnUpReleased);
|
|
|
|
inputs.UnregisterOnPress(Down, OnDownPressed);
|
|
inputs.UnregisterOnRelease(Down, OnDownReleased);
|
|
}
|
|
|
|
private void OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = true; networkClient.SendToServer(new EntityPaddlePositionPacket(this)); }
|
|
private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = false; networkClient.SendToServer(new EntityPaddlePositionPacket(this)); }
|
|
private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = true; networkClient.SendToServer(new EntityPaddlePositionPacket(this)); }
|
|
private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = false; networkClient.SendToServer(new EntityPaddlePositionPacket(this)); }
|
|
|
|
public void OnServerPacketArrived(EntityPaddlePositionPacket packet, string from)
|
|
{
|
|
if (packet is not EntityPaddlePositionPacket position)
|
|
return;
|
|
|
|
if (position.EntityId.CompareTo(Id) != 0)
|
|
return;
|
|
|
|
networkServer?.SendToClient("*", position);
|
|
}
|
|
|
|
public void OnClientPacketArrived(EntityPaddlePositionPacket packet)
|
|
{
|
|
if (packet is not EntityPaddlePositionPacket position)
|
|
return;
|
|
|
|
if (position.EntityId.CompareTo(Id) != 0)
|
|
return;
|
|
|
|
isMovingUp = position.IsUpPressed;
|
|
isMovingDown = position.IsDownPressed;
|
|
}
|
|
|
|
public class EntityPaddlePositionPacket : IEntityNetworkPacket
|
|
{
|
|
public string EntityId { get; set; } = default!;
|
|
public bool IsUpPressed { get; set; } = default!;
|
|
public bool IsDownPressed { get; set; } = default!;
|
|
|
|
string IEntityNetworkPacket.EntityId => EntityId;
|
|
|
|
public EntityPaddlePositionPacket() { }
|
|
public EntityPaddlePositionPacket(PaddleBehaviour paddleBehaviour)
|
|
{
|
|
EntityId = paddleBehaviour.Id;
|
|
IsUpPressed = paddleBehaviour.isUpPressed;
|
|
IsDownPressed = paddleBehaviour.isDownPressed;
|
|
}
|
|
}
|
|
}
|