From 2cf08465979becbf3e23a1a0568c486a404c5faf Mon Sep 17 00:00:00 2001 From: Luca Junge Date: Fri, 27 Sep 2024 09:37:12 +0200 Subject: [PATCH] added basic scene manager --- game.gd | 11 +++++--- game.tscn | 3 +-- globals/scene_manager.gd | 22 ++++++++++++++++ project.godot | 2 ++ resources/monster_data.gd | 4 +-- resources/save_data.gd | 41 ----------------------------- resources/save_game/save_manager.gd | 11 +++----- 7 files changed, 37 insertions(+), 57 deletions(-) create mode 100644 globals/scene_manager.gd delete mode 100644 resources/save_data.gd diff --git a/game.gd b/game.gd index 603bc2a..a398833 100644 --- a/game.gd +++ b/game.gd @@ -10,7 +10,11 @@ func _ready() -> void: if skip_menu == true: UI.show_ingame_controls(!skip_menu) UI.show_main_menu(!skip_menu) - start_new_game() + if SaveManager.save_exists(): + continue_game() + else: + start_new_game() + return # On start of the game, the main menu is shown @@ -21,15 +25,14 @@ func _ready() -> void: # this event comes from the MainMenu Node in the UI func start_new_game() -> void: - var world = preload("res://worlds/debug_level.tscn").instantiate() UI.show_ingame_controls() # Add the starting world - %CurrentLevel.add_child(world) + SceneManager.set_world("res://worlds/debug_level.tscn") # Add the starting player to the starting world var player = preload("res://entities/player/player.tscn").instantiate() - world.add_child(player) + SceneManager.current_world.add_child(player) # Give the player a monster var monster = MonsterData.new() diff --git a/game.tscn b/game.tscn index 4949935..3d5e48c 100644 --- a/game.tscn +++ b/game.tscn @@ -5,9 +5,8 @@ [node name="Game" type="Node3D"] script = ExtResource("1_mfbtr") -skip_menu = false [node name="UI" parent="." instance=ExtResource("2_ynhuf")] -[node name="CurrentLevel" type="Node3D" parent="."] +[node name="CurrentScene" type="Node3D" parent="."] unique_name_in_owner = true diff --git a/globals/scene_manager.gd b/globals/scene_manager.gd new file mode 100644 index 0000000..fef5d5b --- /dev/null +++ b/globals/scene_manager.gd @@ -0,0 +1,22 @@ +extends Node + +@onready var scene_root = $/root/Game/CurrentScene +var current_world : Node = null + +func set_world(path: String): + if ResourceLoader.exists(path): + var world = load(path).instantiate() + scene_root.add_child(world) + current_world = world + else: + printerr("Level %s does not exist" % path) + +func remove_world(): + var world = scene_root.get_child(0) + if world == null: + printerr("No world node found") + return + + scene_root.remove_child(world) + world.queue_free() + current_world = null diff --git a/project.godot b/project.godot index 0206d18..504ba84 100644 --- a/project.godot +++ b/project.godot @@ -20,6 +20,7 @@ config/icon="res://assets/logo/logo.png" SaveManager="*res://resources/save_game/save_manager.gd" UI="*res://ui/ui.gd" +SceneManager="*res://globals/scene_manager.gd" [display] @@ -34,6 +35,7 @@ window/handheld/orientation=1 folder_colors={ "res://assets/": "red", "res://entities/": "blue", +"res://globals/": "purple", "res://materials/": "pink", "res://resources/": "yellow", "res://ui/": "green" diff --git a/resources/monster_data.gd b/resources/monster_data.gd index 82d28d1..64d2841 100644 --- a/resources/monster_data.gd +++ b/resources/monster_data.gd @@ -28,7 +28,7 @@ var level: int = 1 var xp: int = 1 -# the ID is the filename of the monster from res://resources/monsters/.tres +# the ID is the filename of the monster from res://entities/monsters/.tres func set_data(p_unique_id = "", current_values: Dictionary = {}, p_level: int = 1) -> void: if p_unique_id.is_empty(): printerr("Monster.gd: No ID given") @@ -59,7 +59,7 @@ func set_data(p_unique_id = "", current_values: Dictionary = {}, p_level: int = func load_base_monster_data(p_unique_id): var path = "res://resources/monsters/" + p_unique_id + ".tres" - if FileAccess.file_exists(path): + if ResourceLoader.exists(path): var base_monster = load(path) return base_monster diff --git a/resources/save_data.gd b/resources/save_data.gd deleted file mode 100644 index 9eb815d..0000000 --- a/resources/save_data.gd +++ /dev/null @@ -1,41 +0,0 @@ -extends Node - -# SIGNALS -signal inventory_changed - -# Player data -var player_name: String = "Player" -var money: int = 0 -var world: String = "res://scenes/worlds/debug_level.tscn" - -# Inventory data -# Items with their unique id and amount - -# Monster Party data -# monster with their values (unique id, atk, def, tmp for now) -var monsters: Array - -func _init() -> void: - set_name("SaveData") - -func save(): - var player_data = { - "player_name": player_name, - "money": money, - "world": world - } - - # Collect all data to save in one dictionary - var save_dict = { - "player_data": player_data - } - - return save_dict - -func setup_new_inventory() -> void: - var data = MonsterData.new() - data.set_data("debuggy", {}, 5) - print("After setting it in inventory: ", data.display_name) - - monsters.append(data) - inventory_changed.emit() diff --git a/resources/save_game/save_manager.gd b/resources/save_game/save_manager.gd index 1bbcc68..d06920a 100644 --- a/resources/save_game/save_manager.gd +++ b/resources/save_game/save_manager.gd @@ -44,17 +44,12 @@ func load_game(): # Save a reference to the current save game current_save = saved_game - print(saved_game.party[0].display_name) # Clean up all non-static objects from the scene get_tree().call_group("save_nodes", "on_before_load_game") # Restore world data - # doing it via get_tree, until the SceneManager exists - var _world : Node = load(saved_game.world).instantiate() - get_node("/root/Game/CurrentLevel").add_child(_world) - world = _world - #SceneManager.set_world(saved_game.world) + SceneManager.set_world(saved_game.world) # Restore all other nodes for node in saved_game.saved_data: @@ -63,8 +58,8 @@ func load_game(): var restored_node = scene.instantiate() # Add the item to the world - # todo: add parent as value to save - world.add_child(restored_node) + # todo: add parent as value to save and restore to the parent + SceneManager.current_world.add_child(restored_node) # run a callback to restore the item's state if restored_node.has_method("on_load_game"):