tiny_plane.gd 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. class_name TinyPlane
  2. extends RigidBody3D
  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 = 10.0
  7. @export var lift: float = 500.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. var _current_commander: LocalInput = null
  13. func trigger_thrust(activate: bool) -> void:
  14. if activate:
  15. target_thrust = Vector3.FORWARD * thrust_power
  16. else:
  17. target_thrust = Vector3.ZERO
  18. func trigger_direction(dir: Vector2) -> void:
  19. target_torque = -dir.x * turn_to_torque
  20. target_pitch = -dir.y * move_to_pitch
  21. ## Make the vehicle responds to driver commands
  22. func drive_with(commander: LocalInput) -> void:
  23. commander.dir_changed.connect(_on_dir_changed)
  24. commander.main_action.connect(_on_main_action)
  25. _current_commander = commander
  26. func get_out() -> void:
  27. _current_commander.dir_changed.disconnect(_on_dir_changed)
  28. _current_commander.main_action.disconnect(_on_main_action)
  29. _current_commander = null
  30. func get_free_seat() -> Node3D:
  31. return $DrivingSeat
  32. func _physics_process(delta: float) -> void:
  33. _apply_plane_rotation()
  34. _apply_plane_thrust()
  35. _apply_wing_resistance()
  36. _apply_lift()
  37. func _apply_plane_rotation() -> void:
  38. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  39. apply_torque(torque)
  40. func _apply_plane_thrust() -> void:
  41. var force: Vector3 = transform.basis * target_thrust
  42. apply_central_force(force)
  43. func _apply_wing_resistance() -> void:
  44. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  45. var local_wing_force = Vector3.UP * -wing_resistance * vertical_speed
  46. var wing_force = transform.basis * local_wing_force
  47. if _current_commander != null:
  48. print("= = =")
  49. print("Vertical speed : ", vertical_speed)
  50. print("Wing force local :", local_wing_force)
  51. print("Wing force : ", wing_force)
  52. apply_central_force(wing_force)
  53. func _apply_lift() -> void:
  54. var forward_speed = linear_velocity.dot(transform.basis * Vector3.FORWARD)
  55. if forward_speed < 0.0 :
  56. forward_speed = 0.0
  57. var local_lift_force = Vector3.UP * lift * forward_speed
  58. var lift_force = transform.basis * local_lift_force
  59. if _current_commander != null:
  60. print("- - -")
  61. print("Forward speed : ", forward_speed)
  62. print("Lift force local :", local_lift_force)
  63. print("Lift force : ", lift_force)
  64. apply_central_force(lift_force)
  65. func _on_dir_changed(dir: Vector2) -> void:
  66. trigger_direction(dir)
  67. func _on_main_action(pressed: bool) -> void:
  68. trigger_thrust(pressed)