2023-11-24 15:50:23 +03:00
|
|
|
using System;
|
2024-01-31 12:17:43 +03:00
|
|
|
|
2023-11-24 15:50:23 +03:00
|
|
|
using Microsoft.Xna.Framework.Input;
|
2024-01-31 12:17:43 +03:00
|
|
|
|
2024-02-02 12:53:25 +03:00
|
|
|
using LiteNetLib;
|
|
|
|
using LiteNetLib.Utils;
|
|
|
|
|
2023-11-24 15:50:23 +03:00
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
using Syntriax.Engine.Input;
|
2024-02-09 10:00:36 +03:00
|
|
|
using Syntriax.Engine.Network;
|
|
|
|
using Syntriax.Engine.Network.Abstract;
|
2023-11-24 15:50:23 +03:00
|
|
|
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
|
2024-02-13 11:15:27 +03:00
|
|
|
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : BehaviourOverride
|
2023-11-24 15:50:23 +03:00
|
|
|
{
|
|
|
|
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;
|
2023-11-24 15:50:23 +03:00
|
|
|
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-02-02 12:53:25 +03:00
|
|
|
private INetworkCommunicator communicator = null!;
|
2023-11-24 15:50:23 +03:00
|
|
|
|
2024-01-23 12:16:58 +03:00
|
|
|
protected override void OnUpdate()
|
2023-11-24 15:50:23 +03:00
|
|
|
{
|
|
|
|
if (isUpPressed && isDownPressed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isUpPressed)
|
2024-02-02 12:53:25 +03:00
|
|
|
MovePaddle(Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
2023-11-24 15:50:23 +03:00
|
|
|
else if (isDownPressed)
|
2024-02-02 12:53:25 +03:00
|
|
|
MovePaddle(-Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
2023-11-24 15:50:23 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:53:25 +03:00
|
|
|
private void MovePaddle(Vector2D vectorToAdd)
|
|
|
|
{
|
|
|
|
GameObject.Transform.Position += vectorToAdd;
|
2024-07-14 23:55:06 +03:00
|
|
|
GameObject.Transform.Position = new Vector2D(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
|
2024-02-02 12:53:25 +03:00
|
|
|
}
|
|
|
|
|
2024-01-23 12:16:58 +03:00
|
|
|
protected override void OnFirstActiveFrame()
|
2023-11-24 15:50:23 +03:00
|
|
|
{
|
2024-02-13 11:15:27 +03:00
|
|
|
BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult);
|
|
|
|
inputs = behaviourResult ?? throw new Exception($"{nameof(IButtonInputs<Keys>)} is missing on {GameObject.Name}.");
|
2023-11-27 17:41:21 +03:00
|
|
|
|
2024-02-02 12:53:25 +03:00
|
|
|
if (!GameObject.GameManager.TryFindBehaviour(out INetworkCommunicator? foundCommunicator))
|
|
|
|
throw new Exception($"{nameof(INetworkCommunicator)} is missing on GameManager.");
|
|
|
|
|
2023-11-24 15:50:23 +03:00
|
|
|
inputs.RegisterOnPress(Up, OnUpPressed);
|
|
|
|
inputs.RegisterOnRelease(Up, OnUpReleased);
|
|
|
|
|
|
|
|
inputs.RegisterOnPress(Down, OnDownPressed);
|
|
|
|
inputs.RegisterOnRelease(Down, OnDownReleased);
|
2024-02-02 12:53:25 +03:00
|
|
|
|
|
|
|
communicator = foundCommunicator;
|
2024-07-14 23:55:06 +03:00
|
|
|
communicator.RegisterEntityListener(this, OnDataReceived);
|
2023-11-24 15:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnFinalize()
|
|
|
|
{
|
|
|
|
inputs.UnregisterOnPress(Up, OnUpPressed);
|
|
|
|
inputs.UnregisterOnRelease(Up, OnUpReleased);
|
|
|
|
|
|
|
|
inputs.UnregisterOnPress(Down, OnDownPressed);
|
|
|
|
inputs.UnregisterOnRelease(Down, OnDownReleased);
|
|
|
|
}
|
|
|
|
|
2024-07-14 23:55:06 +03:00
|
|
|
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(); }
|
2023-11-24 15:50:23 +03:00
|
|
|
}
|