WIP 2D rework
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
[Export] private Sprite2D _hoeSprite;
|
||||
[Export] private Sprite2D _wateringCanSprite;
|
||||
[Export] private PackedScene _fieldPrefab;
|
||||
[Export] private Node2D _movingPlayer;
|
||||
[Export] private Camera2D _camera;
|
||||
|
||||
public FieldService2D FieldParent;
|
||||
|
||||
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)
|
||||
{
|
||||
bool success = ActivateTool(activate, _wateringCanSprite);
|
||||
_waterCanInHand = success;
|
||||
return success;
|
||||
}
|
||||
|
||||
private bool ActivateTool(bool activate, Sprite2D tool)
|
||||
{
|
||||
tool.Visible = !activate;
|
||||
return !activate;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
Vector2 mousePosition = GetViewport().GetMousePosition();
|
||||
|
||||
Vector2I adjustedPosition = new Vector2I(AdjustValue(mousePosition.X), AdjustValue(mousePosition.Y));
|
||||
|
||||
if (@event.IsActionPressed("click") && _hoeInHand)
|
||||
{
|
||||
MakeField(adjustedPosition);
|
||||
}
|
||||
|
||||
if (@event.IsActionPressed("click") && _waterCanInHand)
|
||||
{
|
||||
WaterTheField(adjustedPosition);
|
||||
}
|
||||
}
|
||||
|
||||
private void WaterTheField(Vector2I fieldPosition)
|
||||
{
|
||||
FieldBehaviour2D field = FieldParent.Get(fieldPosition);
|
||||
if (field == null)
|
||||
return;
|
||||
|
||||
field.Water();
|
||||
}
|
||||
|
||||
private void MakeField(Vector2I fieldPosition)
|
||||
{
|
||||
if(FieldParent == null || _fieldPrefab == null)
|
||||
return;
|
||||
|
||||
// only instantiate a field if there isn't one already.
|
||||
if(FieldParent.Get(fieldPosition) == null)
|
||||
{
|
||||
Node fieldInstance = _fieldPrefab.Instantiate();
|
||||
if (fieldInstance is Node2D field2d)
|
||||
{
|
||||
// add dictionary entry for the field
|
||||
Array<Node> fields = field2d.FindChildren("*", nameof(FieldBehaviour2D));
|
||||
if (fields.Count > 0)
|
||||
FieldParent.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour2D);
|
||||
|
||||
// reposition and reparent the instance
|
||||
field2d.Position = new Vector2(fieldPosition.X, fieldPosition.Y);;
|
||||
FieldParent.AddChild(fieldInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int AdjustValue(float value)
|
||||
{
|
||||
return (int) Mathf.Floor(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user