feat: Paddle Data Send
This commit is contained in:
		@@ -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);
 | 
			
		||||
        else if (isDownPressed)
 | 
			
		||||
            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)
 | 
			
		||||
    {
 | 
			
		||||
        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()
 | 
			
		||||
@@ -59,6 +58,7 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
 | 
			
		||||
        inputs.RegisterOnRelease(Down, OnDownReleased);
 | 
			
		||||
 | 
			
		||||
        communicator = foundCommunicator;
 | 
			
		||||
        communicator.RegisterEntityListener(this, OnDataReceived);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) => isUpPressed = true;
 | 
			
		||||
    private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) => isUpPressed = false;
 | 
			
		||||
    private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) => isDownPressed = true;
 | 
			
		||||
    private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) => isDownPressed = false;
 | 
			
		||||
    private void UpdateNetwork()
 | 
			
		||||
    {
 | 
			
		||||
        NetDataWriter netDataWriter = communicator.GetEntityWriter(this);
 | 
			
		||||
        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)
 | 
			
		||||
        => 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)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user