Added trash object savestate

This commit is contained in:
2025-12-16 15:03:27 +01:00
parent 7f58aeae76
commit 13fcf059e9
5 changed files with 85 additions and 25 deletions
@@ -0,0 +1,69 @@
using Godot;
using Babushka.scripts.CSharp.Common.Savegame;
using Godot.Collections;
/// <summary>
/// Simple collectible scene objects with saveable state.
/// </summary>
public partial class TrashObject : Sprite2D, ISaveable
{
private bool _collected;
/// <summary>
/// Loads objects state on scene start.
/// </summary>
public override void _Ready()
{
LoadFromSaveData();
}
/// <summary>
/// Sets object state to collected and updates save data.
/// </summary>
public void Collect()
{
SetCollectedState();
UpdateSaveData();
}
private void SetCollectedState()
{
_collected = true;
Visible = false;
ProcessMode = ProcessModeEnum.Disabled;
}
/// <summary>
/// Updates the save data with the current state of the object.
/// </summary>
public void UpdateSaveData()
{
var payloadData = new Dictionary<string, Variant>
{
{ "collectedState", _collected },
};
string id = GetMeta("SaveID").AsString();
SavegameService.AppendDataToSave( id, payloadData);
}
/// <summary>
/// Loads objects state from save data.
/// </summary>
public void LoadFromSaveData()
{
string id = GetMeta("SaveID").AsString();
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
if (save.Count > 0)
{
if (save.TryGetValue("collectedState", out Variant collectedVar))
{
if (collectedVar.AsBool())
{
SetCollectedState();
}
}
}
}
}
@@ -0,0 +1 @@
uid://c2cgj153m05sp
-2
View File
@@ -1,6 +1,4 @@
using System.Threading.Tasks;
using Babushka.scripts.CSharp.Common.Savegame;
using Babushka.scripts.CSharp.Low_Code.Variables;
using Godot;
using Godot.Collections;