ship.gd 738 B

1234567891011121314151617181920212223242526272829303132
  1. class_name Ship
  2. extends RigidBody2D
  3. # Common base script for ships
  4. signal moved(speed: float)
  5. const THRUST_STRENGTH: float = 400_000.0
  6. const TORQUE_THRUST: float = 20.0
  7. const MOVE_CEIL : float = 2
  8. var current_force := Vector2.ZERO
  9. var current_torque : float = 0.0
  10. func _physics_process(delta):
  11. apply_central_force(current_force.rotated(rotation))
  12. apply_torque(current_torque)
  13. var square_speed: float = linear_velocity.length_squared()
  14. if square_speed > MOVE_CEIL:
  15. moved.emit(sqrt(square_speed))
  16. else:
  17. moved.emit(0.0)
  18. func _on_command(dir: Vector2) -> void:
  19. _thrust(dir)
  20. func _thrust(dir: Vector2) -> void:
  21. current_force = Vector2.UP * dir.y * THRUST_STRENGTH
  22. current_torque = dir.x * TORQUE_THRUST * THRUST_STRENGTH