Reworking 2D interactions
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
using Babushka.scripts.CSharp.Common.Farming;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
public partial class VesnaBehaviour : Node
|
||||
{
|
||||
[ExportGroup("Farming")]
|
||||
[Export] private FieldService2D _fieldParent;
|
||||
[Export] private FarmingControls2D _farmingControls;
|
||||
|
||||
[Signal] public delegate void ToolPickupEventHandler(bool success);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_farmingControls.FieldParent = _fieldParent;
|
||||
}
|
||||
|
||||
#region Farming
|
||||
|
||||
public void ActivateHoe(bool activate)
|
||||
{
|
||||
ActivateTool(activate, 0);
|
||||
}
|
||||
|
||||
public void ActivateWateringCan(bool activate)
|
||||
{
|
||||
ActivateTool(activate, 1);
|
||||
}
|
||||
|
||||
private void ActivateTool(bool activate , int toolId)
|
||||
{
|
||||
bool success = false;
|
||||
if (toolId == 0)
|
||||
{
|
||||
success = _farmingControls.ActivateHoe(activate);
|
||||
}
|
||||
else if (toolId == 1)
|
||||
{
|
||||
success = _farmingControls.ActivateWateringCan(activate);
|
||||
}
|
||||
EmitSignal(SignalName.ToolPickup, success);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://bblprbhnbyv77
|
||||
@@ -13,29 +13,30 @@ public partial class FarmingControls2D : Node2D
|
||||
[Export] private Camera2D _camera;
|
||||
|
||||
public FieldService2D FieldParent;
|
||||
|
||||
private bool _hoeInHand = false;
|
||||
private bool _waterCanInHand = false;
|
||||
|
||||
private int _toolId = -1;
|
||||
|
||||
#region Tools
|
||||
|
||||
public bool ActivateHoe(bool activate)
|
||||
|
||||
public bool ActivateTool(bool activate, int toolId)
|
||||
{
|
||||
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;
|
||||
if(_toolId < 0)
|
||||
return false;
|
||||
|
||||
switch (toolId)
|
||||
{
|
||||
case 0:
|
||||
_hoeSprite.Visible = activate;
|
||||
break;
|
||||
case 1:
|
||||
_wateringCanSprite.Visible = activate;
|
||||
|
||||
break;
|
||||
default:
|
||||
_toolId = -1;
|
||||
break;
|
||||
}
|
||||
_toolId = toolId;
|
||||
return !activate;
|
||||
}
|
||||
|
||||
@@ -47,12 +48,12 @@ public partial class FarmingControls2D : Node2D
|
||||
|
||||
Vector2I adjustedPosition = new Vector2I(AdjustValue(mousePosition.X), AdjustValue(mousePosition.Y));
|
||||
|
||||
if (@event.IsActionPressed("click") && _hoeInHand)
|
||||
if (@event.IsActionPressed("click") && _toolId == 0)
|
||||
{
|
||||
MakeField(adjustedPosition);
|
||||
}
|
||||
|
||||
if (@event.IsActionPressed("click") && _waterCanInHand)
|
||||
if (@event.IsActionPressed("click") && _toolId == 1)
|
||||
{
|
||||
WaterTheField(adjustedPosition);
|
||||
}
|
||||
|
||||
@@ -29,16 +29,8 @@ public partial class VesnaBehaviour2D : Node
|
||||
|
||||
private void ActivateTool(bool activate , int toolId)
|
||||
{
|
||||
bool success = false;
|
||||
if (toolId == 0)
|
||||
{
|
||||
success = _farmingControls.ActivateHoe(activate);
|
||||
}
|
||||
else if (toolId == 1)
|
||||
{
|
||||
success = _farmingControls.ActivateWateringCan(activate);
|
||||
}
|
||||
EmitSignal(SignalName.ToolPickup, success);
|
||||
bool activated = _farmingControls.ActivateTool(activate, toolId);
|
||||
EmitSignal(SignalName.ToolPickup, activated);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Switches between two available Sprite Options.
|
||||
/// </summary>
|
||||
public partial class SpriteSwitcher2D : Node2D
|
||||
{
|
||||
[Export] private Sprite2D _firstSprite;
|
||||
[Export] private Sprite2D _secondSprite;
|
||||
[Export] private bool _state = true;
|
||||
|
||||
[Signal]
|
||||
public delegate void SwitchEventHandler(bool state);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
SetState();
|
||||
}
|
||||
|
||||
public void SwitchState()
|
||||
{
|
||||
_state = !_state;
|
||||
EmitSignal(SignalName.Switch, _state);
|
||||
SetState();
|
||||
}
|
||||
|
||||
private void SetState()
|
||||
{
|
||||
if(_firstSprite != null)
|
||||
_firstSprite.Visible = _state;
|
||||
if(_secondSprite != null)
|
||||
_secondSprite.Visible = !_state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user