🚧 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
|
||||
|
||||
@@ -22,6 +22,8 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
[Export] public ItemRepository ItemRepository;
|
||||
[Export] public InteractionArea2D FieldInteractionArea;
|
||||
[Export] public VariableResource _sceneKeyProvider;
|
||||
[Export] private VariableNode _fieldIndex;
|
||||
|
||||
|
||||
public Vector2 FieldPosition;
|
||||
|
||||
@@ -54,7 +56,7 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
public override void _Ready()
|
||||
{
|
||||
UpdateFieldState(FieldState);
|
||||
FieldService.Instance.TryAddEntry(_sceneKeyProvider.Payload.AsString(), (Vector2I) GlobalPosition, this);
|
||||
FieldService.Instance.TryAddEntry(_sceneKeyProvider.Payload.AsString(),_fieldIndex.Payload.AsInt32(), this);
|
||||
int randomIndex = new Random().Next(0, _maskTexture.Length);
|
||||
_maskSprite.Texture = _maskTexture[randomIndex];
|
||||
_outlineSprite.Texture = _maskOutlineTextures[randomIndex];
|
||||
@@ -139,5 +141,6 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -22,9 +22,9 @@ public partial class FieldService : Node
|
||||
|
||||
|
||||
//Create
|
||||
public bool TryAddEntry(string sceneName, Vector2I position, FieldBehaviour2D field)
|
||||
public bool TryAddEntry(string sceneName, int fieldIndex, FieldBehaviour2D field)
|
||||
{
|
||||
GD.Print("Trying to add a field at: " + position);
|
||||
GD.Print("Trying to add a field at: " + fieldIndex);
|
||||
if (_outerDict != null )
|
||||
{
|
||||
FieldsInScene innerDict;
|
||||
@@ -36,21 +36,25 @@ public partial class FieldService : Node
|
||||
_outerDict.Add(sceneName, innerDict);
|
||||
}
|
||||
|
||||
if (!innerDict.fields.ContainsKey(position))
|
||||
if (!innerDict.fields.ContainsKey(fieldIndex))
|
||||
{
|
||||
innerDict.fields.Add(position, field);
|
||||
innerDict.fields.Add(fieldIndex, field);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.PrintErr("Duplicate field at: " + fieldIndex);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read
|
||||
public FieldBehaviour2D? TryGet(string key, Vector2I fieldPosition)
|
||||
public FieldBehaviour2D? TryGet(string key, int fieldIndex)
|
||||
{
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.TryGetValue(fieldPosition, out FieldBehaviour2D? fieldInstance))
|
||||
if (field.fields.TryGetValue(fieldIndex, out FieldBehaviour2D? fieldInstance))
|
||||
{
|
||||
return fieldInstance;
|
||||
}
|
||||
@@ -60,42 +64,47 @@ public partial class FieldService : Node
|
||||
|
||||
|
||||
//Update
|
||||
public void UpdateEntry(string key, Vector2I fieldPosition, FieldBehaviour2D state)
|
||||
public void UpdateEntry(string key, int fieldIndex, FieldBehaviour2D state)
|
||||
{
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.ContainsKey(fieldPosition))
|
||||
if (field.fields.ContainsKey(fieldIndex))
|
||||
{
|
||||
field.fields[fieldPosition] = state;
|
||||
field.fields[fieldIndex] = state;
|
||||
}
|
||||
else
|
||||
{
|
||||
TryAddEntry(key, fieldPosition, state);
|
||||
TryAddEntry(key, fieldIndex, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Delete
|
||||
|
||||
public void RemoveEntry(string key, Vector2I fieldPosition)
|
||||
public void RemoveEntry(string key, int fieldIndex)
|
||||
{
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.ContainsKey(fieldPosition))
|
||||
if (field.fields.ContainsKey(fieldIndex))
|
||||
{
|
||||
field.fields.Remove(fieldPosition);
|
||||
field.fields.Remove(fieldIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int PositionToIndex(Vector2 position)
|
||||
{
|
||||
// some awesome code here
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal class FieldsInScene
|
||||
{
|
||||
public Dictionary<Vector2I, FieldBehaviour2D?> fields;
|
||||
public Dictionary<int, FieldBehaviour2D?> fields;
|
||||
|
||||
public FieldsInScene()
|
||||
{
|
||||
fields = new Dictionary<Vector2I, FieldBehaviour2D?>();
|
||||
fields = new Dictionary<int, FieldBehaviour2D?>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
|
||||
public partial class VariableNode : Node
|
||||
{
|
||||
[Export] public Variant Payload { get; set; }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://j2mhvb45egej
|
||||
Reference in New Issue
Block a user