: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
|
||||
}
|
||||
Reference in New Issue
Block a user