36 lines
844 B
C#
36 lines
844 B
C#
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.");
|
|
}
|
|
}
|