game save class

controller-support
HumanoidSandvichDispenser 2023-08-12 15:01:22 -07:00
parent f3452f7d3e
commit 1b80fa54a7
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
4 changed files with 55 additions and 11 deletions

View File

@ -1,4 +1,5 @@
using Godot; using Godot;
using SupaLidlGame.Utils;
namespace SupaLidlGame.State.Global; namespace SupaLidlGame.State.Global;
@ -17,4 +18,18 @@ public partial class GlobalState : Node
public delegate void SummonBossEventHandler(string bossName); public delegate void SummonBossEventHandler(string bossName);
public void Print(string str) => GD.Print(str); 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;
}
} }

View File

@ -1,6 +1,8 @@
using Godot; using Godot;
using Godot.Collections; using Godot.Collections;
namespace SupaLidlGame.State.Global;
public partial class MapState : Resource public partial class MapState : Resource
{ {
[Export] [Export]

22
Utils/Save.cs 100644
View File

@ -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();
}
}

View File

@ -282,21 +282,26 @@ public partial class World : Node
{ {
SetSpawn(CurrentPlayer.GlobalPosition); SetSpawn(CurrentPlayer.GlobalPosition);
// TODO: create a single save resource file var save = GetSave();
ResourceSaver.Save(GlobalState.Progression, "user://progression.tres"); GlobalState.ExportToSave(save);
ResourceSaver.Save(GlobalState.MapState, "user://map-state.tres"); ResourceSaver.Save(save, "user://save.tres");
ResourceSaver.Save(GlobalState.Stats, "user://stats.tres"); }
private Save GetSave()
{
if (ResourceLoader.Exists("user://save.tres"))
{
return ResourceLoader.Load<Save>("user://save.tres");
}
else
{
return new Save();
}
} }
public void LoadGame() public void LoadGame()
{ {
var prog = ResourceLoader.Load<Progression>("user://progression.tres"); GlobalState.ImportFromSave(GetSave());
var mapState = ResourceLoader.Load<MapState>("user://map-state.tres");
var stats = ResourceLoader.Load<Stats>("user://stats.tres");
GlobalState.Progression = prog ?? new Progression();
GlobalState.MapState = mapState ?? new MapState();
GlobalState.Stats = stats ?? new Stats();
// load the player scene // load the player scene
// TODO: implement // TODO: implement