Reworked player controls

This commit is contained in:
2025-10-21 13:01:18 +03:00
parent ae59ef5669
commit 19be23c539
2 changed files with 21 additions and 21 deletions
+1 -6
View File
@@ -10,9 +10,4 @@ var position : Vector2:
func _process(delta: float) -> void:
var input_direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
if input_direction.is_zero_approx():
$Ship.decelerate($Ship.deceleration * delta)
else:
var acceleration : Vector2 = input_direction * $Ship.acceleration * delta
$Ship.accelerate(acceleration)
$Ship.accelerate(input_direction, delta)
+20 -15
View File
@@ -25,20 +25,25 @@ func _process(delta: float) -> void:
position += _velocity * delta
func accelerate(value: Vector2) -> void:
_velocity += value
_velocity = _velocity.clamp(Vector2(-max_speed, -max_speed), Vector2(max_speed, max_speed))
func accelerate(direction: Vector2, delta: float) -> void:
var accel : Vector2 = direction * acceleration * delta
var decel : float = deceleration * delta
_velocity.x = _get_new_speed(accel.x, decel, _velocity.x)
_velocity.y = _get_new_speed(accel.y, decel, _velocity.y)
if _velocity.length() > max_speed:
_velocity = _velocity.normalized() * max_speed
func decelerate(value: float) -> void:
var current_speed := _velocity.length()
if current_speed <= 0:
_velocity = Vector2.ZERO
return
var new_speed := current_speed - value
if new_speed < 0:
new_speed = 0
_velocity = _velocity.normalized() * new_speed
func _get_new_speed(accel: float, decel: float, current_speed: float) -> float:
if is_zero_approx(accel):
if absf(current_speed) < decel:
return 0.0
else:
if current_speed < 0:
return current_speed + decel
else:
return current_speed - decel
else:
return current_speed + accel