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-09-08 14:00:02 +02:00
|
|
|
@export var autostart_game_scene:bool = true
|
2024-08-15 22:15:24 +02:00
|
|
|
|
|
|
|
@onready var scene = %Scene
|
2024-10-11 18:40:40 +02:00
|
|
|
@onready var ui_panels:Array = [ %MainMenuUI, %StartGameMenuUI, %CreateGameUI, %GameUI ]
|
2024-09-08 14:00:02 +02:00
|
|
|
@onready var active_ui_panel:Control = null
|
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
|
|
|
|
2024-08-15 23:21:31 +02:00
|
|
|
var current_level_resource:String
|
2024-10-18 12:01:15 +02:00
|
|
|
var current_game_profile:GameProfileResource = null
|
2024-10-11 18:40:40 +02:00
|
|
|
var current_game_profile_directory:String = ""
|
2024-08-15 23:21:31 +02:00
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
var _game_build_system:BuildSystem = null
|
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
func _ready():
|
2024-09-08 14:00:02 +02:00
|
|
|
if autostart_game_scene:
|
|
|
|
load_scene(game_scene)
|
|
|
|
set_root_ui_state(ROOT_UI_STATE.GAME)
|
|
|
|
else:
|
|
|
|
set_root_ui_state(ROOT_UI_STATE.MENU)
|
2024-10-11 18:40:40 +02:00
|
|
|
|
|
|
|
%CreateGameUI.start_game.connect(_on_start_game_profile)
|
|
|
|
%StartGameMenuUI.start_game.connect(_on_start_game_profile)
|
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 load_scene(scene_resource:PackedScene):
|
2024-09-08 14:00:02 +02:00
|
|
|
for scene_child in scene.get_children():
|
|
|
|
scene_child.queue_free()
|
|
|
|
scene_child.get_parent().remove_child(scene_child)
|
2024-08-15 22:15:24 +02:00
|
|
|
|
2024-08-15 23:21:31 +02:00
|
|
|
print("Loading level '" + scene_resource.resource_path + "'")
|
2024-09-27 11:46:31 +02:00
|
|
|
scene.add_child(scene_resource .instantiate())
|
2024-08-15 23:21:31 +02:00
|
|
|
current_level_resource = scene_resource.resource_path
|
2024-10-18 12:01:15 +02:00
|
|
|
|
|
|
|
_game_build_system = scene.find_child("BuildSystem", true, false) as BuildSystem
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
func save_game():
|
|
|
|
var player:Player = scene.find_child("Player", true, false)
|
|
|
|
|
|
|
|
if player == null:
|
|
|
|
push_error("Cannot load game: no player found!")
|
|
|
|
return
|
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
var save_data:SaveGameResource = SaveGameResource.new()
|
2024-09-08 14:00:02 +02:00
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
save_data.level = current_level_resource
|
|
|
|
save_data.level_pickup_items = %GameUI.picked_up_level_items
|
|
|
|
save_data.player_transform = player.global_transform
|
2024-09-08 14:00:02 +02:00
|
|
|
|
2024-09-30 18:05:40 +02:00
|
|
|
for node:Node in get_tree().get_nodes_in_group("quest_state"):
|
2024-10-11 18:40:40 +02:00
|
|
|
save_data.quest_states[node.name] = {}
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
for property in node.get_property_list():
|
|
|
|
if (property.usage & PROPERTY_USAGE_STORAGE != 0) and (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) != 0:
|
2024-10-11 18:40:40 +02:00
|
|
|
save_data.quest_states[node.name][property.name] = node.get(property.name)
|
2024-08-15 22:15:24 +02:00
|
|
|
|
2024-09-08 14:00:02 +02:00
|
|
|
for item_stack in player.inventory.get_item_stacks():
|
2024-10-11 18:40:40 +02:00
|
|
|
save_data.inventory.append({"item": item_stack.item, "count": item_stack.count})
|
2024-08-15 23:43:05 +02:00
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
for structure:BuiltStructure in get_tree().get_nodes_in_group("built_structure"):
|
|
|
|
var built_structure_resource = BuiltStructureResource.new()
|
|
|
|
built_structure_resource.global_transform = structure.global_transform
|
|
|
|
built_structure_resource.item = structure.item
|
|
|
|
var item_array = save_data.built_structures.find_key(structure.item)
|
|
|
|
|
|
|
|
if item_array == null:
|
|
|
|
item_array = []
|
|
|
|
save_data.built_structures[structure.item] = item_array
|
|
|
|
|
|
|
|
item_array.append(structure.global_transform)
|
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
print("Saving game: " + current_game_profile_directory + "/savegame.tres")
|
|
|
|
ResourceSaver.save(save_data, current_game_profile_directory + "/savegame.tres")
|
2024-09-08 14:00:02 +02:00
|
|
|
activate_ui_panel(%GameUI)
|
2024-08-15 22:15:24 +02:00
|
|
|
|
2024-09-08 14:00:02 +02:00
|
|
|
func load_game():
|
|
|
|
set_root_ui_state(RootUI.ROOT_UI_STATE.UNDEFINED)
|
|
|
|
|
|
|
|
%GameUI.picked_up_level_items.clear()
|
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
print("Loading game: " + current_game_profile_directory + "/savegame.tres")
|
2024-10-18 12:01:15 +02:00
|
|
|
var save_data:SaveGameResource = load(current_game_profile_directory + "/savegame.tres") as SaveGameResource
|
2024-10-11 18:40:40 +02:00
|
|
|
|
|
|
|
var level_resource:PackedScene = load(save_data.level)
|
2024-09-08 14:00:02 +02:00
|
|
|
load_scene(level_resource)
|
|
|
|
|
|
|
|
var player:Player = scene.find_child("Player", true, false)
|
|
|
|
|
|
|
|
if player == null:
|
|
|
|
push_error("Cannot load game: no player found!")
|
|
|
|
return
|
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
player.global_transform = save_data.player_transform
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
# Quest state
|
2024-11-01 18:07:23 +01:00
|
|
|
for quest_state:Node in get_tree().get_nodes_in_group("quest_state"):
|
|
|
|
for property in quest_state.get_property_list():
|
|
|
|
if (property.usage & PROPERTY_USAGE_STORAGE != 0) and (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) != 0 and (save_data.quest_states.has(quest_state.name)) and (save_data.quest_states[quest_state.name].has(property.name)):
|
|
|
|
quest_state.set(property.name, save_data.quest_states[quest_state.name][property.name])
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
# Inventory
|
|
|
|
player.inventory.clear()
|
|
|
|
var inventory_item_stacks:Array[ItemStack] = player.inventory.get_item_stacks()
|
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
assert(inventory_item_stacks.size() >= save_data.inventory.size())
|
2024-09-08 14:00:02 +02:00
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
for i in range(save_data.inventory.size()):
|
|
|
|
inventory_item_stacks[i].item = save_data.inventory[i]["item"]
|
|
|
|
inventory_item_stacks[i].count = save_data.inventory[i]["count"]
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
# Picked up items
|
2024-10-11 18:40:40 +02:00
|
|
|
for item_path in save_data.level_pickup_items:
|
2024-09-08 14:00:02 +02:00
|
|
|
if get_tree().root.has_node(item_path):
|
|
|
|
var item_node = get_tree().root.get_node(item_path)
|
|
|
|
%GameUI.picked_up_level_items.append(item_path)
|
|
|
|
item_node.queue_free()
|
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
# Built structures
|
|
|
|
for item:ItemResource in save_data.built_structures.keys():
|
|
|
|
var item_transform_array = save_data.built_structures[item] as Array[Transform3D]
|
|
|
|
for structure_transform in item_transform_array:
|
|
|
|
_game_build_system.build_structure(item, structure_transform)
|
|
|
|
|
2024-09-08 14:00:02 +02:00
|
|
|
set_root_ui_state(RootUI.ROOT_UI_STATE.GAME)
|
2024-08-15 22:15:24 +02:00
|
|
|
|
|
|
|
|
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-09-08 14:00:02 +02:00
|
|
|
%GameUI.activate_game_scene(scene.get_child(0))
|
2024-07-20 11:20:18 +02:00
|
|
|
activate_ui_panel(%GameUI)
|
2024-09-08 14:00:02 +02:00
|
|
|
elif state == ROOT_UI_STATE.MENU:
|
2024-08-15 22:15:24 +02:00
|
|
|
load_scene(startup_scene)
|
2024-09-08 14:00:02 +02:00
|
|
|
%GameUI.activate_game_scene(null)
|
2024-07-20 11:20:18 +02:00
|
|
|
activate_ui_panel(%MainMenuUI)
|
|
|
|
|
|
|
|
_root_ui_state = state
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
func _on_start_game_profile(profile_path:String) -> void:
|
|
|
|
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
|
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
# Update last_played timestamp
|
|
|
|
current_game_profile.last_played = Time.get_datetime_string_from_system()
|
|
|
|
ResourceSaver.save(current_game_profile, profile_path)
|
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
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")
|
2024-09-08 14:00:02 +02:00
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
func _on_start_game_button_pressed():
|
2024-09-08 14:00:02 +02:00
|
|
|
load_scene(game_scene)
|
2024-07-20 11:20:18 +02:00
|
|
|
set_root_ui_state(ROOT_UI_STATE.GAME)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
func _on_quit_game_scene():
|
2024-10-18 12:01:15 +02:00
|
|
|
save_game()
|
2024-10-11 18:40:40 +02:00
|
|
|
set_root_ui_state(ROOT_UI_STATE.MENU)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
func _on_quit_button_pressed():
|
|
|
|
get_tree().quit()
|
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
func _activate_main_menu_ui():
|
|
|
|
activate_ui_panel(%MainMenuUI)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
func _activate_start_game_menu_ui():
|
|
|
|
activate_ui_panel(%StartGameMenuUI)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
func _activate_create_game_ui():
|
|
|
|
activate_ui_panel(%CreateGameUI)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
func _activate_game_ui():
|
|
|
|
activate_ui_panel(%GameUI)
|