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.
Babushka/scripts/CSharp/Common/Items/InventoryDependentInteracta...

59 lines
1.7 KiB

using Babushka.scripts.CSharp.Common.CharacterControls;
using Babushka.scripts.CSharp.Common.Inventory;
using Babushka.scripts.CSharp.GameEntity.Entities;
using Babushka.scripts.CSharp.GameEntity.Management;
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 VesnaEntity _vesnaEntity;
private InventoryInstance _playerInventory;
public override void _Ready()
{
_vesnaEntity = EntityManager.Instance.GetUniqueEntity<VesnaEntity>();
_playerInventory = _vesnaEntity.inventory;
_vesnaEntity.SlotIndexChanged += HandleInventorySelectedSlotIndexChanged;
}
private void HandleInventorySelectedSlotIndexChanged(int newIndex)
{
int currentSlotIndex = _vesnaEntity.CurrentSelectedSlotIndex;
ItemInstance? item = _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;
}
}