chore: improved paddle synchronization

This commit is contained in:
2025-05-24 17:09:13 +03:00
parent 0f0180d435
commit 79922fadbe
2 changed files with 34 additions and 24 deletions

View File

@@ -5,6 +5,7 @@ using Microsoft.Xna.Framework.Input;
using Syntriax.Engine.Core;
using Syntriax.Engine.Network;
using Syntriax.Engine.Systems.Input;
using Syntriax.Engine.Systems.Tween;
namespace Pong.Behaviours;
@@ -20,22 +21,20 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
private bool isUpPressed = false;
private bool isDownPressed = false;
private bool isMovingUp = false;
private bool isMovingDown = false;
private IButtonInputs<Keys> inputs = null!;
private INetworkCommunicatorClient networkClient = null!;
private INetworkCommunicatorServer? networkServer = null;
private TweenManager tweenManager = null!;
protected override void OnUpdate()
{
if (isMovingUp && isMovingDown)
if (isUpPressed && isDownPressed)
return;
if (isMovingUp)
Transform.Position = Transform.Position + Vector2D.Up * Universe.Time.DeltaTime * Speed;
else if (isMovingDown)
Transform.Position = Transform.Position + -Vector2D.Up * Universe.Time.DeltaTime * Speed;
if (isUpPressed)
Transform.Position += Vector2D.Up * Universe.Time.DeltaTime * Speed;
else if (isDownPressed)
Transform.Position += -Vector2D.Up * Universe.Time.DeltaTime * Speed;
Transform.Position = new Vector2D(Transform.Position.X, MathF.Max(MathF.Min(Transform.Position.Y, High), Low));
}
@@ -45,6 +44,7 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
inputs = Universe.FindRequiredBehaviour<IButtonInputs<Keys>>();
networkClient = Universe.FindRequiredBehaviour<INetworkCommunicatorClient>();
networkServer = Universe.FindBehaviour<INetworkCommunicatorServer>();
tweenManager = Universe.GetRequiredUniverseObject<TweenManager>();
inputs.RegisterOnPress(Up, OnUpPressed);
inputs.RegisterOnRelease(Up, OnUpReleased);
@@ -72,6 +72,7 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
if (packet.EntityId.CompareTo(Id) != 0)
return;
packet.Position = Transform.Position;
networkServer?.SendToClient("*", packet);
}
@@ -80,8 +81,12 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
if (packet.EntityId.CompareTo(Id) != 0)
return;
isMovingUp = packet.IsUpPressed;
isMovingDown = packet.IsDownPressed;
isUpPressed = packet.IsUpPressed;
isDownPressed = packet.IsDownPressed;
if (!isUpPressed && !isDownPressed)
Transform.Position.TweenVector2D(tweenManager, .05f, packet.Position, x => Transform.Position = x);
else
Transform.Position = packet.Position;
}
public class PaddleKeyStatePacket : IEntityNetworkPacket
@@ -89,8 +94,8 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
public string EntityId { get; set; } = default!;
public bool IsUpPressed { get; set; } = default!;
public bool IsDownPressed { get; set; } = default!;
string IEntityNetworkPacket.EntityId => EntityId;
public Vector2D Position { get; set; } = default!;
public long TimeTicker { get; set; } = default!;
public PaddleKeyStatePacket() { }
public PaddleKeyStatePacket(PaddleBehaviour paddleBehaviour)
@@ -98,6 +103,8 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
EntityId = paddleBehaviour.Id;
IsUpPressed = paddleBehaviour.isUpPressed;
IsDownPressed = paddleBehaviour.isDownPressed;
Position = paddleBehaviour.Transform.Position;
TimeTicker = DateTime.UtcNow.Ticks;
}
}
}