mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-11 14:55:55 +00:00
Game saving and loading
This commit is contained in:
parent
fbd00dcd15
commit
73b0847948
9 changed files with 83 additions and 26 deletions
|
|
@ -1,8 +1,10 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using GTweens.Builders;
|
||||
using GTweensGodot.Extensions;
|
||||
|
||||
|
|
@ -201,6 +203,8 @@ public partial class GlobalState : Node
|
|||
_fader.TweenModulateAlpha(0, 1f).PlayUnpausable();
|
||||
}
|
||||
|
||||
private readonly string SaveNameFile = "user://savegame.save";
|
||||
|
||||
public void SaveGame()
|
||||
{
|
||||
var items = InventoryManager.Instance.Save();
|
||||
|
|
@ -213,16 +217,52 @@ public partial class GlobalState : Node
|
|||
{ "Level", SessionSettings.LevelNumber }
|
||||
};
|
||||
|
||||
var saveFile = FileAccess.Open("user://savegame.save", FileAccess.ModeFlags.Write);
|
||||
var saveFile = FileAccess.Open(SaveNameFile, FileAccess.ModeFlags.Write);
|
||||
|
||||
var jsonString = Json.Stringify(serializedSavedata);
|
||||
|
||||
saveFile.StoreLine(jsonString);
|
||||
|
||||
GD.Print("Saved data successfully");
|
||||
}
|
||||
|
||||
public void LoadGame()
|
||||
public bool LoadGame()
|
||||
{
|
||||
if (!FileAccess.FileExists(SaveNameFile))
|
||||
{
|
||||
return false; // Error! We don't have a save to load.
|
||||
}
|
||||
using var saveFile = FileAccess.Open(SaveNameFile, FileAccess.ModeFlags.Read);
|
||||
|
||||
var jsonString = saveFile.GetLine();
|
||||
var json = new Json();
|
||||
|
||||
var parseResult = json.Parse(jsonString);
|
||||
if (parseResult != Error.Ok)
|
||||
{
|
||||
GD.Print($"JSON Parse Error: {json.GetErrorMessage()} in {jsonString} at line {json.GetErrorLine()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var deserializedSaveData = new Dictionary<string, Variant>((Dictionary)json.Data);
|
||||
|
||||
Dictionary<string, int> items = (Dictionary<string, int>)deserializedSaveData["Items"];
|
||||
|
||||
int levelNumber = (int)deserializedSaveData["Level"];
|
||||
|
||||
var levelData = _mapsDatabase.Maps.FirstOrDefault(x => x.LevelId == levelNumber);
|
||||
if (levelData is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.SessionSettings.NewSession();
|
||||
SessionSettings.LevelNumber = levelNumber;
|
||||
SessionSettings.Items = items;
|
||||
|
||||
this.GotoScene(levelData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ResizeCursor(float scale)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue