♻️ refactored field code to work with the new id system
This commit is contained in:
@@ -16,8 +16,8 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
||||
{
|
||||
[ExportGroup("Persistence")]
|
||||
[Export] public string SaveId = "";
|
||||
[Export] private VariableNode _fieldIndex;
|
||||
[Export] private Node _saveIdHolder;
|
||||
[Export] public VariableResource _sceneKeyProvider;
|
||||
[Export] public FieldState FieldState = FieldState.Tilled;
|
||||
|
||||
@@ -78,8 +78,6 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
||||
_currentPlant = PlantingPlaceholder.GetChild<PlantBehaviour2D>(0);
|
||||
UpdateFieldState(FieldState);
|
||||
|
||||
FieldService.Instance.TryAddEntry(_sceneKeyProvider.Payload.AsString(),_fieldIndex.Payload.AsInt32(), this);
|
||||
|
||||
int randomIndex = new Random().Next(0, _maskTexture.Length);
|
||||
_maskSprite.Texture = _maskTexture[randomIndex];
|
||||
_outlineSprite.Texture = _maskOutlineTextures[randomIndex];
|
||||
@@ -202,12 +200,13 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
||||
);
|
||||
}
|
||||
|
||||
SavegameService.AppendDataToSave(SaveId + _fieldIndex.Payload.AsString(), payloadData);
|
||||
string id = _saveIdHolder.GetMeta("SaveID").AsString();
|
||||
SavegameService.AppendDataToSave(id, payloadData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var id = SaveId + _fieldIndex.Payload.AsString();
|
||||
string id = _saveIdHolder.GetMeta("SaveID").AsString();
|
||||
|
||||
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
||||
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
public partial class FieldService : Node
|
||||
{
|
||||
private Dictionary<string, FieldsInScene>? _outerDict = null!;
|
||||
public static FieldService Instance { get; private set; } = null!;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
_outerDict = new Dictionary<string, FieldsInScene>();
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Instance = null;
|
||||
_outerDict = null;
|
||||
}
|
||||
|
||||
|
||||
//Create
|
||||
public bool TryAddEntry(string sceneName, int fieldIndex, FieldBehaviour2D field)
|
||||
{
|
||||
if (_outerDict != null )
|
||||
{
|
||||
FieldsInScene innerDict;
|
||||
bool outerDictEntryExists = _outerDict.TryGetValue(sceneName, out innerDict);
|
||||
|
||||
if (!outerDictEntryExists)
|
||||
{
|
||||
innerDict = new FieldsInScene();
|
||||
_outerDict.Add(sceneName, innerDict);
|
||||
}
|
||||
|
||||
if (!innerDict.fields.ContainsKey(fieldIndex))
|
||||
{
|
||||
innerDict.fields.Add(fieldIndex, field);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read
|
||||
public FieldBehaviour2D? TryGet(string key, int fieldIndex)
|
||||
{
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.TryGetValue(fieldIndex, out FieldBehaviour2D? fieldInstance))
|
||||
{
|
||||
return fieldInstance;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
//Update
|
||||
public void UpdateEntry(string key, int fieldIndex, FieldBehaviour2D state)
|
||||
{
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.ContainsKey(fieldIndex))
|
||||
{
|
||||
field.fields[fieldIndex] = state;
|
||||
}
|
||||
else
|
||||
{
|
||||
TryAddEntry(key, fieldIndex, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Delete
|
||||
public void RemoveEntry(string key, int fieldIndex)
|
||||
{
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.ContainsKey(fieldIndex))
|
||||
{
|
||||
field.fields.Remove(fieldIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class FieldsInScene
|
||||
{
|
||||
public Dictionary<int, FieldBehaviour2D?> fields;
|
||||
|
||||
public FieldsInScene()
|
||||
{
|
||||
fields = new Dictionary<int, FieldBehaviour2D?>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
uid://slo0uydmmvnu
|
||||
Reference in New Issue
Block a user