Pickups + Save-diff
This commit is contained in:
parent
b49c0ee55f
commit
bf874ba633
11 changed files with 133 additions and 16 deletions
36
scripts/core/SaveManager.cs
Normal file
36
scripts/core/SaveManager.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using Godot;
|
||||
using System.Text.Json;
|
||||
|
||||
public partial class SaveManager : Node
|
||||
{
|
||||
public SaveState State { get; private set; } = new();
|
||||
|
||||
private const string SavePath = "user://save.json";
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Load();
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (!FileAccess.FileExists(SavePath))
|
||||
{
|
||||
GD.Print("No save found, starting fresh.");
|
||||
State = new SaveState();
|
||||
return;
|
||||
}
|
||||
|
||||
var json = FileAccess.GetFileAsString(SavePath);
|
||||
State = JsonSerializer.Deserialize<SaveState>(json) ?? new SaveState();
|
||||
GD.Print($"Loaded save. CollectedPickups={State.CollectedPickups.Count}");
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var json = JsonSerializer.Serialize(State, new JsonSerializerOptions { WriteIndented = true });
|
||||
using var f = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
f.StoreString(json);
|
||||
GD.Print("Saved.");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue