✨ added InventoryListener and made field interaction area activate only when the right item (seed) is in the inventory
This commit is contained in:
@@ -19,15 +19,33 @@ public partial class FieldBehaviour2D : Sprite2D
|
||||
[Export] public InteractionArea2D PlantingInteraction;
|
||||
[Export] public Node2D PlantingPlaceholder;
|
||||
[Export] public ItemRepository ItemRepository;
|
||||
|
||||
[Export] public InteractionArea2D FieldInteractionArea;
|
||||
|
||||
public Vector2 FieldPosition;
|
||||
|
||||
private bool _seedsActive;
|
||||
|
||||
[Signal] public delegate void PlantedEventHandler();
|
||||
|
||||
private void UpdateInteractionArea()
|
||||
{
|
||||
// fieldstate == tilled / watered && samen im Inventar
|
||||
bool canPlant = (FieldState == FieldState.Tilled || FieldState == FieldState.Watered) && _seedsActive;
|
||||
// fieldstate == tilled && watering can ausgewählt
|
||||
bool canWater = false;
|
||||
FieldInteractionArea.IsActive = canPlant || canWater;
|
||||
}
|
||||
|
||||
public void ActivatedSeedInInventory(bool activated)
|
||||
{
|
||||
_seedsActive = activated;
|
||||
UpdateInteractionArea();
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
UpdateFieldState(FieldState);
|
||||
UpdateInteractionArea();
|
||||
int randomIndex = new Random().Next(0, _maskTexture.Length);
|
||||
_maskSprite.Texture = _maskTexture[randomIndex];
|
||||
_outlineSprite.Texture = _maskOutlineTextures[randomIndex];
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class InventoryListener : Node
|
||||
{
|
||||
[Export] private ItemResource[] _itemResourcesToListenFor;
|
||||
|
||||
[Signal] public delegate void ItemInstanceActivatedEventHandler(bool activated);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
InventoryManager.Instance.playerInventory.InventoryContentsChanged += HandleNewItemInInventory;
|
||||
InventoryManager.Instance.SlotIndexChanged += HandleNewItemInInventory;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
InventoryManager.Instance.playerInventory.InventoryContentsChanged -= HandleNewItemInInventory;
|
||||
InventoryManager.Instance.SlotIndexChanged -= HandleNewItemInInventory;
|
||||
}
|
||||
|
||||
private void HandleNewItemInInventory(int newIndex)
|
||||
{
|
||||
HandleNewItemInInventory();
|
||||
}
|
||||
|
||||
private void HandleNewItemInInventory()
|
||||
{
|
||||
int currentSlotIndex = InventoryManager.Instance.CurrentSelectedSlotIndex;
|
||||
ItemInstance? instance = InventoryManager.Instance.playerInventory.Slots[currentSlotIndex].itemInstance;
|
||||
if (instance != null)
|
||||
{
|
||||
ItemResource? item = instance.blueprint;
|
||||
|
||||
foreach (var res in _itemResourcesToListenFor)
|
||||
{
|
||||
if (item == res)
|
||||
{
|
||||
EmitSignal(SignalName.ItemInstanceActivated, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EmitSignal(SignalName.ItemInstanceActivated, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://3t0af586fimq
|
||||
Reference in New Issue
Block a user