Reworked the fieldservice

This commit is contained in:
2025-04-14 18:00:10 +02:00
parent c200098f0c
commit c73d3ff370
4 changed files with 40 additions and 24 deletions
@@ -62,20 +62,26 @@ public partial class FarmingControls : Node3D
{
if(FieldParent == null || _fieldPrefab == null)
return;
Node fieldInstance = _fieldPrefab.Instantiate();
if (fieldInstance is Node3D field3d)
// get current player position and adjust it to field / dictionary needs
Vector3 playerPos = _movingPlayer.GlobalPosition;
playerPos = new Vector3(AdjustValue(playerPos.X), 0.1f, AdjustValue(playerPos.Z));
Vector2I intPosition = new Vector2I((int) playerPos.X, (int) playerPos.Z);
// only instantiate a field if there isn't one already.
if(FieldParent.Get(intPosition) == null)
{
Vector3 playerPos = _movingPlayer.GlobalPosition;
playerPos = new Vector3(AdjustValue(playerPos.X), 0.1f, AdjustValue(playerPos.Z));
field3d.Position = playerPos;
Vector2I intPosition = new Vector2I((int) playerPos.X, (int) playerPos.Z);
FieldParent.AddEntry(intPosition, FieldState.Tilled);
Node fieldInstance = _fieldPrefab.Instantiate();
if (fieldInstance is Node3D field3d)
{
// add dictionary entry for the field
FieldParent.TryAddEntry(intPosition, field3d as FieldBehaviour);
// reposition and reparent the instance
field3d.Position = playerPos;
FieldParent.AddChild(fieldInstance);
}
}
FieldParent.AddChild(fieldInstance);
}
private float AdjustValue(float value)