You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.1 KiB
174 lines
5.1 KiB
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
|
|
|
[GlobalClass]
|
|
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;
|
|
|
|
private int _toolId = -1;
|
|
private bool _wateringCanFilled = false;
|
|
|
|
|
|
[Signal] public delegate void WateringFieldEventHandler();
|
|
|
|
#region Tools
|
|
|
|
/// <summary>
|
|
/// If no tool has been set, then set the current tool to the incoming tool id.
|
|
/// </summary>
|
|
/// <param name="toolId">The id of the tool to activate if possible</param>
|
|
/// <returns>If the tool in question was activated.</returns>
|
|
public bool TryActivateTool(int toolId)
|
|
{
|
|
bool activate;
|
|
|
|
//if our hands are empty, activate
|
|
if (toolId == -1)
|
|
{
|
|
activate = false;
|
|
}
|
|
else
|
|
{
|
|
activate = true;
|
|
}
|
|
|
|
_toolId = activate ? toolId : -1;
|
|
|
|
WateringCanState.SetActive(_toolId == WateringCanState.WATERING_CAN_ID);
|
|
|
|
return activate;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (@event.IsActionPressed("click")
|
|
&& _toolId == WateringCanState.WATERING_CAN_ID
|
|
&& WateringCanState.GetFillState() > 0)
|
|
{
|
|
Vector2I adjustedPosition = GetAdjustedMousePosition();
|
|
WaterTheField(adjustedPosition);
|
|
}
|
|
}
|
|
|
|
private Vector2I GetAdjustedMousePosition()
|
|
{
|
|
Vector2 mousePosition = _camera.GetGlobalMousePosition();
|
|
Vector2I mousePositionInteger = (Vector2I) mousePosition;
|
|
Vector2I adjustedPosition = AdjustValue(mousePositionInteger, new Vector2I(735, 651));
|
|
return adjustedPosition;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called by the allowed farming area collision area 2d.
|
|
/// </summary>
|
|
/// <param name="node"></param>
|
|
/// <param name="inputEvent"></param>
|
|
/// <param name="shapeIndex"></param>
|
|
public void InputEventPressedOn(Node node, InputEvent inputEvent, int shapeIndex)
|
|
{
|
|
if (!inputEvent.IsPressed())
|
|
{
|
|
GD.Print("Input Event is not pressed." );
|
|
return;
|
|
}
|
|
|
|
if (!inputEvent.IsActionPressed("click"))
|
|
return;
|
|
|
|
if (inputEvent is InputEventMouseButton inputEventMouseButton)
|
|
{
|
|
GD.Print("Input Event is InputEventMouseButton." );
|
|
if (!inputEventMouseButton.Pressed)
|
|
{
|
|
GD.Print("Input Event Mouse Button is not pressed." );
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
GD.Print("Other Input Event registered." );
|
|
return;
|
|
}
|
|
|
|
GD.Print("Current tool id: " + _toolId );
|
|
if (_toolId == 0)
|
|
{
|
|
GD.Print("Trying to create field." );
|
|
Vector2I adjustedPosition = GetAdjustedMousePosition();
|
|
MakeField(adjustedPosition);
|
|
}
|
|
}
|
|
|
|
#region WATERING
|
|
public void FillWateringCan()
|
|
{
|
|
if (_toolId == WateringCanState.WATERING_CAN_ID)
|
|
{
|
|
WateringCanState.Fill();
|
|
}
|
|
}
|
|
|
|
private void WaterTheField(Vector2I fieldPosition)
|
|
{
|
|
FieldBehaviour2D field = FieldService.Get(fieldPosition);
|
|
if (field == null || field.FieldState == FieldState.Watered)
|
|
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)
|
|
return;
|
|
|
|
// only instantiate a field if there isn't one already.
|
|
if(FieldService.Get(fieldPosition) == null)
|
|
{
|
|
Node fieldInstance = _fieldPrefab.Instantiate();
|
|
if (fieldInstance is Node2D field2d)
|
|
{
|
|
// add dictionary entry for the field
|
|
Array<Node> fields = field2d.FindChildren("*", nameof(FieldBehaviour2D));
|
|
if (fields.Count > 0)
|
|
FieldService.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour2D);
|
|
|
|
// reposition and reparent the instance
|
|
field2d.Position = new Vector2(fieldPosition.X, fieldPosition.Y);;
|
|
FieldService.AddChild(fieldInstance);
|
|
}
|
|
}
|
|
}
|
|
|
|
private int AdjustValue(float value)
|
|
{
|
|
float adjustedValue = value / 500;
|
|
adjustedValue = Mathf.RoundToInt(adjustedValue);
|
|
adjustedValue *= 500;
|
|
return (int)adjustedValue;
|
|
}
|
|
|
|
private Vector2I AdjustValue(Vector2I input, Vector2I step)
|
|
{
|
|
return input.Snapped(step);
|
|
}
|
|
|
|
#endregion
|
|
} |