2023-05-23 07:21:54 +00:00
|
|
|
using Pong.Scripts.Managers;
|
|
|
|
|
2023-05-21 04:42:38 +00:00
|
|
|
namespace Pong.Scripts.Objects;
|
2023-05-17 06:15:31 +00:00
|
|
|
|
|
|
|
public partial class Paddle : RigidBody2D
|
|
|
|
{
|
|
|
|
[Export] private double _moveSpeed;
|
2023-05-23 07:21:54 +00:00
|
|
|
|
2023-05-17 06:15:31 +00:00
|
|
|
private double _verticalInput;
|
2023-05-22 01:19:05 +00:00
|
|
|
private readonly StringName _paddleUp = "paddle_up";
|
|
|
|
private readonly StringName _paddleDown = "paddle_down";
|
2023-05-23 07:21:54 +00:00
|
|
|
private ScalingManager _scalingManager;
|
|
|
|
|
|
|
|
private bool _canMove = true;
|
|
|
|
private Vector2 _originalPosition;
|
|
|
|
|
|
|
|
private Vector2? _newPosition;
|
2023-05-17 06:15:31 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2023-05-18 06:41:26 +00:00
|
|
|
/// property that multiples the moveSpeed by the <see cref="Constants.Meter">Meter</see> constant.
|
2023-05-17 06:15:31 +00:00
|
|
|
/// </summary>
|
|
|
|
private double MoveSpeed => _moveSpeed * Constants.Meter;
|
2023-05-22 01:19:05 +00:00
|
|
|
|
2023-05-23 07:21:54 +00:00
|
|
|
public override void _EnterTree()
|
|
|
|
{
|
|
|
|
SetNotifyTransform(true);
|
|
|
|
SetNotifyLocalTransform(true);
|
|
|
|
_scalingManager = GetNode<ScalingManager>("../ScalingManager");
|
|
|
|
_scalingManager.NewPaddlePosition += (_, vector2) => _newPosition = vector2;
|
|
|
|
}
|
|
|
|
|
2023-05-17 06:15:31 +00:00
|
|
|
public override void _Process(double delta)
|
|
|
|
{
|
|
|
|
_verticalInput = GetVerticalInput();
|
|
|
|
}
|
|
|
|
|
2023-05-23 07:21:54 +00:00
|
|
|
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
2023-05-17 06:15:31 +00:00
|
|
|
{
|
2023-05-23 07:21:54 +00:00
|
|
|
if (_newPosition.HasValue)
|
|
|
|
{
|
|
|
|
state.Transform = new Transform2D(0, _newPosition.Value);
|
|
|
|
_newPosition = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.LinearVelocity = Vector2.Up * MoveSpeed * _verticalInput;
|
2023-05-17 06:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// a method that returns a number that represents input on the vertical axis.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>1 for up, -1 for down, 0 for nothing.</returns>
|
2023-05-22 01:19:05 +00:00
|
|
|
private double GetVerticalInput()
|
2023-05-17 06:15:31 +00:00
|
|
|
{
|
2023-05-22 01:19:05 +00:00
|
|
|
if (Input.IsActionPressed(_paddleUp))
|
2023-05-17 06:15:31 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-05-22 01:19:05 +00:00
|
|
|
if (Input.IsActionPressed(_paddleDown))
|
2023-05-17 06:15:31 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|