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.");
|
||||
}
|
||||
}
|
||||
1
scripts/core/SaveManager.cs.uid
Normal file
1
scripts/core/SaveManager.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://osk74jjhtcjy
|
||||
9
scripts/core/SaveState.cs
Normal file
9
scripts/core/SaveState.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
//using Godot;
|
||||
|
||||
public class SaveState
|
||||
{
|
||||
public HashSet<string> CollectedPickups { get; set; } = new();
|
||||
//public Vector2I? LastPlayerChunk { get; set; } // optional debug, requires "using Godot;"
|
||||
//public int[]? LastPlayerChunk { get; set; } // [x,y] same debug as above, does not require Godot
|
||||
}
|
||||
1
scripts/core/SaveState.cs.uid
Normal file
1
scripts/core/SaveState.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dd83uxjqtlmra
|
||||
Loading…
Add table
Add a link
Reference in a new issue