class_name Ship
extends RigidBody2D
# Common base script for ships

signal moved(speed: float)

const THRUST_STRENGTH: float = 400_000.0
const TORQUE_THRUST: float = 20.0
const MOVE_CEIL : float = 2

var current_force := Vector2.ZERO
var current_torque : float = 0.0


func _physics_process(delta):
	apply_central_force(current_force.rotated(rotation))
	apply_torque(current_torque)

	var square_speed: float = linear_velocity.length_squared()
	if square_speed > MOVE_CEIL:
		moved.emit(sqrt(square_speed))
	else:
		moved.emit(0.0)


func _on_command(dir: Vector2) -> void:
	_thrust(dir)


func _thrust(dir: Vector2) -> void:
	current_force = Vector2.UP * dir.y * THRUST_STRENGTH
	current_torque = dir.x * TORQUE_THRUST * THRUST_STRENGTH
