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.
74 lines
1.8 KiB
74 lines
1.8 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();
|
|
|
|
/*
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
var spaceState = GetWorld2D().DirectSpaceState;
|
|
// use global coordinates, not local to node
|
|
var query = PhysicsRayQueryParameters2D.Create(GetGlobalMousePosition(), new Vector3(0,0,-1),
|
|
CollisionMask, [GetRid()]);
|
|
var result = spaceState.IntersectRay(query);
|
|
if (result.Count > 0)
|
|
GD.Print("Hit at point: ", result["position"]);
|
|
}
|
|
*/
|
|
|
|
|
|
//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);
|
|
}
|
|
}
|
|
} |