From c288af296c69652cc7ea5fd546e489256075031f Mon Sep 17 00:00:00 2001 From: kziolkowski Date: Tue, 2 Dec 2025 17:44:44 +0100 Subject: [PATCH] :sparkles: made plants only grow when watered --- scenes/Babushka_scene_farm_outside_2d.tscn | 1 + .../CSharp/Common/Farming/FieldBehaviour2D.cs | 70 ++++++++++++++----- .../CSharp/Common/Farming/PlantBehaviour2D.cs | 53 ++++++++------ 3 files changed, 84 insertions(+), 40 deletions(-) diff --git a/scenes/Babushka_scene_farm_outside_2d.tscn b/scenes/Babushka_scene_farm_outside_2d.tscn index cb14ed0..cb2e181 100644 --- a/scenes/Babushka_scene_farm_outside_2d.tscn +++ b/scenes/Babushka_scene_farm_outside_2d.tscn @@ -1061,6 +1061,7 @@ position = Vector2(145.5, -224) shape = SubResource("RectangleShape2D_0sfl7") [node name="InteractionArea" parent="YSorted/Well" instance=ExtResource("27_klb81")] +_id = 1 metadata/SaveID = "b8f7b7fe-e057-4974-ba12-9134722998de" [node name="CollisionShape3D" parent="YSorted/Well/InteractionArea/Area2D" index="0"] diff --git a/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs b/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs index 46de1e1..832fc2b 100644 --- a/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs +++ b/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs @@ -59,6 +59,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; } @@ -91,7 +92,7 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable base._Ready(); } - public void UpdateFieldState(FieldState state) + public void UpdateFieldState(FieldState state, bool updateSaveAfter = true) { switch (state) { @@ -119,18 +120,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(); + } } } @@ -198,6 +206,9 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable #region SAVE AND LOAD + /// + /// Update save data as prep for scene transition (when data is saved and loaded from disk). + /// public void UpdateSaveData() { var payloadData = new Dictionary @@ -211,7 +222,8 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable "plant_data", new Dictionary() { { "prefab_path", _currentPlant.PrefabPath }, - { "plant_start_day", _currentPlant.DayPlanted } + { "plant_start_day", _currentPlant.DayPlanted }, + { "plant_watered_days", _currentPlant.DaysWatered } } ); } @@ -220,6 +232,9 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable SavegameService.AppendDataToSave(id, payloadData); } + /// + /// Loads on scene enter. + /// public void LoadFromSaveData() { // Get field and plant data @@ -229,18 +244,7 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable 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 plantDataDict = plantDataVar.AsGodotDictionary(); @@ -256,7 +260,39 @@ public partial class FieldBehaviour2D : Sprite2D, ISaveable if (plantDataDict.TryGetValue("plant_start_day", out Variant plantStartDay) && _currentPlant != null) { _currentPlant.DayPlanted = plantStartDay.AsInt32(); - GD.Print($"Current plant {_currentPlant.Name} was planted on day: {_currentPlant.DayPlanted}"); + } + if (plantDataDict.TryGetValue("plant_watered_days", out Variant plantDaysWatered) && _currentPlant != null) + { + _currentPlant.DaysWatered = plantDaysWatered.AsInt32(); + } + + // get field state + if (save.TryGetValue("field_state", out Variant fieldStateVar)) + { + int fieldStateInt = fieldStateVar.AsInt32(); + + + // if the field has been unlocked, make it visible. + if (fieldStateInt != 0) + { + Visible = true; + + // 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); + } } } diff --git a/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs b/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs index fcba992..f28e87e 100644 --- a/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs +++ b/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs @@ -34,6 +34,7 @@ public partial class PlantBehaviour2D : Node2D private bool _calledOnReady = false; private int _dayPlanted; private int _currentDay; + private int _daysWatered; public PlantState State { @@ -66,28 +67,6 @@ public partial class PlantBehaviour2D : Node2D DayPlanted++; } - private void DaysGrowingChanged() - { - int _daysGrowing = _currentDay - _dayPlanted; - GD.Print($"Plant {Name} is growing for {_daysGrowing}. Day Planted was {_dayPlanted} and the current day is {_currentDay}."); - int lifecycle = _lifecycle.Payload.AsInt32(); - Debug.Assert(lifecycle > 0); - - float growth = (float)_daysGrowing / lifecycle; - int growthFloor = Mathf.FloorToInt(growth); - - _state = growthFloor switch - { - 0 => PlantState.None, - 1 => PlantState.Planted, - 2 => PlantState.SmallPlant, - _ => PlantState.BigPlant - }; - - _calledOnReady = true; - Grow(); - } - public string PrefabPath => _prefabPath; /// @@ -99,6 +78,15 @@ public partial class PlantBehaviour2D : Node2D set => _field = value; } + public int DaysWatered + { + get => _daysWatered; + set + { + _daysWatered = value; + } + } + public override void _Ready() { if (_state == PlantState.None) @@ -113,13 +101,32 @@ public partial class PlantBehaviour2D : Node2D GrowPlant(); } } + + private void DaysGrowingChanged() + { + int lifecycle = _lifecycle.Payload.AsInt32(); + Debug.Assert(lifecycle > 0); + + float growth = (float)_daysWatered / lifecycle; + int growthFloor = Mathf.FloorToInt(growth); + + _state = growthFloor switch + { + 0 => PlantState.None, + 1 => PlantState.Planted, + 2 => PlantState.SmallPlant, + _ => PlantState.BigPlant + }; + + _calledOnReady = true; + Grow(); + } public void Grow() { GrowPlant(); } - /// /// Transitions the plant to its next growth stage. ///