class_name 	EnemyBase
extends CharacterBody2D

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

var is_dying = false
var speed 
var witch
var player
var death_sound = preload("res://assets/music&sfx/sfx/hit2.wav")

func _ready() -> void:
	witch = get_node("/root/Game/Witch")
	player = get_node("/root/Game/Player")
	pass # Replace with function body
	
func _die():
	is_dying = true
	animated_sprite_2d.play("death")
	var player = AudioStreamPlayer.new()
	add_child(player)
	player.stream = death_sound
	player.bus = "SFX"
	player.play()
	await animated_sprite_2d.animation_finished
	queue_free()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass
	
func _chase_witch() -> void:
	var direction = Vector2(witch.global_position - global_position)
	velocity = direction * speed
	velocity = direction * speed
	
	if direction == Vector2.ZERO:
		animated_sprite_2d.play("idle")
	elif abs(direction.x) >= abs(direction.y):
		if direction.x < 0:
			animated_sprite_2d.play("walk_left")
		else:
			animated_sprite_2d.play("walk_right")
	else:
		if direction.y < 0:
			animated_sprite_2d.play("walk_up")
		else:
			animated_sprite_2d.play("walk_down")
	move_and_slide()
