class_name ResourceAmountVisual extends Node3D enum AmountTier { FULL, LOW, DEPLETED, } @export_range(0.0, 1.0, 0.05) var low_amount_ratio := 0.5 var resource_node: ResourceNode var amount_ratio := 1.0 var amount_tier := AmountTier.FULL func _ready() -> void: call_deferred("_bind_resource_node") func _exit_tree() -> void: if ( resource_node != null and resource_node.amount_changed.is_connected(_on_resource_amount_changed) ): resource_node.amount_changed.disconnect(_on_resource_amount_changed) resource_node = null func refresh_from_resource() -> void: if resource_node == null: return _apply_amount(resource_node.get_amount_remaining()) func get_amount_tier() -> AmountTier: return amount_tier func get_amount_ratio() -> float: return amount_ratio func _bind_resource_node() -> void: resource_node = _find_resource_node() if resource_node == null: return if not resource_node.amount_changed.is_connected(_on_resource_amount_changed): resource_node.amount_changed.connect(_on_resource_amount_changed) refresh_from_resource() func _find_resource_node() -> ResourceNode: var candidate := get_parent() while candidate != null: if candidate is ResourceNode: return candidate as ResourceNode candidate = candidate.get_parent() return null func _on_resource_amount_changed(changed_id: StringName, amount_remaining: float) -> void: if resource_node != null and changed_id == resource_node.node_id: _apply_amount(amount_remaining) func _apply_amount(amount_remaining: float) -> void: var initial_amount := maxf(resource_node.initial_amount, 0.0) amount_ratio = ( clampf(amount_remaining / initial_amount, 0.0, 1.0) if initial_amount > 0.0 else 0.0 ) if amount_remaining <= 0.0: amount_tier = AmountTier.DEPLETED elif amount_ratio <= low_amount_ratio: amount_tier = AmountTier.LOW else: amount_tier = AmountTier.FULL _apply_resource_amount_visual(amount_remaining, amount_ratio, amount_tier) func _apply_resource_amount_visual( _amount_remaining: float, _remaining_ratio: float, _tier: AmountTier ) -> void: pass