using Godot;
using Babushka.scripts.CSharp.Common.Savegame;
using Godot.Collections;
///
/// Simple collectible scene objects with saveable state.
///
public partial class TrashObject : Sprite2D, ISaveable
{
private bool _collected;
///
/// Loads objects state on scene start.
///
public override void _Ready()
{
LoadFromSaveData();
}
///
/// Sets object state to collected and updates save data.
///
public void Collect()
{
SetCollectedState();
UpdateSaveData();
}
private void SetCollectedState()
{
_collected = true;
Visible = false;
ProcessMode = ProcessModeEnum.Disabled;
}
///
/// Updates the save data with the current state of the object.
///
public void UpdateSaveData()
{
var payloadData = new Dictionary
{
{ "collectedState", _collected },
};
string id = GetMeta("SaveID").AsString();
SavegameService.AppendDataToSave( id, payloadData);
}
///
/// Loads objects state from save data.
///
public void LoadFromSaveData()
{
string id = GetMeta("SaveID").AsString();
Dictionary save = SavegameService.GetSaveData(id);
if (save.Count > 0)
{
if (save.TryGetValue("collectedState", out Variant collectedVar))
{
if (collectedVar.AsBool())
{
SetCollectedState();
}
}
}
}
}