extends ProgressBar

var addAmount: float
@onready var Player = get_node("audioJungle");

func _ready() -> void:
	await get_tree().process_frame
	var crystals = get_tree().get_nodes_in_group("crystals")
	addAmount = 100.0 / crystals.size()
	print("Pulled " + str(crystals.size()) + " crystals from scene")
	
	for crystal in crystals:
		print("Connecting crystal: " + crystal.name)
		crystal.connect("crystal_picked_up", Callable(self, "collect"))

func collect():
	print("Collected crystal")
	value += addAmount
	if value != 100.0:
		Player.stream = load("res://collectables/crystal/sounds/Picked Coin Echo.wav")
	else:
		Player.stream = load("res://collectables/crystal/sounds/Picked Coin Echo 2.wav")
	
	Player.play();
	
	if value > 99.0:
		value = 100.0

signal complete

func _on_value_changed(value: float) -> void:
	if value == 100.0:
		emit_signal("complete")
