1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-27 18:44:23 +00:00
hella-farm/behaviours/sampler.gd

49 lines
1.2 KiB
GDScript3
Raw Normal View History

@icon("res://behaviours/sampler.svg")
2024-04-16 03:08:25 +00:00
extends Node
class_name Sampler
## Abstract base class for sampling a certain reference among multiple.
## [Array] of [Node]s that can be [func sample]d by this [Sampler].
@export var possibilities: Array[Node] = []
## If true, the [Sampler] will attempt to automatically [method sample_and_enable] on NOTIFICATION_READY.
@export var autoenable_on_ready: bool = true
2024-04-23 07:44:00 +00:00
var selected: Node = null
2024-04-22 22:13:54 +00:00
2024-04-16 03:08:25 +00:00
## Get a reference.
func sample() -> Node:
2024-04-16 03:08:25 +00:00
Log.e(self, "Not implemented.")
return null
## Set the [field enabled] property to true on a [method sample]d node, and to false on all others.
2024-04-22 22:13:54 +00:00
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():
2024-04-22 22:13:54 +00:00
possibility.enabled = (selected_ref == possibility)
2024-04-22 22:13:54 +00:00
func get_ref(node: Node) -> Node:
return node
## Get all possible nodes referenced by [field possibilities].
##
## Useful as it may be overridden by some other [Sampler]s, such as [SamplerPriority].
func get_all_refs() -> Array[Node]:
return possibilities
func _ready() -> void:
if autoenable_on_ready:
sample_and_enable.call_deferred()