Finalized new watering can mechanic with vfx and ui

This commit is contained in:
2025-07-05 22:54:52 +02:00
parent 35c7e9a25e
commit fbad33213c
7 changed files with 93 additions and 11 deletions
@@ -10,6 +10,7 @@ public partial class Player2D : CharacterBody2D
[Export] private float _speed = 100f;
[Export] private AnimatedSprite2D _sprite;
[Export] private SceneTree.GroupCallFlags _fieldFlags;
[Export] private CpuParticles2D _wateringParticles;
// -1 means no tool.
private int _toolID = -1;
@@ -156,6 +157,7 @@ public partial class Player2D : CharacterBody2D
_sprite.Animation = "diagonal wateringcan";
_sprite.Play();
_canHandleInput = false;
_wateringParticles.Emitting = true;
Task.Run(DelayedInputHandlerReset);
}
}
@@ -163,6 +165,7 @@ public partial class Player2D : CharacterBody2D
private async Task DelayedInputHandlerReset()
{
await Task.Delay(1000);
_wateringParticles.Emitting = false;
_canHandleInput = true;
}
@@ -9,6 +9,8 @@ public partial class FarmingControls2D : Node2D
[Export] private PackedScene _fieldPrefab;
[Export] private Node2D _movingPlayer;
[Export] private Camera2D _camera;
[Export] private CpuParticles2D _wateringParticles;
[Export] private float _wateringCanParticlesVerticalOffset = 50f;
public FieldService2D FieldService;
@@ -40,6 +42,9 @@ public partial class FarmingControls2D : Node2D
}
_toolId = activate ? toolId : -1;
WateringCanState.SetActive(_toolId == WateringCanState.WATERING_CAN_ID);
return activate;
}
@@ -56,15 +61,18 @@ public partial class FarmingControls2D : Node2D
MakeField(adjustedPosition);
}
if (@event.IsActionPressed("click") && _toolId == 1 && _wateringCanFilled)
if (@event.IsActionPressed("click")
&& _toolId == WateringCanState.WATERING_CAN_ID
&& WateringCanState.GetFillState() > 0)
{
WaterTheField(adjustedPosition);
}
}
#region WATERING
public void FillWateringCan()
{
if (_toolId == 1 )
if (_toolId == WateringCanState.WATERING_CAN_ID)
{
WateringCanState.Fill();
}
@@ -77,10 +85,14 @@ public partial class FarmingControls2D : Node2D
return;
field.Water();
_wateringParticles.GlobalPosition = new Vector2(field.GlobalPosition.X, field.GlobalPosition.Y + _wateringCanParticlesVerticalOffset);
WateringCanState.Water();
EmitSignal(SignalName.WateringField);
}
#endregion
#region FIELD CREATION
private void MakeField(Vector2I fieldPosition)
{
if(FieldService == null || _fieldPrefab == null)
@@ -120,4 +132,6 @@ public partial class FarmingControls2D : Node2D
{
return input.Snapped(step);
}
#endregion
}
@@ -1,29 +1,70 @@
namespace Babushka.scripts.CSharp.Common.Farming;
/// <summary>
/// Holds Information about the current state of the watering can.
/// Since there is only one watering can, we can track this in one central static class.
/// </summary>
public static class WateringCanState
{
private static int _fillstate = 0;
/// <summary>
/// How many fields can be watered with one filling of the watering can.
/// </summary>
public const int MAX_FILLSTATE = 6;
/// <summary>
/// The Tool ID of the watering can. Used to identify it amongst other pickup items (and things that can be held by Vesna).
/// </summary>
public const int WATERING_CAN_ID = 1;
/// <summary>
/// Whether or not the watering can is currently active, i.e. held in hand by Vesna.
/// Triggers animations and ui.
/// </summary>
public static bool Active = false;
/// <summary>
/// Resets the fillstate to the max amount.
/// </summary>
public static void Fill()
{
_fillstate = MAX_FILLSTATE;
}
/// <summary>
/// Called when watering a field. Reduces the current fillstate.
/// </summary>
public static void Water()
{
if(_fillstate > 0)
_fillstate--;
}
/// <summary>
/// Resets the watering can. Equivalent to "Empty" state.
/// </summary>
public static void Reset()
{
_fillstate = 0;
}
/// <summary>
/// Returns the current fill state of the watering can.
/// </summary>
/// <returns></returns>
public static int GetFillState()
{
return _fillstate;
}
/// <summary>
/// Sets the Active state of the watering can, i.e. if it is currently in hand and if the ui should be active.
/// </summary>
/// <param name="active"></param>
public static void SetActive(bool active)
{
Active = active;
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ public partial class WateringCanUi : Node2D
{
for (int i = 0; i < _stages.Length; i++)
{
_stages[i].Visible = i < WateringCanState.GetFillState();
_stages[i].Visible = WateringCanState.Active && i < WateringCanState.GetFillState();
}
}