🚧 WIP binding the plant growth to the day count in the savefile
This commit is contained in:
@@ -14,6 +14,7 @@ public partial class CalendarController : Node
|
||||
int days = _dayCounter.Payload.AsInt32();
|
||||
days++;
|
||||
_dayCounter.Payload = days;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,7 +195,7 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
||||
{
|
||||
{ "prefab_path", _currentPlant.PrefabPath },
|
||||
{ "plant_state", (int)_currentPlant.State },
|
||||
{ "plant_days_growing", _currentPlant.DaysGrowing }
|
||||
{ "plant_start_day", _currentPlant.DayPlanted }
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -243,11 +243,18 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable
|
||||
_currentPlant.GrowPlant();
|
||||
}
|
||||
|
||||
if (plantDataDict.TryGetValue("plant_days_growing", out Variant plantDaysGrowingVar) && _currentPlant != null)
|
||||
if (plantDataDict.TryGetValue("plant_start_day", out Variant plantDaysGrowingVar) && _currentPlant != null)
|
||||
{
|
||||
_currentPlant.DaysGrowing = plantDaysGrowingVar.AsInt32();
|
||||
_currentPlant.DayPlanted = plantDaysGrowingVar.AsInt32();
|
||||
}
|
||||
|
||||
|
||||
if (_currentPlant != null)
|
||||
{
|
||||
//todo: find out how to load the current day from save and provide it to the plant script
|
||||
_currentPlant.CurrentDayInCalendar = GD.RandRange(0, 12);
|
||||
GD.Print($"Set current Day in calendar for plant {_currentPlant.Name} to {_currentPlant.CurrentDayInCalendar}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
@@ -9,7 +12,8 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
/// </summary>
|
||||
public partial class PlantBehaviour2D : Node2D
|
||||
{
|
||||
[Export] private string _prefabPath;
|
||||
|
||||
[ExportGroup("Plant State")]
|
||||
[Export] private Sprite2D[] _seeds;
|
||||
[Export] private Sprite2D[] _smallPlants;
|
||||
[Export] private Sprite2D[] _bigPlants;
|
||||
@@ -19,11 +23,17 @@ public partial class PlantBehaviour2D : Node2D
|
||||
[Export] private ItemOnGround2D _harvestablePlant;
|
||||
[Export] private CpuParticles2D _magicEffect;
|
||||
[Export] private bool _magicWordNeeded = true;
|
||||
|
||||
[ExportGroup("PlantConfig")]
|
||||
[Export] private string _prefabPath;
|
||||
[Export] private VariableNode _lifecycle;
|
||||
|
||||
private string _magicWordDialogicEventName = "MagicWord";
|
||||
private Sprite2D? _currentPlantSprite = null;
|
||||
private bool _magicWordSaid = false;
|
||||
private bool _calledOnReady = false;
|
||||
private int _dayPlanted;
|
||||
private int _currentDay;
|
||||
|
||||
public PlantState State
|
||||
{
|
||||
@@ -31,8 +41,45 @@ public partial class PlantBehaviour2D : Node2D
|
||||
set => _state = value;
|
||||
}
|
||||
|
||||
public int DaysGrowing { get; set; }
|
||||
|
||||
public int DayPlanted { get; set; }
|
||||
|
||||
public int CurrentDayInCalendar
|
||||
{
|
||||
get => _currentDay;
|
||||
set
|
||||
{
|
||||
if (_currentDay == value) return;
|
||||
_currentDay = value;
|
||||
DaysGrowingChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public void IncrementDaysGrowing()
|
||||
{
|
||||
DayPlanted++;
|
||||
}
|
||||
|
||||
private void DaysGrowingChanged()
|
||||
{
|
||||
int _daysGrowing = _currentDay - _dayPlanted;
|
||||
int lifecycle = _lifecycle.Payload.AsInt32();
|
||||
Debug.Assert(lifecycle > 0);
|
||||
|
||||
float growth = (float)_daysGrowing / lifecycle;
|
||||
int growthFloor = Mathf.FloorToInt(growth);
|
||||
|
||||
_state = growthFloor switch
|
||||
{
|
||||
0 => PlantState.Planted,
|
||||
1 => PlantState.SmallPlant,
|
||||
2 => PlantState.BigPlant,
|
||||
_ => PlantState.Ready
|
||||
};
|
||||
|
||||
_calledOnReady = true;
|
||||
Grow();
|
||||
}
|
||||
|
||||
public string PrefabPath => _prefabPath;
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user