seated_vehicle.gd 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. class_name SeatedVehicle
  2. extends VehicleBody3D
  3. const CRASH_EFFECT: Resource = preload("res://effect/crash/crash.tscn")
  4. const MINIMAL_CRASH_SPEED: float = 1.0 # m2/s2
  5. var _current_commander: LocalInput = null
  6. var _has_collided: bool = false
  7. func _init() -> void:
  8. body_shape_entered.connect(_on_body_shape_entered)
  9. ## Make the vehicle responds to driver commands
  10. func drive_with(commander: LocalInput) -> void:
  11. commander.dir_changed.connect(_on_dir_changed)
  12. commander.main_action.connect(_on_main_action)
  13. commander.stop_action.connect(_on_stop_action)
  14. commander.analog_action.connect(_on_analog_action)
  15. _current_commander = commander
  16. func get_out() -> void:
  17. _current_commander.dir_changed.disconnect(_on_dir_changed)
  18. _current_commander.main_action.disconnect(_on_main_action)
  19. _current_commander.stop_action.disconnect(_on_stop_action)
  20. _current_commander.analog_action.disconnect(_on_analog_action)
  21. _current_commander = null
  22. func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
  23. if state.get_contact_count() <= 0:
  24. return
  25. if _has_collided and linear_velocity.length_squared() > MINIMAL_CRASH_SPEED:
  26. _crash_on(state.get_contact_collider_position(0))
  27. _has_collided = false
  28. func _on_body_shape_entered(_body_rid, body: Node, _body_shape_index, _local_shape_index) -> void:
  29. if body.is_in_group("walker"):
  30. return
  31. _has_collided = true
  32. func _crash_on(pos: Vector3) -> void:
  33. var crash_effect: CrashEffect = CRASH_EFFECT.instantiate()
  34. crash_effect.position = pos
  35. get_parent_node_3d().add_child(crash_effect)
  36. ## Need to be overridden to return an available seat
  37. func get_free_seat() -> Node3D:
  38. return null
  39. ## Need to be overridden to return an arbitrary door
  40. func get_door() -> Node3D:
  41. return null
  42. ## Need to be overridden to return the nearest door from local position
  43. func get_closest_door(from: Vector3) -> Node3D:
  44. return null
  45. ## Need to be overridden to react to direction changed
  46. func _on_dir_changed(_dir: Vector2) -> void:
  47. pass
  48. ## Need to be overridden to react to main action
  49. func _on_main_action(_pressed: bool) -> void:
  50. pass
  51. ## Need to be overridden to react to main action
  52. func _on_stop_action(_pressed: bool) -> void:
  53. pass
  54. ## Need to be overridden to react to analog actions
  55. func _on_analog_action(_side: int, _value: float) -> void:
  56. pass