class_name Fireball
extends Node2D

var _direction = Vector2(0, 0)
var _damage
var already_hit : Array = []
var _speed
var started : bool = false

func start(direction : Vector2, damage : int = 1, speed : float = 5):
	_direction = direction.normalized()
	_damage = damage
	_speed = speed
	AudioManager.create_audio(SoundEffect.SOUND_EFFECT_TYPE.FIREBALL)
	started = true

func _process(delta: float) -> void:
	if Globals.paused or not started:
		return
	global_position += _direction * _speed * delta

func _on_area_2d_body_entered(body: Node2D) -> void:
	if not already_hit.has(body):
		already_hit.append(body)
	else:
		return
	
	if body is GameAgent:
		body.take_damage(_damage)
		body.knockback(_direction)
		queue_free()
	elif body is TileMapLayer:
		queue_free()


func _on_area_2d_area_entered(area: Area2D) -> void:
	if not already_hit.has(area):
		already_hit.append(area)
	else:
		return
	if area.name == "Hitbox":
		var body = area.get_parent()
		if body is GameAgent:
			body.take_damage(_damage)
			body.knockback(_direction)
			queue_free()
		elif body is TileMapLayer:
			queue_free()
		
