parent
7f58aeae76
commit
13fcf059e9
@ -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
|
||||||
Loading…
Reference in new issue