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.
106 lines
3.1 KiB
106 lines
3.1 KiB
using Babushka.scripts.CSharp.Common.Animation;
|
|
using Babushka.scripts.CSharp.Common.CharacterControls;
|
|
using Babushka.scripts.CSharp.Common.Inventory;
|
|
using Babushka.scripts.CSharp.Common.Services;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
|
|
|
public partial class VesnaBehaviour2D : Node
|
|
{
|
|
[ExportGroup("Farming")]
|
|
[Export] private FarmingControls2D _farmingControls;
|
|
[Export] private PlayerMovement _player2d;
|
|
[Export] private VesnaAnimations _vesnaAnimations;
|
|
[Export] private ItemResource _hoe;
|
|
[Export] private ItemResource _wateringCan;
|
|
|
|
[Signal] public delegate void PickedUpToolEventHandler(bool success, int toolId);
|
|
[Signal] public delegate void FilledWateringCanEventHandler();
|
|
|
|
[Signal] public delegate void InventorySelectionChangedEventHandler(int toolId);
|
|
|
|
private InventoryManager _inventoryManager;
|
|
private InventoryInstance _inventoryInstance;
|
|
|
|
public override void _Ready()
|
|
{
|
|
_inventoryManager = InventoryManager.Instance;
|
|
_inventoryInstance = _inventoryManager.playerInventory;
|
|
_inventoryManager.SlotIndexChanged += HandleInventorySelectedSlotIndexChanged;
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
_inventoryManager.SlotIndexChanged -= HandleInventorySelectedSlotIndexChanged;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when picking up an item.
|
|
/// Makes sure that item animations are also updated when they are occupying a currently empty spot.
|
|
/// </summary>
|
|
public void HandlePickUp()
|
|
{
|
|
//Calls the same event handler as the inventory to ensure the currently selected item is updated in the animation.
|
|
HandleInventorySelectedSlotIndexChanged(0);
|
|
}
|
|
|
|
private void HandleInventorySelectedSlotIndexChanged(int newIndex = 0)
|
|
{
|
|
InventorySlot currentSlot = InventoryManager.Instance.GetCurrentSelectedSlot();
|
|
ItemInstance? currentItem = currentSlot.itemInstance;
|
|
|
|
int toolId = -1;
|
|
|
|
if (currentItem != null && currentItem.blueprint == _hoe)
|
|
{
|
|
toolId = 0;
|
|
}
|
|
|
|
if (currentItem != null && currentItem.blueprint == _wateringCan)
|
|
{
|
|
toolId = 1;
|
|
}
|
|
|
|
ActivateTool(toolId);
|
|
_vesnaAnimations.ActivateTool(toolId >= 0, toolId);
|
|
EmitSignal(SignalName.InventorySelectionChanged, toolId);
|
|
|
|
}
|
|
|
|
#region Farming
|
|
|
|
public void ActivateTool(int toolId)
|
|
{
|
|
bool activated = _farmingControls.TryActivateTool(toolId);
|
|
EmitSignal(SignalName.PickedUpTool, activated, toolId);
|
|
}
|
|
|
|
public void TryFillWateringCan(int toolId)
|
|
{
|
|
if (toolId == 1)
|
|
{
|
|
_farmingControls.FillWateringCan();
|
|
_vesnaAnimations.PlayWateringCanFillupAnimation();
|
|
EmitSignal(SignalName.FilledWateringCan);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Enables the input.
|
|
/// </summary>
|
|
public void EnableMovement()
|
|
{
|
|
InputService.Instance.InputEnabled = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disables the input.
|
|
/// </summary>
|
|
public void DisableMovement()
|
|
{
|
|
InputService.Instance.InputEnabled = false;
|
|
}
|
|
} |