🚧 reworked farming system to work with indices instead of positions
This commit is contained in:
@@ -9,17 +9,16 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
[Export] private VariableResource _sceneKeyProvider;
|
||||
[Export] private PackedScene? _fieldPrefab = null!;
|
||||
[Export] private Node2D _movingPlayer;
|
||||
[Export] private Camera2D _camera;
|
||||
[Export] private CpuParticles2D _wateringParticles;
|
||||
[Export] private float _wateringCanParticlesVerticalOffset = 50f;
|
||||
[Export] private Vector2I _fieldOffsetVector = new Vector2I(735, 651);
|
||||
[Export] private Node2D _fieldParent;
|
||||
[Export] private VariableResource _cursorOnField;
|
||||
|
||||
private int _toolId = -1;
|
||||
private bool _wateringCanFilled = false;
|
||||
private bool _canCreateFields = false;
|
||||
|
||||
[Signal] public delegate void WateringFieldEventHandler();
|
||||
|
||||
@@ -59,6 +58,7 @@ public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
if (@event.IsActionPressed("click"))
|
||||
{
|
||||
bool cursorOnField = _cursorOnField.Payload.AsBool();
|
||||
if (_toolId == WateringCanState.WATERING_CAN_ID
|
||||
&& WateringCanState.GetFillState() > 0)
|
||||
{
|
||||
@@ -66,7 +66,7 @@ public partial class FarmingControls2D : Node2D
|
||||
WaterTheField(adjustedPosition);
|
||||
}
|
||||
|
||||
if (_canCreateFields && _toolId == 0)
|
||||
if ( _toolId == 0)
|
||||
{
|
||||
Vector2I adjustedPosition = GetAdjustedMousePosition();
|
||||
MakeField(adjustedPosition);
|
||||
@@ -86,16 +86,6 @@ public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
return input.Snapped(step);
|
||||
}
|
||||
|
||||
public void CanCreateFields()
|
||||
{
|
||||
_canCreateFields = true;
|
||||
}
|
||||
|
||||
public void CannotCreateFields()
|
||||
{
|
||||
_canCreateFields = false;
|
||||
}
|
||||
|
||||
#region WATERING
|
||||
public void FillWateringCan()
|
||||
@@ -108,7 +98,8 @@ public partial class FarmingControls2D : Node2D
|
||||
|
||||
private void WaterTheField(Vector2I fieldPosition)
|
||||
{
|
||||
FieldBehaviour2D? field = FieldService.Instance.TryGet(_sceneKeyProvider.Payload.AsString(), fieldPosition);
|
||||
int potentialFieldIndex = FieldService.Instance.PositionToIndex(fieldPosition);
|
||||
FieldBehaviour2D? field = FieldService.Instance.TryGet(_sceneKeyProvider.Payload.AsString(), potentialFieldIndex);
|
||||
if (field == null || field.FieldState == FieldState.Watered)
|
||||
{
|
||||
return;
|
||||
@@ -123,31 +114,11 @@ public partial class FarmingControls2D : Node2D
|
||||
#endregion
|
||||
|
||||
#region FIELD CREATION
|
||||
private void MakeField(Vector2I fieldPosition)
|
||||
private void MakeField(Vector2I mousePosition)
|
||||
{
|
||||
if(_fieldPrefab == null)
|
||||
return;
|
||||
|
||||
// only instantiate a field if there isn't one already.
|
||||
if(FieldService.Instance.TryGet(_sceneKeyProvider.Payload.AsString(), 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.Instance.TryAddEntry(_sceneKeyProvider.Payload.AsString(), fieldPosition, fields[0] as FieldBehaviour2D);
|
||||
}
|
||||
|
||||
// reposition and reparent the instance
|
||||
field2d.Position = new Vector2(fieldPosition.X, fieldPosition.Y);
|
||||
|
||||
_fieldParent.AddChild(fieldInstance);
|
||||
EmitSignal(SignalName.FieldCreated);
|
||||
}
|
||||
}
|
||||
int potentialFieldIndex = FieldService.Instance.PositionToIndex(mousePosition);
|
||||
if(FieldService.Instance.TryGet(_sceneKeyProvider.Payload.AsString(), potentialFieldIndex) == null)
|
||||
EmitSignal(SignalName.FieldCreated);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user