tiny_rocket.gd 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. class_name TinyRocket
  2. extends SeatedVehicle
  3. @export var thrust_power: float = 10000.0
  4. @export var turn_to_torque: float = 2000.0
  5. @export var move_to_pitch: float = 4000.0
  6. var target_torque: float = 0.0
  7. var target_pitch: float = 0.0
  8. var target_rotation := Vector3.ZERO
  9. var target_thrust := Vector3.ZERO
  10. @onready var thrust : Node3D = $Thrust
  11. func trigger_thrust(activate: bool) -> void:
  12. if activate:
  13. target_thrust = Vector3.FORWARD * thrust_power
  14. thrust.show()
  15. else:
  16. target_thrust = Vector3.ZERO
  17. thrust.hide()
  18. func trigger_direction(dir: Vector2) -> void:
  19. target_torque = -dir.x * turn_to_torque
  20. target_pitch = -dir.y * move_to_pitch
  21. func _physics_process(_delta) -> void:
  22. _apply_rocket_rotation()
  23. _apply_rocket_thrust()
  24. func _apply_rocket_rotation() -> void:
  25. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  26. apply_torque(torque)
  27. func _apply_rocket_thrust() -> void:
  28. var force: Vector3 = transform.basis * target_thrust
  29. apply_central_force(force)
  30. func _on_dir_changed(dir: Vector2) -> void:
  31. trigger_direction(dir)
  32. func _on_main_action(pressed: bool) -> void:
  33. trigger_thrust(pressed)