88 lines
3.0 KiB
C#
88 lines
3.0 KiB
C#
using System;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using LiteNetLib;
|
|
using LiteNetLib.Utils;
|
|
|
|
using Pong.Network;
|
|
|
|
using Syntriax.Engine.Core;
|
|
using Syntriax.Engine.Input;
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : BehaviourOverride
|
|
{
|
|
private Keys Up { get; } = Up;
|
|
private Keys Down { get; } = Down;
|
|
public float High { get; } = High;
|
|
public float Low { get; } = Low;
|
|
public float Speed { get; set; } = Speed;
|
|
|
|
private bool isUpPressed = false;
|
|
private bool isDownPressed = false;
|
|
|
|
private IButtonInputs<Keys> inputs = null!;
|
|
private INetworkCommunicator communicator = null!;
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
if (isUpPressed && isDownPressed)
|
|
return;
|
|
|
|
if (isUpPressed)
|
|
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;
|
|
|
|
NetDataWriter dataWriter = communicator.GetEntityWriter(this);
|
|
dataWriter.Put(Transform.Position);
|
|
communicator.Manager.SendToAll(dataWriter, LiteNetLib.DeliveryMethod.Unreliable);
|
|
}
|
|
|
|
protected override void OnFirstActiveFrame()
|
|
{
|
|
if (!BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult))
|
|
inputs = behaviourResult ?? BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
|
|
|
if (!GameObject.GameManager.TryFindBehaviour(out INetworkCommunicator? foundCommunicator))
|
|
throw new Exception($"{nameof(INetworkCommunicator)} is missing on GameManager.");
|
|
|
|
inputs.RegisterOnPress(Up, OnUpPressed);
|
|
inputs.RegisterOnRelease(Up, OnUpReleased);
|
|
|
|
inputs.RegisterOnPress(Down, OnDownPressed);
|
|
inputs.RegisterOnRelease(Down, OnDownReleased);
|
|
|
|
communicator = foundCommunicator;
|
|
communicator.RegisterEntityListener(this, OnDataReceived);
|
|
}
|
|
|
|
private void OnDataReceived(NetPacketReader reader)
|
|
{
|
|
Transform.Position = reader.GetVector2D();
|
|
}
|
|
|
|
protected override void OnFinalize()
|
|
{
|
|
inputs.UnregisterOnPress(Up, OnUpPressed);
|
|
inputs.UnregisterOnRelease(Up, OnUpReleased);
|
|
|
|
inputs.UnregisterOnPress(Down, OnDownPressed);
|
|
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;
|
|
}
|