91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
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
|
|
{
|
|
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
|
|
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
|
public Action<PongManagerBehaviour>? 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<Keys> buttonInputs = Universe.FindRequired<IButtonInputs<Keys>>();
|
|
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => Reset());
|
|
|
|
ball = Universe.FindRequiredBehaviour<BallBehaviour>();
|
|
networkClient = Universe.Find<INetworkCommunicatorClient>();
|
|
networkServer = Universe.Find<INetworkCommunicatorServer>();
|
|
}
|
|
|
|
public void ScoreToLeft()
|
|
{
|
|
ScoreLeft++;
|
|
OnScored?.InvokeSafe(this);
|
|
|
|
CheckFinish();
|
|
}
|
|
|
|
public void ScoreToRight()
|
|
{
|
|
ScoreRight++;
|
|
OnScored?.InvokeSafe(this);
|
|
|
|
CheckFinish();
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
ScoreLeft = ScoreRight = 0;
|
|
|
|
ball.ResetBall();
|
|
ball.LaunchBall(GetBallLaunchDirection());
|
|
|
|
OnReset?.InvokeSafe(this);
|
|
}
|
|
|
|
private void CheckFinish()
|
|
{
|
|
int halfwayScore = (int)(WinScore * .5f);
|
|
ball.ResetBall();
|
|
|
|
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
|
|
{
|
|
OnFinished?.InvokeSafe(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);
|
|
}
|
|
}
|