Engine-Pong/Game/Behaviours/PaddleBehaviour.cs

100 lines
3.7 KiB
C#
Raw Permalink Normal View History

using System;
2024-07-18 23:07:05 +03:00
using LiteNetLib.Utils;
using Microsoft.Xna.Framework.Input;
2024-01-31 12:17:43 +03:00
using Syntriax.Engine.Core;
using Syntriax.Engine.Input;
2024-07-18 23:07:05 +03:00
using Syntriax.Engine.Network;
using Syntriax.Engine.Network.Abstract;
namespace Pong.Behaviours;
2024-07-18 23:07:05 +03:00
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : NetworkBehaviour
{
private Keys Up { get; } = Up;
private Keys Down { get; } = Down;
2023-11-27 17:06:18 +03:00
public float High { get; } = High;
public float Low { get; } = Low;
public float Speed { get; set; } = Speed;
private bool isUpPressed = false;
private bool isDownPressed = false;
2024-01-23 12:16:58 +03:00
private IButtonInputs<Keys> inputs = null!;
2024-01-23 12:16:58 +03:00
protected override void OnUpdate()
{
if (isUpPressed && isDownPressed)
return;
if (isUpPressed)
2024-07-18 23:07:05 +03:00
Move(Vector2D.Up);
else if (isDownPressed)
2024-07-18 23:07:05 +03:00
Move(-Vector2D.Up);
}
2023-11-27 17:06:18 +03:00
2024-07-18 23:07:05 +03:00
private void Move(Vector2D vectorToMove)
{
GameObject.Transform.Position = GameObject.Transform.Position + vectorToMove * (float)Time.Elapsed.TotalSeconds * Speed;
2024-01-23 12:16:58 +03:00
GameObject.Transform.Position = new Vector2D(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
}
2024-01-23 12:16:58 +03:00
protected override void OnFirstActiveFrame()
{
2024-01-23 12:16:58 +03:00
if (!BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult))
2024-01-27 21:50:28 +03:00
inputs = behaviourResult ?? BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
2023-11-27 17:41:21 +03:00
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);
}
2024-07-29 23:18:48 +03:00
private void OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = true; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed, PositionY = Transform.Position.Y }); }
private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) { isUpPressed = false; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed, PositionY = Transform.Position.Y }); }
private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = true; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed, PositionY = Transform.Position.Y }); }
private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) { isDownPressed = false; SendData(new PaddleInputs() { IsUpPressed = isUpPressed, IsDownPressed = isDownPressed, PositionY = Transform.Position.Y }); }
2024-07-18 23:07:05 +03:00
public override void ReceiveData<T>(T data)
{
2024-07-29 23:01:27 +03:00
if (data is not PaddleInputs paddleInputs)
return;
2024-07-29 23:18:48 +03:00
Transform.Position = new(Transform.Position.X, paddleInputs.PositionY);
2024-07-29 23:01:27 +03:00
isUpPressed = paddleInputs.IsUpPressed;
isDownPressed = paddleInputs.IsDownPressed;
2024-07-18 23:07:05 +03:00
}
[System.Serializable]
public struct PaddleInputs : INetworkPacket
{
2024-07-29 23:18:48 +03:00
public float PositionY { get; set; }
2024-07-18 23:07:05 +03:00
public bool IsUpPressed { get; set; }
public bool IsDownPressed { get; set; }
public void Deserialize(NetDataReader reader)
{
2024-07-29 23:18:48 +03:00
PositionY = reader.GetFloat();
2024-07-18 23:07:05 +03:00
IsUpPressed = reader.GetBool();
IsDownPressed = reader.GetBool();
}
public void Serialize(NetDataWriter writer)
{
2024-07-29 23:18:48 +03:00
writer.Put(PositionY);
2024-07-18 23:07:05 +03:00
writer.Put(IsUpPressed);
writer.Put(IsDownPressed);
}
}
}