extends CharacterBody2D

var camera
var is_casting = false
var fireball = preload("res://scenes/fireball.tscn")
var shuriken = preload("res://scenes/shuriken.tscn")
var fire_swirl = preload("res://scenes/fire_swirl.tscn")
var shuriken_count = 1

func _ready() -> void:
	$CauldronBar.witch = self
	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.progres_bar(DropsBase)
	
		
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)
		camera.shake(0.3,0.8)

func shoot_fire_swirl():
	var fs = fire_swirl.instantiate()
	fs.global_position = global_position
	get_parent().add_child(fs)
	camera.shake(0.3, 0.8)
		
func shoot_shuriken():
	for i in range(shuriken_count):
		var sh = shuriken.instantiate()
		sh.global_position = global_position
		get_parent().add_child(sh)
		await get_tree().create_timer(0.2).timeout
		
func get_nearest_enemy(from: Vector2, filter: Callable = Callable()) -> Node:
	var nearest = null
	var min_distance = INF
	for enemy in get_tree().get_nodes_in_group("enemies"):
		if filter.is_valid() and not filter.call(enemy):
			continue
		var dist = from.distance_to(enemy.global_position)
		if dist < min_distance:
			min_distance = dist
			nearest = enemy
	return nearest
