:feature: implemented saving and loading for ducks, 🔥 removed save implementation from interactionArea
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
using System.Threading.Tasks;
|
||||
using Babushka.scripts.CSharp.Common.Savegame;
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Temp;
|
||||
|
||||
/// <summary>
|
||||
/// Temporary Duck behaviour to make sure we can use them in the first showcase
|
||||
/// </summary>
|
||||
public partial class MVPDuck : Node2D
|
||||
public partial class MVPDuck : Node2D, ISaveable
|
||||
{
|
||||
[ExportGroup("Persistence")]
|
||||
[Export] public VariableResource _sceneKeyProvider;
|
||||
|
||||
[ExportGroup("Animation")]
|
||||
[Export] private Node2D _penTarget;
|
||||
[Export] private int _transferDelayMs;
|
||||
[Export] private AnimationPlayer _animationPlayer;
|
||||
@@ -17,6 +24,11 @@ public partial class MVPDuck : Node2D
|
||||
|
||||
[Signal] public delegate void DuckCollectedEventHandler();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
LoadFromSaveData();
|
||||
}
|
||||
|
||||
public void TransferToTargetAfterDelay()
|
||||
{
|
||||
if (!_collected)
|
||||
@@ -25,7 +37,6 @@ public partial class MVPDuck : Node2D
|
||||
PlayAnimation();
|
||||
_collected = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void PlayAnimation()
|
||||
@@ -40,7 +51,48 @@ public partial class MVPDuck : Node2D
|
||||
if(!_penTarget.Equals(null))
|
||||
Position = _penTarget.GlobalPosition;
|
||||
EmitSignal(SignalName.DuckCollected);
|
||||
UpdateSaveData();
|
||||
}
|
||||
|
||||
#region SAVE AND LOAD
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
var payloadData = new Dictionary<string, Variant>
|
||||
{
|
||||
{ "globalPositionX", GlobalPosition.X },
|
||||
{ "globalPositionY", GlobalPosition.Y },
|
||||
};
|
||||
|
||||
string id = GetMeta("SaveID").AsString();
|
||||
GD.Print($"Updating Save ID for object {Name}: {id}");
|
||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), id, payloadData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
||||
string id = GetMeta("SaveID").AsString();
|
||||
GD.Print($"Loading Save ID for object {Name}: " + id);
|
||||
|
||||
Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
||||
if (save.Count > 0)
|
||||
{
|
||||
float xPos = 0;
|
||||
float yPos = 0;
|
||||
if (save.TryGetValue("globalPositionX", out Variant xPosVar))
|
||||
{
|
||||
xPos = xPosVar.AsSingle();
|
||||
}
|
||||
|
||||
if (save.TryGetValue("globalPositionY", out Variant yPosVar))
|
||||
{
|
||||
yPos = yPosVar.AsSingle();
|
||||
}
|
||||
|
||||
GlobalPosition = new Vector2(xPos, yPos);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user