From 249c58af4642dfbf1724e8d50d9b7aab7cd234f1 Mon Sep 17 00:00:00 2001 From: tonydero Date: Tue, 29 Oct 2024 10:30:19 -0600 Subject: [PATCH] initial game save implementation using ConfigFile, plus some experimentation with ResourceSaver though nonfunctional --- scenes/main_menu.tscn | 18 ++++++++++++++--- scripts/Globals.gd | 36 +++++++++++++++++++++++++++++++-- scripts/Player.gd | 21 +++++++++++++++---- scripts/PlayerData.gd | 46 ++++++++++++++++++++++++++++++++++++++++++ scripts/PlayerState.gd | 7 +++---- scripts/main_menu.gd | 14 +++++++++++++ 6 files changed, 129 insertions(+), 13 deletions(-) create mode 100644 scripts/PlayerData.gd diff --git a/scenes/main_menu.tscn b/scenes/main_menu.tscn index 9321ae6..77d3cec 100644 --- a/scenes/main_menu.tscn +++ b/scenes/main_menu.tscn @@ -30,15 +30,27 @@ layout_mode = 2 text = "Play " -[node name="TestButton" type="Button" parent="MenuButtons"] +[node name="SaveButton" type="Button" parent="MenuButtons"] layout_mode = 2 -text = "Test +text = "Save" + +[node name="LoadButton" type="Button" parent="MenuButtons"] +layout_mode = 2 +text = "Load " [node name="ExitButton" type="Button" parent="MenuButtons"] layout_mode = 2 text = "Exit" +[node name="TestButton" type="Button" parent="MenuButtons"] +visible = false +layout_mode = 2 +text = "Test +" + [connection signal="pressed" from="MenuButtons/PlayButton" to="." method="_on_play_button_pressed"] -[connection signal="pressed" from="MenuButtons/TestButton" to="." method="_on_test_button_pressed"] +[connection signal="pressed" from="MenuButtons/SaveButton" to="." method="_on_save_button_pressed"] +[connection signal="pressed" from="MenuButtons/LoadButton" to="." method="_on_load_button_pressed"] [connection signal="pressed" from="MenuButtons/ExitButton" to="." method="_on_exit_button_pressed"] +[connection signal="pressed" from="MenuButtons/TestButton" to="." method="_on_test_button_pressed"] diff --git a/scripts/Globals.gd b/scripts/Globals.gd index a25d2fb..9c83f74 100644 --- a/scripts/Globals.gd +++ b/scripts/Globals.gd @@ -2,7 +2,9 @@ extends Node -var debugSpeedEnabled : bool +var debugSpeedEnabled: bool +var player_save := ConfigFile.new() + class World: enum Zone { @@ -95,7 +97,7 @@ func elapsed_time_format(elapsed_time: Array, round_seconds: bool = true) -> Str var days_format = "%d days " if elapsed_time[3] == 0: days_format = "" - elapsed_time = elapsed_time.slice(0,3) + elapsed_time = elapsed_time.slice(0, 3) if round_seconds: seconds_format = "%02d" elapsed_time.reverse() @@ -108,3 +110,33 @@ func _input(event: InputEvent) -> void: Engine.time_scale = 10.0 if event.is_action_pressed("game_speed_normal"): Engine.time_scale = 1.0 + + +func save_game() -> void: + player_save.set_value("player", "player_name", PlayerState.player_name) + player_save.set_value("player", "constitution", PlayerState.constitution) + player_save.set_value("player", "experience", PlayerState.experience) + player_save.set_value("player", "level", PlayerState.level) + player_save.set_value("player", "base_health", PlayerState.base_health) + player_save.set_value("player", "base_resource", PlayerState.base_resource) + player_save.set_value("player", "weapon_type", PlayerState.weapon_type) + player_save.set_value("player", "zone_duration", PlayerState.zone_duration) + + player_save.save_encrypted_pass("user://player_save.sav", "test") + + +func load_game() -> void: + var err = player_save.load_encrypted_pass("user://player_save.sav", "test") + + if err != OK: + print("Failed to load save file.") + return + + PlayerState.player_name = player_save.get_value("player", "player_name") + PlayerState.constitution = player_save.get_value("player", "constitution") + PlayerState.experience = player_save.get_value("player", "experience") + PlayerState.level = player_save.get_value("player", "level") + PlayerState.base_health = player_save.get_value("player", "base_health") + PlayerState.base_resource = player_save.get_value("player", "base_resource") + PlayerState.weapon_type = player_save.get_value("player", "weapon_type") + PlayerState.zone_duration = player_save.get_value("player", "zone_duration") diff --git a/scripts/Player.gd b/scripts/Player.gd index 072b5b3..bb48f6e 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -7,7 +7,6 @@ const EXP_EXPONENT := 2.4 var area_exp := 0.0 var current_health: int -var resource_type: String var current_resource: int var max_health var max_resource @@ -17,12 +16,26 @@ func _ready() -> void: SignalBus.enemy_died.connect(_on_enemy_died) SignalBus.quest_completed.connect(_on_quest_completed) SignalBus.enemy_damaged_player.connect(_on_enemy_damaged_player) - max_health = PlayerState.max_health - max_resource = PlayerState.max_resource + max_health = _set_player_health() + max_resource = _set_player_resource() _set_player_health_display() _set_player_resource_display() $PlayerExpBar.value = _get_level_progress_percent() $PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level)) + + +func _set_player_health() -> int: + # modify player health based on stats or temporary buffs + return PlayerState.base_health * PlayerState.constitution + + +func _set_player_resource() -> int: + # modify player resources based on weapon and stats or temporary buffs + match PlayerState.weapon_type: + Globals.Weapon.WeaponType.BOW: + return PlayerState.base_resource + _: + return PlayerState.base_resource func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void: @@ -84,7 +97,7 @@ func _update_player_resource(change: int) -> void: var prev_resource = current_resource var new_resource = prev_resource + change current_resource = new_resource - PlayerState.current_resource = current_resource + # PlayerState.current_resource = current_resource if current_resource <= 0: $PlayerResourceBar/PlayerResourceCurrent.text = str(0) $PlayerResourceBar.value = 0 diff --git a/scripts/PlayerData.gd b/scripts/PlayerData.gd new file mode 100644 index 0000000..9419a96 --- /dev/null +++ b/scripts/PlayerData.gd @@ -0,0 +1,46 @@ +# player save data resource to use with ResourceSaver +class_name PlayerData +extends Resource + + +@export var player_name := "Player" +@export var constitution := 1 +@export var experience := 0.0 +@export var level := 1.0 +@export var base_health := 100 +@export var base_resource := 100 +@export var weapon_type: Globals.Weapon.WeaponType +@export var zone_duration := 0 + + +# Confused on where this all goes... +# Let's assume the PlayerState is the node where we save the player data in game +var PlayerState: Node + +var save_path := "user://player_data.tres" # <- tres is Text RESource + +func save() -> void: + var data := PlayerData.new() + data.player_name = PlayerState.player_name + data.constitution = PlayerState.constitution + data.experience = PlayerState.experience + data.level = PlayerState.level + data.base_health = PlayerState.base_health + data.base_resource = PlayerState.base_resource + data.weapon_type = PlayerState.weapon_type + data.zone_duration = PlayerState.zone_duration + + var error := ResourceSaver.save(data, save_path) + if error: + print("An error happened while saving data: ", error) + +func load() -> void: + var data: PlayerData = load(save_path) + PlayerState.player_name = data.player_name + PlayerState.constitution = data.constitution + PlayerState.experience = data.experience + PlayerState.level = data.level + PlayerState.base_health = data.base_health + PlayerState.base_resource = data.base_resource + PlayerState.weapon_type = data.weapon_type + PlayerState.zone_duration = data.zone_duration \ No newline at end of file diff --git a/scripts/PlayerState.gd b/scripts/PlayerState.gd index 125edde..c883b3b 100644 --- a/scripts/PlayerState.gd +++ b/scripts/PlayerState.gd @@ -4,12 +4,11 @@ extends Node +var player_name := "Player" var constitution := 1 var experience := 0.0 var level := 1.0 -var max_health := constitution * 100 -var max_resource := 100 -var current_resource := max_resource +var base_health := 100 +var base_resource := 100 var weapon_type: Globals.Weapon.WeaponType - var zone_duration := 0 diff --git a/scripts/main_menu.gd b/scripts/main_menu.gd index e3624dc..489ee06 100644 --- a/scripts/main_menu.gd +++ b/scripts/main_menu.gd @@ -1,6 +1,11 @@ extends Control +func _ready() -> void: + if OS.is_debug_build(): + $MenuButtons/TestButton.show() + + func _on_play_button_pressed() -> void: get_tree().change_scene_to_file("res://scenes/combatUIScene.tscn") @@ -9,5 +14,14 @@ func _on_exit_button_pressed() -> void: get_tree().quit() +func _on_load_button_pressed() -> void: + Globals.load_game() + + +func _on_save_button_pressed() -> void: + # ResourceSaver.save(PlayerState, "user://player_state.tres") + Globals.save_game() + + func _on_test_button_pressed() -> void: pass # Replace with function body.