Files

107 lines
3.1 KiB
C#
Raw Permalink Normal View History

2025-04-13 14:56:25 +02:00
using System.Collections.Generic;
2025-04-24 23:36:23 +02:00
using System.Diagnostics;
using System.Linq;
2025-04-06 18:46:59 +02:00
using Godot;
2025-04-24 23:36:23 +02:00
using Godot.Collections;
2025-04-06 18:46:59 +02:00
namespace Babushka.scripts.CSharp.Common.Farming;
[GlobalClass]
2025-04-06 18:46:59 +02:00
public partial class FarmingControls : Node3D
{
2025-04-06 22:03:13 +02:00
[Export] private Sprite3D _hoeSprite;
[Export] private Sprite3D _wateringCanSprite;
2025-04-06 23:13:25 +02:00
[Export] private PackedScene _fieldPrefab;
[Export] private Node3D _movingPlayer;
[Export] private Camera3D _camera;
2025-04-06 18:46:59 +02:00
public FieldService FieldParent;
2025-04-06 22:03:13 +02:00
private bool _hoeInHand = false;
private bool _waterCanInHand = false;
#region Tools
public bool ActivateHoe(bool activate)
{
bool success = ActivateTool(activate, _hoeSprite);
_hoeInHand = success;
return success;
}
public bool ActivateWateringCan(bool activate)
2025-04-06 22:03:13 +02:00
{
bool success = ActivateTool(activate, _wateringCanSprite);
_waterCanInHand = success;
return success;
2025-04-06 22:03:13 +02:00
}
private bool ActivateTool(bool activate, Sprite3D tool)
{
tool.Visible = !activate;
return !activate;
}
#endregion
2025-04-06 23:13:25 +02:00
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));
if (position3D.HasValue)
{
Vector2I adjustedPosition = new Vector2I(AdjustValue(position3D.Value.X), AdjustValue(position3D.Value.Y));
if (@event.IsActionPressed("click") && _hoeInHand)
{
MakeField(adjustedPosition);
}
if (@event.IsActionPressed("click") && _waterCanInHand)
{
WaterTheField(adjustedPosition);
}
}
}
private void WaterTheField(Vector2I fieldPosition)
{
FieldBehaviour field = FieldParent.Get(fieldPosition);
2025-04-24 23:36:23 +02:00
if (field == null)
return;
2025-04-24 23:36:23 +02:00
field.Water();
2025-04-06 23:13:25 +02:00
}
private void MakeField(Vector2I fieldPosition)
2025-04-06 23:13:25 +02:00
{
if(FieldParent == null || _fieldPrefab == null)
2025-04-06 23:13:25 +02:00
return;
2025-04-14 18:00:10 +02:00
// only instantiate a field if there isn't one already.
if(FieldParent.Get(fieldPosition) == null)
2025-04-06 23:13:25 +02:00
{
2025-04-14 18:00:10 +02:00
Node fieldInstance = _fieldPrefab.Instantiate();
if (fieldInstance is Node3D field3d)
{
// add dictionary entry for the field
2025-04-24 23:36:23 +02:00
Array<Node> fields = field3d.FindChildren("*", nameof(FieldBehaviour));
if (fields.Count > 0)
FieldParent.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour);
2025-04-14 18:00:10 +02:00
// reposition and reparent the instance
field3d.Position = new Vector3(fieldPosition.X, 0.1f, fieldPosition.Y * -1);;
2025-04-14 18:00:10 +02:00
FieldParent.AddChild(fieldInstance);
}
2025-04-06 23:13:25 +02:00
}
2025-04-13 14:56:25 +02:00
}
2025-04-06 23:13:25 +02:00
2025-04-24 23:36:23 +02:00
private int AdjustValue(float value)
2025-04-13 14:56:25 +02:00
{
2025-04-24 23:36:23 +02:00
return (int) Mathf.Floor(value);
2025-04-06 23:13:25 +02:00
}
2025-04-13 14:56:25 +02:00
2025-04-06 18:46:59 +02:00
}