class_name MeetSpookSpawner extends Marker3D
## Spawner which is part of a MeetSpook event

## Time to freeze the mob before moving after being spotted, in seconds.
@export var freeze_time := 0.4

## Time after which the mob will despawn after being spotted, in seconds.
@export var mob_lifespan := 1.0

var _instance: MeetSpookMob


func get_target() -> Marker3D:
	for c: Node in get_children():
		if c is Marker3D:
			return c
	return null


func spawn(instance: MeetSpookMob) -> void:
	if is_instance_valid(_instance):
		_instance.queue_free()
	_instance = instance
	_instance.target = get_target()
	_instance.freeze_time = freeze_time
	_instance.lifespan = mob_lifespan
	add_child(_instance)
	_instance.global_transform = global_transform


func resolve() -> void:
	## Despawn if instance is not active
	if is_instance_valid(_instance) and not _instance.is_active():
		_instance.queue_free()
