Implemented first version of an interactable area that reacts to inventory selection
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Items;
|
||||
|
||||
public partial class InventoryDependentInteractable : Node2D
|
||||
{
|
||||
[Export] private InteractionArea2D _interactionArea;
|
||||
[Export] private Array<ItemResource> _itemsToReactTo;
|
||||
[Export] private bool _activateOnItem = true;
|
||||
|
||||
private InventoryManager _inventoryManager;
|
||||
private InventoryInstance _inventoryInstance;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_inventoryManager = InventoryManager.Instance;
|
||||
_inventoryInstance = _inventoryManager.playerInventory;
|
||||
_inventoryManager.SlotIndexChanged += HandleInventorySelectedSlotIndexChanged;
|
||||
}
|
||||
|
||||
private void HandleInventorySelectedSlotIndexChanged(int newIndex)
|
||||
{
|
||||
int currentSlotIndex = InventoryManager.Instance.CurrentSelectedSlotIndex;
|
||||
ItemInstance? item = InventoryManager.Instance.playerInventory.Slots[currentSlotIndex].itemInstance;
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
if (_activateOnItem)
|
||||
{
|
||||
_interactionArea.IsActive = Match(item.blueprint);
|
||||
}
|
||||
else
|
||||
{
|
||||
_interactionArea.IsActive = !Match(item.blueprint);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private bool Match(ItemResource inventoryItem)
|
||||
{
|
||||
bool matched = false;
|
||||
|
||||
foreach (ItemResource item in _itemsToReactTo)
|
||||
{
|
||||
if (inventoryItem == item)
|
||||
{
|
||||
matched = true;
|
||||
}
|
||||
}
|
||||
|
||||
return matched;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://doxr432r22dd0
|
||||
Reference in New Issue
Block a user