🚧 WIP trying to fix a bug that won't let me have more than 9 fields. Dont ask me why.
This commit is contained in:
@@ -13,6 +13,7 @@ public partial class FarmingControls2D : Node2D
|
||||
[Export] private Camera2D _camera;
|
||||
[Export] private CpuParticles2D _wateringParticles;
|
||||
[Export] private float _wateringCanParticlesVerticalOffset = 50f;
|
||||
[Export] private Vector2I _fieldOffsetVector = new Vector2I(735, 651);
|
||||
|
||||
private int _toolId = -1;
|
||||
private bool _wateringCanFilled = false;
|
||||
@@ -20,6 +21,8 @@ public partial class FarmingControls2D : Node2D
|
||||
|
||||
[Signal] public delegate void WateringFieldEventHandler();
|
||||
|
||||
[Signal] public delegate void FieldCreatedEventHandler();
|
||||
|
||||
#region Tools
|
||||
|
||||
/// <summary>
|
||||
@@ -52,13 +55,22 @@ public partial class FarmingControls2D : Node2D
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event.IsActionPressed("click")
|
||||
&& _toolId == WateringCanState.WATERING_CAN_ID
|
||||
&& WateringCanState.GetFillState() > 0)
|
||||
if (@event.IsActionPressed("click"))
|
||||
{
|
||||
GD.Print("Trying to use the watering can.");
|
||||
Vector2I adjustedPosition = GetAdjustedMousePosition();
|
||||
WaterTheField(adjustedPosition);
|
||||
if (_toolId == WateringCanState.WATERING_CAN_ID
|
||||
&& WateringCanState.GetFillState() > 0)
|
||||
{
|
||||
GD.Print("Trying to use the watering can.");
|
||||
Vector2I adjustedPosition = GetAdjustedMousePosition();
|
||||
WaterTheField(adjustedPosition);
|
||||
}
|
||||
|
||||
if (_toolId == 0)
|
||||
{
|
||||
GD.Print("Trying to create a field.");
|
||||
Vector2I adjustedPosition = GetAdjustedMousePosition();
|
||||
MakeField(adjustedPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +78,7 @@ public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
Vector2 mousePosition = _camera.GetGlobalMousePosition();
|
||||
Vector2I mousePositionInteger = (Vector2I) mousePosition;
|
||||
Vector2I adjustedPosition = AdjustValue(mousePositionInteger, new Vector2I(735, 651));
|
||||
Vector2I adjustedPosition = AdjustValue(mousePositionInteger, _fieldOffsetVector);
|
||||
return adjustedPosition;
|
||||
}
|
||||
|
||||
@@ -78,6 +90,9 @@ public partial class FarmingControls2D : Node2D
|
||||
/// <param name="shapeIndex"></param>
|
||||
public void InputEventPressedOn(Node node, InputEvent inputEvent, int shapeIndex)
|
||||
{
|
||||
// Bug is here: Whenever I use the collider to limit the farming area, Godot crashes after 10 fields.
|
||||
/*
|
||||
GD.Print($"Input event registered on {node.Name} as {inputEvent.GetType().Name}.");
|
||||
if (!inputEvent.IsPressed())
|
||||
{
|
||||
return;
|
||||
@@ -103,6 +118,7 @@ public partial class FarmingControls2D : Node2D
|
||||
Vector2I adjustedPosition = GetAdjustedMousePosition();
|
||||
MakeField(adjustedPosition);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
#region WATERING
|
||||
@@ -152,18 +168,11 @@ public partial class FarmingControls2D : Node2D
|
||||
// reposition and reparent the instance
|
||||
field2d.Position = new Vector2(fieldPosition.X, fieldPosition.Y);;
|
||||
FieldService.Instance.AddChild(fieldInstance);
|
||||
EmitSignal(SignalName.FieldCreated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -5,37 +5,39 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
public partial class FieldService : Node
|
||||
{
|
||||
private Dictionary<string, FieldsInScene>? outerDict;
|
||||
|
||||
[Signal] public delegate void FieldCreatedEventHandler();
|
||||
|
||||
private Dictionary<string, FieldsInScene>? _outerDict = null!;
|
||||
public static FieldService Instance { get; private set; } = null!;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
outerDict = new Dictionary<string, FieldsInScene>();
|
||||
_outerDict = new Dictionary<string, FieldsInScene>();
|
||||
}
|
||||
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Instance = null;
|
||||
_outerDict = null;
|
||||
}
|
||||
|
||||
|
||||
//Create
|
||||
public bool TryAddEntry(string sceneName, Vector2I position, FieldBehaviour2D field)
|
||||
{
|
||||
if (outerDict != null )
|
||||
if (_outerDict != null )
|
||||
{
|
||||
FieldsInScene fieldsInScene;
|
||||
bool outerDictEntryExists = outerDict.TryGetValue(sceneName, out fieldsInScene);
|
||||
FieldsInScene innerDict;
|
||||
bool outerDictEntryExists = _outerDict.TryGetValue(sceneName, out innerDict);
|
||||
|
||||
if (!outerDictEntryExists)
|
||||
{
|
||||
fieldsInScene = new FieldsInScene();
|
||||
outerDict.Add(sceneName, fieldsInScene);
|
||||
innerDict = new FieldsInScene();
|
||||
_outerDict.Add(sceneName, innerDict);
|
||||
}
|
||||
|
||||
if (!fieldsInScene.fields.ContainsKey(position))
|
||||
if (!innerDict.fields.ContainsKey(position))
|
||||
{
|
||||
fieldsInScene.fields.Add(position, field);
|
||||
EmitSignal(SignalName.FieldCreated);
|
||||
innerDict.fields.Add(position, field);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -45,13 +47,15 @@ public partial class FieldService : Node
|
||||
// Read
|
||||
public FieldBehaviour2D? TryGet(string key, Vector2I fieldPosition)
|
||||
{
|
||||
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.TryGetValue(fieldPosition, out FieldBehaviour2D? fieldInstance))
|
||||
{
|
||||
GD.Print("Getting field...");
|
||||
return fieldInstance;
|
||||
}
|
||||
}
|
||||
GD.Print($"No field found for key: {key} and position: {fieldPosition} ");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -59,7 +63,7 @@ public partial class FieldService : Node
|
||||
//Update
|
||||
public void UpdateEntry(string key, Vector2I fieldPosition, FieldBehaviour2D state)
|
||||
{
|
||||
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
@@ -76,7 +80,7 @@ public partial class FieldService : Node
|
||||
|
||||
public void RemoveEntry(string key, Vector2I fieldPosition)
|
||||
{
|
||||
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user