extends ProgressBar

var addAmount: float
@onready var Player = get_node("audioJungle");
var rng = RandomNumberGenerator.new()

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 > 99.0:
		value = 100.0
		
	if value != 100.0:
		Player.pitch_scale = rng.randf_range(0.5, 2.0)
		Player.stream = load("res://collectables/crystal/sounds/Picked Coin Echo.wav")
	else:
		Player.pitch_scale = 1.0;
		Player.stream = load("res://collectables/crystal/sounds/Picked Coin Echo 2.wav")
	
	Player.play();

signal complete

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