diff --git a/behaviours/priority.gd b/behaviours/priority.gd index 54509b1..9a4f924 100644 --- a/behaviours/priority.gd +++ b/behaviours/priority.gd @@ -9,12 +9,9 @@ class_name Priority ## Emitted when the priority is changed. signal priority_changed(new: int, old: int) -## Emitted when the priority is changed. No args are provided to work around a Godot bug. -signal priority_changed_no_args - - @export var default_priority: int = 0 @export var alternative_priority: int = 1 +@export var setup_signals_on_ready: bool = true var priority: int = 0: get: @@ -61,9 +58,9 @@ func get_ref() -> Node: func log_priority() -> void: Log.p(self, "Priority: %d" % priority) - -func _on_priority_changed(_new: int, _old: int) -> void: - priority_changed_no_args.emit() - func _ready() -> void: priority = default_priority + +func link(parent: SamplerPriority) -> void: + if setup_signals_on_ready: + priority_changed.connect(func(_old, _new): parent.on_priority_changed(self)) diff --git a/behaviours/sampler.gd b/behaviours/sampler.gd index 00b87eb..bf26ace 100644 --- a/behaviours/sampler.gd +++ b/behaviours/sampler.gd @@ -9,6 +9,8 @@ class_name Sampler ## [Array] of [Node]s that can be [func sample]d by this [Sampler]. @export var possibilities: Array[Node] = [] +var selected: Node = null; + ## Get a reference. func sample() -> Node: @@ -16,11 +18,19 @@ func sample() -> Node: return null ## Set the [field enabled] property to true on a [method sample]d node, and to false on all others. -func enable() -> void: - var selected = sample() - for possibility in get_all_refs(): - possibility.enabled = (selected == possibility) +func sample_and_enable() -> void: + set_enabled(sample()) +func set_enabled(node: Node) -> void: + if node == selected: + return + selected = node + var selected_ref = get_ref(selected) + for possibility in get_all_refs(): + possibility.enabled = (selected_ref == possibility) + +func get_ref(node: Node) -> Node: + return node ## Get all possible nodes referenced by [field possibilities]. ## diff --git a/behaviours/sampler_priority.gd b/behaviours/sampler_priority.gd index c925afb..b5a9487 100644 --- a/behaviours/sampler_priority.gd +++ b/behaviours/sampler_priority.gd @@ -3,7 +3,11 @@ class_name SamplerPriority ## Always sample the object with the highest priority in the queue. +var selected_priority = 0; +func _ready(): + for possibility in possibilities: + possibility.link(self) ## Get a reference. func sample() -> Priority: @@ -19,10 +23,25 @@ func sample() -> Priority: if highest_possibility == null: return null - return highest_possibility.get_ref() + selected_priority = highest_possibility.priority + + return highest_possibility + +func on_priority_changed(node: Node): + if node == selected: + if node.priority > selected_priority: + selected_priority = node.priority + else: + sample_and_enable() + else: + if selected == null or node.priority > selected.priority: + set_enabled(node) func get_all_refs() -> Array[Node]: var refs: Array[Node] = [] for possibility in possibilities: refs.append(possibility.get_ref()) return refs + +func get_ref(node: Node) -> Node: + return node.get_ref() \ No newline at end of file diff --git a/entities/imp.tscn b/entities/imp.tscn index e58fc3e..0f5a120 100644 --- a/entities/imp.tscn +++ b/entities/imp.tscn @@ -124,11 +124,9 @@ stream = ExtResource("19_7la6c") [connection signal="ready" from="MovementSampler" to="MovementSampler" method="enable"] [connection signal="changed_direction" from="MovementWander" to="MovementWander/WanderPriority" method="priority_conditional"] [connection signal="move" from="MovementWander" to="." method="_on_move"] -[connection signal="priority_changed_no_args" from="MovementWander/WanderPriority" to="MovementSampler" method="enable"] [connection signal="timeout" from="MovementWander/ChangeDirectionTimer" to="MovementWander" method="randomize_direction"] [connection signal="changed_target" from="MovementHunt" to="MovementHunt/HuntPriority" method="priority_conditional"] [connection signal="move" from="MovementHunt" to="." method="_on_move"] -[connection signal="priority_changed_no_args" from="MovementHunt/HuntPriority" to="MovementSampler" method="enable"] [connection signal="tracked" from="MovementHunt/TrackerMeat" to="MovementHunt/TrackerMeat/TargetPicker" method="sample_target_if_null" unbinds=1] [connection signal="untracked" from="MovementHunt/TrackerMeat" to="MovementHunt/TrackerMeat/TargetPicker" method="clear_if_target"] [connection signal="target_changed" from="MovementHunt/TrackerMeat/TargetPicker" to="MovementHunt" method="set_target" unbinds=1] @@ -140,7 +138,6 @@ stream = ExtResource("19_7la6c") [connection signal="fallen" from="MovementDrag" to="MovementDrag/DragPriority" method="priority_default"] [connection signal="fallen" from="MovementDrag" to="MovementDrag/FallSound" method="play"] [connection signal="move" from="MovementDrag" to="." method="_on_move"] -[connection signal="priority_changed_no_args" from="MovementDrag/DragPriority" to="MovementSampler" method="enable"] [connection signal="dragged" from="MovementDrag/Draggable" to="MovementDrag" method="drag"] [connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="drop"] [connection signal="eaten" from="Eater" to="." method="_on_eater_eaten"] diff --git a/entities/sheep.tscn b/entities/sheep.tscn index b07e995..777129e 100644 --- a/entities/sheep.tscn +++ b/entities/sheep.tscn @@ -188,24 +188,20 @@ stream = ExtResource("17_8kst2") [connection signal="move" from="MovementIdle" to="." method="_on_move"] [connection signal="move_disabled" from="MovementIdle" to="MovementIdle/BoredTimer" method="stop"] [connection signal="move_enabled" from="MovementIdle" to="MovementIdle/BoredTimer" method="start"] -[connection signal="priority_changed_no_args" from="MovementIdle/IdlePriority" to="MovementSampler" method="enable"] [connection signal="timeout" from="MovementIdle/BoredTimer" to="MovementWander" method="randomize_direction"] [connection signal="changed_direction" from="MovementWander" to="MovementWander/WanderPriority" method="priority_conditional"] [connection signal="move" from="MovementWander" to="." method="_on_move"] [connection signal="move_disabled" from="MovementWander" to="MovementWander/TiredTimer" method="stop"] [connection signal="move_enabled" from="MovementWander" to="MovementWander/TiredTimer" method="start"] -[connection signal="priority_changed_no_args" from="MovementWander/WanderPriority" to="MovementSampler" method="enable"] [connection signal="timeout" from="MovementWander/TiredTimer" to="MovementWander" method="clear_direction"] [connection signal="changed_target" from="MovementRunFromMouse" to="MovementRunFromMouse/RunFromMousePriority" method="priority_conditional"] [connection signal="move" from="MovementRunFromMouse" to="." method="_on_move"] [connection signal="move_enabled" from="MovementRunFromMouse" to="MovementWander" method="clear_direction"] -[connection signal="priority_changed_no_args" from="MovementRunFromMouse/RunFromMousePriority" to="MovementSampler" method="enable"] [connection signal="cursor_entered" from="MovementRunFromMouse/CursorSense" to="MovementRunFromMouse" method="set_target"] [connection signal="cursor_exited" from="MovementRunFromMouse/CursorSense" to="MovementRunFromMouse" method="clear_target" unbinds=1] [connection signal="changed_target" from="MovementRunFromHunter" to="MovementRunFromHunter/RunFromHunterPriority" method="priority_conditional"] [connection signal="move" from="MovementRunFromHunter" to="." method="_on_move"] [connection signal="move_disabled" from="MovementRunFromHunter" to="MovementWander" method="clear_direction"] -[connection signal="priority_changed_no_args" from="MovementRunFromHunter/RunFromHunterPriority" to="MovementSampler" method="enable"] [connection signal="tracked" from="MovementRunFromHunter/HunterSense" to="MovementRunFromHunter/HunterSense/TargetPicker" method="set_target_if_null"] [connection signal="untracked" from="MovementRunFromHunter/HunterSense" to="MovementRunFromHunter/HunterSense/TargetPicker" method="clear_if_target"] [connection signal="target_changed" from="MovementRunFromHunter/HunterSense/TargetPicker" to="MovementRunFromHunter" method="set_target" unbinds=1] @@ -217,6 +213,5 @@ stream = ExtResource("17_8kst2") [connection signal="fallen" from="MovementDrag" to="MovementDrag/FallSound" method="play"] [connection signal="move" from="MovementDrag" to="." method="_on_move"] [connection signal="move_enabled" from="MovementDrag" to="MovementWander" method="clear_direction"] -[connection signal="priority_changed_no_args" from="MovementDrag/DragPriority" to="MovementSampler" method="enable"] [connection signal="dragged" from="MovementDrag/Draggable" to="MovementDrag" method="drag"] [connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="drop"]