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

Move the autosetup code up to SamplerPriority

This commit is contained in:
Steffo 2024-04-23 09:28:45 +02:00
parent 73c4cce99c
commit 1abd1c1ec3
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
4 changed files with 24 additions and 28 deletions

View file

@ -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))

View file

@ -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:
@ -28,20 +29,8 @@ 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)

View file

@ -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

View file

@ -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