class_name RootUI extends CanvasLayer @export var startup_scene:PackedScene @export var game_scene:PackedScene @onready var ui_panels:Array = [ %MainMenuUI, %NewGameUI, %GameUI, %GameMenuUI ] @onready var active_ui_panel:Control = null @onready var inventory_dialog:InventoryDialog = %InventoryDialog @onready var scene = %Scene enum ROOT_UI_STATE { UNDEFINED, MENU, GAME } var _root_ui_state:ROOT_UI_STATE = ROOT_UI_STATE.UNDEFINED var _all_recipes:Array[Recipe] = [] var _player:Player = null var current_level_resource:String # Called when the node enters the scene tree for the first time. func _ready(): for file in DirAccess.get_files_at("res://data/recipes"): var resource_file = "res://data/recipes/" + file var recipe:Recipe = load(resource_file) as Recipe _all_recipes.append(recipe) set_root_ui_state(ROOT_UI_STATE.GAME) func activate_ui_panel(ui_panel:Control): for control in ui_panels: if control == ui_panel: control.visible = true else: control.visible = false active_ui_panel = ui_panel func disconnect_player_signals(): _player.disconnect("trigger_message", %GameUI._on_player_trigger_message) func update_player_signals(): _player.connect("trigger_message", %GameUI._on_player_trigger_message) func load_scene(scene_resource:PackedScene): if _player: disconnect_player_signals() for child in scene.get_children(): child.queue_free() child.get_parent().remove_child(child) print("Loading level '" + scene_resource.resource_path + "'") scene.add_child(scene_resource.instantiate()) current_level_resource = scene_resource.resource_path for pickup_item in get_tree().get_nodes_in_group("pickup_item"): pickup_item.connect("item_picked_up", get_parent().on_item_pickup) _player = scene.find_child("Player", true, false) if _player: update_player_signals() func set_root_ui_state(state:ROOT_UI_STATE): if _root_ui_state == state: return if state == ROOT_UI_STATE.GAME: load_scene(game_scene) %GameUI.game_scene = scene.get_child(0) activate_ui_panel(%GameUI) else: load_scene(startup_scene) %GameUI.game_scene = null %GameMenuUI.hide() activate_ui_panel(%MainMenuUI) _root_ui_state = state func _on_start_game_button_pressed(): set_root_ui_state(ROOT_UI_STATE.GAME) func _on_quit_button_pressed(): get_tree().quit() func _on_new_game_button_pressed(): activate_ui_panel(%NewGameUI) func _to_main_menu_button_pressed(): set_root_ui_state(ROOT_UI_STATE.MENU) func _unhandled_input(event): if event.is_action_released("toggle_inventory"): inventory_dialog.open(_all_recipes, _player.inventory)