seated_vehicle.gd 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 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 react to direction changed
  40. func _on_dir_changed(_dir: Vector2) -> void:
  41. pass
  42. ## Need to be overridden to react to main action
  43. func _on_main_action(_pressed: bool) -> void:
  44. pass
  45. ## Need to be overridden to react to main action
  46. func _on_stop_action(_pressed: bool) -> void:
  47. pass
  48. ## Need to be overridden to react to analog actions
  49. func _on_analog_action(_side: int, _value: float) -> void:
  50. pass