From 0de6bc0c04a17676ecf12baf8fa4fba85df82aa8 Mon Sep 17 00:00:00 2001 From: kziolkowski Date: Sat, 17 May 2025 18:23:35 +0200 Subject: [PATCH] the most beautiful grid of fields you can imagine --- .../Common/Farming/FarmingControls2D.cs | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/scripts/CSharp/Common/Farming/FarmingControls2D.cs b/scripts/CSharp/Common/Farming/FarmingControls2D.cs index f7b1952..ee9dc3e 100644 --- a/scripts/CSharp/Common/Farming/FarmingControls2D.cs +++ b/scripts/CSharp/Common/Farming/FarmingControls2D.cs @@ -67,8 +67,10 @@ public partial class FarmingControls2D : Node2D public override void _Input(InputEvent @event) { Vector2 mousePosition = _camera.GetGlobalMousePosition(); - - Vector2I adjustedPosition = new Vector2I(AdjustValue(mousePosition.X), AdjustValue(mousePosition.Y)); + Vector2I mousePositionInteger = (Vector2I) mousePosition; + GD.Print(mousePosition); + Vector2I adjustedPosition = AdjustValue(mousePositionInteger, new Vector2I(735, 651)); + GD.Print(adjustedPosition); if (@event.IsActionPressed("click") && _toolId == 0) { @@ -94,15 +96,11 @@ public partial class FarmingControls2D : Node2D { if(FieldParent == null || _fieldPrefab == null) return; - - bool fieldAllowed = FieldParent.FieldAllowed(); - GD.Print($"Field allowed at {fieldPosition}: {fieldAllowed}"); + // only try to instantiate a field if you're in the allowed area - if (!fieldAllowed) + if (!FieldParent.FieldAllowed()) return; - - // only instantiate a field if there isn't one already. if(FieldParent.Get(fieldPosition) == null) { @@ -123,6 +121,14 @@ public partial class FarmingControls2D : Node2D private int AdjustValue(float value) { - return (int) Mathf.Floor(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); } } \ No newline at end of file