Engine-Pong/Game/Behaviours/PongManagerBehaviour.cs

105 lines
2.8 KiB
C#
Raw Permalink Normal View History

2024-01-28 15:33:36 +03:00
using System;
using Microsoft.Xna.Framework.Input;
2024-02-02 12:53:25 +03:00
using LiteNetLib;
2024-01-28 15:33:36 +03:00
using Syntriax.Engine.Core;
using Syntriax.Engine.Network;
using Syntriax.Engine.Network.Abstract;
2024-01-28 15:33:36 +03:00
namespace Pong.Behaviours;
2024-02-12 16:35:24 +03:00
public class PongManagerBehaviour : NetworkBehaviour
2024-01-28 15:33:36 +03:00
{
2024-01-31 12:17:43 +03:00
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
public Action<PongManagerBehaviour>? OnScoresUpdated { get; set; } = null;
2024-01-31 12:17:43 +03:00
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
2024-01-28 15:33:36 +03:00
2024-02-02 12:53:25 +03:00
private INetworkCommunicator communicator = null!;
2024-01-28 15:33:36 +03:00
public int ScoreLeft { get; private set; } = 0;
public int ScoreRight { get; private set; } = 0;
public int ScoreSum => ScoreLeft + ScoreRight;
2024-01-30 12:43:30 +03:00
public int WinScore { get; } = 5;
2024-01-28 15:33:36 +03:00
2024-01-31 12:17:43 +03:00
public PongManagerBehaviour() => WinScore = 5;
public PongManagerBehaviour(int winScore) => WinScore = winScore;
protected override void OnFirstActiveFrame()
{
KeyboardInputsBehaviour? buttonInputs = null!;
if (!BehaviourController.TryGetBehaviour(out buttonInputs))
buttonInputs = BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
2024-02-02 12:53:25 +03:00
if (!GameObject.GameManager.TryFindBehaviour(out INetworkCommunicator? foundCommunicator))
throw new Exception($"{nameof(INetworkCommunicator)} is missing on GameManager.");
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => Reset());
2024-02-02 12:53:25 +03:00
communicator = foundCommunicator;
}
2024-01-28 15:33:36 +03:00
public void ScoreToLeft()
{
ScoreLeft++;
OnScoresUpdated?.Invoke(this);
2024-01-30 12:43:30 +03:00
OnScored?.Invoke(this);
2024-01-28 15:33:36 +03:00
2024-02-02 12:53:25 +03:00
SendData();
2024-01-28 15:33:36 +03:00
CheckFinish();
}
public void ScoreToRight()
{
ScoreRight++;
OnScoresUpdated?.Invoke(this);
2024-01-30 12:43:30 +03:00
OnScored?.Invoke(this);
2024-01-28 15:33:36 +03:00
2024-02-02 12:53:25 +03:00
SendData();
2024-01-28 15:33:36 +03:00
CheckFinish();
}
public void Reset()
{
ScoreLeft = ScoreRight = 0;
OnScoresUpdated?.Invoke(this);
2024-01-30 12:43:30 +03:00
OnReset?.Invoke(this);
2024-02-02 12:53:25 +03:00
SendData();
2024-01-28 15:33:36 +03:00
}
private void CheckFinish()
{
2024-01-29 12:34:27 +03:00
int halfwayScore = (int)(WinScore * .5f);
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
2024-01-30 12:43:30 +03:00
OnFinished?.Invoke(this);
2024-01-28 15:33:36 +03:00
}
2024-02-02 12:53:25 +03:00
private void SendData()
{
if (communicator is not INetworkServer server)
return;
2024-02-12 16:35:24 +03:00
LiteNetLib.Utils.NetDataWriter dataWriter = communicator.GetMessageWriter(this);
2024-02-02 12:53:25 +03:00
dataWriter.Put(ScoreLeft);
dataWriter.Put(ScoreRight);
server.Manager.SendToAll(dataWriter, LiteNetLib.DeliveryMethod.ReliableOrdered);
}
2024-02-12 16:35:24 +03:00
protected override void OnMessageReceived(NetPacketReader reader, NetPeer peer)
{
ScoreLeft = reader.GetInt();
ScoreRight = reader.GetInt();
OnScoresUpdated?.Invoke(this);
CheckFinish();
}
2024-01-28 15:33:36 +03:00
}