extends RigidBody2D

var phase2_active = false
var health = 10
var spawn_remaining = 7
@onready var player = $"../Player"

func hit(body):
	if not phase2_active:
		return
	
	if body.is_in_group("destructible"):
		if body.has_method("destroy"):
			body.destroy()
		else:
			body.queue_free()
		health -= 1
	if body.name == "Border":
		health -= 1
		
	if health <= 0 and not is_queued_for_deletion():
		# do the death stuff
		queue_free()

func _ready():
	await get_tree().create_timer(1).timeout
	phase()
	
func _physics_process(delta):
	if phase2_active:
		#print((global_position - player.global_position).normalized())
		apply_central_force((global_position - (player.global_position + player.linear_velocity * 1)).normalized() * -1600 * (player.speed / 1000.0))

func phase():
	if phase2_active:
		return
	if spawn_remaining <= 0:
		phase2()
		return
	
	spawn_remaining -= 1
	$Sprite.play("spawn")
	$"Riangle Zone".spawn()
	await get_tree().create_timer(1).timeout
	$Sprite.play("idle")
	$"Riangle Zone".eject()

func phase2():
	$Sprite.play("transform")
	await $Sprite.animation_finished
	$Sprite.play("phase2")
	phase2_active = true
	freeze = false
