fix: paddles being knocked back by the ball collision
This commit is contained in:
@@ -9,7 +9,8 @@ using Syntriax.Engine.Systems.Input;
|
||||
|
||||
namespace Pong.Behaviours;
|
||||
|
||||
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : Behaviour2D, IFirstFrameUpdate, IPostPhysicsUpdate,
|
||||
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : Behaviour2D,
|
||||
IFirstFrameUpdate, IPhysicsIteration, IPostPhysicsUpdate,
|
||||
IPacketListenerServer<PaddleBehaviour.PaddleKeyStatePacket>, IPacketListenerClient<PaddleBehaviour.PaddleKeyStatePacket>
|
||||
{
|
||||
private Keys Up { get; } = Up;
|
||||
@@ -27,6 +28,14 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
private IRigidBody2D rigidBody = null!;
|
||||
private IPhysicsEngine2D physicsEngine2D = null!;
|
||||
|
||||
public void PhysicsIterate(float delta)
|
||||
{
|
||||
if (isUpPressed)
|
||||
rigidBody.Transform.Position += Vector2D.Up * Speed * delta;
|
||||
else if (isDownPressed)
|
||||
rigidBody.Transform.Position -= Vector2D.Up * Speed * delta;
|
||||
}
|
||||
|
||||
public void PostPhysicsUpdate(float delta)
|
||||
{
|
||||
Transform.Position = new Vector2D(Transform.Position.X, MathF.Max(MathF.Min(Transform.Position.Y, High), Low));
|
||||
@@ -56,26 +65,15 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
inputs.UnregisterOnRelease(Down, OnDownReleased);
|
||||
}
|
||||
|
||||
private void OnUpPressed(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isUpPressed = true; UpdateVelocity(); networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnUpReleased(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isUpPressed = false; UpdateVelocity(); networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnDownPressed(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isDownPressed = true; UpdateVelocity(); networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnDownReleased(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isDownPressed = false; UpdateVelocity(); networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
|
||||
private void UpdateVelocity()
|
||||
{
|
||||
if (isUpPressed)
|
||||
rigidBody.Velocity = Vector2D.Up * Speed;
|
||||
else if (isDownPressed)
|
||||
rigidBody.Velocity = -Vector2D.Up * Speed;
|
||||
else
|
||||
rigidBody.Velocity = Vector2D.Zero;
|
||||
}
|
||||
private void OnUpPressed(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isUpPressed = true; networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnUpReleased(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isUpPressed = false; networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnDownPressed(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isDownPressed = true; networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
private void OnDownReleased(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) { isDownPressed = false; networkClient?.SendToServer(new PaddleKeyStatePacket(this)); }
|
||||
|
||||
public void OnServerPacketArrived(string sender, PaddleKeyStatePacket packet)
|
||||
{
|
||||
isUpPressed = packet.IsUpPressed;
|
||||
isDownPressed = packet.IsDownPressed;
|
||||
UpdateVelocity();
|
||||
|
||||
networkServer?.SendToClient("*", new PaddleKeyStatePacket(this));
|
||||
}
|
||||
@@ -86,8 +84,6 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
isDownPressed = packet.IsDownPressed;
|
||||
Transform.Position = packet.Position;
|
||||
|
||||
UpdateVelocity();
|
||||
|
||||
physicsEngine2D.StepIndividual(rigidBody, new DateTime(DateTime.UtcNow.Ticks - packet.Timestamp).Second);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user