diff --git a/behaviours/priority.gd b/behaviours/priority.gd index 9a4f924..cda9e94 100644 --- a/behaviours/priority.gd +++ b/behaviours/priority.gd @@ -11,7 +11,6 @@ signal priority_changed(new: int, old: int) @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: @@ -60,7 +59,3 @@ func log_priority() -> void: 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_priority.gd b/behaviours/sampler_priority.gd index 5221fa3..5ea3e95 100644 --- a/behaviours/sampler_priority.gd +++ b/behaviours/sampler_priority.gd @@ -2,18 +2,19 @@ extends Sampler 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) +## If true, the [SamplerPriority] will attempt to automatically setup the recommended signals for all the [field possibilities] on NOTIFICATION_READY. +@export var autosetup_signals_on_ready: bool = true ## Update [field possibilities] with the most likely subset of nodes. -func autodetect_possibilities(): +func autodetect_possibilities() -> void: possibilities = find_children("*", "Priority", true, false) +## Setup the recommended signals for each node in [field possibilities]. +func autosetup_signals() -> void: + for possibility in possibilities: + possibility.priority_changed.connect(_autosetup_on_possibility_priority_changed.bind(possibility)) + ## Get a reference. func sample() -> Priority: if len(possibilities) == 0: @@ -27,21 +28,9 @@ func sample() -> Priority: if highest_possibility == null: return null - - 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: @@ -50,3 +39,17 @@ func get_all_refs() -> Array[Node]: func get_ref(node: Node) -> Node: return node.get_ref() + + +func _ready(): + if autosetup_signals_on_ready: + autosetup_signals() + +func _autosetup_on_possibility_priority_changed(node: Priority, new: int, old: int): + if node == selected: + if new < old: + sample_and_enable() + else: + # A nice optimization! + if selected == null or new > selected.priority: + set_enabled(node) diff --git a/entities/imp.tscn b/entities/imp.tscn index 0f5a120..d761f73 100644 --- a/entities/imp.tscn +++ b/entities/imp.tscn @@ -57,8 +57,7 @@ deviation = 4.0 min_secs = 1.0 max_secs = 9.0 -[node name="MovementSampler" parent="." node_paths=PackedStringArray("possibilities") instance=ExtResource("7_ech8d")] -possibilities = [NodePath("../MovementWander/WanderPriority"), NodePath("../MovementHunt/HuntPriority"), NodePath("../MovementDrag/DragPriority")] +[node name="MovementSampler" parent="." instance=ExtResource("7_ech8d")] [node name="MovementWander" parent="." instance=ExtResource("8_ffcw0")] speed = 25.0 diff --git a/entities/sheep.tscn b/entities/sheep.tscn index 777129e..c0cb53c 100644 --- a/entities/sheep.tscn +++ b/entities/sheep.tscn @@ -107,8 +107,7 @@ libraries = { [node name="Edible" parent="." instance=ExtResource("6_3odsh")] diet = &"Meat" -[node name="MovementSampler" parent="." node_paths=PackedStringArray("possibilities") instance=ExtResource("9_s5lod")] -possibilities = [NodePath("../MovementIdle/IdlePriority"), NodePath("../MovementWander/WanderPriority"), NodePath("../MovementRunFromMouse/RunFromMousePriority"), NodePath("../MovementRunFromHunter/RunFromHunterPriority"), NodePath("../MovementDrag/DragPriority")] +[node name="MovementSampler" parent="." instance=ExtResource("9_s5lod")] [node name="MovementIdle" parent="." instance=ExtResource("10_05kcd")] speed = 0.0