|
|
|
@ -3,75 +3,109 @@ using Godot;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static FieldService Instance { get; private set; } = null!;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void _EnterTree()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Instance = this;
|
|
|
|
|
|
|
|
outerDict = new Dictionary<string, FieldsInScene>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Signal] public delegate void FieldCreatedEventHandler();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Create
|
|
|
|
//Create
|
|
|
|
public bool TryAddEntry(PackedScene scene, Vector2I key, FieldBehaviour2D field)
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!fieldsPerScene.ContainsKey(scene))
|
|
|
|
FieldsInScene fieldsInScene;
|
|
|
|
|
|
|
|
bool outerDictEntryExists = outerDict.TryGetValue(sceneName, out fieldsInScene);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!outerDictEntryExists)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
FieldsInScene fieldInstance = new FieldsInScene();
|
|
|
|
fieldsInScene = new FieldsInScene();
|
|
|
|
fieldsPerScene.Add(scene, fieldInstance);
|
|
|
|
outerDict.Add(sceneName, fieldsInScene);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!fieldsInScene.fields.ContainsKey(position))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fieldsInScene.fields.Add(position, field);
|
|
|
|
EmitSignal(SignalName.FieldCreated);
|
|
|
|
EmitSignal(SignalName.FieldCreated);
|
|
|
|
|
|
|
|
GD.Print("Fieldservice: successfully added field to dictionary.");
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GD.Print("Fieldservice: Either no outerdict found or there is already a field here!");
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read
|
|
|
|
// Read
|
|
|
|
public FieldBehaviour2D Get(PackedScene key, Vector2I fieldPosition)
|
|
|
|
public FieldBehaviour2D? TryGet(string key, Vector2I fieldPosition)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (fieldsPerScene.TryGetValue(key, out FieldsInScene field))
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (field.fields.TryGetValue(fieldPosition, out FieldBehaviour2D fieldInstance))
|
|
|
|
GD.Print($"Fieldservice: Successfully retrieved fieldInstance at position: {fieldPosition}.");
|
|
|
|
return fieldInstance;
|
|
|
|
return fieldInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GD.Print("Fieldservice TryGet: No field found.");
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//todo:
|
|
|
|
|
|
|
|
// - Make this a singleton
|
|
|
|
|
|
|
|
// - Let this interact with the FarmingControls and the FieldBehaviours
|
|
|
|
|
|
|
|
// - Replace FieldService2D and remove it from scenes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
//Update
|
|
|
|
//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 (fields.ContainsKey(fieldPosition))
|
|
|
|
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fields[fieldPosition] = state;
|
|
|
|
if (field.fields.ContainsKey(fieldPosition))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
field.fields[fieldPosition] = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TryAddEntry(fieldPosition, state);
|
|
|
|
TryAddEntry(key, fieldPosition, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Delete
|
|
|
|
//Delete
|
|
|
|
|
|
|
|
|
|
|
|
public void RemoveEntry(Vector2I fieldPosition)
|
|
|
|
public void RemoveEntry(string key, Vector2I fieldPosition)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
GD.Print($"FieldService: Removing entry at {fieldPosition}.");
|
|
|
|
|
|
|
|
if (outerDict != null && outerDict.TryGetValue(key, out FieldsInScene? field))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (fields.ContainsKey(fieldPosition))
|
|
|
|
if (field.fields.ContainsKey(fieldPosition))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fields.Remove(fieldPosition);
|
|
|
|
field.fields.Remove(fieldPosition);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class FieldsInScene
|
|
|
|
internal class FieldsInScene
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public Dictionary<Vector2I, FieldBehaviour2D> fields;
|
|
|
|
public Dictionary<Vector2I, FieldBehaviour2D?> fields;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public FieldsInScene()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fields = new Dictionary<Vector2I, FieldBehaviour2D?>();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|