|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
using System;
|
|
|
|
|
using Babushka.scripts.CSharp.Common.CharacterControls;
|
|
|
|
|
using Babushka.scripts.CSharp.Common.DayAndNight;
|
|
|
|
|
using Babushka.scripts.CSharp.Common.Inventory;
|
|
|
|
|
using Babushka.scripts.CSharp.Common.Savegame;
|
|
|
|
|
using Babushka.scripts.CSharp.Low_Code.Events;
|
|
|
|
|
@ -45,8 +46,11 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
|
|
|
|
|
private bool _canPlant;
|
|
|
|
|
private bool _canWater;
|
|
|
|
|
private int _currentDay;
|
|
|
|
|
|
|
|
|
|
private PlantBehaviour2D? _currentPlant;
|
|
|
|
|
|
|
|
|
|
private const string DAY_COUNTER_SAVE_ID = "12c6da2e-fc71-4281-a04a-dfd3c7943975";
|
|
|
|
|
|
|
|
|
|
[Signal] public delegate void PlantedEventHandler();
|
|
|
|
|
|
|
|
|
|
@ -56,6 +60,7 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
_canPlant = (FieldState == FieldState.Tilled || FieldState == FieldState.Watered) && _seedsActive;
|
|
|
|
|
// fieldstate == tilled && watering can ausgewählt
|
|
|
|
|
_canWater = (FieldState == FieldState.Tilled || FieldState == FieldState.Planted) && _wateringCanActive;
|
|
|
|
|
|
|
|
|
|
FieldInteractionArea.IsActive = _canPlant || _canWater;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -71,9 +76,13 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
UpdateInteractionArea();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
|
public override void _EnterTree()
|
|
|
|
|
{
|
|
|
|
|
LoadFromSaveData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
|
|
|
|
if(PlantingPlaceholder.GetChildCount() > 0)
|
|
|
|
|
_currentPlant = PlantingPlaceholder.GetChild<PlantBehaviour2D>(0);
|
|
|
|
|
UpdateFieldState(FieldState);
|
|
|
|
|
@ -84,7 +93,7 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
base._Ready();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateFieldState(FieldState state)
|
|
|
|
|
public void UpdateFieldState(FieldState state, bool updateSaveAfter = true)
|
|
|
|
|
{
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
@ -112,18 +121,25 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
UpdateInteractionArea();
|
|
|
|
|
UpdateSaveData();
|
|
|
|
|
if(updateSaveAfter)
|
|
|
|
|
UpdateSaveData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Water()
|
|
|
|
|
{
|
|
|
|
|
if (WateringCanState.GetFillState() > 0)
|
|
|
|
|
if (WateringCanState.GetFillState() > 0 && FieldState != FieldState.Watered)
|
|
|
|
|
{
|
|
|
|
|
UpdateFieldState(FieldState.Watered);
|
|
|
|
|
_wateringParticles.Emitting = true;
|
|
|
|
|
WateringCanState.Water();
|
|
|
|
|
_wateringEvent.Raise();
|
|
|
|
|
|
|
|
|
|
if (_currentPlant != null)
|
|
|
|
|
{
|
|
|
|
|
_currentPlant.DaysWatered++;
|
|
|
|
|
UpdateSaveData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -166,6 +182,16 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlantPrefab(string prefabPath)
|
|
|
|
|
{
|
|
|
|
|
InstantiatePlant(prefabPath);
|
|
|
|
|
|
|
|
|
|
if (_currentPlant != null)
|
|
|
|
|
{
|
|
|
|
|
_currentPlant.DayPlanted = _currentDay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstantiatePlant(string prefabPath)
|
|
|
|
|
{
|
|
|
|
|
PackedScene prefab = ResourceLoader.Load<PackedScene>(prefabPath, nameof(PackedScene));
|
|
|
|
|
Node2D plant2d = prefab.Instantiate<Node2D>();
|
|
|
|
|
@ -181,11 +207,15 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
|
|
|
|
|
#region SAVE AND LOAD
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update save data as prep for scene transition (when data is saved and loaded from disk).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UpdateSaveData()
|
|
|
|
|
{
|
|
|
|
|
var payloadData = new Dictionary<string, Variant>
|
|
|
|
|
{
|
|
|
|
|
{ "field_state", (int)FieldState }
|
|
|
|
|
{ "field_state", (int)FieldState },
|
|
|
|
|
{ "day_count_on_last_exit", _currentDay}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (_currentPlant != null)
|
|
|
|
|
@ -194,8 +224,8 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
"plant_data", new Dictionary<string, Variant>()
|
|
|
|
|
{
|
|
|
|
|
{ "prefab_path", _currentPlant.PrefabPath },
|
|
|
|
|
{ "plant_state", (int)_currentPlant.State },
|
|
|
|
|
{ "plant_days_growing", _currentPlant.DaysGrowing }
|
|
|
|
|
{ "plant_start_day", _currentPlant.DayPlanted },
|
|
|
|
|
{ "plant_watered_days", _currentPlant.DaysWatered }
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
@ -204,50 +234,91 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
|
|
|
|
SavegameService.AppendDataToSave(id, payloadData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads on scene enter.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void LoadFromSaveData()
|
|
|
|
|
{
|
|
|
|
|
// Get field and plant data
|
|
|
|
|
string id = _saveIdHolder.GetMeta("SaveID").AsString();
|
|
|
|
|
|
|
|
|
|
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get plant first because it's also relevant for the field state
|
|
|
|
|
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());
|
|
|
|
|
InstantiatePlant(prefabPathVar.AsString());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (plantDataDict.TryGetValue("plant_start_day", out Variant plantStartDay) && _currentPlant != null)
|
|
|
|
|
{
|
|
|
|
|
_currentPlant.DayPlanted = plantStartDay.AsInt32();
|
|
|
|
|
}
|
|
|
|
|
if (plantDataDict.TryGetValue("plant_watered_days", out Variant plantDaysWatered) && _currentPlant != null)
|
|
|
|
|
{
|
|
|
|
|
_currentPlant.DaysWatered = plantDaysWatered.AsInt32();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get current day count: Load only. Saving the day count is handled on the day and night prefab.
|
|
|
|
|
Dictionary<string, Variant> dayCountSave = SavegameService.GetSaveData(DAY_COUNTER_SAVE_ID);
|
|
|
|
|
|
|
|
|
|
if (plantDataDict.TryGetValue("plant_state", out Variant plantStateVar) && _currentPlant != null)
|
|
|
|
|
if (dayCountSave.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
if (dayCountSave.TryGetValue("payload", out Variant dayCountVar))
|
|
|
|
|
{
|
|
|
|
|
_currentPlant.State = (PlantState) plantStateVar.AsInt32();
|
|
|
|
|
_currentPlant.GrowPlant();
|
|
|
|
|
_currentDay = dayCountVar.AsInt32();
|
|
|
|
|
if (_currentPlant != null)
|
|
|
|
|
{
|
|
|
|
|
_currentPlant.CurrentDayInCalendar = _currentDay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get field state
|
|
|
|
|
if (save.TryGetValue("field_state", out Variant fieldStateVar))
|
|
|
|
|
{
|
|
|
|
|
int fieldStateInt = fieldStateVar.AsInt32();
|
|
|
|
|
|
|
|
|
|
if (plantDataDict.TryGetValue("plant_days_growing", out Variant plantDaysGrowingVar) && _currentPlant != null)
|
|
|
|
|
// if the field has been unlocked, make it visible.
|
|
|
|
|
if (fieldStateInt != 0)
|
|
|
|
|
{
|
|
|
|
|
_currentPlant.DaysGrowing = plantDaysGrowingVar.AsInt32();
|
|
|
|
|
Visible = true;
|
|
|
|
|
|
|
|
|
|
if (save.TryGetValue("day_count_on_last_exit", out Variant lastDayCountVar))
|
|
|
|
|
{
|
|
|
|
|
int lastDayCount = lastDayCountVar.AsInt32();
|
|
|
|
|
|
|
|
|
|
// if day is today, then just use the provided field state as is.
|
|
|
|
|
if (CalendarController.Instance != null && _currentDay != lastDayCount)
|
|
|
|
|
{
|
|
|
|
|
// if the field was watered the day before, set it to tilled or planted.
|
|
|
|
|
if (fieldStateInt == 3)
|
|
|
|
|
{
|
|
|
|
|
if (_currentPlant != null)
|
|
|
|
|
{
|
|
|
|
|
fieldStateInt = 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fieldStateInt = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FieldState = (FieldState) fieldStateInt;
|
|
|
|
|
UpdateFieldState(FieldState, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|