feature/save_and_loaaaaaad #32
@@ -1,12 +1,20 @@
|
||||
using System.Linq;
|
||||
using Babushka.scripts.CSharp.Common.Savegame;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
public partial class InteractionArea2D : Node2D
|
||||
public partial class InteractionArea2D : Node2D, ISaveable
|
||||
{
|
||||
[ExportGroup("Persistence")]
|
||||
[Export] public string SaveId = "";
|
||||
[Export] private VariableNode _sceneID;
|
||||
[Export] public VariableResource _sceneKeyProvider;
|
||||
|
||||
[ExportGroup("Settings")]
|
||||
[Export] private Area2D _area;
|
||||
[Export] private Label _label;
|
||||
[Export] private bool _active = true;
|
||||
|
kziolkowski marked this conversation as resolved
|
||||
@@ -17,6 +25,7 @@ public partial class InteractionArea2D : Node2D
|
||||
[Export] private int _id = -1; // TODO: remove
|
||||
|
||||
private Material[] _backupMaterials;
|
||||
private int _interactionCounter;
|
||||
|
||||
[Signal] public delegate void InteractedToolEventHandler(int id); // TODO: remove
|
||||
|
||||
@@ -106,6 +115,7 @@ public partial class InteractionArea2D : Node2D
|
||||
|
||||
EmitSignal(SignalName.InteractedTool, _id);
|
||||
EmitSignal(SignalName.Interacted);
|
||||
_interactionCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,4 +130,26 @@ public partial class InteractionArea2D : Node2D
|
||||
_active = !_active;
|
||||
_label.Hide();
|
||||
}
|
||||
|
||||
#region SAVE AND LOAD
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
var payloadData = new Dictionary<string, Variant>
|
||||
{
|
||||
{ "interaction counter", _interactionCounter }
|
||||
};
|
||||
|
||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), SaveId + _sceneID.Payload.AsString(), payloadData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
||||
var id = SaveId + _sceneID.Payload.AsString();
|
||||
|
||||
Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -185,10 +185,6 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
var saveData = new SaveData();
|
||||
|
||||
saveData.SceneName = _sceneKeyProvider.Payload.AsString();
|
||||
saveData.Id = SaveId + _fieldIndex.Payload.AsString();
|
||||
var payloadData = new Dictionary<string, Variant>
|
||||
{
|
||||
{ "field_state", (int)FieldState }
|
||||
@@ -206,20 +202,15 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
||||
);
|
||||
}
|
||||
|
||||
saveData.JsonPayload = Json.Stringify(payloadData, indent: "\t");
|
||||
|
||||
SavegameService.AppendSave(saveData);
|
||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), SaveId + _fieldIndex.Payload.AsString(), payloadData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
||||
var id = SaveId + _fieldIndex.Payload.AsString();
|
||||
string jsonPayload = SavegameService.GetSaveData(sceneName, id);
|
||||
if (string.IsNullOrEmpty(jsonPayload))
|
||||
return;
|
||||
|
||||
Dictionary<string, Variant> save = Json.ParseString(jsonPayload).AsGodotDictionary<string, Variant>();
|
||||
Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
||||
|
||||
if (save.Count > 0)
|
||||
{
|
||||
|
||||
@@ -178,10 +178,6 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
var saveData = new SaveData();
|
||||
|
||||
saveData.SceneName = SCENE_NAME;
|
||||
saveData.Id = ID;
|
||||
var payloadData = new Godot.Collections.Dictionary<string, Variant>();
|
||||
|
||||
for (int i = 0; i < _slots.Count; i++)
|
||||
@@ -196,20 +192,15 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
}
|
||||
}
|
||||
|
||||
saveData.JsonPayload = Json.Stringify(payloadData, indent: "\t");
|
||||
|
||||
SavegameService.AppendSave(saveData);
|
||||
SavegameService.AppendDataToSave(SCENE_NAME, ID, payloadData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = SCENE_NAME;
|
||||
var id = ID;
|
||||
string jsonPayload = SavegameService.GetSaveData(sceneName, id);
|
||||
if (string.IsNullOrEmpty(jsonPayload))
|
||||
return;
|
||||
|
||||
Godot.Collections.Dictionary<string, Variant> save = Json.ParseString(jsonPayload).AsGodotDictionary<string, Variant>();
|
||||
Godot.Collections.Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
||||
|
||||
if (save.Count > 0)
|
||||
{
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://bqapxuprxuj20
|
||||
@@ -18,13 +18,23 @@ public static class SavegameService
|
||||
public static Dictionary<string, string> SaveDatas = new ();
|
||||
|
||||
public static bool _loaded = false;
|
||||
|
||||
|
||||
|
||||
public static void AppendDataToSave(string scenename, string id, Dictionary<string, Variant> payload)
|
||||
{
|
||||
var saveData = new SaveData();
|
||||
|
||||
saveData.SceneName = scenename;
|
||||
saveData.Id = id;
|
||||
saveData.JsonPayload = Json.Stringify(payload, indent: "\t");
|
||||
AppendSave(saveData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds or overwrites an entry in the SaveData dictionary.
|
||||
/// </summary>
|
||||
/// <param name="saveData"></param>
|
||||
public static void AppendSave(SaveData saveData)
|
||||
private static void AppendSave(SaveData saveData)
|
||||
{
|
||||
string key = string.Concat(saveData.SceneName, "_", saveData.Id);
|
||||
|
||||
@@ -46,21 +56,23 @@ public static class SavegameService
|
||||
/// <param name="sceneName"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetSaveData(string sceneName, string id)
|
||||
public static Dictionary<string, Variant> GetSaveData(string sceneName, string id)
|
||||
{
|
||||
string saveData = "";
|
||||
Dictionary<string, Variant> saveData = new();
|
||||
|
||||
string key = string.Concat(sceneName, "_", id);
|
||||
string saveDataString = "";
|
||||
|
||||
if (!_loaded)
|
||||
{
|
||||
GD.Print("SavegameService: SaveFile not loaded.");
|
||||
return saveData;
|
||||
}
|
||||
|
||||
|
||||
if (SaveDatas.ContainsKey(key))
|
||||
{
|
||||
saveData = SaveDatas[key];
|
||||
saveDataString = SaveDatas[key];
|
||||
saveData = Json.ParseString(saveDataString).AsGodotDictionary<string, Variant>();
|
||||
}
|
||||
|
||||
return saveData;
|
||||
|
||||
Reference in New Issue
Block a user
Is this ever used?
good catch, this is residue from an attempt at serializing the InteractionArea, which we won't do. So I'm deleting this now.