TinyAdventure/root_ui.gd

66 lines
1.7 KiB
GDScript

class_name RootUI
extends CanvasLayer
@onready var ui_panels:Array = [ %MainMenuUI, %NewGameUI, %GameUI, %GameMenuUI ]
@onready var active_ui_panel:Control = null
@onready var player = %Player
@onready var inventory_dialog:InventoryDialog = %InventoryDialog
enum ROOT_UI_STATE { MENU, GAME }
var _root_ui_state:ROOT_UI_STATE = ROOT_UI_STATE.GAME
var _all_recipes:Array[Recipe] = []
# 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.MENU)
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 set_root_ui_state(state:ROOT_UI_STATE):
if state == ROOT_UI_STATE.GAME:
%GameScene.show()
%StartupScene.hide()
%GameScene.process_mode = Node.PROCESS_MODE_INHERIT
activate_ui_panel(%GameUI)
else:
%StartupScene.show()
%GameScene.hide()
%GameMenuUI.hide()
%GameScene.process_mode = Node.PROCESS_MODE_DISABLED
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)