|
8 | 8 | @icon("res://assets/editor/icons/Contact.svg") |
9 | 9 | class_name Trigger extends Cutscene |
10 | 10 |
|
11 | | -## Emitted when a [Gamepiece] begins moving to the cell occupied by the [code]Trigger[/code]. |
12 | | -signal gamepiece_entered(gamepiece: Gamepiece) |
13 | | - |
14 | | -## Emitted when a [Gamepiece] begins moving away from the cell occupied by the [code]Trigger[/code]. |
15 | | -signal gamepiece_exited(gamepiece: Gamepiece) |
16 | | - |
17 | | -## Emitted when a [Gamepiece] is finishing moving to the cell occupied by the [code]Trigger[/code]. |
18 | | -signal triggered(gamepiece: Gamepiece) |
19 | | - |
20 | | -## An active [code]Trigger[/code] may be run, whereas one that is inactive may only be run |
21 | | -## directly through code via the [method Cutscene.run] method. |
22 | | -@export var is_active: = true: |
23 | | - set(value): |
24 | | - is_active = value |
25 | | - |
26 | | - if not Engine.is_editor_hint(): |
27 | | - if not is_inside_tree(): |
28 | | - await ready |
29 | | - |
30 | | - # We use "Visible Collision Shapes" to debug positions on the gameboard, so we'll want |
31 | | - # to change the state of child collision shapes.These could be either CollisionShape2Ds |
32 | | - # or CollisionPolygon2Ds. |
33 | | - # Note that we only want to disable the collision shapes of objects that are actually |
34 | | - # connected to this Interaction. |
35 | | - for data in get_incoming_connections(): |
36 | | - var callable: = data["callable"] as Callable |
37 | | - if callable == _on_area_entered : |
38 | | - var connected_area: = data["signal"].get_object() as Area2D |
39 | | - if connected_area: |
40 | | - for node in connected_area.find_children("*", "CollisionShape2D"): |
41 | | - (node as CollisionShape2D).disabled = !is_active |
42 | | - for node in connected_area.find_children("*", "CollisionPolygon2D"): |
43 | | - (node as CollisionPolygon2D).disabled = !is_active |
44 | | - |
45 | | - |
46 | | -func _ready() -> void: |
47 | | - if not Engine.is_editor_hint(): |
48 | | - FieldEvents.input_paused.connect(_on_input_paused) |
49 | | - |
50 | | - |
51 | | -# Ensure that something is connected to _on_area_entered, which the Trigger requires. |
52 | | -# If nothing is connected, issue a configuration warning. |
53 | | -func _get_configuration_warnings() -> PackedStringArray: |
54 | | - var warnings: PackedStringArray = [] |
55 | | - var has_area_entered_bindings: = false |
56 | | - |
57 | | - for data in get_incoming_connections(): |
58 | | - if data["callable"] == _on_area_entered: |
59 | | - has_area_entered_bindings = true |
60 | | - |
61 | | - if not has_area_entered_bindings: |
62 | | - warnings.append("This object does not have a CollisionObject2D's signals connected to " + |
63 | | - "this Trigger's _on_area_entered method. The Trigger will never be triggered!") |
64 | | - return warnings |
65 | | - |
66 | | - |
67 | | -# Pause any collision objects that would normally send signals regarding interactions. |
68 | | -# This will automatically accept or ignore currently overlapping areas. |
69 | | -func _on_input_paused(is_paused: bool) -> void: |
70 | | - for data in get_incoming_connections(): |
71 | | - # Note that we only want to check _on_area_entered, since _on_area_exited will clean up any |
72 | | - # lingering references once the Area2Ds are 'shut off' (i.e. not monitoring/monitorable). |
73 | | - if data["callable"] == _on_area_entered: |
74 | | - var connected_area: = data["signal"].get_object() as Area2D |
75 | | - if connected_area: |
76 | | - connected_area.monitoring = !is_paused |
77 | | - connected_area.monitorable = !is_paused |
78 | | - |
79 | | - |
80 | | -# Register the colliding gamepiece and wait for it to finish moving before running the trigger. |
81 | | -func _on_area_entered(area: Area2D) -> void: |
82 | | - var gamepiece: = area.owner as Gamepiece |
83 | | - |
84 | | - # Check to make sure that the gamepiece is moving before connecting to its 'arriving' |
85 | | - # signal. This catches edge cases where the Trigger is unpaused while a colliding object |
86 | | - # is standing on top of it (which would mean that _on_gamepiece_arriving would trigger once |
87 | | - # the gamepiece moves OFF of it. Which is bad.). |
88 | | - if gamepiece and gamepiece.is_moving(): |
89 | | - gamepiece_entered.emit(gamepiece) |
90 | | - gamepiece.arriving.connect(_on_gamepiece_arriving.bind(gamepiece), CONNECT_ONE_SHOT) |
91 | | - |
92 | | - # Triggers need to block input early. Otherwise, if waiting until the gamepiece is arriving, |
93 | | - # there's a chance that the player's controller may have received the gamepiece.arriving |
94 | | - # signal first and continue moving the gamepiece. |
95 | | - await get_tree().process_frame |
96 | | - _is_cutscene_in_progress = true |
97 | | - |
98 | | - |
99 | | -func _on_area_exited(area: Area2D) -> void: |
100 | | - var gamepiece: = area.owner as Gamepiece |
101 | | - if gamepiece: |
102 | | - gamepiece_exited.emit(gamepiece) |
103 | | - |
104 | | - |
105 | | -func _on_gamepiece_arriving(_distance: float, gamepiece: Gamepiece) -> void: |
106 | | - triggered.emit(gamepiece) |
107 | | - run() |
| 11 | +### Emitted when a [Gamepiece] begins moving to the cell occupied by the [code]Trigger[/code]. |
| 12 | +#signal gamepiece_entered(gamepiece: Gamepiece) |
| 13 | +# |
| 14 | +### Emitted when a [Gamepiece] begins moving away from the cell occupied by the [code]Trigger[/code]. |
| 15 | +#signal gamepiece_exited(gamepiece: Gamepiece) |
| 16 | +# |
| 17 | +### Emitted when a [Gamepiece] is finishing moving to the cell occupied by the [code]Trigger[/code]. |
| 18 | +#signal triggered(gamepiece: Gamepiece) |
| 19 | +# |
| 20 | +### An active [code]Trigger[/code] may be run, whereas one that is inactive may only be run |
| 21 | +### directly through code via the [method Cutscene.run] method. |
| 22 | +#@export var is_active: = true: |
| 23 | + #set(value): |
| 24 | + #is_active = value |
| 25 | + # |
| 26 | + #if not Engine.is_editor_hint(): |
| 27 | + #if not is_inside_tree(): |
| 28 | + #await ready |
| 29 | + # |
| 30 | + ## We use "Visible Collision Shapes" to debug positions on the gameboard, so we'll want |
| 31 | + ## to change the state of child collision shapes.These could be either CollisionShape2Ds |
| 32 | + ## or CollisionPolygon2Ds. |
| 33 | + ## Note that we only want to disable the collision shapes of objects that are actually |
| 34 | + ## connected to this Interaction. |
| 35 | + #for data in get_incoming_connections(): |
| 36 | + #var callable: = data["callable"] as Callable |
| 37 | + #if callable == _on_area_entered : |
| 38 | + #var connected_area: = data["signal"].get_object() as Area2D |
| 39 | + #if connected_area: |
| 40 | + #for node in connected_area.find_children("*", "CollisionShape2D"): |
| 41 | + #(node as CollisionShape2D).disabled = !is_active |
| 42 | + #for node in connected_area.find_children("*", "CollisionPolygon2D"): |
| 43 | + #(node as CollisionPolygon2D).disabled = !is_active |
| 44 | +# |
| 45 | +# |
| 46 | +#func _ready() -> void: |
| 47 | + #if not Engine.is_editor_hint(): |
| 48 | + #FieldEvents.input_paused.connect(_on_input_paused) |
| 49 | +# |
| 50 | +# |
| 51 | +## Ensure that something is connected to _on_area_entered, which the Trigger requires. |
| 52 | +## If nothing is connected, issue a configuration warning. |
| 53 | +#func _get_configuration_warnings() -> PackedStringArray: |
| 54 | + #var warnings: PackedStringArray = [] |
| 55 | + #var has_area_entered_bindings: = false |
| 56 | + # |
| 57 | + #for data in get_incoming_connections(): |
| 58 | + #if data["callable"] == _on_area_entered: |
| 59 | + #has_area_entered_bindings = true |
| 60 | + # |
| 61 | + #if not has_area_entered_bindings: |
| 62 | + #warnings.append("This object does not have a CollisionObject2D's signals connected to " + |
| 63 | + #"this Trigger's _on_area_entered method. The Trigger will never be triggered!") |
| 64 | + #return warnings |
| 65 | +# |
| 66 | +# |
| 67 | +## Pause any collision objects that would normally send signals regarding interactions. |
| 68 | +## This will automatically accept or ignore currently overlapping areas. |
| 69 | +#func _on_input_paused(is_paused: bool) -> void: |
| 70 | + #for data in get_incoming_connections(): |
| 71 | + ## Note that we only want to check _on_area_entered, since _on_area_exited will clean up any |
| 72 | + ## lingering references once the Area2Ds are 'shut off' (i.e. not monitoring/monitorable). |
| 73 | + #if data["callable"] == _on_area_entered: |
| 74 | + #var connected_area: = data["signal"].get_object() as Area2D |
| 75 | + #if connected_area: |
| 76 | + #connected_area.monitoring = !is_paused |
| 77 | + #connected_area.monitorable = !is_paused |
| 78 | +# |
| 79 | +# |
| 80 | +## Register the colliding gamepiece and wait for it to finish moving before running the trigger. |
| 81 | +#func _on_area_entered(area: Area2D) -> void: |
| 82 | + #var gamepiece: = area.owner as Gamepiece |
| 83 | + # |
| 84 | + ## Check to make sure that the gamepiece is moving before connecting to its 'arriving' |
| 85 | + ## signal. This catches edge cases where the Trigger is unpaused while a colliding object |
| 86 | + ## is standing on top of it (which would mean that _on_gamepiece_arriving would trigger once |
| 87 | + ## the gamepiece moves OFF of it. Which is bad.). |
| 88 | + #if gamepiece and gamepiece.is_moving(): |
| 89 | + #gamepiece_entered.emit(gamepiece) |
| 90 | + #gamepiece.arriving.connect(_on_gamepiece_arriving.bind(gamepiece), CONNECT_ONE_SHOT) |
| 91 | + # |
| 92 | + ## Triggers need to block input early. Otherwise, if waiting until the gamepiece is arriving, |
| 93 | + ## there's a chance that the player's controller may have received the gamepiece.arriving |
| 94 | + ## signal first and continue moving the gamepiece. |
| 95 | + #await get_tree().process_frame |
| 96 | + #_is_cutscene_in_progress = true |
| 97 | +# |
| 98 | +# |
| 99 | +#func _on_area_exited(area: Area2D) -> void: |
| 100 | + #var gamepiece: = area.owner as Gamepiece |
| 101 | + #if gamepiece: |
| 102 | + #gamepiece_exited.emit(gamepiece) |
| 103 | +# |
| 104 | +# |
| 105 | +#func _on_gamepiece_arriving(_distance: float, gamepiece: Gamepiece) -> void: |
| 106 | + #triggered.emit(gamepiece) |
| 107 | + #run() |
0 commit comments