Files

86 lines
2.1 KiB
C#
Raw Permalink Normal View History

2025-08-02 15:08:38 +02:00
using Babushka.scripts.CSharp.Common.Services;
using Godot;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
public partial class PlayerMovement : CharacterBody2D
{
2025-08-06 21:25:28 +02:00
[Export] private float _speed = 1000f;
2025-08-27 17:12:12 +02:00
[Export] private Timer _stepTimer;
2025-08-06 21:25:28 +02:00
public override void _Process(double delta)
{
bool anyActionPressed = false;
Vector2 currentVelocity = Vector2.Zero;
if (!InputService.Instance.InputEnabled)
return;
bool right = Input.IsActionPressed("move_right");
bool left = Input.IsActionPressed("move_left");
bool up = Input.IsActionPressed("move_up");
bool down = Input.IsActionPressed("move_down");
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
if (up)
{
currentVelocity += new Vector2(0, -_speed);
anyActionPressed = true;
}
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
if (down)
{
currentVelocity += new Vector2(0, _speed);
anyActionPressed = true;
}
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
if (right)
{
currentVelocity += new Vector2(_speed, 0);
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
anyActionPressed = true;
}
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
if (left)
{
currentVelocity += new Vector2(-_speed, 0);
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
anyActionPressed = true;
}
2025-08-27 17:12:12 +02:00
if (Velocity.LengthSquared() == 0 && currentVelocity.LengthSquared() > 0)
{
_stepTimer.Start();
_stepTimer.SetPaused(false);
}
if (currentVelocity.LengthSquared() == 0 && Velocity.LengthSquared() != 0)
{
_stepTimer.Stop();
_stepTimer.SetPaused(true);
}
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
if (anyActionPressed)
{
if (currentVelocity.X != 0 && currentVelocity.Y != 0)
{
currentVelocity *= 0.7f;
}
2025-08-02 15:08:38 +02:00
2025-08-06 21:25:28 +02:00
// speed hack
var setting = ProjectSettings.GetSetting("babushka/hacks/speed_hack",-1).AsSingle();
if (setting > 0)
{
currentVelocity *= setting;
}
Velocity = currentVelocity;
MoveAndSlide();
}
2025-08-27 17:12:12 +02:00
else
{
Velocity = Vector2.Zero;
}
2025-08-06 21:25:28 +02:00
}
2025-08-02 15:08:38 +02:00
}