diff --git a/project.godot b/project.godot index 455bb40..df595f1 100644 --- a/project.godot +++ b/project.godot @@ -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] diff --git a/savegame/savegame.json b/savegame/savegame.json index f1cf059..f6cf423 100644 --- a/savegame/savegame.json +++ b/savegame/savegame.json @@ -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}", diff --git a/scenes/Babushka_scene_farm_outside_2d.tscn b/scenes/Babushka_scene_farm_outside_2d.tscn index 321ddbe..82183f1 100644 --- a/scenes/Babushka_scene_farm_outside_2d.tscn +++ b/scenes/Babushka_scene_farm_outside_2d.tscn @@ -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 diff --git a/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs b/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs index 6b8b68f..31157cb 100644 --- a/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs +++ b/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs @@ -73,7 +73,7 @@ public partial class FieldBehaviour2D : Sprite2D public override void _Ready() { - + LoadFromSaveData(); if(PlantingPlaceholder.GetChildCount() > 0) _currentPlant = PlantingPlaceholder.GetChild(0); UpdateFieldState(FieldState); @@ -159,17 +159,7 @@ public partial class FieldBehaviour2D : Sprite2D if (!string.IsNullOrEmpty(plantPrefabPath)) { - PackedScene prefab = ResourceLoader.Load(plantPrefabPath, nameof(PackedScene)); - Node2D plant2d = prefab.Instantiate(); - 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(prefabPath, nameof(PackedScene)); + Node2D plant2d = prefab.Instantiate(); + 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 save = Json.ParseString(jsonPayload).AsGodotDictionary(); + + 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 plantDataDict = plantDataVar.AsGodotDictionary(); + + 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(); + } + + } + } + + } + } diff --git a/scripts/CSharp/Common/Farming/FieldService.cs b/scripts/CSharp/Common/Farming/FieldService.cs index 3a6a395..a280d34 100644 --- a/scripts/CSharp/Common/Farming/FieldService.cs +++ b/scripts/CSharp/Common/Farming/FieldService.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Babushka.scripts.CSharp.Common.Services; using Godot; namespace Babushka.scripts.CSharp.Common.Farming; diff --git a/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs b/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs index ab8e6cf..82cdeaf 100644 --- a/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs +++ b/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs @@ -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; } diff --git a/scripts/CSharp/Common/Savegame/SaveData.cs b/scripts/CSharp/Common/Savegame/SaveData.cs index 4e00e30..f7970f2 100644 --- a/scripts/CSharp/Common/Savegame/SaveData.cs +++ b/scripts/CSharp/Common/Savegame/SaveData.cs @@ -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; diff --git a/scripts/CSharp/Common/Savegame/SaveGameManager.cs b/scripts/CSharp/Common/Savegame/SaveGameManager.cs new file mode 100644 index 0000000..9d53244 --- /dev/null +++ b/scripts/CSharp/Common/Savegame/SaveGameManager.cs @@ -0,0 +1,19 @@ +using Godot; + +namespace Babushka.scripts.CSharp.Common.Savegame; + +/// +/// This class manages the loading of the savegame throughout the gameplay flow. +/// +public partial class SaveGameManager : Node +{ + public static SaveGameManager? Instance { get; private set; } = null!; + + public override void _EnterTree() + { + Instance = this; + + SavegameService.Load(); + } + +} \ No newline at end of file diff --git a/scripts/CSharp/Common/Savegame/SaveGameManager.cs.uid b/scripts/CSharp/Common/Savegame/SaveGameManager.cs.uid new file mode 100644 index 0000000..8c4c6e5 --- /dev/null +++ b/scripts/CSharp/Common/Savegame/SaveGameManager.cs.uid @@ -0,0 +1 @@ +uid://c1srnefvhigef diff --git a/scripts/CSharp/Common/Savegame/SavegameService.cs b/scripts/CSharp/Common/Savegame/SavegameService.cs index 4a8c234..9e12b18 100644 --- a/scripts/CSharp/Common/Savegame/SavegameService.cs +++ b/scripts/CSharp/Common/Savegame/SavegameService.cs @@ -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 SaveDatas = new (); + public static Dictionary 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(); + } + catch(Exception e) + { + GD.PrintRich(e.Message); + return; + } + + _loaded = true; } } \ No newline at end of file diff --git a/scripts/CSharp/Common/SceneTransition.cs b/scripts/CSharp/Common/SceneTransition.cs index d36a488..7709acb 100644 --- a/scripts/CSharp/Common/SceneTransition.cs +++ b/scripts/CSharp/Common/SceneTransition.cs @@ -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;