Small updates to dispatchers in items and puzzles

This commit is contained in:
2025-09-26 14:06:03 +02:00
committed by Michal Pikulski
parent 0bb3ad10a0
commit c8cbc45f04
10 changed files with 531 additions and 182 deletions

View File

@@ -25,7 +25,7 @@ namespace Interactions
public UnityEvent interactionInterrupted;
public UnityEvent characterArrived;
public UnityEvent<bool> interactionComplete;
// Helpers for managing interaction state
private bool _interactionInProgress;
private PlayerTouchController _playerRef;

View File

@@ -1,18 +1,38 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System; // for Action<T>
namespace Interactions
{
// New enum describing possible states for the slotted item
public enum ItemSlotState
{
None,
Correct,
Incorrect,
Forbidden
}
/// <summary>
/// Interaction requirement that allows slotting, swapping, or picking up items in a slot.
/// </summary>
[RequireComponent(typeof(Interactable))]
public class ItemSlot : Pickup
{
// Tracks the current state of the slotted item
private ItemSlotState _currentState = ItemSlotState.None;
/// <summary>
/// Read-only access to the current slotted item state.
/// </summary>
public ItemSlotState CurrentSlottedState => _currentState;
public UnityEvent onItemSlotted;
public UnityEvent onItemSlotRemoved;
public UnityEvent onCorrectItemSlotted;
// Native C# event alternative to the UnityEvent for code-only subscribers
public event Action<PickupItemData, PickupItemData> OnCorrectItemSlotted;
public UnityEvent onIncorrectItemSlotted;
public UnityEvent onForbiddenItemSlotted;
private PickupItemData _currentlySlottedItemData;
@@ -48,6 +68,7 @@ namespace Interactions
{
DebugUIMessage.Show("Can't place that here.", Color.red);
onForbiddenItemSlotted?.Invoke();
_currentState = ItemSlotState.Forbidden;
Interactable.BroadcastInteractionComplete(false);
return;
}
@@ -62,6 +83,7 @@ namespace Interactions
{
FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData, false);
onItemSlotRemoved?.Invoke();
_currentState = ItemSlotState.None;
SlotItem(heldItemObj, heldItemData, _currentlySlottedItemObject == null);
return;
}
@@ -109,6 +131,8 @@ namespace Interactions
{
_currentlySlottedItemObject = null;
_currentlySlottedItemData = null;
// Clear state when no item is slotted
_currentState = ItemSlotState.None;
}
else
{
@@ -133,6 +157,8 @@ namespace Interactions
{
DebugUIMessage.Show("You correctly slotted " + itemToSlotData.itemName + " into: " + itemData.itemName, Color.green);
onCorrectItemSlotted?.Invoke();
OnCorrectItemSlotted?.Invoke(itemData, _currentlySlottedItemData);
_currentState = ItemSlotState.Correct;
}
Interactable.BroadcastInteractionComplete(true);
@@ -143,6 +169,7 @@ namespace Interactions
{
DebugUIMessage.Show("I'm not sure this works.", Color.yellow);
onIncorrectItemSlotted?.Invoke();
_currentState = ItemSlotState.Incorrect;
}
Interactable.BroadcastInteractionComplete(false);
}

View File

@@ -1,5 +1,6 @@
using Input;
using UnityEngine;
using System; // added for Action<T>
namespace Interactions
{
@@ -11,6 +12,9 @@ namespace Interactions
protected Interactable Interactable;
private PlayerTouchController _playerRef;
protected FollowerController FollowerController;
// Event: invoked when the item was picked up successfully
public event Action<PickupItemData> OnItemPickedUp;
/// <summary>
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
@@ -89,7 +93,14 @@ namespace Interactions
}
FollowerController?.TryPickupItem(gameObject, itemData);
Interactable.BroadcastInteractionComplete(combinationResult == FollowerController.CombinationResult.NotApplicable);
bool wasPickedUp = (combinationResult == FollowerController.CombinationResult.NotApplicable);
Interactable.BroadcastInteractionComplete(wasPickedUp);
// Invoke native C# event when the item was picked up successfully
if (wasPickedUp)
{
OnItemPickedUp?.Invoke(itemData);
}
}
}
}