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.
60 lines
1.3 KiB
60 lines
1.3 KiB
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
|
|
|
[GlobalClass]
|
|
public partial class FieldService2D : Node2D
|
|
{
|
|
[Export] private Dictionary<Vector2I, FieldBehaviour2D> fields;
|
|
|
|
[Export] private Area2D _allowedArea;
|
|
|
|
[Signal] public delegate void FieldCreatedEventHandler();
|
|
|
|
//Create
|
|
public bool TryAddEntry(Vector2I key, FieldBehaviour2D field)
|
|
{
|
|
if (!fields.ContainsKey(key))
|
|
{
|
|
fields.Add(key, field);
|
|
EmitSignal(SignalName.FieldCreated);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Read
|
|
public FieldBehaviour2D Get(Vector2I key)
|
|
{
|
|
if (fields.TryGetValue(key, out FieldBehaviour2D field))
|
|
return field;
|
|
return field;
|
|
|
|
return null;
|
|
}
|
|
|
|
//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);
|
|
}
|
|
}
|
|
} |