parent
63fd81e54d
commit
6ba26c360d
@ -0,0 +1,77 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||||
|
|
||||||
|
public partial class FieldService : Node2D
|
||||||
|
{
|
||||||
|
private Dictionary<PackedScene, FieldsInScene>? fieldsPerScene;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Signal] public delegate void FieldCreatedEventHandler();
|
||||||
|
|
||||||
|
//Create
|
||||||
|
public bool TryAddEntry(PackedScene scene, Vector2I key, FieldBehaviour2D field)
|
||||||
|
{
|
||||||
|
if (!fieldsPerScene.ContainsKey(scene))
|
||||||
|
{
|
||||||
|
FieldsInScene fieldInstance = new FieldsInScene();
|
||||||
|
fieldsPerScene.Add(scene, fieldInstance);
|
||||||
|
EmitSignal(SignalName.FieldCreated);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read
|
||||||
|
public FieldBehaviour2D Get(PackedScene key, Vector2I fieldPosition)
|
||||||
|
{
|
||||||
|
if (fieldsPerScene.TryGetValue(key, out FieldsInScene field))
|
||||||
|
{
|
||||||
|
if (field.fields.TryGetValue(fieldPosition, out FieldBehaviour2D fieldInstance))
|
||||||
|
return fieldInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (fields.ContainsKey(fieldPosition))
|
||||||
|
{
|
||||||
|
fields[fieldPosition] = state;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TryAddEntry(fieldPosition, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Delete
|
||||||
|
|
||||||
|
public void RemoveEntry(Vector2I fieldPosition)
|
||||||
|
{
|
||||||
|
if (fields.ContainsKey(fieldPosition))
|
||||||
|
{
|
||||||
|
fields.Remove(fieldPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class FieldsInScene
|
||||||
|
{
|
||||||
|
public Dictionary<Vector2I, FieldBehaviour2D> fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in new issue