extends CharacterBody2D

var camera
var bar_progress = 0
var is_casting = false
var fireball = preload("res://scenes/fireball.tscn")
var shuriken = preload("res://scenes/shuriken.tscn")

func _ready() -> void:
	camera = get_node("/root/Game/Camera2D")
	
func _physics_process(delta: float) -> void:
	# Add the gravity.
	pass

func _on_collect(DropsBase):
	if is_casting:
		return
	$CauldronBar.change_texture(bar_progress,3)
	bar_progress += 1
	if bar_progress == 3:
		bar_progress = 0
		is_casting = true
		await $CauldronBar.ignite_cauldrons()
		shoot_shuriken()
		camera.shake(0.3,0.8)
		$CauldronBar.reset_texture()
		is_casting = false
		
func shoot_fireballs():
	var enemies = get_tree().get_nodes_in_group("enemies")
	for enemy in enemies:
		var fb = fireball.instantiate()
		fb.global_position = global_position
		get_parent().add_child(fb)
		fb.launch(enemy.global_position)
		
func shoot_shuriken():
	var sh = shuriken.instantiate()
	sh.global_position = global_position
	get_parent().add_child(sh)
		
func get_nearest_enemy(from: Vector2) -> Node:
	var nearest = null
	var min_distance = INF
	for enemy in get_tree().get_nodes_in_group("enemies"):
		var dist = from.distance_to(enemy.global_position)
		if dist < min_distance:
			min_distance = dist
			nearest = enemy
	return nearest
