Fixed field creation bug

This commit is contained in:
2025-07-06 10:57:38 +02:00
parent a54003c658
commit 09ef508f49
5 changed files with 70 additions and 36 deletions
@@ -52,23 +52,64 @@ public partial class FarmingControls2D : Node2D
public override void _Input(InputEvent @event)
{
Vector2 mousePosition = _camera.GetGlobalMousePosition();
Vector2I mousePositionInteger = (Vector2I) mousePosition;
Vector2I adjustedPosition = AdjustValue(mousePositionInteger, new Vector2I(735, 651));
if (@event.IsActionPressed("click") && _toolId == 0)
{
MakeField(adjustedPosition);
}
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()
{
@@ -98,10 +139,6 @@ public partial class FarmingControls2D : Node2D
if(FieldService == null || _fieldPrefab == null)
return;
// only try to instantiate a field if you're in the allowed area
if (!FieldService.FieldAllowed())
return;
// only instantiate a field if there isn't one already.
if(FieldService.Get(fieldPosition) == null)
{
+13 -16
View File
@@ -7,27 +7,24 @@ namespace Babushka.scripts.CSharp.Common.Farming;
public partial class FieldService2D : Node2D
{
[Export] private Dictionary<Vector2I, FieldBehaviour2D> fields = new Dictionary<Vector2I, FieldBehaviour2D>();
private bool _fieldAllowed = false;
[Export] private Area2D _allowedArea;
[Signal] public delegate void FieldCreatedEventHandler();
//Validate
public void MouseEnteredAllowedArea()
{
_fieldAllowed = true;
}
public void MouseExitedAllowedArea()
{
_fieldAllowed = false;
}
public bool FieldAllowed()
/*
public override void _PhysicsProcess(double delta)
{
return _fieldAllowed;
var spaceState = GetWorld2D().DirectSpaceState;
// use global coordinates, not local to node
var query = PhysicsRayQueryParameters2D.Create(GetGlobalMousePosition(), new Vector3(0,0,-1),
CollisionMask, [GetRid()]);
var result = spaceState.IntersectRay(query);
if (result.Count > 0)
GD.Print("Hit at point: ", result["position"]);
}
*/
//Create
public bool TryAddEntry(Vector2I key, FieldBehaviour2D field)