using System; using Microsoft.Xna.Framework.Input; using Syntriax.Engine.Core; using Syntriax.Engine.Core.Debug; using Syntriax.Engine.Network; using Syntriax.Engine.Systems.Input; namespace Pong.Behaviours; public class PongManagerBehaviour : Behaviour, INetworkEntity, IFirstFrameUpdate, IPacketListenerServer, IPacketListenerClient { public Action? OnReset { get; set; } = null; public Action? OnFinished { get; set; } = null; public Action? OnScoreUpdated { get; set; } = null; private Random random = new(); private BallBehaviour ball = null!; private INetworkCommunicatorClient? networkClient = null!; private INetworkCommunicatorServer? networkServer = null; private ILogger? logger = null; public int ScoreLeft { get; private set; } = 0; public int ScoreRight { get; private set; } = 0; public int ScoreSum => ScoreLeft + ScoreRight; public int WinScore { get; } = 5; public PongManagerBehaviour() => WinScore = 5; public PongManagerBehaviour(int winScore) => WinScore = winScore; public void FirstActiveFrame() { IButtonInputs? buttonInputs = Universe.FindBehaviour>(); buttonInputs?.RegisterOnRelease(Keys.Space, (_, _1) => networkClient?.SendToServer(new RequestStartPacket())); ball = Universe.FindRequiredBehaviour(); networkClient = Universe.FindBehaviour(); networkServer = Universe.FindBehaviour(); logger = Universe.FindBehaviour(); } public void ScoreToLeft() { ScoreLeft++; OnScoreUpdated?.Invoke(this); PostScoreUpdate(); } public void ScoreToRight() { ScoreRight++; OnScoreUpdated?.Invoke(this); PostScoreUpdate(); } public void Reset() { ScoreLeft = ScoreRight = 0; ball.ResetBall(); ball.LaunchBall(GetBallLaunchDirection()); OnReset?.Invoke(this); } private void PostScoreUpdate() { ball.ResetBall(); if (networkServer is not null) { networkServer.SendToAll(new ScorePacket(this)); logger?.Log(this, $"Sending score update packet to all: {ScoreLeft} - {ScoreRight}"); } int halfwayScore = (int)(WinScore * .5f); if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore) { OnFinished?.Invoke(this); return; } ball.LaunchBall(GetBallLaunchDirection()); } private Vector2D GetBallLaunchDirection() { const float AllowedRadians = 45f * Syntriax.Engine.Core.Math.DegreeToRadian; float rotation = (float)random.NextDouble() * 2f * AllowedRadians - AllowedRadians; bool isBackwards = (random.Next() % 2) == 1; return Vector2D.Right.Rotate(isBackwards ? rotation + Syntriax.Engine.Core.Math.Pi : rotation); } void IPacketListenerClient.OnClientPacketArrived(IConnection sender, ScorePacket packet) { ScoreLeft = packet.Left; ScoreRight = packet.Right; OnScoreUpdated?.Invoke(this); logger?.Log(this, $"Client score update packet arrived: {packet.Left} - {packet.Right}"); } void IPacketListenerServer.OnServerPacketArrived(IConnection sender, RequestStartPacket packet) { if (ball.RigidBody.Velocity.MagnitudeSquared > 0.01f) return; ball.LaunchBall(GetBallLaunchDirection()); } private class RequestStartPacket : INetworkPacket; private class ScorePacket : INetworkPacket { public int Left { get; set; } public int Right { get; set; } public ScorePacket() { } public ScorePacket(PongManagerBehaviour pongManagerBehaviour) { Left = pongManagerBehaviour.ScoreLeft; Right = pongManagerBehaviour.ScoreRight; } } }