:feature: implemented saving and loading for ducks, 🔥 removed save implementation from interactionArea
This commit is contained in:
@@ -1,19 +1,11 @@
|
||||
using System.Linq;
|
||||
using Babushka.scripts.CSharp.Common.Savegame;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
public partial class InteractionArea2D : Node2D, ISaveable
|
||||
public partial class InteractionArea2D : Node2D
|
||||
{
|
||||
[ExportGroup("Persistence")]
|
||||
[Export] public VariableResource _sceneKeyProvider;
|
||||
[Export] private string _saveId = ""; // todo: find good default / generated solution
|
||||
//todo: rewire broken instances in scenes
|
||||
|
||||
[ExportGroup("Settings")]
|
||||
[Export] private Area2D _area;
|
||||
[Export] private Label _label;
|
||||
@@ -48,15 +40,6 @@ public partial class InteractionArea2D : Node2D, ISaveable
|
||||
{
|
||||
_backupMaterials = _spritesToOutline.Select(s => s.Material).ToArray();
|
||||
}
|
||||
|
||||
// bad solution for interaction areas, because they are all named the same.
|
||||
// option (equally bad) 1: take grandparent's name (could be null though)
|
||||
// option 2 (also bad): Write Identity Provider class that uses, checks and assigns GUIDs for this purpose.
|
||||
if (string.IsNullOrEmpty(_saveId))
|
||||
{
|
||||
_saveId = Name;
|
||||
}
|
||||
LoadFromSaveData();
|
||||
}
|
||||
|
||||
public void OnPlayerEntered(Node2D player)
|
||||
@@ -120,9 +103,7 @@ public partial class InteractionArea2D : Node2D, ISaveable
|
||||
sprite.Material = _backupMaterials[i];
|
||||
}
|
||||
}
|
||||
|
||||
Interact();
|
||||
UpdateSaveData();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,38 +126,4 @@ public partial class InteractionArea2D : Node2D, ISaveable
|
||||
_label.Hide();
|
||||
}
|
||||
|
||||
#region SAVE AND LOAD
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
var payloadData = new Dictionary<string, Variant>
|
||||
{
|
||||
{ "interaction_counter", _interactionCounter }
|
||||
};
|
||||
|
||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), _saveId, payloadData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
||||
var id = _saveId;
|
||||
int counter = 0;
|
||||
|
||||
Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
||||
if (save.Count > 0)
|
||||
{
|
||||
if (save.TryGetValue("interaction_counter", out Variant interactionCounterVar))
|
||||
{
|
||||
counter = interactionCounterVar.AsInt32();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < counter; i++)
|
||||
{
|
||||
Interact();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||
|
||||
[Tool]
|
||||
public partial class SaveIDProviderTool : Node
|
||||
{
|
||||
[ExportToolButton("Assign IDs")] private Callable assignIDs => Callable.From(AssignIDs);
|
||||
|
||||
private void AssignIDs()
|
||||
{
|
||||
Array<Node> saveables = GetTree().GetNodesInGroup("Saveable");
|
||||
foreach (var node in saveables)
|
||||
{
|
||||
GD.Print($"Checking {node.Name}.");
|
||||
GD.Print($"Node has Meta SaveID: {node.HasMeta("SaveID")} and it's: " + node.GetMeta("SaveID").AsString());
|
||||
if (!node.HasMeta("SaveID") || string.IsNullOrEmpty(node.GetMeta("SaveID").AsString()))
|
||||
{
|
||||
string saveID = Guid.NewGuid().ToString();
|
||||
node.SetMeta("SaveID", saveID);
|
||||
GD.Print($"Setting Save ID for node {node.Name}: " + saveID);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://ca4s0algeij1h
|
||||
@@ -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