InteractionAreas are now bound to SpriteSwitchers and farming tool interaction works
This commit is contained in:
@@ -6,10 +6,12 @@ public partial class InteractionArea2D : Node2D
|
||||
{
|
||||
[Export] private Area2D _area;
|
||||
[Export] private Label _label;
|
||||
[Export] private SpriteSwitcher2D _sprites;
|
||||
[Export] private bool _showLabel = true;
|
||||
[Export] private int _id;
|
||||
|
||||
[Signal]
|
||||
public delegate void InteractedEventHandler();
|
||||
public delegate void InteractedEventHandler(int id);
|
||||
|
||||
public void OnPlayerEntered(Node2D player)
|
||||
{
|
||||
@@ -27,7 +29,15 @@ public partial class InteractionArea2D : Node2D
|
||||
if (@event.IsAction("interact") && @event.IsPressed() && _area.HasOverlappingBodies())
|
||||
{
|
||||
_label.Hide();
|
||||
EmitSignal(SignalName.Interacted);
|
||||
EmitSignal(SignalName.Interacted, _id);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSpriteActiveState(bool success, int id)
|
||||
{
|
||||
if (_id == id)
|
||||
{
|
||||
_sprites.SwitchState(!success);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,33 @@ public partial class FarmingControls2D : Node2D
|
||||
private int _toolId = -1;
|
||||
|
||||
#region Tools
|
||||
|
||||
public bool ActivateTool(bool activate, int toolId)
|
||||
|
||||
/// <summary>
|
||||
/// If no tool has been set, then set the current tool to the incoming tool id.
|
||||
/// </summary>
|
||||
/// <param name="toolId">The id of the tool to activate if possible</param>
|
||||
/// <returns>If the tool in question was activated.</returns>
|
||||
public bool TryActivateTool(int toolId)
|
||||
{
|
||||
if(_toolId < 0)
|
||||
return false;
|
||||
bool activate;
|
||||
|
||||
//if our hands are empty, activate
|
||||
if (_toolId == -1)
|
||||
{
|
||||
activate = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//if we're already carrying a tool, deactivate or fail return
|
||||
if (_toolId == toolId)
|
||||
{
|
||||
activate = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
switch (toolId)
|
||||
{
|
||||
@@ -30,14 +52,14 @@ public partial class FarmingControls2D : Node2D
|
||||
break;
|
||||
case 1:
|
||||
_wateringCanSprite.Visible = activate;
|
||||
|
||||
break;
|
||||
default:
|
||||
_toolId = -1;
|
||||
break;
|
||||
}
|
||||
_toolId = toolId;
|
||||
return !activate;
|
||||
|
||||
_toolId = activate ? toolId : -1;
|
||||
return activate;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -8,7 +8,7 @@ public partial class VesnaBehaviour2D : Node
|
||||
[Export] private FieldService2D _fieldParent;
|
||||
[Export] private FarmingControls2D _farmingControls;
|
||||
|
||||
[Signal] public delegate void ToolPickupEventHandler(bool success);
|
||||
[Signal] public delegate void PickedUpToolEventHandler(bool success, int toolId);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -17,20 +17,10 @@ public partial class VesnaBehaviour2D : Node
|
||||
|
||||
#region Farming
|
||||
|
||||
public void ActivateHoe(bool activate)
|
||||
public void ActivateTool(int toolId)
|
||||
{
|
||||
ActivateTool(activate, 0);
|
||||
}
|
||||
|
||||
public void ActivateWateringCan(bool activate)
|
||||
{
|
||||
ActivateTool(activate, 1);
|
||||
}
|
||||
|
||||
private void ActivateTool(bool activate , int toolId)
|
||||
{
|
||||
bool activated = _farmingControls.ActivateTool(activate, toolId);
|
||||
EmitSignal(SignalName.ToolPickup, activated);
|
||||
bool activated = _farmingControls.TryActivateTool(toolId);
|
||||
EmitSignal(SignalName.PickedUpTool, activated, toolId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -7,30 +7,30 @@ namespace Babushka.scripts.CSharp.Common;
|
||||
/// </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);
|
||||
[Export] private Sprite2D _activeSprite;
|
||||
[Export] private Sprite2D _inactiveSprite;
|
||||
[Export] private bool _active = false;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
SetState();
|
||||
SetSprites();
|
||||
}
|
||||
|
||||
public void SwitchState()
|
||||
/// <summary>
|
||||
/// Switches the State of the Sprites to the opposite.
|
||||
/// Emits Switch Signal.
|
||||
/// </summary>
|
||||
public void SwitchState(bool state)
|
||||
{
|
||||
_state = !_state;
|
||||
EmitSignal(SignalName.Switch, _state);
|
||||
SetState();
|
||||
_active = state;
|
||||
SetSprites();
|
||||
}
|
||||
|
||||
private void SetState()
|
||||
|
||||
private void SetSprites()
|
||||
{
|
||||
if(_firstSprite != null)
|
||||
_firstSprite.Visible = _state;
|
||||
if(_secondSprite != null)
|
||||
_secondSprite.Visible = !_state;
|
||||
if(_activeSprite != null)
|
||||
_activeSprite.Visible = _active;
|
||||
if(_inactiveSprite != null)
|
||||
_inactiveSprite.Visible = !_active;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user