ship.gd 959 B

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