From 1b80fa54a729c7c96e3a15dbad7e67c19a9a8657 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Sat, 12 Aug 2023 15:01:22 -0700 Subject: [PATCH] game save class --- State/Global/GlobalState.cs | 15 +++++++++++++++ State/Global/MapState.cs | 2 ++ Utils/Save.cs | 22 ++++++++++++++++++++++ Utils/World.cs | 27 ++++++++++++++++----------- 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 Utils/Save.cs diff --git a/State/Global/GlobalState.cs b/State/Global/GlobalState.cs index ae6a98c..ed915bd 100644 --- a/State/Global/GlobalState.cs +++ b/State/Global/GlobalState.cs @@ -1,4 +1,5 @@ using Godot; +using SupaLidlGame.Utils; namespace SupaLidlGame.State.Global; @@ -17,4 +18,18 @@ public partial class GlobalState : Node public delegate void SummonBossEventHandler(string bossName); public void Print(string str) => GD.Print(str); + + public void ImportFromSave(Save save) + { + Progression = save.Progression; + MapState = save.MapState; + Stats = save.Stats; + } + + public void ExportToSave(Save save) + { + save.Progression = Progression; + save.MapState = MapState; + save.Stats = Stats; + } } diff --git a/State/Global/MapState.cs b/State/Global/MapState.cs index 112156d..80f2d8c 100644 --- a/State/Global/MapState.cs +++ b/State/Global/MapState.cs @@ -1,6 +1,8 @@ using Godot; using Godot.Collections; +namespace SupaLidlGame.State.Global; + public partial class MapState : Resource { [Export] diff --git a/Utils/Save.cs b/Utils/Save.cs new file mode 100644 index 0000000..2ee0471 --- /dev/null +++ b/Utils/Save.cs @@ -0,0 +1,22 @@ +using Godot; + +namespace SupaLidlGame.Utils; + +public partial class Save : Resource +{ + [Export] + public State.Global.Progression Progression { get; set; } + + [Export] + public State.Global.MapState MapState { get; set; } + + [Export] + public State.Global.Stats Stats { get; set; } + + public Save() + { + Progression = new(); + MapState = new(); + Stats = new(); + } +} diff --git a/Utils/World.cs b/Utils/World.cs index 2ecf576..f3c0149 100644 --- a/Utils/World.cs +++ b/Utils/World.cs @@ -282,21 +282,26 @@ public partial class World : Node { SetSpawn(CurrentPlayer.GlobalPosition); - // TODO: create a single save resource file - ResourceSaver.Save(GlobalState.Progression, "user://progression.tres"); - ResourceSaver.Save(GlobalState.MapState, "user://map-state.tres"); - ResourceSaver.Save(GlobalState.Stats, "user://stats.tres"); + var save = GetSave(); + GlobalState.ExportToSave(save); + ResourceSaver.Save(save, "user://save.tres"); + } + + private Save GetSave() + { + if (ResourceLoader.Exists("user://save.tres")) + { + return ResourceLoader.Load("user://save.tres"); + } + else + { + return new Save(); + } } public void LoadGame() { - var prog = ResourceLoader.Load("user://progression.tres"); - var mapState = ResourceLoader.Load("user://map-state.tres"); - var stats = ResourceLoader.Load("user://stats.tres"); - - GlobalState.Progression = prog ?? new Progression(); - GlobalState.MapState = mapState ?? new MapState(); - GlobalState.Stats = stats ?? new Stats(); + GlobalState.ImportFromSave(GetSave()); // load the player scene // TODO: implement