1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-21 23:54:23 +00:00

Remove priority backref

This commit is contained in:
Lorenzo Rossi 2024-04-23 00:13:54 +02:00
parent c5d40c0e85
commit 5a28feb2ca
5 changed files with 39 additions and 21 deletions

View file

@ -9,12 +9,9 @@ class_name Priority
## Emitted when the priority is changed. ## Emitted when the priority is changed.
signal priority_changed(new: int, old: int) 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 default_priority: int = 0
@export var alternative_priority: int = 1 @export var alternative_priority: int = 1
@export var setup_signals_on_ready: bool = true
var priority: int = 0: var priority: int = 0:
get: get:
@ -61,9 +58,9 @@ func get_ref() -> Node:
func log_priority() -> void: func log_priority() -> void:
Log.p(self, "Priority: %d" % priority) Log.p(self, "Priority: %d" % priority)
func _on_priority_changed(_new: int, _old: int) -> void:
priority_changed_no_args.emit()
func _ready() -> void: func _ready() -> void:
priority = default_priority priority = default_priority
func link(parent: SamplerPriority) -> void:
if setup_signals_on_ready:
priority_changed.connect(func(_old, _new): parent.on_priority_changed(self))

View file

@ -9,6 +9,8 @@ class_name Sampler
## [Array] of [Node]s that can be [func sample]d by this [Sampler]. ## [Array] of [Node]s that can be [func sample]d by this [Sampler].
@export var possibilities: Array[Node] = [] @export var possibilities: Array[Node] = []
var selected: Node = null;
## Get a reference. ## Get a reference.
func sample() -> Node: func sample() -> Node:
@ -16,11 +18,19 @@ func sample() -> Node:
return null return null
## Set the [field enabled] property to true on a [method sample]d node, and to false on all others. ## Set the [field enabled] property to true on a [method sample]d node, and to false on all others.
func enable() -> void: func sample_and_enable() -> void:
var selected = sample() set_enabled(sample())
for possibility in get_all_refs():
possibility.enabled = (selected == possibility)
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]. ## Get all possible nodes referenced by [field possibilities].
## ##

View file

@ -3,7 +3,11 @@ class_name SamplerPriority
## Always sample the object with the highest priority in the queue. ## 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. ## Get a reference.
func sample() -> Priority: func sample() -> Priority:
@ -19,10 +23,25 @@ func sample() -> Priority:
if highest_possibility == null: if highest_possibility == null:
return 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]: func get_all_refs() -> Array[Node]:
var refs: Array[Node] = [] var refs: Array[Node] = []
for possibility in possibilities: for possibility in possibilities:
refs.append(possibility.get_ref()) refs.append(possibility.get_ref())
return refs return refs
func get_ref(node: Node) -> Node:
return node.get_ref()

View file

@ -124,11 +124,9 @@ stream = ExtResource("19_7la6c")
[connection signal="ready" from="MovementSampler" to="MovementSampler" method="enable"] [connection signal="ready" from="MovementSampler" to="MovementSampler" method="enable"]
[connection signal="changed_direction" from="MovementWander" to="MovementWander/WanderPriority" method="priority_conditional"] [connection signal="changed_direction" from="MovementWander" to="MovementWander/WanderPriority" method="priority_conditional"]
[connection signal="move" from="MovementWander" to="." method="_on_move"] [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="timeout" from="MovementWander/ChangeDirectionTimer" to="MovementWander" method="randomize_direction"]
[connection signal="changed_target" from="MovementHunt" to="MovementHunt/HuntPriority" method="priority_conditional"] [connection signal="changed_target" from="MovementHunt" to="MovementHunt/HuntPriority" method="priority_conditional"]
[connection signal="move" from="MovementHunt" to="." method="_on_move"] [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="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="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] [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/DragPriority" method="priority_default"]
[connection signal="fallen" from="MovementDrag" to="MovementDrag/FallSound" method="play"] [connection signal="fallen" from="MovementDrag" to="MovementDrag/FallSound" method="play"]
[connection signal="move" from="MovementDrag" to="." method="_on_move"] [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="dragged" from="MovementDrag/Draggable" to="MovementDrag" method="drag"]
[connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="drop"] [connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="drop"]
[connection signal="eaten" from="Eater" to="." method="_on_eater_eaten"] [connection signal="eaten" from="Eater" to="." method="_on_eater_eaten"]

View file

@ -188,24 +188,20 @@ stream = ExtResource("17_8kst2")
[connection signal="move" from="MovementIdle" to="." method="_on_move"] [connection signal="move" from="MovementIdle" to="." method="_on_move"]
[connection signal="move_disabled" from="MovementIdle" to="MovementIdle/BoredTimer" method="stop"] [connection signal="move_disabled" from="MovementIdle" to="MovementIdle/BoredTimer" method="stop"]
[connection signal="move_enabled" from="MovementIdle" to="MovementIdle/BoredTimer" method="start"] [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="timeout" from="MovementIdle/BoredTimer" to="MovementWander" method="randomize_direction"]
[connection signal="changed_direction" from="MovementWander" to="MovementWander/WanderPriority" method="priority_conditional"] [connection signal="changed_direction" from="MovementWander" to="MovementWander/WanderPriority" method="priority_conditional"]
[connection signal="move" from="MovementWander" to="." method="_on_move"] [connection signal="move" from="MovementWander" to="." method="_on_move"]
[connection signal="move_disabled" from="MovementWander" to="MovementWander/TiredTimer" method="stop"] [connection signal="move_disabled" from="MovementWander" to="MovementWander/TiredTimer" method="stop"]
[connection signal="move_enabled" from="MovementWander" to="MovementWander/TiredTimer" method="start"] [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="timeout" from="MovementWander/TiredTimer" to="MovementWander" method="clear_direction"]
[connection signal="changed_target" from="MovementRunFromMouse" to="MovementRunFromMouse/RunFromMousePriority" method="priority_conditional"] [connection signal="changed_target" from="MovementRunFromMouse" to="MovementRunFromMouse/RunFromMousePriority" method="priority_conditional"]
[connection signal="move" from="MovementRunFromMouse" to="." method="_on_move"] [connection signal="move" from="MovementRunFromMouse" to="." method="_on_move"]
[connection signal="move_enabled" from="MovementRunFromMouse" to="MovementWander" method="clear_direction"] [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_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="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="changed_target" from="MovementRunFromHunter" to="MovementRunFromHunter/RunFromHunterPriority" method="priority_conditional"]
[connection signal="move" from="MovementRunFromHunter" to="." method="_on_move"] [connection signal="move" from="MovementRunFromHunter" to="." method="_on_move"]
[connection signal="move_disabled" from="MovementRunFromHunter" to="MovementWander" method="clear_direction"] [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="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="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] [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="fallen" from="MovementDrag" to="MovementDrag/FallSound" method="play"]
[connection signal="move" from="MovementDrag" to="." method="_on_move"] [connection signal="move" from="MovementDrag" to="." method="_on_move"]
[connection signal="move_enabled" from="MovementDrag" to="MovementWander" method="clear_direction"] [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="dragged" from="MovementDrag/Draggable" to="MovementDrag" method="drag"]
[connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="drop"] [connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="drop"]