walker.gd 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. class_name Walker
  2. extends CharacterBody3D
  3. signal focus_required(me: Node3D)
  4. signal got_in
  5. signal got_out
  6. ## How fast the player moves in meters per second.
  7. @export var speed = 14
  8. ## Convert an axis from [-1, -1] to rad
  9. @export var turn_to_rad: float = 0.020
  10. ## The downward acceleration when in the air, in meters per second squared.
  11. @export var fall_acceleration = 75
  12. ## Vertical impulse applied to the character upon jumping in meters per second.
  13. @export var jump_impulse = 20
  14. @export_group("Seat", "seat_")
  15. @export var seat_min_dist: float = 0.0005
  16. @export var seat_access_speed: float = 2.0
  17. @export var seat_rotation_duration: float = 1.0
  18. var target_velocity := Vector3.ZERO
  19. var _vehicle: Node3D = null
  20. var _seat: Node3D = null
  21. @onready var _vehicle_range: RayCast3D = $VehicleRange
  22. func _ready() -> void:
  23. focus_required.emit(self)
  24. func trigger_jump() -> void:
  25. if is_on_floor():
  26. target_velocity.y = jump_impulse
  27. func trigger_direction(dir: Vector2) -> void:
  28. var target_character_direction = Vector3(dir.x, 0.0, -dir.y)
  29. var camera_basis: Basis = get_viewport().get_camera_3d().get_camera_transform().basis
  30. var target_world_direction: Vector3 = camera_basis * target_character_direction
  31. target_world_direction.y = 0.0
  32. var target_walk_velocity = target_world_direction.normalized() * speed
  33. target_velocity.x = target_walk_velocity.x
  34. target_velocity.z = target_walk_velocity.z
  35. ## Return true if inside a vehicle
  36. func is_onboard() -> bool:
  37. return _vehicle != null
  38. func _physics_process(delta: float) -> void:
  39. if is_onboard():
  40. _get_on_driver_seat()
  41. else:
  42. _move_with_feet(delta)
  43. func _move_with_feet(delta: float):
  44. # Face forward
  45. _look_forward(target_velocity)
  46. # Gravity
  47. if not is_on_floor():
  48. target_velocity.y = target_velocity.y - (fall_acceleration * delta)
  49. # Moving the Character
  50. velocity = target_velocity
  51. move_and_slide()
  52. func _get_on_driver_seat() -> void:
  53. if _seat == null :
  54. return
  55. if position.distance_squared_to(_seat.position) < seat_min_dist:
  56. return
  57. var rotation_tween: Tween = create_tween()
  58. rotation_tween.tween_property(self, "quaternion", _seat.quaternion, seat_rotation_duration)
  59. var local_velocity = (_seat.position - position).normalized() * seat_access_speed
  60. velocity = get_parent().get_transform().basis * local_velocity
  61. move_and_slide()
  62. func _on_dir_changed(dir: Vector2) -> void:
  63. if is_onboard():
  64. return
  65. trigger_direction(dir)
  66. func _on_main_action(pressed: bool) -> void:
  67. if is_onboard():
  68. return
  69. if pressed:
  70. trigger_jump()
  71. func _on_get_in_action(commander: LocalInput) -> void:
  72. if is_onboard():
  73. _get_out_vehicle()
  74. else:
  75. _get_in_vehicle(commander)
  76. ## Seek the first vehicle in front of the player
  77. ## and get in to it
  78. ## and take the wheel !
  79. func _get_in_vehicle(commander: LocalInput) -> void:
  80. var closest_vehicle: Node3D = _get_closest_vehicle()
  81. if closest_vehicle == null:
  82. return # No vehicle to get inside
  83. _vehicle = closest_vehicle
  84. reparent(_vehicle)
  85. add_collision_exception_with(_vehicle)
  86. _vehicle.drive_with(commander)
  87. _seat = _find_seat_on(_vehicle)
  88. got_in.emit()
  89. ## Get out of the current vehicle
  90. func _get_out_vehicle() -> void:
  91. _vehicle.get_out()
  92. remove_collision_exception_with(_vehicle)
  93. reparent(_vehicle.get_parent())
  94. _vehicle = null
  95. _seat = null
  96. _get_head_up()
  97. got_out.emit()
  98. ## Make the player stand up
  99. func _get_head_up():
  100. set_rotation(Vector3(0.0, rotation.y, 0.0))
  101. ## Make the player look forward
  102. func _look_forward(forward: Vector3) -> void:
  103. var horizontal_forward: Vector3 = forward.slide(Vector3.UP)
  104. if horizontal_forward.is_zero_approx():
  105. return
  106. else:
  107. look_at(position + horizontal_forward)
  108. ## Return closest vehicle within reach
  109. ## or null if there are none
  110. func _get_closest_vehicle() -> Node3D:
  111. _vehicle_range.force_raycast_update()
  112. var object_within_range: Object = _vehicle_range.get_collider()
  113. if object_within_range is Node3D:
  114. var node_within_range: Node3D = object_within_range as Node3D
  115. if node_within_range.is_in_group("vehicle"):
  116. print("Get in ", node_within_range)
  117. return node_within_range
  118. print("No vehicle found")
  119. return null
  120. ## Have a seat on the driven vehicle if a seat
  121. ## is available
  122. func _find_seat_on(vehicule: Node3D) -> Node3D:
  123. if vehicule.has_method("get_free_seat") :
  124. return vehicule.get_free_seat()
  125. return null