refactor: paddle packets renamed and cleaned up
This commit is contained in:
parent
57868ce178
commit
4f3a2b4a4e
@ -9,7 +9,7 @@ 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>
|
||||
IPacketListenerServer<PaddleBehaviour.PaddleKeyStatePacket>, IPacketListenerClient<PaddleBehaviour.PaddleKeyStatePacket>
|
||||
{
|
||||
private Keys Up { get; } = Up;
|
||||
private Keys Down { get; } = Down;
|
||||
@ -62,35 +62,29 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
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)); }
|
||||
private void OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = true; networkClient.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = false; networkClient.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = true; networkClient.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = false; networkClient.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
|
||||
public void OnServerPacketArrived(EntityPaddlePositionPacket packet, string from)
|
||||
public void OnServerPacketArrived(PaddleKeyStatePacket packet, string from)
|
||||
{
|
||||
if (packet is not EntityPaddlePositionPacket position)
|
||||
if (packet.EntityId.CompareTo(Id) != 0)
|
||||
return;
|
||||
|
||||
if (position.EntityId.CompareTo(Id) != 0)
|
||||
return;
|
||||
|
||||
networkServer?.SendToClient("*", position);
|
||||
networkServer?.SendToClient("*", packet);
|
||||
}
|
||||
|
||||
public void OnClientPacketArrived(EntityPaddlePositionPacket packet)
|
||||
public void OnClientPacketArrived(PaddleKeyStatePacket packet)
|
||||
{
|
||||
if (packet is not EntityPaddlePositionPacket position)
|
||||
if (packet.EntityId.CompareTo(Id) != 0)
|
||||
return;
|
||||
|
||||
if (position.EntityId.CompareTo(Id) != 0)
|
||||
return;
|
||||
|
||||
isMovingUp = position.IsUpPressed;
|
||||
isMovingDown = position.IsDownPressed;
|
||||
isMovingUp = packet.IsUpPressed;
|
||||
isMovingDown = packet.IsDownPressed;
|
||||
}
|
||||
|
||||
public class EntityPaddlePositionPacket : IEntityNetworkPacket
|
||||
public class PaddleKeyStatePacket : IEntityNetworkPacket
|
||||
{
|
||||
public string EntityId { get; set; } = default!;
|
||||
public bool IsUpPressed { get; set; } = default!;
|
||||
@ -98,8 +92,8 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
|
||||
string IEntityNetworkPacket.EntityId => EntityId;
|
||||
|
||||
public EntityPaddlePositionPacket() { }
|
||||
public EntityPaddlePositionPacket(PaddleBehaviour paddleBehaviour)
|
||||
public PaddleKeyStatePacket() { }
|
||||
public PaddleKeyStatePacket(PaddleBehaviour paddleBehaviour)
|
||||
{
|
||||
EntityId = paddleBehaviour.Id;
|
||||
IsUpPressed = paddleBehaviour.isUpPressed;
|
||||
|
Loading…
x
Reference in New Issue
Block a user