Reworked the fieldservice
This commit is contained in:
@@ -1344,5 +1344,3 @@ _wateringCanSprite = NodePath("../CharacterBody3D/Farming/Watering can")
|
|||||||
_fieldPrefab = ExtResource("2_oq5hi")
|
_fieldPrefab = ExtResource("2_oq5hi")
|
||||||
_movingPlayer = NodePath("../CharacterBody3D")
|
_movingPlayer = NodePath("../CharacterBody3D")
|
||||||
metadata/_custom_type_script = "uid://b1sscdr4ptec8"
|
metadata/_custom_type_script = "uid://b1sscdr4ptec8"
|
||||||
|
|
||||||
[connection signal="WaterField" from="FarmingControls" to="." method="UseWateringCan"]
|
|
||||||
|
|||||||
@@ -62,20 +62,26 @@ public partial class FarmingControls : Node3D
|
|||||||
{
|
{
|
||||||
if(FieldParent == null || _fieldPrefab == null)
|
if(FieldParent == null || _fieldPrefab == null)
|
||||||
return;
|
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;
|
Node fieldInstance = _fieldPrefab.Instantiate();
|
||||||
playerPos = new Vector3(AdjustValue(playerPos.X), 0.1f, AdjustValue(playerPos.Z));
|
if (fieldInstance is Node3D field3d)
|
||||||
field3d.Position = playerPos;
|
{
|
||||||
Vector2I intPosition = new Vector2I((int) playerPos.X, (int) playerPos.Z);
|
// add dictionary entry for the field
|
||||||
FieldParent.AddEntry(intPosition, FieldState.Tilled);
|
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)
|
private float AdjustValue(float value)
|
||||||
|
|||||||
@@ -7,14 +7,15 @@ public enum FieldState
|
|||||||
Empty = 0,
|
Empty = 0,
|
||||||
Tilled = 1,
|
Tilled = 1,
|
||||||
Planted = 2,
|
Planted = 2,
|
||||||
Watered = 3
|
Watered = 3,
|
||||||
|
NotFound = 99
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class FieldBehaviour : Sprite3D
|
public partial class FieldBehaviour : Sprite3D
|
||||||
{
|
{
|
||||||
[Export] private Texture2D Tilled;
|
[Export] private Texture2D Tilled;
|
||||||
[Export] private Texture2D Watered;
|
[Export] private Texture2D Watered;
|
||||||
[Export] private FieldState FieldState = FieldState.Empty;
|
[Export] public FieldState FieldState = FieldState.Empty;
|
||||||
|
|
||||||
public Vector2 FieldPosition;
|
public Vector2 FieldPosition;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
using Godot;
|
using Godot;
|
||||||
using Godot.Collections;
|
using Godot.Collections;
|
||||||
|
|
||||||
@@ -6,32 +7,41 @@ namespace Babushka.scripts.CSharp.Common.Farming;
|
|||||||
[GlobalClass]
|
[GlobalClass]
|
||||||
public partial class FieldService : Node3D
|
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
|
//Create
|
||||||
|
public bool TryAddEntry(Vector2I key, FieldBehaviour field)
|
||||||
public void AddEntry(Vector2I key, FieldState state)
|
|
||||||
{
|
{
|
||||||
fields.Add(key, state);
|
if (!fields.ContainsKey(key))
|
||||||
|
{
|
||||||
|
fields.Add(key, field);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Debug.Print("Added entry: " + key);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read
|
// Read
|
||||||
|
public FieldBehaviour Get(Vector2I key)
|
||||||
public FieldState 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
|
//Update
|
||||||
public void UpdateEntry(Vector2I fieldPosition, FieldState state)
|
public void UpdateEntry(Vector2I fieldPosition, FieldBehaviour state)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Debug.Print("Updating entry: " + fieldPosition);
|
||||||
if (fields.ContainsKey(fieldPosition))
|
if (fields.ContainsKey(fieldPosition))
|
||||||
{
|
{
|
||||||
fields[fieldPosition] = state;
|
fields[fieldPosition] = state;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
AddEntry(fieldPosition, state);
|
TryAddEntry(fieldPosition, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +49,7 @@ public partial class FieldService : Node3D
|
|||||||
|
|
||||||
public void RemoveEntry(Vector2I fieldPosition)
|
public void RemoveEntry(Vector2I fieldPosition)
|
||||||
{
|
{
|
||||||
|
Debug.Print("Removing entry: " + fieldPosition);
|
||||||
if (fields.ContainsKey(fieldPosition))
|
if (fields.ContainsKey(fieldPosition))
|
||||||
{
|
{
|
||||||
fields.Remove(fieldPosition);
|
fields.Remove(fieldPosition);
|
||||||
|
|||||||
Reference in New Issue
Block a user