stick-the-quick/autoload/PlayerControl/PlayerControl.gd

100 lines
2.4 KiB
GDScript

extends Node
enum Status {
ENABLED,
BORROWED,
PAUSED,
NO_RUNNER,
RUNNER_NOT_IN_FOCUS,
CAMERA_NOT_CURRENT,
ERROR
}
@onready var _disabled_reentrometer := StateReentrometer.new()
@onready var _paused_reentrometer := StateReentrometer.new()
@onready var _runner_controller := $RunnerController as RunnerController
func _ready() -> void:
_disabled_reentrometer.entered.connect(
_on_disabled_reentrometer_entered
)
_disabled_reentrometer.exited.connect(
_on_disabled_reentrometer_exited
)
_paused_reentrometer.entered.connect(
_on_paused_reentrometer_entered
)
_paused_reentrometer.exited.connect(
_on_paused_reentrometer_exited
)
func status() -> Status:
if _disabled_reentrometer.depth() > _paused_reentrometer.depth():
return Status.BORROWED
elif _paused_reentrometer.depth() > 0:
return Status.PAUSED
elif (
(not _runner_controller) or
(not _runner_controller.is_inside_tree()) or
(not _runner_controller.is_node_ready())
):
return Status.ERROR
elif not _runner_controller.current:
return Status.CAMERA_NOT_CURRENT
elif not get_runner():
return Status.NO_RUNNER
elif _runner_controller.target_override:
return Status.RUNNER_NOT_IN_FOCUS
else:
return Status.ENABLED
func get_runner() -> Runner:
return _runner_controller.runner
func switch_runner(
whom: Runner,
distance: float = RunnerController.DEFAULT_DISTANCE
) -> void:
_runner_controller.switch_runner(whom, distance)
func get_camera() -> RunnerObserver:
return _runner_controller
func borrow() -> void:
_disabled_reentrometer.descend()
func pause() -> void:
_disabled_reentrometer.descend()
_paused_reentrometer.descend()
func unpause() -> void:
_paused_reentrometer.ascend()
_disabled_reentrometer.ascend()
func restore() -> void:
assert(status() == Status.BORROWED)
_disabled_reentrometer.ascend()
func _on_disabled_reentrometer_entered() -> void:
_runner_controller.control_enabled = false
func _on_disabled_reentrometer_exited() -> void:
_runner_controller.make_current()
_runner_controller.target_override = null
_runner_controller.control_enabled = true
func _on_paused_reentrometer_entered() -> void:
get_tree().paused = true
func _on_paused_reentrometer_exited() -> void:
get_tree().paused = false
func enabled() -> bool:
return status() == Status.ENABLED
func borrowed() -> bool:
return status() == Status.BORROWED
func paused() -> bool:
return status() == Status.PAUSED