Fries
8072cf4961
i made a BasePaddle class so i can put commonly used fields in a base class. this means i made a new plugin called CustomTypes which contains both Base classes.
55 lines
1.2 KiB
C#
55 lines
1.2 KiB
C#
using Pong.Scripts.Data;
|
|
using Pong.Scripts.Managers;
|
|
|
|
namespace Pong.Scripts.Objects;
|
|
|
|
public partial class Paddle : BasePaddle
|
|
{
|
|
private double _verticalInput;
|
|
private readonly StringName _paddleUp = "paddle_up";
|
|
private readonly StringName _paddleDown = "paddle_down";
|
|
|
|
private bool _canMove = true;
|
|
private Vector2 _originalPosition;
|
|
|
|
public override void _EnterTree()
|
|
{
|
|
base._EnterTree();
|
|
ScalingManager.NewPaddlePosition += SetNewPosition;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
_verticalInput = GetVerticalInput();
|
|
}
|
|
|
|
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
|
{
|
|
if (NewPosition.HasValue)
|
|
{
|
|
state.Transform = new Transform2D(0, NewPosition.Value);
|
|
NewPosition = null;
|
|
}
|
|
|
|
state.LinearVelocity = Vector2.Up * MoveSpeed.ByMeter() * _verticalInput;
|
|
}
|
|
|
|
/// <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>
|
|
private double GetVerticalInput()
|
|
{
|
|
if (Input.IsActionPressed(_paddleUp))
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
if (Input.IsActionPressed(_paddleDown))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|