TinyAdventure/root_ui.gd

135 lines
3.7 KiB
GDScript

class_name RootUI
extends CanvasLayer
const GAME_PROFILE_FOLDER:String = "game_profiles"
@export var startup_scene:PackedScene
@export var game_scene:PackedScene
@export var autostart_game_profile:String = ""
@onready var scene = %Scene
@onready var ui_panels:Array = [ %MainMenuUI, %StartGameMenuUI, %CreateGameUI, %GameUI ]
@onready var active_ui_panel:Control = null
enum ROOT_UI_STATE { UNDEFINED, MENU, GAME }
var _root_ui_state:ROOT_UI_STATE = ROOT_UI_STATE.UNDEFINED
var current_game_profile:GameProfileResource = null
var current_game_profile_directory:String = ""
func _ready():
var autostart_load_success = false
if not autostart_game_profile.is_empty():
autostart_load_success = _on_start_game_profile("user://" + GAME_PROFILE_FOLDER + "/" + autostart_game_profile + "/game.tres")
if not autostart_load_success:
set_root_ui_state(ROOT_UI_STATE.MENU)
%CreateGameUI.start_game.connect(_on_start_game_profile)
%StartGameMenuUI.start_game.connect(_on_start_game_profile)
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 load_scene(scene_resource:PackedScene):
for scene_child in scene.get_children():
scene_child.queue_free()
scene_child.get_parent().remove_child(scene_child)
print("Loading level '" + scene_resource.resource_path + "'")
scene.add_child(scene_resource .instantiate())
func save_game():
var game:Game = %Scene.get_child(0) as Game
game.save_game(current_game_profile_directory)
activate_ui_panel(%GameUI)
func load_game():
set_root_ui_state(RootUI.ROOT_UI_STATE.UNDEFINED)
load_scene(game_scene)
var game:Game = %Scene.get_child(0) as Game
game.load_game(current_game_profile_directory)
set_root_ui_state(RootUI.ROOT_UI_STATE.GAME)
func set_root_ui_state(state:ROOT_UI_STATE):
if _root_ui_state == state:
return
if state == ROOT_UI_STATE.GAME:
%GameUI.activate_game_scene(scene.get_child(0))
activate_ui_panel(%GameUI)
elif state == ROOT_UI_STATE.MENU:
load_scene(startup_scene)
%GameUI.activate_game_scene(null)
activate_ui_panel(%MainMenuUI)
_root_ui_state = state
func _on_start_game_profile(profile_path:String) -> bool:
current_game_profile = null
current_game_profile_directory = ""
print ("Root UI: Starting game from profile path " + profile_path)
current_game_profile = ResourceLoader.load(profile_path)
if current_game_profile == null:
push_error("Error loading game profile " + profile_path)
return false
# Update last_played timestamp
current_game_profile.last_played = Time.get_datetime_string_from_system()
ResourceSaver.save(current_game_profile, profile_path)
current_game_profile_directory = profile_path.get_base_dir()
if not _is_savegame_available():
var level_resource:PackedScene = game_scene
load_scene(level_resource)
save_game()
load_game()
activate_ui_panel(%GameUI)
return true
func _is_savegame_available() -> bool:
if current_game_profile_directory == "":
return false
var game_profile_directory = DirAccess.open(current_game_profile_directory)
return game_profile_directory.file_exists("savegame.tres")
func _on_start_game_button_pressed():
load_scene(game_scene)
set_root_ui_state(ROOT_UI_STATE.GAME)
func _on_quit_game_scene():
save_game()
set_root_ui_state(ROOT_UI_STATE.MENU)
func _on_quit_button_pressed():
get_tree().quit()
func _activate_main_menu_ui():
activate_ui_panel(%MainMenuUI)
func _activate_start_game_menu_ui():
activate_ui_panel(%StartGameMenuUI)
func _activate_create_game_ui():
%CreateGameUI.game_name_edit.text = ""
activate_ui_panel(%CreateGameUI)
func _activate_game_ui():
activate_ui_panel(%GameUI)