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.
Babushka/scripts/CSharp/Common/Farming/FarmingControls.cs

52 lines
1.3 KiB

using System.Collections.Generic;
using Godot;
namespace Babushka.scripts.CSharp.Common.Farming;
[GlobalClass]
public partial class FarmingControls : Node3D
{
[Export] private Sprite3D _hoeSprite;
[Export] private PackedScene _fieldPrefab;
[Export] private Node3D _movingPlayer;
public Node FieldParent;
private bool _hoeInHand = false;
private Dictionary<KeyValuePair<int, int>, bool> _spawnedFields = new Dictionary<KeyValuePair<int, int>, bool>();
public void ActivateHoe(bool activate)
{
_hoeSprite.Visible = !activate;
_hoeInHand = !activate;
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("click") && _hoeInHand)
{
MakeField();
}
}
private void MakeField()
{
if(FieldParent == null || _fieldPrefab == null)
return;
Node fieldInstance = _fieldPrefab.Instantiate();
if (fieldInstance is Node3D field3d)
{
Vector3 playerPos = _movingPlayer.GlobalPosition;
field3d.Position = new Vector3(AdjustValue(playerPos.X), 0.1f, AdjustValue(playerPos.Z));
}
FieldParent.AddChild(fieldInstance);
}
private float AdjustValue(float value)
{
return Mathf.Floor(value);
}
}