Reworked the fieldservice
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
@@ -6,32 +7,41 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
[GlobalClass]
|
||||
public partial class FieldService : Node3D
|
||||
{
|
||||
[Export] private Dictionary<Vector2I, FieldState> fields = new Dictionary<Vector2I, FieldState>();
|
||||
[Export] private Dictionary<Vector2I, FieldBehaviour> fields = new Dictionary<Vector2I, FieldBehaviour>();
|
||||
|
||||
//Create
|
||||
|
||||
public void AddEntry(Vector2I key, FieldState state)
|
||||
public bool TryAddEntry(Vector2I key, FieldBehaviour field)
|
||||
{
|
||||
fields.Add(key, state);
|
||||
if (!fields.ContainsKey(key))
|
||||
{
|
||||
fields.Add(key, field);
|
||||
return true;
|
||||
}
|
||||
Debug.Print("Added entry: " + key);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read
|
||||
|
||||
public FieldState Get(Vector2I key)
|
||||
public FieldBehaviour Get(Vector2I key)
|
||||
{
|
||||
return fields[key];
|
||||
Debug.Print($"Getting field at {key}. Found: {fields.ContainsKey(key)}.");
|
||||
if(fields.TryGetValue(key, out FieldBehaviour field))
|
||||
return field;
|
||||
return null;
|
||||
}
|
||||
|
||||
//Update
|
||||
public void UpdateEntry(Vector2I fieldPosition, FieldState state)
|
||||
public void UpdateEntry(Vector2I fieldPosition, FieldBehaviour state)
|
||||
{
|
||||
|
||||
Debug.Print("Updating entry: " + fieldPosition);
|
||||
if (fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
fields[fieldPosition] = state;
|
||||
}
|
||||
else
|
||||
{
|
||||
AddEntry(fieldPosition, state);
|
||||
TryAddEntry(fieldPosition, state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +49,7 @@ public partial class FieldService : Node3D
|
||||
|
||||
public void RemoveEntry(Vector2I fieldPosition)
|
||||
{
|
||||
Debug.Print("Removing entry: " + fieldPosition);
|
||||
if (fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
fields.Remove(fieldPosition);
|
||||
|
||||
Reference in New Issue
Block a user