🚧 saving of field parameters as json implemented
This commit is contained in:
@@ -1,15 +1,27 @@
|
||||
using System;
|
||||
using Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Babushka.scripts.CSharp.Common.Savegame;
|
||||
using Babushka.scripts.CSharp.Low_Code.Events;
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the behaviour of the field, i.e. interactions, states and effects.
|
||||
/// </summary>
|
||||
[GlobalClass]
|
||||
public partial class FieldBehaviour2D : Sprite2D
|
||||
{
|
||||
[ExportGroup("Persistence")]
|
||||
[Export] public string SaveId = "";
|
||||
[Export] private VariableNode _fieldIndex;
|
||||
[Export] public VariableResource _sceneKeyProvider;
|
||||
[Export] public FieldState FieldState = FieldState.Tilled;
|
||||
|
||||
[ExportGroup("Field Visuals")]
|
||||
[Export] private Sprite2D _fieldSprite;
|
||||
[Export] private Sprite2D _maskSprite;
|
||||
[Export] private Sprite2D _outlineSprite;
|
||||
@@ -17,25 +29,25 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
[Export] private Texture2D[] _maskTexture;
|
||||
[Export] private Texture2D Tilled;
|
||||
[Export] private Texture2D Watered;
|
||||
[Export] public FieldState FieldState = FieldState.Tilled;
|
||||
|
||||
[ExportGroup("Field Interactions")]
|
||||
[Export] public InteractionArea2D PlantingInteraction;
|
||||
[Export] public InteractionArea2D FieldInteractionArea;
|
||||
|
||||
[ExportGroup("Configuration")]
|
||||
[Export] public Node2D PlantingPlaceholder;
|
||||
[Export] public ItemRepository ItemRepository;
|
||||
[Export] public InteractionArea2D FieldInteractionArea;
|
||||
[Export] public VariableResource _sceneKeyProvider;
|
||||
[Export] private VariableNode _fieldIndex;
|
||||
[Export] private CpuParticles2D _wateringParticles;
|
||||
[Export] private EventResource _wateringEvent;
|
||||
|
||||
|
||||
public Vector2 FieldPosition;
|
||||
|
||||
private bool _seedsActive;
|
||||
private bool _wateringCanActive;
|
||||
|
||||
private bool _canPlant;
|
||||
private bool _canWater;
|
||||
|
||||
private PlantBehaviour2D? _currentPlant;
|
||||
|
||||
[Signal] public delegate void PlantedEventHandler();
|
||||
|
||||
private void UpdateInteractionArea()
|
||||
@@ -58,11 +70,16 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
_wateringCanActive = activated;
|
||||
UpdateInteractionArea();
|
||||
}
|
||||
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
|
||||
if(PlantingPlaceholder.GetChildCount() > 0)
|
||||
_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];
|
||||
@@ -97,6 +114,7 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
break;
|
||||
}
|
||||
UpdateInteractionArea();
|
||||
UpdateSaveData();
|
||||
}
|
||||
|
||||
|
||||
@@ -137,19 +155,19 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
if (item == null || PlantingPlaceholder.GetChildCount() > 0 || item.amount == 0)
|
||||
return success;
|
||||
|
||||
string prefabPath = ItemRepository.TryGetPrefabPath(item.blueprint);
|
||||
string plantPrefabPath = ItemRepository.TryGetPrefabPath(item.blueprint);
|
||||
|
||||
if (prefabPath != null)
|
||||
if (!string.IsNullOrEmpty(plantPrefabPath))
|
||||
{
|
||||
PackedScene prefab = ResourceLoader.Load<PackedScene>(prefabPath, nameof(PackedScene));
|
||||
PackedScene prefab = ResourceLoader.Load<PackedScene>(plantPrefabPath, nameof(PackedScene));
|
||||
Node2D plant2d = prefab.Instantiate<Node2D>();
|
||||
PlantingPlaceholder.AddChild(plant2d);
|
||||
plant2d.GlobalPosition = PlantingPlaceholder.GlobalPosition;
|
||||
PlantBehaviour2D? plantBehaviour = plant2d as PlantBehaviour2D;
|
||||
_currentPlant = plant2d as PlantBehaviour2D;
|
||||
|
||||
if (plantBehaviour != null)
|
||||
if (_currentPlant != null)
|
||||
{
|
||||
plantBehaviour.Field = this;
|
||||
_currentPlant.Field = this;
|
||||
}
|
||||
|
||||
InventoryManager.Instance.playerInventory.RemoveItem(currentSlotIndex);
|
||||
@@ -158,6 +176,34 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
var saveData = new SaveData();
|
||||
|
||||
saveData.SceneName = _sceneKeyProvider.Payload.AsString();
|
||||
saveData.Id = SaveId + _fieldIndex.Payload.AsString();
|
||||
var payloadData = new Dictionary<string, Variant>
|
||||
{
|
||||
{ "field_state", (int)FieldState }
|
||||
};
|
||||
|
||||
if (_currentPlant != null)
|
||||
{
|
||||
payloadData.Add(
|
||||
"plant_data", new Dictionary<string, Variant>()
|
||||
{
|
||||
{ "prefab_path", _currentPlant.PrefabPath },
|
||||
{ "plant_state", (int)_currentPlant.State },
|
||||
{ "plant_days_growing", _currentPlant.DaysGrowing }
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
saveData.JsonPayload = Json.Stringify(payloadData, indent: "\t");
|
||||
|
||||
SavegameService.AppendSave(saveData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
@@ -40,10 +41,6 @@ public partial class FieldService : Node
|
||||
innerDict.fields.Add(fieldIndex, field);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.PrintErr("Duplicate field at: " + fieldIndex);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
/// </summary>
|
||||
public partial class PlantBehaviour2D : Node2D
|
||||
{
|
||||
[Export] private string _prefabPath;
|
||||
[Export] private Sprite2D[] _seeds;
|
||||
[Export] private Sprite2D[] _smallPlants;
|
||||
[Export] private Sprite2D[] _bigPlants;
|
||||
@@ -24,6 +25,12 @@ public partial class PlantBehaviour2D : Node2D
|
||||
private bool _magicWordSaid = false;
|
||||
private bool _calledOnReady = false;
|
||||
|
||||
public PlantState State => _state;
|
||||
|
||||
public int DaysGrowing { get; set; }
|
||||
|
||||
public string PrefabPath => _prefabPath;
|
||||
|
||||
/// <summary>
|
||||
/// public accessor for the field reference
|
||||
/// </summary>
|
||||
@@ -35,7 +42,6 @@ public partial class PlantBehaviour2D : Node2D
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
GD.Print($"Ready: {Name}");
|
||||
if (_state == PlantState.None)
|
||||
{
|
||||
_state = PlantState.Planted;
|
||||
@@ -44,7 +50,6 @@ public partial class PlantBehaviour2D : Node2D
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print("plant state not none.");
|
||||
_calledOnReady = true;
|
||||
GrowPlant();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||
|
||||
[Serializable]
|
||||
public class SaveData
|
||||
{
|
||||
public string SceneName;
|
||||
public string Id;
|
||||
public string JsonPayload;
|
||||
|
||||
public string ToString()
|
||||
{
|
||||
return SceneName + " " + Id + " " + JsonPayload;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bkftl5mj33eah
|
||||
@@ -0,0 +1,34 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using FileAccess = Godot.FileAccess;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||
|
||||
public static class SavegameService
|
||||
{
|
||||
public static readonly string SavePath = "res://savegame/savegame.json";
|
||||
|
||||
public static Dictionary<string, Variant> SaveDatas = new ();
|
||||
|
||||
public static void AppendSave(SaveData saveData)
|
||||
{
|
||||
string key = string.Concat(saveData.SceneName, "_", saveData.Id);
|
||||
|
||||
if (SaveDatas.TryGetValue(key, out var value))
|
||||
{
|
||||
SaveDatas[key] = saveData.JsonPayload;
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveDatas.Add(key, saveData.JsonPayload);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Save()
|
||||
{
|
||||
string json = Json.Stringify(SaveDatas, indent: "\t");
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
file.StoreString(json);
|
||||
GD.Print($"Game saved to {file}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://rm23q50boqe5
|
||||
@@ -1,4 +1,6 @@
|
||||
using Babushka.scripts.CSharp.Common.Savegame;
|
||||
using Babushka.scripts.CSharp.Common.SceneManagement;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common;
|
||||
@@ -16,6 +18,7 @@ public partial class SceneTransition : Node
|
||||
|
||||
public void LoadSceneAtIndex(int index)
|
||||
{
|
||||
SavegameService.Save();
|
||||
string sceneName = _sceneNamesToLoad[index];
|
||||
SceneTransitionThreaded.Instance.ChangeSceneToFileThreaded(sceneName);
|
||||
UnloadAfterDelay();
|
||||
|
||||
Reference in New Issue
Block a user