Watering fields
This commit is contained in:
@@ -10,8 +10,8 @@ public partial class InteractionArea2D : Node2D
|
||||
[Export] private bool _showLabel = true;
|
||||
[Export] private int _id;
|
||||
|
||||
[Signal]
|
||||
public delegate void InteractedEventHandler(int id);
|
||||
[Signal] public delegate void InteractedToolEventHandler(int id);
|
||||
[Signal] public delegate void InteractedEventHandler();
|
||||
|
||||
public void OnPlayerEntered(Node2D player)
|
||||
{
|
||||
@@ -29,7 +29,8 @@ public partial class InteractionArea2D : Node2D
|
||||
if (@event.IsAction("interact") && @event.IsPressed() && _area.HasOverlappingBodies())
|
||||
{
|
||||
_label.Hide();
|
||||
EmitSignal(SignalName.Interacted, _id);
|
||||
EmitSignal(SignalName.InteractedTool, _id);
|
||||
EmitSignal(SignalName.Interacted);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,18 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
[Export] private Sprite2D _hoeSprite;
|
||||
[Export] private Sprite2D _wateringCanSprite;
|
||||
[Export] private Sprite2D _wateringCanEmptySprite;
|
||||
[Export] private Sprite2D _wateringCanFilledSprite;
|
||||
[Export] private PackedScene _fieldPrefab;
|
||||
[Export] private Node2D _movingPlayer;
|
||||
[Export] private Camera2D _camera;
|
||||
|
||||
public FieldService2D FieldParent;
|
||||
public FieldService2D FieldService;
|
||||
|
||||
private int _toolId = -1;
|
||||
private bool _wateringCanFilled = false;
|
||||
private int _currentWateringCanStep = 0;
|
||||
private int _wateringCanCapacity = 3;
|
||||
|
||||
#region Tools
|
||||
|
||||
@@ -51,7 +55,16 @@ public partial class FarmingControls2D : Node2D
|
||||
_hoeSprite.Visible = activate;
|
||||
break;
|
||||
case 1:
|
||||
_wateringCanSprite.Visible = activate;
|
||||
if (activate)
|
||||
{
|
||||
_wateringCanEmptySprite.Visible = true;
|
||||
GD.Print("Activating empty Watering can sprite.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_wateringCanEmptySprite.Visible = false;
|
||||
_wateringCanFilledSprite.Visible = false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
_toolId = -1;
|
||||
@@ -75,32 +88,54 @@ public partial class FarmingControls2D : Node2D
|
||||
MakeField(adjustedPosition);
|
||||
}
|
||||
|
||||
if (@event.IsActionPressed("click") && _toolId == 1)
|
||||
if (@event.IsActionPressed("click") && _toolId == 1 && _wateringCanFilled)
|
||||
{
|
||||
WaterTheField(adjustedPosition);
|
||||
}
|
||||
}
|
||||
|
||||
public void FillWateringCan(bool fillUp)
|
||||
{
|
||||
if (_toolId == 1 )
|
||||
{
|
||||
GD.Print("Watering can in hand, filling.");
|
||||
_wateringCanEmptySprite.Visible = !fillUp;
|
||||
_wateringCanFilledSprite.Visible = fillUp;
|
||||
_wateringCanFilled = fillUp;
|
||||
}
|
||||
}
|
||||
|
||||
private void WaterTheField(Vector2I fieldPosition)
|
||||
{
|
||||
FieldBehaviour2D field = FieldParent.Get(fieldPosition);
|
||||
if (field == null)
|
||||
FieldBehaviour2D field = FieldService.Get(fieldPosition);
|
||||
if (field == null || field.FieldState == FieldState.Watered)
|
||||
return;
|
||||
|
||||
field.Water();
|
||||
|
||||
if (_currentWateringCanStep < _wateringCanCapacity)
|
||||
{
|
||||
_currentWateringCanStep++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentWateringCanStep = 0;
|
||||
FillWateringCan(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void MakeField(Vector2I fieldPosition)
|
||||
{
|
||||
if(FieldParent == null || _fieldPrefab == null)
|
||||
if(FieldService == null || _fieldPrefab == null)
|
||||
return;
|
||||
|
||||
// only try to instantiate a field if you're in the allowed area
|
||||
if (!FieldParent.FieldAllowed())
|
||||
if (!FieldService.FieldAllowed())
|
||||
return;
|
||||
|
||||
// only instantiate a field if there isn't one already.
|
||||
if(FieldParent.Get(fieldPosition) == null)
|
||||
if(FieldService.Get(fieldPosition) == null)
|
||||
{
|
||||
Node fieldInstance = _fieldPrefab.Instantiate();
|
||||
if (fieldInstance is Node2D field2d)
|
||||
@@ -108,11 +143,11 @@ public partial class FarmingControls2D : Node2D
|
||||
// add dictionary entry for the field
|
||||
Array<Node> fields = field2d.FindChildren("*", nameof(FieldBehaviour2D));
|
||||
if (fields.Count > 0)
|
||||
FieldParent.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour2D);
|
||||
FieldService.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour2D);
|
||||
|
||||
// reposition and reparent the instance
|
||||
field2d.Position = new Vector2(fieldPosition.X, fieldPosition.Y);;
|
||||
FieldParent.AddChild(fieldInstance);
|
||||
FieldService.AddChild(fieldInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public partial class VesnaBehaviour2D : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_farmingControls.FieldParent = _fieldParent;
|
||||
_farmingControls.FieldService = _fieldParent;
|
||||
}
|
||||
|
||||
#region Farming
|
||||
@@ -22,6 +22,14 @@ public partial class VesnaBehaviour2D : Node
|
||||
bool activated = _farmingControls.TryActivateTool(toolId);
|
||||
EmitSignal(SignalName.PickedUpTool, activated, toolId);
|
||||
}
|
||||
|
||||
public void TryFillWateringCan(int toolId)
|
||||
{
|
||||
if (toolId == 1)
|
||||
{
|
||||
_farmingControls.FillWateringCan(true);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user