extends Node2D

@export var speed = 300;
@export var spawn_points : Node2D
var target_pos := Vector2(0, 0)
@onready var marker : Sprite2D = $DangerRound
@onready var claw : Claw = $DangerRound/Claw

enum s { Follow, Smash, Spawn, Laser}
var state = s.Follow

var max_follow_time = 2;
var following = max_follow_time;

var timer = 0;

func _ready() -> void:
	target_pos = position

func _physics_process(delta: float) -> void:
	match(state):
		s.Follow:
			follow(delta)
		s.Smash:
			pass

func spawn_minions(times : int):
	var points : Array[Node]= spawn_points.get_children()
	points.shuffle()
	for i in range(times):
		var p = points[i].global_position
		await create_tween().tween_property(marker, "global_position", p, 1).finished
		await claw.pickup()
	state = s.Follow

func smash():
	await claw.smash();
	if randf() < 0.7:
		state = s.Follow
	else:
		state = s.Spawn
		spawn_minions(3)

func follow(delta : float):
	var player : Player = get_tree().get_first_node_in_group("player")
	if not player: return
	move_to(player.global_position, delta)
	following -= delta
	if following < 0:
		state = s.Smash
		smash()
		following = max_follow_time;

func move_to(pos : Vector2, delta : float):
	marker.global_position = marker.global_position.move_toward(pos, delta * speed)
