🐛 field creation at the same spot no longer possible, also watering works now.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
@@ -6,14 +7,13 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
[GlobalClass]
|
||||
public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
[Export] private PackedScene _fieldPrefab;
|
||||
[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;
|
||||
|
||||
public FieldService2D FieldService;
|
||||
|
||||
private int _toolId = -1;
|
||||
private bool _wateringCanFilled = false;
|
||||
|
||||
@@ -116,7 +116,7 @@ public partial class FarmingControls2D : Node2D
|
||||
|
||||
private void WaterTheField(Vector2I fieldPosition)
|
||||
{
|
||||
FieldBehaviour2D field = FieldService.Get(fieldPosition);
|
||||
FieldBehaviour2D? field = FieldService.Instance.TryGet(_sceneKeyProvider.Payload.AsString(), fieldPosition);
|
||||
if (field == null || field.FieldState == FieldState.Watered)
|
||||
{
|
||||
GD.Print($"The field at position {fieldPosition} is null!");
|
||||
@@ -135,25 +135,30 @@ public partial class FarmingControls2D : Node2D
|
||||
#region FIELD CREATION
|
||||
private void MakeField(Vector2I fieldPosition)
|
||||
{
|
||||
if(FieldService == null || _fieldPrefab == null)
|
||||
if(_fieldPrefab == null)
|
||||
return;
|
||||
|
||||
// only instantiate a field if there isn't one already.
|
||||
if(FieldService.Get(fieldPosition) == null)
|
||||
if(FieldService.Instance.TryGet(_sceneKeyProvider.Payload.AsString(), fieldPosition) == null)
|
||||
{
|
||||
GD.Print($"FarmingControls: Creating a field at {fieldPosition}");
|
||||
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.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour2D);
|
||||
FieldService.Instance.TryAddEntry(_sceneKeyProvider.Payload.AsString(), fieldPosition, fields[0] as FieldBehaviour2D);
|
||||
|
||||
// reposition and reparent the instance
|
||||
field2d.Position = new Vector2(fieldPosition.X, fieldPosition.Y);;
|
||||
FieldService.AddChild(fieldInstance);
|
||||
FieldService.Instance.AddChild(fieldInstance);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print($"FarmingControls: Could not create a field at {fieldPosition}.");
|
||||
}
|
||||
}
|
||||
|
||||
private int AdjustValue(float value)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
@@ -20,7 +21,6 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
|
||||
|
||||
public Vector2 FieldPosition;
|
||||
public FieldService2D FieldService;
|
||||
|
||||
[Signal] public delegate void PlantedEventHandler();
|
||||
|
||||
|
||||
@@ -3,75 +3,109 @@ using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
public partial class FieldService : Node2D
|
||||
public partial class FieldService : Node
|
||||
{
|
||||
private Dictionary<PackedScene, FieldsInScene>? fieldsPerScene;
|
||||
|
||||
|
||||
|
||||
private Dictionary<string, FieldsInScene>? outerDict;
|
||||
|
||||
[Signal] public delegate void FieldCreatedEventHandler();
|
||||
|
||||
//Create
|
||||
public bool TryAddEntry(PackedScene scene, Vector2I key, FieldBehaviour2D field)
|
||||
public static FieldService Instance { get; private set; } = null!;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
if (!fieldsPerScene.ContainsKey(scene))
|
||||
Instance = this;
|
||||
outerDict = new Dictionary<string, FieldsInScene>();
|
||||
}
|
||||
|
||||
|
||||
//Create
|
||||
public bool TryAddEntry(string sceneName, Vector2I position, FieldBehaviour2D field)
|
||||
{
|
||||
GD.Print($"Fieldservice: Trying to add a new entry. SceneName: {sceneName} Position: {position}.");
|
||||
if (outerDict != null )
|
||||
{
|
||||
FieldsInScene fieldInstance = new FieldsInScene();
|
||||
fieldsPerScene.Add(scene, fieldInstance);
|
||||
EmitSignal(SignalName.FieldCreated);
|
||||
return true;
|
||||
FieldsInScene fieldsInScene;
|
||||
bool outerDictEntryExists = outerDict.TryGetValue(sceneName, out fieldsInScene);
|
||||
|
||||
if (!outerDictEntryExists)
|
||||
{
|
||||
fieldsInScene = new FieldsInScene();
|
||||
outerDict.Add(sceneName, fieldsInScene);
|
||||
}
|
||||
|
||||
if (!fieldsInScene.fields.ContainsKey(position))
|
||||
{
|
||||
fieldsInScene.fields.Add(position, field);
|
||||
EmitSignal(SignalName.FieldCreated);
|
||||
GD.Print("Fieldservice: successfully added field to dictionary.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
GD.Print("Fieldservice: Either no outerdict found or there is already a field here!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read
|
||||
public FieldBehaviour2D Get(PackedScene key, Vector2I fieldPosition)
|
||||
public FieldBehaviour2D? TryGet(string key, Vector2I fieldPosition)
|
||||
{
|
||||
if (fieldsPerScene.TryGetValue(key, out FieldsInScene field))
|
||||
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.TryGetValue(fieldPosition, out FieldBehaviour2D fieldInstance))
|
||||
GD.Print($"Fieldservice: Successfully retrieved fieldsPerScene Dictionary. " +
|
||||
$"OuterDict Count: {outerDict.Count} InnerDict Count {outerDict[key].fields.Count}");
|
||||
if (field.fields.TryGetValue(fieldPosition, out FieldBehaviour2D? fieldInstance))
|
||||
{
|
||||
GD.Print($"Fieldservice: Successfully retrieved fieldInstance at position: {fieldPosition}.");
|
||||
return fieldInstance;
|
||||
}
|
||||
}
|
||||
|
||||
GD.Print("Fieldservice TryGet: No field found.");
|
||||
return null;
|
||||
}
|
||||
|
||||
//todo:
|
||||
// - Make this a singleton
|
||||
// - Let this interact with the FarmingControls and the FieldBehaviours
|
||||
// - Replace FieldService2D and remove it from scenes.
|
||||
|
||||
|
||||
/*
|
||||
//Update
|
||||
public void UpdateEntry(Vector2I fieldPosition, FieldBehaviour2D state)
|
||||
public void UpdateEntry(string key, Vector2I fieldPosition, FieldBehaviour2D state)
|
||||
{
|
||||
GD.Print($"FieldService: Updating entry at {fieldPosition}");
|
||||
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
if (field.fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
field.fields[fieldPosition] = state;
|
||||
}
|
||||
else
|
||||
{
|
||||
TryAddEntry(key, fieldPosition, state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
fields[fieldPosition] = state;
|
||||
}
|
||||
else
|
||||
{
|
||||
TryAddEntry(fieldPosition, state);
|
||||
}
|
||||
}
|
||||
|
||||
//Delete
|
||||
|
||||
public void RemoveEntry(Vector2I fieldPosition)
|
||||
public void RemoveEntry(string key, Vector2I fieldPosition)
|
||||
{
|
||||
if (fields.ContainsKey(fieldPosition))
|
||||
GD.Print($"FieldService: Removing entry at {fieldPosition}.");
|
||||
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
||||
{
|
||||
fields.Remove(fieldPosition);
|
||||
if (field.fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
field.fields.Remove(fieldPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
internal class FieldsInScene
|
||||
{
|
||||
public Dictionary<Vector2I, FieldBehaviour2D> fields;
|
||||
public Dictionary<Vector2I, FieldBehaviour2D?> fields;
|
||||
|
||||
public FieldsInScene()
|
||||
{
|
||||
fields = new Dictionary<Vector2I, FieldBehaviour2D?>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://slo0uydmmvnu
|
||||
@@ -9,7 +9,6 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
public partial class VesnaBehaviour2D : Node
|
||||
{
|
||||
[ExportGroup("Farming")]
|
||||
[Export] private FieldService2D _fieldParent;
|
||||
[Export] private FarmingControls2D _farmingControls;
|
||||
[Export] private PlayerMovement _player2d;
|
||||
[Export] private VesnaAnimations _vesnaAnimations;
|
||||
@@ -26,7 +25,6 @@ public partial class VesnaBehaviour2D : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_farmingControls.FieldService = _fieldParent;
|
||||
_inventoryManager = InventoryManager.Instance;
|
||||
_inventoryInstance = _inventoryManager.playerInventory;
|
||||
_inventoryManager.SlotIndexChanged += HandleInventorySelectedSlotIndexChanged;
|
||||
|
||||
Reference in New Issue
Block a user