fix: games not working when hosting a game

This commit is contained in:
2026-03-31 17:47:26 +03:00
parent eb8bf5837c
commit 174b34dcb6
3 changed files with 38 additions and 20 deletions

View File

@@ -9,7 +9,7 @@ using Engine.Systems.Tween;
namespace Pong.Behaviours;
public class Ball : Behaviour2D, IFirstFrameUpdate, ILoadContent, IPhysicsUpdate, INetworkEntity,
public class Ball : CommonNetworkBehaviour, IFirstFrameUpdate, ILoadContent, IPhysicsUpdate, INetworkEntity,
IPacketListenerClient<Ball.BallUpdatePacket>,
IPacketListenerClient<Ball.BallResetPacket>
{
@@ -17,21 +17,23 @@ public class Ball : Behaviour2D, IFirstFrameUpdate, ILoadContent, IPhysicsUpdate
public float SpeedUpMultiplier { get; set; } = .025f;
public IRigidBody2D RigidBody { get; private set; } = null!;
public ITransform2D Transform { get; private set; } = null!;
private IPhysicsEngine2D physicsEngine2D = null!;
private ITweenManager tweenManager = null!;
private INetworkCommunicatorServer? networkServer = null;
private ITween? networkTween = null;
private SoundEffect? bounceSoundEffect = null;
public void FirstActiveFrame()
public override void FirstActiveFrame()
{
base.FirstActiveFrame();
Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
BehaviourController.GetRequiredBehaviour<ICollider2D>().OnCollisionDetected.AddListener(OnCollisionDetected);
physicsEngine2D = Universe.FindRequiredBehaviour<IPhysicsEngine2D>();
tweenManager = Universe.FindRequiredBehaviour<ITweenManager>();
RigidBody = BehaviourController.GetRequiredBehaviour<IRigidBody2D>();
networkServer = Universe.FindBehaviour<INetworkCommunicatorServer>();
}
public void LoadContent(ContentManager content)
@@ -43,7 +45,7 @@ public class Ball : Behaviour2D, IFirstFrameUpdate, ILoadContent, IPhysicsUpdate
{
ResetBall();
RigidBody.Velocity = launchDirection * Speed;
networkServer?.SendToAll(new BallUpdatePacket(this));
server?.SendToAll(new BallUpdatePacket(this));
}
public void ResetBall()
@@ -52,7 +54,7 @@ public class Ball : Behaviour2D, IFirstFrameUpdate, ILoadContent, IPhysicsUpdate
tweenManager.CancelTween(networkTween);
Transform.Position = Vector2D.Zero;
RigidBody.Velocity = Vector2D.Zero;
networkServer?.SendToAll(new BallResetPacket());
server?.SendToAll(new BallResetPacket());
}
public void PhysicsUpdate(float delta)
@@ -72,24 +74,34 @@ public class Ball : Behaviour2D, IFirstFrameUpdate, ILoadContent, IPhysicsUpdate
RigidBody.Velocity = RigidBody.Velocity.Reflect(information.Normal);
if (information.Detected.Transform.BehaviourController.GetBehaviour<ScoreWall>() is null)
networkServer?.SendToAll(new BallUpdatePacket(this));
server?.SendToAll(new BallUpdatePacket(this));
}
public Ball() { }
public Ball(float speed) => Speed = speed;
void IPacketListenerClient<BallResetPacket>.OnClientPacketArrived(IConnection sender, BallResetPacket packet) => ResetBall();
void IPacketListenerClient<BallResetPacket>.OnClientPacketArrived(IConnection sender, BallResetPacket packet)
{
if (IsServer)
return;
ResetBall();
}
void IPacketListenerClient<BallUpdatePacket>.OnClientPacketArrived(IConnection sender, BallUpdatePacket packet)
{
if (RigidBody.Velocity.MagnitudeSquared >= .01f)
bounceSoundEffect?.Play();
if (IsServer)
return;
Vector2D localToServerPosition = Transform.Position.FromTo(packet.Position);
if (localToServerPosition.MagnitudeSquared < 4f)
networkTween = Transform.TweenPositionAdditive(tweenManager, .25f, localToServerPosition);
else
Transform.Position = packet.Position;
if (RigidBody.Velocity.MagnitudeSquared >= .01f)
bounceSoundEffect?.Play();
RigidBody.Velocity = packet.Velocity;
physicsEngine2D.StepIndividual(RigidBody, sender.Ping);
}