✨ Implemented Loading function on fields and plants
This commit is contained in:
@@ -33,6 +33,7 @@ QuestManager="*res://prefabs/quests/quest_manager_autoload.tscn"
|
||||
Signal_Debugger="*res://addons/SignalVisualizer/Debugger/SignalDebugger.gd"
|
||||
FightWorldAutoload="*res://prefabs/fight/fight_world_autoload.tscn"
|
||||
FieldService="*res://scripts/CSharp/Common/Farming/FieldService.cs"
|
||||
SaveGameManager="*res://scripts/CSharp/Common/Savegame/SaveGameManager.cs"
|
||||
|
||||
[dialogic]
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
"beetRootScene_field6": "{\n\t\"field_state\": 1,\n\t\"plant_data\": {\n\t\t\"plant_days_growing\": 0,\n\t\t\"plant_state\": 3,\n\t\t\"prefab_path\": \"res://prefabs/farm/plants/beet_plant.tscn\"\n\t}\n}",
|
||||
"beetRootScene_field7": "{\n\t\"field_state\": 1,\n\t\"plant_data\": {\n\t\t\"plant_days_growing\": 0,\n\t\t\"plant_state\": 3,\n\t\t\"prefab_path\": \"res://prefabs/farm/plants/beet_plant.tscn\"\n\t}\n}",
|
||||
"beetRootScene_field8": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field0": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field1": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field0": "{\n\t\"field_state\": 3\n}",
|
||||
"farmOutside_field1": "{\n\t\"field_state\": 3\n}",
|
||||
"farmOutside_field10": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field11": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field12": "{\n\t\"field_state\": 0\n}",
|
||||
@@ -20,15 +20,15 @@
|
||||
"farmOutside_field17": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field18": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field19": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field2": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field2": "{\n\t\"field_state\": 3,\n\t\"plant_data\": {\n\t\t\"plant_days_growing\": 0,\n\t\t\"plant_state\": 1,\n\t\t\"prefab_path\": \"res://prefabs/farm/plants/beet_plant.tscn\"\n\t}\n}",
|
||||
"farmOutside_field20": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field21": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field22": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field23": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field24": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field25": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field3": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field4": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field3": "{\n\t\"field_state\": 2,\n\t\"plant_data\": {\n\t\t\"plant_days_growing\": 0,\n\t\t\"plant_state\": 1,\n\t\t\"prefab_path\": \"res://prefabs/farm/plants/tomato_plant.tscn\"\n\t}\n}",
|
||||
"farmOutside_field4": "{\n\t\"field_state\": 1\n}",
|
||||
"farmOutside_field5": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field6": "{\n\t\"field_state\": 0\n}",
|
||||
"farmOutside_field7": "{\n\t\"field_state\": 0\n}",
|
||||
|
||||
@@ -1956,7 +1956,7 @@ region_rect = Rect2(332, 194, 179, 154)
|
||||
[node name="grass3" type="Sprite2D" parent="YSorted/Farm visuals/Static/greenery/grass"]
|
||||
modulate = Color(0.954868, 0.882599, 0.798501, 1)
|
||||
z_index = -5
|
||||
position = Vector2(18189, 1960)
|
||||
position = Vector2(18165, 1966)
|
||||
scale = Vector2(5.77602, 3.87779)
|
||||
texture = ExtResource("42_loeum")
|
||||
region_enabled = true
|
||||
|
||||
@@ -73,7 +73,7 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
|
||||
LoadFromSaveData();
|
||||
if(PlantingPlaceholder.GetChildCount() > 0)
|
||||
_currentPlant = PlantingPlaceholder.GetChild<PlantBehaviour2D>(0);
|
||||
UpdateFieldState(FieldState);
|
||||
@@ -159,17 +159,7 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
|
||||
if (!string.IsNullOrEmpty(plantPrefabPath))
|
||||
{
|
||||
PackedScene prefab = ResourceLoader.Load<PackedScene>(plantPrefabPath, nameof(PackedScene));
|
||||
Node2D plant2d = prefab.Instantiate<Node2D>();
|
||||
PlantingPlaceholder.AddChild(plant2d);
|
||||
plant2d.GlobalPosition = PlantingPlaceholder.GlobalPosition;
|
||||
_currentPlant = plant2d as PlantBehaviour2D;
|
||||
|
||||
if (_currentPlant != null)
|
||||
{
|
||||
_currentPlant.Field = this;
|
||||
}
|
||||
|
||||
PlantPrefab(plantPrefabPath);
|
||||
InventoryManager.Instance.playerInventory.RemoveItem(currentSlotIndex);
|
||||
success = true;
|
||||
}
|
||||
@@ -177,6 +167,21 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
return success;
|
||||
}
|
||||
|
||||
private void PlantPrefab(string prefabPath)
|
||||
{
|
||||
PackedScene prefab = ResourceLoader.Load<PackedScene>(prefabPath, nameof(PackedScene));
|
||||
Node2D plant2d = prefab.Instantiate<Node2D>();
|
||||
PlantingPlaceholder.AddChild(plant2d);
|
||||
plant2d.GlobalPosition = PlantingPlaceholder.GlobalPosition;
|
||||
_currentPlant = plant2d as PlantBehaviour2D;
|
||||
|
||||
if (_currentPlant != null)
|
||||
{
|
||||
_currentPlant.Field = this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
@@ -206,4 +211,54 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
SavegameService.AppendSave(saveData);
|
||||
}
|
||||
|
||||
public void LoadFromSaveData()
|
||||
{
|
||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
||||
var id = SaveId + _fieldIndex.Payload.AsString();
|
||||
string jsonPayload = SavegameService.GetSaveData(sceneName, id);
|
||||
Dictionary<string, Variant> save = Json.ParseString(jsonPayload).AsGodotDictionary<string, Variant>();
|
||||
|
||||
if (save.Count > 0)
|
||||
{
|
||||
if (save.TryGetValue("field_state", out Variant fieldStateVar))
|
||||
{
|
||||
int fieldStateInt = fieldStateVar.AsInt32();
|
||||
FieldState = (FieldState) fieldStateInt;
|
||||
|
||||
if (fieldStateInt != 0)
|
||||
{
|
||||
Visible = true;
|
||||
UpdateFieldState(FieldState);
|
||||
}
|
||||
}
|
||||
|
||||
if (save.TryGetValue("plant_data", out Variant plantDataVar))
|
||||
{
|
||||
Dictionary<string, Variant> plantDataDict = plantDataVar.AsGodotDictionary<string, Variant>();
|
||||
|
||||
if (plantDataDict.TryGetValue("prefab_path", out Variant prefabPathVar))
|
||||
{
|
||||
PlantPrefab(prefabPathVar.AsString());
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (plantDataDict.TryGetValue("plant_state", out Variant plantStateVar) && _currentPlant != null)
|
||||
{
|
||||
_currentPlant.State = (PlantState) plantStateVar.AsInt32();
|
||||
_currentPlant.GrowPlant();
|
||||
}
|
||||
|
||||
if (plantDataDict.TryGetValue("plant_days_growing", out Variant plantDaysGrowingVar) && _currentPlant != null)
|
||||
{
|
||||
_currentPlant.DaysGrowing = plantDaysGrowingVar.AsInt32();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
@@ -25,7 +25,11 @@ public partial class PlantBehaviour2D : Node2D
|
||||
private bool _magicWordSaid = false;
|
||||
private bool _calledOnReady = false;
|
||||
|
||||
public PlantState State => _state;
|
||||
public PlantState State
|
||||
{
|
||||
get => _state;
|
||||
set => _state = value;
|
||||
}
|
||||
|
||||
public int DaysGrowing { get; set; }
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||
[Serializable]
|
||||
public class SaveData
|
||||
{
|
||||
public string SceneName;
|
||||
public string SceneName;
|
||||
public string Id;
|
||||
public string JsonPayload;
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||
|
||||
/// <summary>
|
||||
/// This class manages the loading of the savegame throughout the gameplay flow.
|
||||
/// </summary>
|
||||
public partial class SaveGameManager : Node
|
||||
{
|
||||
public static SaveGameManager? Instance { get; private set; } = null!;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
|
||||
SavegameService.Load();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://c1srnefvhigef
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using FileAccess = Godot.FileAccess;
|
||||
@@ -8,7 +9,10 @@ public static class SavegameService
|
||||
{
|
||||
public static readonly string SavePath = "res://savegame/savegame.json";
|
||||
|
||||
public static Dictionary<string, Variant> SaveDatas = new ();
|
||||
public static Dictionary<string, string> SaveDatas = new ();
|
||||
|
||||
public static bool _loaded = false;
|
||||
|
||||
|
||||
public static void AppendSave(SaveData saveData)
|
||||
{
|
||||
@@ -23,12 +27,49 @@ public static class SavegameService
|
||||
SaveDatas.Add(key, saveData.JsonPayload);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static string GetSaveData(string sceneName, string id)
|
||||
{
|
||||
string saveData = "";
|
||||
|
||||
string key = string.Concat(sceneName, "_", id);
|
||||
|
||||
if (!_loaded)
|
||||
{
|
||||
GD.Print("SavegameService: SaveFile not loaded.");
|
||||
return saveData;
|
||||
}
|
||||
|
||||
if (SaveDatas.ContainsKey(key))
|
||||
{
|
||||
saveData = SaveDatas[key];
|
||||
}
|
||||
|
||||
return saveData;
|
||||
}
|
||||
|
||||
|
||||
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}");
|
||||
}
|
||||
|
||||
public static void Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
GD.PrintRich(e.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
_loaded = true;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user