Create a simple dialogue authoring system, tied into our items (#10)
- Editor dialogue graph - Asset importer for processing the graph into runtime data - DialogueComponent that steers the dialogue interactions - DialogueCanbas with a scalable speech bubble to display everything - Brief README overview of the system Co-authored-by: AlexanderT <alexander@foolhardyhorizons.com> Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #10
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -1,20 +1,51 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using System; // for Action<T>
|
||||
using Core; // register with ItemManager
|
||||
|
||||
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;
|
||||
// Native C# event alternative for code-only subscribers
|
||||
public event Action<PickupItemData> OnItemSlotRemoved;
|
||||
|
||||
public UnityEvent onCorrectItemSlotted;
|
||||
// Native C# event alternative to the UnityEvent for code-only subscribers
|
||||
public event Action<PickupItemData, PickupItemData> OnCorrectItemSlotted;
|
||||
|
||||
public UnityEvent onIncorrectItemSlotted;
|
||||
// Native C# event alternative for code-only subscribers
|
||||
public event Action<PickupItemData, PickupItemData> OnIncorrectItemSlotted;
|
||||
|
||||
public UnityEvent onForbiddenItemSlotted;
|
||||
// Native C# event alternative for code-only subscribers
|
||||
public event Action<PickupItemData, PickupItemData> OnForbiddenItemSlotted;
|
||||
|
||||
private PickupItemData _currentlySlottedItemData;
|
||||
public SpriteRenderer slottedItemRenderer;
|
||||
private GameObject _currentlySlottedItemObject = null;
|
||||
@@ -35,6 +66,8 @@ namespace Interactions
|
||||
|
||||
protected override void OnCharacterArrived()
|
||||
{
|
||||
Debug.Log("[ItemSlot] OnCharacterArrived");
|
||||
|
||||
var heldItemData = FollowerController.CurrentlyHeldItemData;
|
||||
var heldItemObj = FollowerController.GetHeldPickupObject();
|
||||
var config = GameManager.Instance.GetSlotItemConfig(itemData);
|
||||
@@ -48,6 +81,8 @@ namespace Interactions
|
||||
{
|
||||
DebugUIMessage.Show("Can't place that here.", Color.red);
|
||||
onForbiddenItemSlotted?.Invoke();
|
||||
OnForbiddenItemSlotted?.Invoke(itemData, heldItemData);
|
||||
_currentState = ItemSlotState.Forbidden;
|
||||
Interactable.BroadcastInteractionComplete(false);
|
||||
return;
|
||||
}
|
||||
@@ -62,6 +97,8 @@ namespace Interactions
|
||||
{
|
||||
FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData, false);
|
||||
onItemSlotRemoved?.Invoke();
|
||||
OnItemSlotRemoved?.Invoke(_currentlySlottedItemData);
|
||||
_currentState = ItemSlotState.None;
|
||||
SlotItem(heldItemObj, heldItemData, _currentlySlottedItemObject == null);
|
||||
return;
|
||||
}
|
||||
@@ -105,10 +142,22 @@ namespace Interactions
|
||||
|
||||
public void SlotItem(GameObject itemToSlot, PickupItemData itemToSlotData, bool clearFollowerHeldItem = true)
|
||||
{
|
||||
// Cache the previous item data before clearing, needed for OnItemSlotRemoved event
|
||||
var previousItemData = _currentlySlottedItemData;
|
||||
bool wasSlotCleared = _currentlySlottedItemObject != null && itemToSlot == null;
|
||||
|
||||
if (itemToSlot == null)
|
||||
{
|
||||
_currentlySlottedItemObject = null;
|
||||
_currentlySlottedItemData = null;
|
||||
// Clear state when no item is slotted
|
||||
_currentState = ItemSlotState.None;
|
||||
|
||||
// Fire native event for slot clearing
|
||||
if (wasSlotCleared)
|
||||
{
|
||||
OnItemSlotRemoved?.Invoke(previousItemData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -117,6 +166,7 @@ namespace Interactions
|
||||
SetSlottedObject(itemToSlot);
|
||||
_currentlySlottedItemData = itemToSlotData;
|
||||
}
|
||||
|
||||
if (clearFollowerHeldItem)
|
||||
{
|
||||
FollowerController.ClearHeldItem();
|
||||
@@ -133,6 +183,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,9 +195,22 @@ namespace Interactions
|
||||
{
|
||||
DebugUIMessage.Show("I'm not sure this works.", Color.yellow);
|
||||
onIncorrectItemSlotted?.Invoke();
|
||||
OnIncorrectItemSlotted?.Invoke(itemData, _currentlySlottedItemData);
|
||||
_currentState = ItemSlotState.Incorrect;
|
||||
}
|
||||
Interactable.BroadcastInteractionComplete(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Register with ItemManager when enabled
|
||||
void Start()
|
||||
{
|
||||
ItemManager.Instance?.RegisterItemSlot(this);
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
ItemManager.Instance?.UnregisterItemSlot(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Input;
|
||||
using UnityEngine;
|
||||
using System; // added for Action<T>
|
||||
using Core; // register with ItemManager
|
||||
|
||||
namespace Interactions
|
||||
{
|
||||
@@ -12,6 +14,15 @@ namespace Interactions
|
||||
private PlayerTouchController _playerRef;
|
||||
protected FollowerController FollowerController;
|
||||
|
||||
// Track if the item has been picked up
|
||||
public bool isPickedUp { get; private set; }
|
||||
|
||||
// Event: invoked when the item was picked up successfully
|
||||
public event Action<PickupItemData> OnItemPickedUp;
|
||||
|
||||
// Event: invoked when this item is successfully combined with another
|
||||
public event Action<PickupItemData, PickupItemData, PickupItemData> OnItemsCombined;
|
||||
|
||||
/// <summary>
|
||||
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
|
||||
/// </summary>
|
||||
@@ -30,6 +41,14 @@ namespace Interactions
|
||||
ApplyItemData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register with ItemManager on Start
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
ItemManager.Instance?.RegisterPickup(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity OnDestroy callback. Cleans up event handlers.
|
||||
/// </summary>
|
||||
@@ -40,6 +59,9 @@ namespace Interactions
|
||||
Interactable.interactionStarted.RemoveListener(OnInteractionStarted);
|
||||
Interactable.characterArrived.RemoveListener(OnCharacterArrived);
|
||||
}
|
||||
|
||||
// Unregister from ItemManager
|
||||
ItemManager.Instance?.UnregisterPickup(this);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
@@ -81,15 +103,48 @@ namespace Interactions
|
||||
|
||||
protected virtual void OnCharacterArrived()
|
||||
{
|
||||
Debug.Log("[Pickup] OnCharacterArrived");
|
||||
|
||||
var combinationResult = FollowerController.TryCombineItems(this, out var combinationResultItem);
|
||||
if (combinationResultItem != null)
|
||||
{
|
||||
Interactable.BroadcastInteractionComplete(true);
|
||||
|
||||
// Fire the combination event when items are successfully combined
|
||||
if (combinationResult == FollowerController.CombinationResult.Successful)
|
||||
{
|
||||
var resultPickup = combinationResultItem.GetComponent<Pickup>();
|
||||
if (resultPickup != null && resultPickup.itemData != null)
|
||||
{
|
||||
// Get the combined item data
|
||||
var resultItemData = resultPickup.itemData;
|
||||
var heldItem = FollowerController.GetHeldPickupObject();
|
||||
|
||||
if (heldItem != null)
|
||||
{
|
||||
var heldPickup = heldItem.GetComponent<Pickup>();
|
||||
if (heldPickup != null && heldPickup.itemData != null)
|
||||
{
|
||||
// Trigger the combination event
|
||||
OnItemsCombined?.Invoke(itemData, heldPickup.itemData, resultItemData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
FollowerController?.TryPickupItem(gameObject, itemData);
|
||||
Interactable.BroadcastInteractionComplete(combinationResult == FollowerController.CombinationResult.NotApplicable);
|
||||
bool wasPickedUp = (combinationResult == FollowerController.CombinationResult.NotApplicable);
|
||||
Interactable.BroadcastInteractionComplete(wasPickedUp);
|
||||
|
||||
// Update pickup state and invoke event when the item was picked up successfully
|
||||
if (wasPickedUp)
|
||||
{
|
||||
isPickedUp = true;
|
||||
OnItemPickedUp?.Invoke(itemData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,58 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
[CreateAssetMenu(fileName = "PickupItemData", menuName = "Game/Pickup Item Data")]
|
||||
public class PickupItemData : ScriptableObject
|
||||
{
|
||||
[SerializeField] private string _itemId;
|
||||
|
||||
public string itemName;
|
||||
[TextArea]
|
||||
public string description;
|
||||
public Sprite mapSprite;
|
||||
|
||||
// Read-only property for itemId
|
||||
public string itemId => _itemId;
|
||||
|
||||
// Auto-generate ID on creation or validation
|
||||
void OnValidate()
|
||||
{
|
||||
// Only generate if empty
|
||||
if (string.IsNullOrEmpty(_itemId))
|
||||
{
|
||||
_itemId = GenerateItemId();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Mark the asset as dirty to ensure the ID is saved
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private string GenerateItemId()
|
||||
{
|
||||
// Use asset name as the basis for the ID to keep it somewhat readable
|
||||
string baseName = name.Replace(" ", "").ToLowerInvariant();
|
||||
|
||||
// Add a unique suffix based on a GUID
|
||||
string uniqueSuffix = Guid.NewGuid().ToString().Substring(0, 8);
|
||||
|
||||
return $"{baseName}_{uniqueSuffix}";
|
||||
}
|
||||
|
||||
// Method to manually regenerate ID if needed (for editor scripts)
|
||||
public void RegenerateId()
|
||||
{
|
||||
_itemId = GenerateItemId();
|
||||
}
|
||||
|
||||
public static bool AreEquivalent(PickupItemData a, PickupItemData b)
|
||||
{
|
||||
if (ReferenceEquals(a, b)) return true;
|
||||
if (a is null || b is null) return false;
|
||||
// First compare by itemId if available
|
||||
if (!string.IsNullOrEmpty(a.itemId) && !string.IsNullOrEmpty(b.itemId))
|
||||
return a.itemId == b.itemId;
|
||||
|
||||
// Compare by itemName as a fallback
|
||||
return a.itemName == b.itemName;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user