From cf4687350d0cfb48d506bcc618601914b041a22b Mon Sep 17 00:00:00 2001 From: gotfishmakesticks <80163046+gotfishmakesticks@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:06:52 +0300 Subject: [PATCH] Game data save/load, profile autoload --- scenes/menus/StarterBaseMenu.tscn | 4 ++-- scenes/weapons/presets/DoubleLaserMk1.tscn | 1 + scenes/weapons/presets/SingleLaserMk1.tscn | 1 + scenes/weapons/presets/SingleRocketMk1.tscn | 1 + scripts/Space.gd | 20 ++++++++++++++++++++ scripts/menu/MainMenu.gd | 12 +++++++++++- scripts/menu/MainMenuButton.gd | 13 ++++++++++++- 7 files changed, 48 insertions(+), 4 deletions(-) diff --git a/scenes/menus/StarterBaseMenu.tscn b/scenes/menus/StarterBaseMenu.tscn index e36793b..54b4998 100644 --- a/scenes/menus/StarterBaseMenu.tscn +++ b/scenes/menus/StarterBaseMenu.tscn @@ -181,7 +181,7 @@ patch_margin_bottom = 2 script = ExtResource("2_rfxij") data = { "id": "weapon", -"position": Vector2(8, 0), +"position": Vector2(16, 0), "slot": "secondary", "weapon": "SingleRocketMk1" } @@ -271,7 +271,7 @@ patch_margin_bottom = 2 script = ExtResource("2_rfxij") data = { "id": "weapon", -"position": Vector2(16, 0), +"position": Vector2(8, 0), "slot": "primary", "weapon": "SingleLaserMk1" } diff --git a/scenes/weapons/presets/DoubleLaserMk1.tscn b/scenes/weapons/presets/DoubleLaserMk1.tscn index 74f5e08..ceecf90 100644 --- a/scenes/weapons/presets/DoubleLaserMk1.tscn +++ b/scenes/weapons/presets/DoubleLaserMk1.tscn @@ -5,6 +5,7 @@ [ext_resource type="Texture2D" uid="uid://c3gei46k8muk3" path="res://sprites/laserbox.png" id="3_ahhl5"] [node name="DoubleLaserMk1" type="Node2D" node_paths=PackedStringArray("shoot_timer", "spawner_points")] +position = Vector2(8, 0) script = ExtResource("1_ugbl6") projectile = ExtResource("2_b52h8") ammo_type = "Laser Energy" diff --git a/scenes/weapons/presets/SingleLaserMk1.tscn b/scenes/weapons/presets/SingleLaserMk1.tscn index b087959..893aaad 100644 --- a/scenes/weapons/presets/SingleLaserMk1.tscn +++ b/scenes/weapons/presets/SingleLaserMk1.tscn @@ -5,6 +5,7 @@ [ext_resource type="Texture2D" uid="uid://c3gei46k8muk3" path="res://sprites/laserbox.png" id="3_n1sx7"] [node name="SingleLaser" type="Node2D" node_paths=PackedStringArray("shoot_timer", "spawner_points")] +position = Vector2(8, 0) script = ExtResource("1_dwcuc") projectile = ExtResource("2_eiesu") ammo_type = "Laser Energy" diff --git a/scenes/weapons/presets/SingleRocketMk1.tscn b/scenes/weapons/presets/SingleRocketMk1.tscn index c7d3467..06b67f7 100644 --- a/scenes/weapons/presets/SingleRocketMk1.tscn +++ b/scenes/weapons/presets/SingleRocketMk1.tscn @@ -5,6 +5,7 @@ [ext_resource type="Texture2D" uid="uid://c3gei46k8muk3" path="res://sprites/laserbox.png" id="3_7gexv"] [node name="SingleRocketMk1" type="Node2D" node_paths=PackedStringArray("shoot_timer", "spawner_points")] +position = Vector2(16, 0) script = ExtResource("1_wdpx2") projectile = ExtResource("2_fypwx") ammo_type = "Rockets" diff --git a/scripts/Space.gd b/scripts/Space.gd index 20c8d9d..494c55d 100644 --- a/scripts/Space.gd +++ b/scripts/Space.gd @@ -26,6 +26,26 @@ func _ready(): ship.camera.limit_right = map_width/2.0 ship.camera.limit_top = -map_height/2.0 ship.camera.limit_bottom = map_height/2.0 + + if Game.profile.profile_meta.has('game_load'): + Game.profile.profile_meta.erase('game_load') + var save = Game.profile.profile_meta['game'] + ship.money = save['money'] + ship.hull.hp = save['hp'] + ship.hull.fuel = save['fuel'] + ship.hull.ammunition = save['ammo'] + if save.has('primaryweapon'): + for node in ship.primary_slot.get_children(): + node.queue_free() + var weapon_inst = Game.get_weapon(save['primaryweapon']).instantiate() + weapon_inst.id = save['primaryweapon'] + ship.primary_slot.add_child(weapon_inst) + if save.has('secondaryweapon'): + for node in ship.secondary_slot.get_children(): + node.queue_free() + var weapon_inst = Game.get_weapon(save['secondaryweapon']).instantiate() + weapon_inst.id = save['secondaryweapon'] + ship.secondary_slot.add_child(weapon_inst) func addtargetlist(body : Node2D): if !can_target.has(body): diff --git a/scripts/menu/MainMenu.gd b/scripts/menu/MainMenu.gd index 1c285c1..b952d0f 100644 --- a/scripts/menu/MainMenu.gd +++ b/scripts/menu/MainMenu.gd @@ -12,6 +12,10 @@ var menu_id = 0 @onready var input = $Control/Input func _ready(): + var profile_meta = Game.profile_load("") + if profile_meta != {} and profile_meta['meta'].has('last_profile'): + profile_meta = Game.profile_load(profile_meta['meta']['last_profile']) + Game.profile = Profile.create(profile_meta) change_menu(0) profile_status.text = "Current profile: [{profile}]".format({"profile": Game.profile.profile_meta['meta']['profile_name']}) @@ -19,7 +23,7 @@ func change_menu(id): input.visible = false match id: 0: # Main - b1.id = "NewGame" + b1.id = "Play" b2.id = "Profiles" b3.id = "Settings" b4.id = "Credits" @@ -46,6 +50,12 @@ func change_menu(id): b5.id = "Null" input.visible = true input.text = "" + 4: # Play Menu + b1.id = "LoadGame" + b2.id = "NewGame" + b3.id = "Back" + b4.id = "Null" + b5.id = "Null" b1.change_name() b2.change_name() b3.change_name() diff --git a/scripts/menu/MainMenuButton.gd b/scripts/menu/MainMenuButton.gd index 532ba7b..d09bd81 100644 --- a/scripts/menu/MainMenuButton.gd +++ b/scripts/menu/MainMenuButton.gd @@ -14,6 +14,8 @@ var texts = { "Profiles" : "Profiles", "Credits" : "Credits", "Back" : "Back", + "Play" : "Play", + "LoadGame" : "Load from Profile", "Null" : Game.gameversion } @@ -35,7 +37,10 @@ func _on_button_up(): "LoadProfileConfirm": var profile_name = controller.input.text var profile_meta = Game.profile_legit_check(Game.profile_load(profile_name)) - Game.profile = Profile.create(profile_meta) + if profile_meta != {}: + Game.profile.profile_meta['meta']['last_profile'] = profile_name + Game.profile_save(get_tree().current_scene) + Game.profile = Profile.create(profile_meta) var format = {"profile": Game.profile.profile_meta['meta']['profile_name']} controller.profile_status.text = "Current profile: [{profile}]".format(format) "DeleteProfile": @@ -56,6 +61,12 @@ func _on_button_up(): controller.change_menu(2) "LoadProfile": controller.change_menu(3) + "Play": + controller.change_menu(4) + "LoadGame": + if Game.profile.profile_meta.has('game'): + Game.profile.profile_meta['game_load'] = true + get_tree().change_scene_to_file("res://scenes/Space.tscn") func change_name(): visible = id != "Null"