feat: Paddle Data Send
This commit is contained in:
parent
2565617ff9
commit
1b1d9dcdac
|
@ -35,13 +35,12 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||||
MovePaddle(Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
MovePaddle(Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
||||||
else if (isDownPressed)
|
else if (isDownPressed)
|
||||||
MovePaddle(-Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
MovePaddle(-Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
||||||
|
|
||||||
GameObject.Transform.Position = new Vector2D(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MovePaddle(Vector2D vectorToAdd)
|
private void MovePaddle(Vector2D vectorToAdd)
|
||||||
{
|
{
|
||||||
GameObject.Transform.Position += vectorToAdd;
|
GameObject.Transform.Position += vectorToAdd;
|
||||||
|
GameObject.Transform.Position = new Vector2D(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnFirstActiveFrame()
|
protected override void OnFirstActiveFrame()
|
||||||
|
@ -59,6 +58,7 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||||
inputs.RegisterOnRelease(Down, OnDownReleased);
|
inputs.RegisterOnRelease(Down, OnDownReleased);
|
||||||
|
|
||||||
communicator = foundCommunicator;
|
communicator = foundCommunicator;
|
||||||
|
communicator.RegisterEntityListener(this, OnDataReceived);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnFinalize()
|
protected override void OnFinalize()
|
||||||
|
@ -70,8 +70,32 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||||
inputs.UnregisterOnRelease(Down, OnDownReleased);
|
inputs.UnregisterOnRelease(Down, OnDownReleased);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) => isUpPressed = true;
|
private void UpdateNetwork()
|
||||||
private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) => isUpPressed = false;
|
{
|
||||||
private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) => isDownPressed = true;
|
NetDataWriter netDataWriter = communicator.GetEntityWriter(this);
|
||||||
private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) => isDownPressed = false;
|
netDataWriter.Put(isUpPressed);
|
||||||
|
netDataWriter.Put(isDownPressed);
|
||||||
|
netDataWriter.Put(Transform.Position);
|
||||||
|
communicator.Manager.SendToAll(netDataWriter, DeliveryMethod.ReliableOrdered);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDataReceived(NetPacketReader reader, NetPeer peer)
|
||||||
|
{
|
||||||
|
if (communicator is INetworkServer server)
|
||||||
|
{
|
||||||
|
reader.Get(out isUpPressed);
|
||||||
|
reader.Get(out isDownPressed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reader.Get(out isUpPressed);
|
||||||
|
reader.Get(out isDownPressed);
|
||||||
|
Transform.Position = reader.GetVector2D();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = true; UpdateNetwork(); }
|
||||||
|
private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = false; UpdateNetwork(); }
|
||||||
|
private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = true; UpdateNetwork(); }
|
||||||
|
private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = false; UpdateNetwork(); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ public static class NetworkExtensions
|
||||||
{
|
{
|
||||||
public static Vector2D GetVector2D(this NetPacketReader reader)
|
public static Vector2D GetVector2D(this NetPacketReader reader)
|
||||||
=> new(reader.GetFloat(), reader.GetFloat());
|
=> new(reader.GetFloat(), reader.GetFloat());
|
||||||
|
public static void GetVector2D(this NetPacketReader reader, out Vector2D vector2D)
|
||||||
|
=> vector2D = new(reader.GetFloat(), reader.GetFloat());
|
||||||
|
|
||||||
public static void Put(this NetDataWriter writer, Vector2D vector)
|
public static void Put(this NetDataWriter writer, Vector2D vector)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue