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: func _process(delta: float) -> void:
var input_direction := Input.get_vector("move_left", "move_right", "move_up", "move_down") var input_direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
$Ship.accelerate(input_direction, delta)
if input_direction.is_zero_approx():
$Ship.decelerate($Ship.deceleration * delta)
else:
var acceleration : Vector2 = input_direction * $Ship.acceleration * delta
$Ship.accelerate(acceleration)
+20 -15
View File
@@ -25,20 +25,25 @@ func _process(delta: float) -> void:
position += _velocity * delta position += _velocity * delta
func accelerate(value: Vector2) -> void: func accelerate(direction: Vector2, delta: float) -> void:
_velocity += value var accel : Vector2 = direction * acceleration * delta
_velocity = _velocity.clamp(Vector2(-max_speed, -max_speed), Vector2(max_speed, max_speed)) 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: func _get_new_speed(accel: float, decel: float, current_speed: float) -> float:
var current_speed := _velocity.length() if is_zero_approx(accel):
if absf(current_speed) < decel:
if current_speed <= 0: return 0.0
_velocity = Vector2.ZERO else:
return if current_speed < 0:
return current_speed + decel
var new_speed := current_speed - value else:
if new_speed < 0: return current_speed - decel
new_speed = 0 else:
return current_speed + accel
_velocity = _velocity.normalized() * new_speed