You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.7 KiB
108 lines
2.7 KiB
using System.Collections.Generic;
|
|
using Babushka.scripts.CSharp.Common.Services;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
|
|
|
public partial class FieldService : Node
|
|
{
|
|
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>();
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
Instance = null;
|
|
_outerDict = null;
|
|
}
|
|
|
|
|
|
//Create
|
|
public bool TryAddEntry(string sceneName, int fieldIndex, FieldBehaviour2D field)
|
|
{
|
|
if (_outerDict != null )
|
|
{
|
|
FieldsInScene innerDict;
|
|
bool outerDictEntryExists = _outerDict.TryGetValue(sceneName, out innerDict);
|
|
|
|
if (!outerDictEntryExists)
|
|
{
|
|
innerDict = new FieldsInScene();
|
|
_outerDict.Add(sceneName, innerDict);
|
|
}
|
|
|
|
if (!innerDict.fields.ContainsKey(fieldIndex))
|
|
{
|
|
innerDict.fields.Add(fieldIndex, field);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Read
|
|
public FieldBehaviour2D? TryGet(string key, int fieldIndex)
|
|
{
|
|
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
|
{
|
|
if (field.fields.TryGetValue(fieldIndex, out FieldBehaviour2D? fieldInstance))
|
|
{
|
|
return fieldInstance;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
//Update
|
|
public void UpdateEntry(string key, int fieldIndex, FieldBehaviour2D state)
|
|
{
|
|
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
|
{
|
|
if (field.fields.ContainsKey(fieldIndex))
|
|
{
|
|
field.fields[fieldIndex] = state;
|
|
}
|
|
else
|
|
{
|
|
TryAddEntry(key, fieldIndex, state);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Delete
|
|
public void RemoveEntry(string key, int fieldIndex)
|
|
{
|
|
if (_outerDict != null && _outerDict.TryGetValue(key, out FieldsInScene? field))
|
|
{
|
|
if (field.fields.ContainsKey(fieldIndex))
|
|
{
|
|
field.fields.Remove(fieldIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
public int PositionToIndex(Vector2 position)
|
|
{
|
|
// some awesome code here
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
internal class FieldsInScene
|
|
{
|
|
public Dictionary<int, FieldBehaviour2D?> fields;
|
|
|
|
public FieldsInScene()
|
|
{
|
|
fields = new Dictionary<int, FieldBehaviour2D?>();
|
|
}
|
|
}
|
|
|
|
|