tiny_plane.gd 2.1 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 _physics_process(_delta) -> void:
  23. # Command part
  24. _apply_plane_rotation()
  25. _apply_plane_thrust()
  26. # Air simulation part
  27. if is_sleeping():
  28. return
  29. _apply_wing_resistance()
  30. _apply_lift()
  31. func _apply_plane_rotation() -> void:
  32. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  33. apply_torque(torque)
  34. func _apply_plane_thrust() -> void:
  35. var force: Vector3 = transform.basis * target_thrust
  36. apply_central_force(force)
  37. func _apply_wing_resistance() -> void:
  38. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  39. var local_wing_force = Vector3.UP * -wing_resistance * vertical_speed
  40. var wing_force = transform.basis * local_wing_force
  41. print("= = =")
  42. print("Vertical speed : ", vertical_speed)
  43. print("Wing force local :", local_wing_force)
  44. print("Wing force : ", 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. print("- - -")
  53. print("Forward speed : ", forward_speed)
  54. print("Lift force local :", local_lift_force)
  55. print("Lift force : ", lift_force)
  56. apply_central_force(lift_force)
  57. func _on_dir_changed(dir: Vector2) -> void:
  58. trigger_direction(dir)
  59. func _on_main_action(pressed: bool) -> void:
  60. trigger_thrust(pressed)