|
|
|
|
@ -13,6 +13,7 @@ public partial class FarmingControls : Node3D
|
|
|
|
|
[Export] private Sprite3D _wateringCanSprite;
|
|
|
|
|
[Export] private PackedScene _fieldPrefab;
|
|
|
|
|
[Export] private Node3D _movingPlayer;
|
|
|
|
|
[Export] private Camera3D _camera;
|
|
|
|
|
|
|
|
|
|
public FieldService FieldParent;
|
|
|
|
|
|
|
|
|
|
@ -45,40 +46,44 @@ public partial class FarmingControls : Node3D
|
|
|
|
|
|
|
|
|
|
public override void _Input(InputEvent @event)
|
|
|
|
|
{
|
|
|
|
|
Vector2 mousePosition = GetViewport().GetMousePosition();
|
|
|
|
|
var dropPlane = new Plane(new Vector3(0, 0, 10), 10);
|
|
|
|
|
Vector3? position3D = dropPlane.IntersectsRay(_camera.ProjectRayOrigin(mousePosition),
|
|
|
|
|
_camera.ProjectRayNormal(mousePosition));
|
|
|
|
|
Debug.Print(position3D.ToString());
|
|
|
|
|
|
|
|
|
|
if (position3D.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Vector2I adjustedPosition = new Vector2I(AdjustValue(position3D.Value.X), AdjustValue(position3D.Value.Y));
|
|
|
|
|
|
|
|
|
|
if (@event.IsActionPressed("click") && _hoeInHand)
|
|
|
|
|
{
|
|
|
|
|
MakeField();
|
|
|
|
|
MakeField(adjustedPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (@event.IsActionPressed("click") && _waterCanInHand)
|
|
|
|
|
{
|
|
|
|
|
WaterTheField();
|
|
|
|
|
WaterTheField(adjustedPosition);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WaterTheField()
|
|
|
|
|
private void WaterTheField(Vector2I fieldPosition)
|
|
|
|
|
{
|
|
|
|
|
Vector2I currentPos = new Vector2I(AdjustValue(_movingPlayer.GlobalPosition.X), AdjustValue(_movingPlayer.GlobalPosition.Z));
|
|
|
|
|
FieldBehaviour field = FieldParent.Get(currentPos);
|
|
|
|
|
FieldBehaviour field = FieldParent.Get(fieldPosition);
|
|
|
|
|
if (field == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
field.Water();
|
|
|
|
|
Debug.Print("Watered the field.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MakeField()
|
|
|
|
|
private void MakeField(Vector2I fieldPosition)
|
|
|
|
|
{
|
|
|
|
|
if(FieldParent == null || _fieldPrefab == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
if(FieldParent.Get(fieldPosition) == null)
|
|
|
|
|
{
|
|
|
|
|
Node fieldInstance = _fieldPrefab.Instantiate();
|
|
|
|
|
if (fieldInstance is Node3D field3d)
|
|
|
|
|
@ -86,10 +91,10 @@ public partial class FarmingControls : Node3D
|
|
|
|
|
// add dictionary entry for the field
|
|
|
|
|
Array<Node> fields = field3d.FindChildren("*", nameof(FieldBehaviour));
|
|
|
|
|
if (fields.Count > 0)
|
|
|
|
|
FieldParent.TryAddEntry(intPosition, fields[0] as FieldBehaviour);
|
|
|
|
|
FieldParent.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour);
|
|
|
|
|
|
|
|
|
|
// reposition and reparent the instance
|
|
|
|
|
field3d.Position = playerPos;
|
|
|
|
|
field3d.Position = new Vector3(fieldPosition.X, 0.1f, fieldPosition.Y * -1);;
|
|
|
|
|
FieldParent.AddChild(fieldInstance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|