Randoloot/scripts/game/flight_movement.gd

32 lines
1.8 KiB
GDScript

extends RefCounted
## Common steering math for the ship and surface lander.
static func apply(body: Object, delta: float, turn: float, thrust: bool, brake: bool, strafe: float, boost: bool, was_boosting: bool, allowed_speed: float) -> void:
if not is_zero_approx(turn):
body.angular_velocity = move_toward(body.angular_velocity, turn * body.max_turn_speed, body.turn_acceleration * delta)
else:
body.angular_velocity = move_toward(body.angular_velocity, 0.0, body.turn_damping * delta)
body.rotation += body.angular_velocity * delta
var forward := Vector2.UP.rotated(body.rotation)
if brake:
if body.velocity.dot(forward) > 0.0:
body.velocity = body.velocity.move_toward(Vector2.ZERO, body.brake_acceleration * delta)
else:
body.velocity -= forward * body.thrust_acceleration * delta
var reverse_speed: float = body.velocity.dot(forward)
if reverse_speed < -body.reverse_max_speed:
body.velocity += forward * (-body.reverse_max_speed - reverse_speed)
elif thrust or boost:
body.velocity += forward * body.thrust_acceleration * (2.0 if boost else 1.0) * delta
var forward_speed: float = body.velocity.dot(forward)
if forward_speed > allowed_speed and not was_boosting:
body.velocity += forward * (allowed_speed - forward_speed)
var right := Vector2.RIGHT.rotated(body.rotation)
if not is_zero_approx(strafe):
body.velocity += right * strafe * body.thrust_acceleration * delta
var strafe_speed: float = body.velocity.dot(right)
body.velocity += right * (clampf(strafe_speed, -body.max_strafe_speed, body.max_strafe_speed) - strafe_speed)
if not was_boosting and not boost and not brake and not thrust and is_zero_approx(strafe):
body.velocity = body.velocity.move_toward(Vector2.ZERO, body.space_drag * delta)
if body.velocity.length_squared() > allowed_speed * allowed_speed:
body.velocity = body.velocity.normalized() * allowed_speed