This commit is contained in:
2024-07-18 23:07:05 +03:00
parent 19124e733c
commit d011bf9a7a
9 changed files with 176 additions and 23 deletions

View File

@@ -1,13 +1,15 @@
using System;
using LiteNetLib.Utils;
using Microsoft.Xna.Framework.Input;
using Syntriax.Engine.Core;
using Syntriax.Engine.Input;
using Syntriax.Engine.Network;
using Syntriax.Engine.Network.Abstract;
namespace Pong.Behaviours;
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : BehaviourOverride
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : NetworkBehaviour
{
private Keys Up { get; } = Up;
private Keys Down { get; } = Down;
@@ -26,10 +28,14 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
return;
if (isUpPressed)
GameObject.Transform.Position = GameObject.Transform.Position + Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed;
Move(Vector2D.Up);
else if (isDownPressed)
GameObject.Transform.Position = GameObject.Transform.Position + -Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed;
Move(-Vector2D.Up);
}
private void Move(Vector2D vectorToMove)
{
GameObject.Transform.Position = GameObject.Transform.Position + vectorToMove * (float)Time.Elapsed.TotalSeconds * Speed;
GameObject.Transform.Position = new Vector2D(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
}
@@ -54,8 +60,35 @@ 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 OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = true; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed }); }
private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = false; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed }); }
private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = true; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed }); }
private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = false; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed }); }
public override void ReceiveData<T>(T data)
{
if (data is PaddleInputs paddleInputs)
{
System.Diagnostics.Debug.WriteLine($"Paddle Inputs Arrived: {paddleInputs.IsUpPressed}, {paddleInputs.IsDownPressed}");
}
}
[System.Serializable]
public struct PaddleInputs : INetworkPacket
{
public bool IsUpPressed { get; set; }
public bool IsDownPressed { get; set; }
public void Deserialize(NetDataReader reader)
{
IsUpPressed = reader.GetBool();
IsDownPressed = reader.GetBool();
}
public void Serialize(NetDataWriter writer)
{
writer.Put(IsUpPressed);
writer.Put(IsDownPressed);
}
}
}