class_name World extends Node
## Access and flow control for the game world.

@export var initial_level: PackedScene

@export var pause_scene: PackedScene

@onready var level_root: Node3D = %LevelRoot
@onready var ui_root: Control = %UIRoot

static var instance: World


func _ready() -> void:
	World.instance = self
	load_level(initial_level)


func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("pause"):
		pause()


func pause() -> void:
	get_tree().paused = true
	var pause_menu: Control = pause_scene.instantiate()
	ui_root.add_child(pause_menu)
	pause_menu.tree_exiting.connect(unpause)


func unpause() -> void:
	get_tree().paused = false


func load_level(level: PackedScene) -> void:
	for c: Node in level_root.get_children():
		c.queue_free()
	level_root.add_child(level.instantiate())
