extends Node3D

const enemyPrefab = preload("res://Scenes/EnemyType2.tscn")
@export var respawnDelay = 30
var player
@onready var currentPlayer = $"../wholePlayer/Player"
@onready var otherBody = $"../wholePlayer/OtherBody"

@onready var spawnlocation = $SpawnLocation
var enemy
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	spawnEnemy()
	player = otherBody


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("Swap"):
		switchTarget()
		print("switch target to ", player.name)


func _on_timer_timeout() -> void:
	print("Enemy Spawned, target is ", player.name)
	spawnEnemy()

func spawnEnemy() -> void:
	print(enemyPrefab)
	enemy = enemyPrefab.instantiate()
	#enemyPrefab.instantiate()
	$"..".add_child.call_deferred(enemy)
	enemy.position = spawnlocation.global_position
	enemy.setFirstTarget(player)
	$Timer.start(respawnDelay)

func switchTarget():
	if player == currentPlayer:
		player = otherBody
	else:
		player = currentPlayer
