chore: de-networked pong manager
This commit is contained in:
parent
bddf00ce7a
commit
d0ef2ef224
@ -8,18 +8,13 @@ using Syntriax.Engine.Systems.Input;
|
|||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class PongManagerBehaviour : Behaviour,
|
public class PongManagerBehaviour : Behaviour
|
||||||
IPacketListenerServer<PongManagerBehaviour.PongStartPacket>,
|
|
||||||
IPacketListenerServer<PongManagerBehaviour.PongResetPacket>,
|
|
||||||
IPacketListenerClient<PongManagerBehaviour.PongStartPacket>,
|
|
||||||
IPacketListenerClient<PongManagerBehaviour.PongResetPacket>,
|
|
||||||
IPacketListenerClient<PongManagerBehaviour.PongScoreUpdatePacket>
|
|
||||||
{
|
{
|
||||||
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
|
||||||
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
||||||
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
|
||||||
|
|
||||||
private readonly Random random = new();
|
private Random random = new();
|
||||||
private BallBehaviour ball = null!;
|
private BallBehaviour ball = null!;
|
||||||
|
|
||||||
private INetworkCommunicatorClient? networkClient = null!;
|
private INetworkCommunicatorClient? networkClient = null!;
|
||||||
@ -37,7 +32,7 @@ public class PongManagerBehaviour : Behaviour,
|
|||||||
protected override void OnFirstActiveFrame()
|
protected override void OnFirstActiveFrame()
|
||||||
{
|
{
|
||||||
IButtonInputs<Keys> buttonInputs = Universe.FindRequired<IButtonInputs<Keys>>();
|
IButtonInputs<Keys> buttonInputs = Universe.FindRequired<IButtonInputs<Keys>>();
|
||||||
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => networkClient?.SendToServer(new PongResetPacket()));
|
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => Reset());
|
||||||
|
|
||||||
ball = Universe.FindRequiredBehaviour<BallBehaviour>();
|
ball = Universe.FindRequiredBehaviour<BallBehaviour>();
|
||||||
networkClient = Universe.Find<INetworkCommunicatorClient>();
|
networkClient = Universe.Find<INetworkCommunicatorClient>();
|
||||||
@ -46,22 +41,16 @@ public class PongManagerBehaviour : Behaviour,
|
|||||||
|
|
||||||
public void ScoreToLeft()
|
public void ScoreToLeft()
|
||||||
{
|
{
|
||||||
if (networkServer is not null)
|
ScoreLeft++;
|
||||||
{
|
OnScored?.InvokeSafe(this);
|
||||||
ScoreLeft++;
|
|
||||||
OnScored?.InvokeSafe(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckFinish();
|
CheckFinish();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ScoreToRight()
|
public void ScoreToRight()
|
||||||
{
|
{
|
||||||
if (networkServer is not null)
|
ScoreRight++;
|
||||||
{
|
OnScored?.InvokeSafe(this);
|
||||||
ScoreRight++;
|
|
||||||
OnScored?.InvokeSafe(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckFinish();
|
CheckFinish();
|
||||||
}
|
}
|
||||||
@ -69,6 +58,10 @@ public class PongManagerBehaviour : Behaviour,
|
|||||||
public void Reset()
|
public void Reset()
|
||||||
{
|
{
|
||||||
ScoreLeft = ScoreRight = 0;
|
ScoreLeft = ScoreRight = 0;
|
||||||
|
|
||||||
|
ball.ResetBall();
|
||||||
|
ball.LaunchBall(GetBallLaunchDirection());
|
||||||
|
|
||||||
OnReset?.InvokeSafe(this);
|
OnReset?.InvokeSafe(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,11 +70,14 @@ public class PongManagerBehaviour : Behaviour,
|
|||||||
int halfwayScore = (int)(WinScore * .5f);
|
int halfwayScore = (int)(WinScore * .5f);
|
||||||
ball.ResetBall();
|
ball.ResetBall();
|
||||||
|
|
||||||
networkServer?.SendToClient("*", new PongScoreUpdatePacket(this));
|
|
||||||
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
|
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
|
||||||
|
{
|
||||||
OnFinished?.InvokeSafe(this);
|
OnFinished?.InvokeSafe(this);
|
||||||
else
|
return;
|
||||||
networkServer?.SendToClient("*", new PongStartPacket() { BallVelocity = GetBallLaunchDirection() });
|
}
|
||||||
|
|
||||||
|
ball.ResetBall();
|
||||||
|
ball.LaunchBall(GetBallLaunchDirection());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector2D GetBallLaunchDirection()
|
private Vector2D GetBallLaunchDirection()
|
||||||
@ -91,49 +87,4 @@ public class PongManagerBehaviour : Behaviour,
|
|||||||
bool isBackwards = (random.Next() % 2) == 1;
|
bool isBackwards = (random.Next() % 2) == 1;
|
||||||
return Vector2D.Right.Rotate(isBackwards ? rotation + Syntriax.Engine.Core.Math.PI : rotation);
|
return Vector2D.Right.Rotate(isBackwards ? rotation + Syntriax.Engine.Core.Math.PI : rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnClientPacketArrived(PongStartPacket packet) => ball.LaunchBall(packet.BallVelocity);
|
|
||||||
public void OnClientPacketArrived(PongResetPacket packet) => Reset();
|
|
||||||
public void OnClientPacketArrived(PongScoreUpdatePacket packet)
|
|
||||||
{
|
|
||||||
ScoreLeft = packet.Left;
|
|
||||||
ScoreRight = packet.Right;
|
|
||||||
OnScored?.InvokeSafe(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnServerPacketArrived(PongResetPacket packet, string from)
|
|
||||||
{
|
|
||||||
Reset();
|
|
||||||
Vector2D ballVelocity = GetBallLaunchDirection();
|
|
||||||
ball.LaunchBall(ballVelocity);
|
|
||||||
networkServer?.SendToClient("*", new PongResetPacket());
|
|
||||||
networkServer?.SendToClient("*", new PongStartPacket() { BallVelocity = ballVelocity });
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnServerPacketArrived(PongStartPacket packet, string from)
|
|
||||||
{
|
|
||||||
packet = new() { BallVelocity = GetBallLaunchDirection() };
|
|
||||||
ball.LaunchBall(packet.BallVelocity);
|
|
||||||
networkServer?.SendToClient("*", packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PongStartPacket : INetworkPacket
|
|
||||||
{
|
|
||||||
public Vector2D BallVelocity { get; set; } = Vector2D.Zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PongResetPacket : INetworkPacket;
|
|
||||||
|
|
||||||
public class PongScoreUpdatePacket : INetworkPacket
|
|
||||||
{
|
|
||||||
public int Left { get; set; } = 0;
|
|
||||||
public int Right { get; set; } = 0;
|
|
||||||
|
|
||||||
public PongScoreUpdatePacket() { }
|
|
||||||
public PongScoreUpdatePacket(PongManagerBehaviour pongManagerBehaviour)
|
|
||||||
{
|
|
||||||
Left = pongManagerBehaviour.ScoreLeft;
|
|
||||||
Right = pongManagerBehaviour.ScoreRight;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user