tiny_plane.gd 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. class_name TinyPlane
  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. @export var wing_resistance: float = 5000.0
  7. @export var lift: float = 100.0
  8. var target_torque: float = 0.0
  9. var target_pitch: float = 0.0
  10. var target_rotation := Vector3.ZERO
  11. var target_thrust := Vector3.ZERO
  12. func trigger_thrust(activate: bool) -> void:
  13. if activate:
  14. target_thrust = Vector3.FORWARD * thrust_power
  15. else:
  16. target_thrust = Vector3.ZERO
  17. func trigger_direction(dir: Vector2) -> void:
  18. target_torque = -dir.x * turn_to_torque
  19. target_pitch = -dir.y * move_to_pitch
  20. func get_free_seat() -> Node3D:
  21. return $DrivingSeat
  22. func get_door() -> Node3D:
  23. return $UpDoor
  24. func get_closest_door(from: Vector3) -> Node3D:
  25. return get_door()
  26. func _physics_process(_delta) -> void:
  27. # Command part
  28. _apply_plane_rotation()
  29. _apply_plane_thrust()
  30. # Air simulation part
  31. if is_sleeping():
  32. return
  33. _apply_wing_resistance()
  34. _apply_lift()
  35. func _apply_plane_rotation() -> void:
  36. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  37. apply_torque(torque)
  38. func _apply_plane_thrust() -> void:
  39. var force: Vector3 = transform.basis * target_thrust
  40. apply_central_force(force)
  41. func _apply_wing_resistance() -> void:
  42. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  43. var local_wing_force = Vector3.UP * -wing_resistance * vertical_speed
  44. var wing_force = transform.basis * local_wing_force
  45. apply_central_force(wing_force)
  46. func _apply_lift() -> void:
  47. var forward_speed = linear_velocity.dot(transform.basis * Vector3.FORWARD)
  48. if forward_speed < 0.0 :
  49. forward_speed = 0.0
  50. var local_lift_force = Vector3.UP * lift * forward_speed
  51. var lift_force = transform.basis * local_lift_force
  52. apply_central_force(lift_force)
  53. func _on_dir_changed(dir: Vector2) -> void:
  54. trigger_direction(dir)
  55. func _on_main_action(pressed: bool) -> void:
  56. trigger_thrust(pressed)