using System; using Microsoft.Xna.Framework.Input; using Syntriax.Engine.Core; using Syntriax.Engine.Network; using Syntriax.Engine.Systems.Input; namespace Pong.Behaviours; public class PongManagerBehaviour : Behaviour, INetworkEntity, IPacketListenerServer { public Action? OnReset { get; set; } = null; public Action? OnFinished { get; set; } = null; public Action? OnScored { get; set; } = null; private Random random = new(); private BallBehaviour ball = null!; private INetworkCommunicatorClient? networkClient = null!; private INetworkCommunicatorServer? networkServer = 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; protected override void OnFirstActiveFrame() { IButtonInputs buttonInputs = Universe.FindRequired>(); buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => networkClient?.SendToServer(new RequestStartPacket())); ball = Universe.FindRequiredBehaviour(); networkClient = Universe.Find(); networkServer = Universe.Find(); } public void ScoreToLeft() { ScoreLeft++; OnScored?.Invoke(this); CheckFinish(); } public void ScoreToRight() { ScoreRight++; OnScored?.Invoke(this); CheckFinish(); } public void Reset() { ScoreLeft = ScoreRight = 0; ball.ResetBall(); ball.LaunchBall(GetBallLaunchDirection()); OnReset?.Invoke(this); } private void CheckFinish() { int halfwayScore = (int)(WinScore * .5f); ball.ResetBall(); if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore) { OnFinished?.Invoke(this); return; } ball.ResetBall(); 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 IPacketListenerServer.OnServerPacketArrived(string sender, RequestStartPacket packet) { if (ball.RigidBody.Velocity.MagnitudeSquared > 0.01f) return; ball.LaunchBall(GetBallLaunchDirection()); } private class RequestStartPacket : INetworkPacket; }