parent
e9cd4ce276
commit
9d818e5079
@ -1,100 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class FieldsInScene
|
||||
{
|
||||
public Dictionary<int, FieldBehaviour2D?> fields;
|
||||
|
||||
public FieldsInScene()
|
||||
{
|
||||
fields = new Dictionary<int, FieldBehaviour2D?>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1 +0,0 @@
|
||||
uid://slo0uydmmvnu
|
||||
Loading…
Reference in new issue