stick-the-quick/zones/SequenceTrigger/SequenceTrigger.gd

56 lines
1.6 KiB
GDScript

class_name SequenceTrigger extends Area3D
signal returned(value: Variant)
enum Condition {ENTER, EXIT, INTERACT}
@export var condition := Condition.ENTER
@export var sequence := &''
@export var args := {}
@export var cursor_color := Color.WHITE
@export var cursor_offset := Vector3(0.0, 1.0, 0.0)
@export var camera_distance: float = -1.0
var _busy := false
var value_last_returned: Variant
func _ready() -> void:
if camera_distance < 0.0: camera_distance = FX.DEFAULT_CAMERA_DISTANCE/1.5
match condition:
Condition.ENTER: body_entered.connect(_run_if_node_is_player)
Condition.EXIT: body_exited.connect(_run_if_node_is_player)
Condition.INTERACT:
Cursor3D.clicked.connect(_run_if_node_is_self)
body_entered.connect(_select_if_node_is_player)
body_exited.connect(_deselect_if_node_is_player)
func _run_if_node_is_player(whom: Node) -> void:
if whom == PlayerControl.get_runner():
_run()
func _run_if_node_is_self(whom: Node) -> void:
if whom == self:
_run()
func _run() -> void:
if not _busy:
_busy = true
PlayerControl.borrow()
var real_args = args.duplicate()
real_args[&'trigger'] = self
value_last_returned = await Sequence.Call(sequence, real_args)
returned.emit(value_last_returned)
PlayerControl.restore()
_busy = false
func _select_if_node_is_player(whom: Node) -> void:
if PlayerControl.enabled() and whom == PlayerControl.get_runner():
Cursor3D.select(self, cursor_color, cursor_offset, camera_distance)
func _deselect_if_node_is_player(whom: Node) -> void:
if whom == PlayerControl.get_runner():
Cursor3D.deselect(self)
func _exit_tree() -> void:
Cursor3D.deselect(self)