biplan.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. class_name Biplan
  2. extends SeatedVehicle
  3. const PLANE_FORWARD := Vector3.BACK # z increases when moving forward
  4. @export var thrust_power: float = 3500.0
  5. @export var turn_to_torque: float = 1000.0
  6. @export var move_to_pitch: float = 2000.0
  7. @export var wing_resistance: float = 2500.0
  8. @export var foil_resistance: float = 250.0
  9. @export var lift: float = 25.0
  10. var target_torque: float = 0.0
  11. var target_pitch: float = 0.0
  12. var target_rotation := Vector3.ZERO
  13. var target_thrust := Vector3.ZERO
  14. @onready var VerticalFoil : Node3D = $VerticalFoil
  15. func trigger_thrust(activate: bool) -> void:
  16. if activate:
  17. target_thrust = PLANE_FORWARD * thrust_power
  18. else:
  19. target_thrust = Vector3.ZERO
  20. func trigger_direction(dir: Vector2) -> void:
  21. target_torque = dir.x * turn_to_torque
  22. target_pitch = dir.y * move_to_pitch
  23. func get_free_seat() -> Node3D:
  24. return $DrivingSeat
  25. func get_door() -> Node3D:
  26. return $FrontRightDoor
  27. func get_closest_door(from: Vector3) -> Node3D:
  28. return get_door()
  29. func _physics_process(_delta) -> void:
  30. # Command part
  31. _apply_plane_rotation()
  32. _apply_plane_thrust()
  33. # Air simulation part
  34. if is_sleeping():
  35. return
  36. _apply_wing_resistance()
  37. _apply_foil_resistance()
  38. _apply_lift()
  39. transform = transform.orthonormalized()
  40. func _apply_plane_rotation() -> void:
  41. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  42. apply_torque(torque)
  43. func _apply_plane_thrust() -> void:
  44. var force: Vector3 = transform.basis * target_thrust
  45. apply_central_force(force)
  46. func _apply_wing_resistance() -> void:
  47. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  48. var local_wing_force = Vector3.UP * -wing_resistance * vertical_speed
  49. var wing_force = transform.basis * local_wing_force
  50. apply_central_force(wing_force)
  51. func _apply_foil_resistance() -> void:
  52. var horizontal_speed = linear_velocity.dot(transform.basis.x)
  53. var local_foil_force = transform.basis.x * -foil_resistance * horizontal_speed
  54. var foil_local_position: Vector3 = VerticalFoil.get_position()
  55. var foil_global_offset: Vector3 = transform.basis * foil_local_position
  56. apply_force(local_foil_force, foil_global_offset)
  57. func _apply_lift() -> void:
  58. var forward_speed = linear_velocity.dot(transform.basis * PLANE_FORWARD)
  59. if forward_speed < 0.0 :
  60. forward_speed = 0.0
  61. var local_lift_force = Vector3.UP * lift * forward_speed
  62. var lift_force = transform.basis * local_lift_force
  63. apply_central_force(lift_force)
  64. func _on_dir_changed(dir: Vector2) -> void:
  65. trigger_direction(dir)
  66. func _on_main_action(pressed: bool) -> void:
  67. trigger_thrust(pressed)