extends EnemyBase


var hits_remaining = 2
var is_hurt = false

func _ready() -> void:
	super()
	speed = 0.1
	animated_sprite_2d.sprite_frames = animated_sprite_2d.sprite_frames.duplicate()
	$Area2D.body_entered.connect(_on_area_2d_body_entered)

func _process(delta: float) -> void:
	if is_dying or is_hurt:
		return
	_chase_witch()

func die() -> void:
	hits_remaining -= 1
	if hits_remaining <= 0:
		super()
	else:
		_play_hurt()

func _play_hurt() -> void:
	is_hurt = true
	var hurt_anim: String
	if abs(last_direction.x) >= abs(last_direction.y):
		hurt_anim = "hurt_left" if last_direction.x < 0 else "hurt_right"
	else:
		hurt_anim = "hurt_up" if last_direction.y < 0 else "hurt_down"
	animated_sprite_2d.sprite_frames.set_animation_loop(hurt_anim, false)
	animated_sprite_2d.play(hurt_anim)
	await animated_sprite_2d.animation_finished
	is_hurt = false

func _on_area_2d_body_entered(body: Node2D) -> void:
	if is_dying or is_hurt:
		return
	if body == player:
		die()
