You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Babushka/scripts/CSharp/Common/Interactables/TrashObject.cs

70 lines
1.7 KiB

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();
}
}
}
}
}