2023-05-24 04:48:30 +00:00
|
|
|
using Pong.Scripts.Data;
|
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
|
|
|
|
2023-05-24 04:48:30 +00:00
|
|
|
public partial class Paddle : BasePaddle
|
2023-05-17 06:15:31 +00:00
|
|
|
{
|
2024-05-04 20:20:03 +00:00
|
|
|
private float _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 bool _canMove = true;
|
|
|
|
private Vector2 _originalPosition;
|
|
|
|
|
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-06-03 00:15:44 +00:00
|
|
|
if (!Running) return;
|
|
|
|
|
2023-05-24 04:48:30 +00:00
|
|
|
if (NewPosition.HasValue)
|
2023-05-23 07:21:54 +00:00
|
|
|
{
|
2023-05-24 04:48:30 +00:00
|
|
|
state.Transform = new Transform2D(0, NewPosition.Value);
|
|
|
|
NewPosition = null;
|
2023-05-23 07:21:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 04:48:30 +00:00
|
|
|
state.LinearVelocity = Vector2.Up * MoveSpeed.ByMeter() * _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>
|
2024-05-04 20:20:03 +00:00
|
|
|
private float 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;
|
|
|
|
}
|
|
|
|
}
|