2024-07-20 11:20:18 +02:00
|
|
|
class_name RootUI
|
2024-07-09 22:33:38 +02:00
|
|
|
extends CanvasLayer
|
|
|
|
|
2024-08-15 22:15:24 +02:00
|
|
|
@export var startup_scene:PackedScene
|
|
|
|
@export var game_scene:PackedScene
|
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
@onready var ui_panels:Array = [ %MainMenuUI, %NewGameUI, %GameUI, %GameMenuUI ]
|
|
|
|
@onready var active_ui_panel:Control = null
|
|
|
|
|
|
|
|
@onready var inventory_dialog:InventoryDialog = %InventoryDialog
|
2024-08-15 22:15:24 +02:00
|
|
|
@onready var scene = %Scene
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-08-15 22:15:24 +02:00
|
|
|
enum ROOT_UI_STATE { UNDEFINED, MENU, GAME }
|
|
|
|
var _root_ui_state:ROOT_UI_STATE = ROOT_UI_STATE.UNDEFINED
|
2024-07-09 22:33:38 +02:00
|
|
|
var _all_recipes:Array[Recipe] = []
|
2024-08-15 22:15:24 +02:00
|
|
|
var _player:Player = null
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-08-15 23:21:31 +02:00
|
|
|
var current_level_resource:String
|
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
# 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)
|
|
|
|
|
2024-08-02 01:03:40 +02:00
|
|
|
set_root_ui_state(ROOT_UI_STATE.GAME)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-08-15 22:15:24 +02:00
|
|
|
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)
|
|
|
|
|
2024-08-15 23:21:31 +02:00
|
|
|
print("Loading level '" + scene_resource.resource_path + "'")
|
2024-08-15 22:15:24 +02:00
|
|
|
scene.add_child(scene_resource.instantiate())
|
2024-08-15 23:21:31 +02:00
|
|
|
current_level_resource = scene_resource.resource_path
|
2024-08-15 22:15:24 +02:00
|
|
|
|
|
|
|
_player = scene.find_child("Player", true, false)
|
|
|
|
|
|
|
|
if _player:
|
|
|
|
update_player_signals()
|
|
|
|
|
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
func set_root_ui_state(state:ROOT_UI_STATE):
|
2024-08-15 22:15:24 +02:00
|
|
|
if _root_ui_state == state:
|
|
|
|
return
|
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
if state == ROOT_UI_STATE.GAME:
|
2024-08-15 22:15:24 +02:00
|
|
|
load_scene(game_scene)
|
|
|
|
%GameUI.game_scene = scene.get_child(0)
|
2024-07-20 11:20:18 +02:00
|
|
|
activate_ui_panel(%GameUI)
|
|
|
|
else:
|
2024-08-15 22:15:24 +02:00
|
|
|
load_scene(startup_scene)
|
|
|
|
%GameUI.game_scene = null
|
2024-07-20 11:20:18 +02:00
|
|
|
%GameMenuUI.hide()
|
|
|
|
activate_ui_panel(%MainMenuUI)
|
|
|
|
|
|
|
|
_root_ui_state = state
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
func _on_start_game_button_pressed():
|
2024-07-20 11:20:18 +02:00
|
|
|
set_root_ui_state(ROOT_UI_STATE.GAME)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_quit_button_pressed():
|
|
|
|
get_tree().quit()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_new_game_button_pressed():
|
|
|
|
activate_ui_panel(%NewGameUI)
|
|
|
|
|
|
|
|
|
|
|
|
func _to_main_menu_button_pressed():
|
2024-07-20 11:20:18 +02:00
|
|
|
set_root_ui_state(ROOT_UI_STATE.MENU)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _unhandled_input(event):
|
|
|
|
if event.is_action_released("toggle_inventory"):
|
2024-08-15 22:15:24 +02:00
|
|
|
inventory_dialog.open(_all_recipes, _player.inventory)
|