extends Node3D
## Game mechanic testing level

@onready var gunk_hall: GunkBody = %GunkHallBody

@onready var bulkhead: Node3D = $Bulkhead
@onready var open_switch: Node3D = %OpenSwitch
@onready var close_switch: Node3D = %CloseSwitch

@onready var nodule_spawn_point: Marker3D = %NoduleSpawnPoint
@onready var alarm_spawn_point: Marker3D = %AlarmSpawnPoint
@onready var signal_test_spawn_point: Marker3D = %SignalTestSpawnPoint
@onready var prop_test_spawn_point: Marker3D = %PropTestSpawnPoint
@onready var item_test_spawn_point: Marker3D = %ItemTestSpawnPoint

static var nodule_scene: PackedScene = load("res://src/world/gunk_node/grunk_nodule.tscn")
static var alarm_scene: PackedScene = load("res://src/world/mechanics/alarm/gunk_alarm.tscn")
static var signal_test_scene: PackedScene = load("res://levels/mechanic_test/signal_test.tscn")
static var prop_test_scene: PackedScene = load("res://levels/mechanic_test/prop_test.tscn")
static var item_test_scene: PackedScene = load("res://levels/mechanic_test/item_test.tscn")


func reset() -> void:
	print("Resetting level!")
	gunk_hall.mask_control.clear(Color.BLACK)
	Callable(bulkhead, "close").call()
	Callable(open_switch, "enable").call()
	Callable(close_switch, "disable").call()
	_do_spawn(signal_test_spawn_point, signal_test_scene)
	_do_spawn(prop_test_spawn_point, prop_test_scene)
	_do_spawn(item_test_spawn_point, item_test_scene)


func _do_spawn(spawn_point: Node3D, scene: PackedScene) -> void:
	for c: Node in spawn_point.get_children():
		c.queue_free()
	var instance := scene.instantiate()
	spawn_point.add_child(instance)


func spawn_nodule() -> void:
	_do_spawn(nodule_spawn_point, nodule_scene)


func spawn_alarm() -> void:
	_do_spawn(alarm_spawn_point, alarm_scene)


func trigger_spawned_alarm() -> void:
	var alarm := alarm_spawn_point.get_child(0) as GunkAlarm
	if alarm:
		alarm.trigger()
