extends Node2D
class_name BeeSpawner

var bee : Resource = preload("res://entities/Bee.tscn")

@onready var beehive : Beehive = get_node("../Beehive")
@onready var bee_sound : AudioStreamPlayer = get_node("BigBeeSound")
@onready var small_bee_sound : AudioStreamPlayer = get_node("BeeSound")

var bee_count : int = 0 
var max_bees : int = 100
var spawn_interval : float = 0.5
var spawn_timer : float = 0.0
var bee_sound_timer : float = 0.0

func spawn_bee() -> void:
	var bee_instance : Bee = bee.instantiate()
	add_child(bee_instance)
	bee_instance.position = beehive.global_position

func _process(delta : float) -> void:
	spawn_timer += delta
	
	if spawn_timer > spawn_interval and bee_count < max_bees and beehive.dancer_in_range:
		spawn_bee()
		spawn_timer = 0.0
		bee_count += 1
		Log.pr("Bee count: " + str(bee_count))	

	if bee_count > 0 and small_bee_sound.playing == false:
		small_bee_sound.play()

func _physics_process(delta : float) -> void:
	bee_sound_timer += delta
	if bee_sound_timer > 1.0 and bee_count > 0:
		bee_sound_timer = 0.0
		if randf() > 0.9 and bee_sound.playing == false:
			bee_sound.play()
