Working slotting items

This commit is contained in:
Michal Pikulski
2025-09-29 11:15:37 +02:00
parent e6cb55975c
commit cee5d575b1
3 changed files with 274 additions and 81 deletions

View File

@@ -18,6 +18,13 @@ namespace Dialogue
private bool initialized = false;
private SpeechBubble speechBubble;
// Flag to track when a condition has been met but dialogue hasn't advanced yet
private bool _conditionSatisfiedPendingAdvance = false;
// Track the current slot state for WaitOnSlot nodes
private ItemSlotState _currentSlotState = ItemSlotState.None;
private PickupItemData _lastSlottedItem;
// Properties
public bool IsActive { get; private set; }
public bool IsCompleted { get; private set; }
@@ -36,6 +43,9 @@ namespace Dialogue
{
ItemManager.Instance.OnItemPickedUp += OnAnyItemPickedUp;
ItemManager.Instance.OnCorrectItemSlotted += OnAnyItemSlotted;
ItemManager.Instance.OnIncorrectItemSlotted += OnAnyIncorrectItemSlotted;
ItemManager.Instance.OnForbiddenItemSlotted += OnAnyForbiddenItemSlotted;
ItemManager.Instance.OnItemSlotCleared += OnAnyItemSlotCleared;
ItemManager.Instance.OnItemsCombined += OnAnyItemsCombined;
}
@@ -87,6 +97,9 @@ namespace Dialogue
{
ItemManager.Instance.OnItemPickedUp -= OnAnyItemPickedUp;
ItemManager.Instance.OnCorrectItemSlotted -= OnAnyItemSlotted;
ItemManager.Instance.OnIncorrectItemSlotted -= OnAnyIncorrectItemSlotted;
ItemManager.Instance.OnForbiddenItemSlotted -= OnAnyForbiddenItemSlotted;
ItemManager.Instance.OnItemSlotCleared -= OnAnyItemSlotCleared;
ItemManager.Instance.OnItemsCombined -= OnAnyItemsCombined;
}
}
@@ -133,17 +146,55 @@ namespace Dialogue
StartDialogue();
}
if (!IsActive || IsCompleted || currentNode == null || currentNode.dialogueLines.Count == 0)
if (!IsActive || IsCompleted || currentNode == null)
return string.Empty;
// For WaitOnSlot nodes, use the appropriate line type based on slot state
if (currentNode.nodeType == RuntimeDialogueNodeType.WaitOnSlot)
{
// Choose the appropriate line collection based on the current slot state
List<string> linesForState = currentNode.dialogueLines; // Default lines
switch (_currentSlotState)
{
case ItemSlotState.Incorrect:
// Use incorrect item lines if available, otherwise fall back to default lines
if (currentNode.incorrectItemLines != null && currentNode.incorrectItemLines.Count > 0)
linesForState = currentNode.incorrectItemLines;
break;
case ItemSlotState.Forbidden:
// Use forbidden item lines if available, otherwise fall back to default lines
if (currentNode.forbiddenItemLines != null && currentNode.forbiddenItemLines.Count > 0)
linesForState = currentNode.forbiddenItemLines;
break;
// For None or Correct state, use the default lines
default:
linesForState = currentNode.dialogueLines;
break;
}
// If we have lines for this state, return the current one
if (linesForState != null && linesForState.Count > 0)
{
// Make sure index is within bounds
int index = Mathf.Clamp(currentLineIndex, 0, linesForState.Count - 1);
return linesForState[index];
}
}
// For other node types, use the default dialogueLines
if (currentNode.dialogueLines == null || currentNode.dialogueLines.Count == 0)
return string.Empty;
// Get current line
string currentLine = string.Empty;
if (currentLineIndex >= 0 && currentLineIndex < currentNode.dialogueLines.Count)
{
currentLine = currentNode.dialogueLines[currentLineIndex];
}
Debug.Log("Returning line: " + currentLine);
// Return the current line
return currentLine;
}
@@ -155,6 +206,14 @@ namespace Dialogue
if (!IsActive || IsCompleted || currentNode == null)
return;
// If the condition was satisfied earlier, move to the next node immediately
if (_conditionSatisfiedPendingAdvance)
{
_conditionSatisfiedPendingAdvance = false; // Reset flag
MoveToNextNode();
return;
}
// If we have more lines in the current node, advance to the next line
if (currentLineIndex < currentNode.dialogueLines.Count - 1)
{
@@ -186,6 +245,8 @@ namespace Dialogue
private void MoveToNextNode()
{
Debug.Log("MoveToNextNode");
// If there's no next node, complete the dialogue
if (string.IsNullOrEmpty(currentNode.nextNodeID))
{
@@ -276,14 +337,10 @@ namespace Dialogue
// Check if this is the step we're waiting for
if (step.stepId == currentNode.puzzleStepID)
{
// Move to next node automatically when condition is met
MoveToNextNode();
// Instead of immediately moving to the next node, set the flag
_conditionSatisfiedPendingAdvance = true;
// Notify any listeners about the dialogue change
// string line = GetCurrentDialogueLine();
// OnDialogueChanged?.Invoke(line);
// Update bubble visibility after state change
// Update bubble visibility after state change to show interaction prompt
if (speechBubble != null)
{
speechBubble.UpdatePromptVisibility(HasAnyLines());
@@ -301,25 +358,15 @@ namespace Dialogue
// Check if this is the item we're waiting for
if (item.itemId == currentNode.pickupItemID)
{
// Move to next node automatically when condition is met
MoveToNextNode();
// Instead of immediately moving to the next node, set the flag
_conditionSatisfiedPendingAdvance = true;
// Notify any listeners about the dialogue change
// string line = GetCurrentDialogueLine();
// OnDialogueChanged?.Invoke(line);
// Update bubble visibility after state change
// Update bubble visibility after state change to show interaction prompt
if (speechBubble != null)
{
speechBubble.UpdatePromptVisibility(HasAnyLines());
}
}
// Always check if any dialogue was unblocked by this pickup
// if (speechBubble != null)
// {
// speechBubble.UpdatePromptVisibility(HasAnyLines());
// }
}
private void OnAnyItemSlotted(PickupItemData slotDefinition, PickupItemData slottedItem)
@@ -334,25 +381,15 @@ namespace Dialogue
// Check if this is the slot we're waiting for
if (slotDefinition.itemId == currentNode.slotItemID)
{
// Move to next node automatically when condition is met
MoveToNextNode();
// Instead of immediately moving to the next node, set the flag
_conditionSatisfiedPendingAdvance = true;
// Notify any listeners about the dialogue change
// string line = GetCurrentDialogueLine();
// OnDialogueChanged?.Invoke(line);
// Update bubble visibility after state change
// Update bubble visibility after state change to show interaction prompt
if (speechBubble != null)
{
speechBubble.UpdatePromptVisibility(HasAnyLines());
}
}
// Always check if any dialogue was unblocked by this slotting
// if (speechBubble != null)
// {
// speechBubble.UpdatePromptVisibility(HasAnyLines());
// }
}
private void OnAnyItemsCombined(PickupItemData itemA, PickupItemData itemB, PickupItemData resultItem)
@@ -365,25 +402,77 @@ namespace Dialogue
// Check if this is the result item we're waiting for
if (resultItem.itemId == currentNode.combinationResultItemID)
{
// Move to next node automatically when condition is met
MoveToNextNode();
// Instead of immediately moving to the next node, set the flag
_conditionSatisfiedPendingAdvance = true;
// Notify any listeners about the dialogue change
// string line = GetCurrentDialogueLine();
// OnDialogueChanged?.Invoke(line);
// Update bubble visibility after state change
// Update bubble visibility after state change to show interaction prompt
if (speechBubble != null)
{
speechBubble.UpdatePromptVisibility(HasAnyLines());
}
}
}
private void OnAnyIncorrectItemSlotted(PickupItemData slotDefinition, PickupItemData slottedItem)
{
// Update the slot state for displaying the correct dialogue lines
if (!IsActive || IsCompleted || currentNode == null)
return;
// Only update state if we're actively waiting on this slot
if (currentNode.nodeType == RuntimeDialogueNodeType.WaitOnSlot &&
slotDefinition.itemId == currentNode.slotItemID)
{
_currentSlotState = ItemSlotState.Incorrect;
_lastSlottedItem = slottedItem;
// Trigger dialogue update
if (speechBubble != null)
{
speechBubble.UpdatePromptVisibility(HasAnyLines());
}
}
}
private void OnAnyForbiddenItemSlotted(PickupItemData slotDefinition, PickupItemData slottedItem)
{
// Update the slot state for displaying the correct dialogue lines
if (!IsActive || IsCompleted || currentNode == null)
return;
// Only update state if we're actively waiting on this slot
if (currentNode.nodeType == RuntimeDialogueNodeType.WaitOnSlot &&
slotDefinition.itemId == currentNode.slotItemID)
{
_currentSlotState = ItemSlotState.Forbidden;
_lastSlottedItem = slottedItem;
// Trigger dialogue update
if (speechBubble != null)
{
speechBubble.UpdatePromptVisibility(HasAnyLines());
}
}
}
private void OnAnyItemSlotCleared(PickupItemData removedItem)
{
// Update the slot state when an item is removed
if (!IsActive || IsCompleted || currentNode == null)
return;
// Reset slot state if we were tracking this item
if (_lastSlottedItem != null && _lastSlottedItem == removedItem)
{
_currentSlotState = ItemSlotState.None;
_lastSlottedItem = null;
// Trigger dialogue update
if (speechBubble != null)
{
speechBubble.UpdatePromptVisibility(HasAnyLines());
}
}
// Always check if any dialogue was unblocked by this combination
// if (speechBubble != null)
// {
// speechBubble.UpdatePromptVisibility(HasAnyLines());
// }
}
// Helper methods
@@ -468,8 +557,47 @@ namespace Dialogue
if (!IsActive || IsCompleted || currentNode == null)
return false;
// Check if the current node has any lines
if (currentNode.dialogueLines.Count > 0)
// Special case: if condition has been satisfied but not yet advanced, we should show lines
if (_conditionSatisfiedPendingAdvance && !string.IsNullOrEmpty(currentNode.nextNodeID))
{
// Check if the next node would have lines
RuntimeDialogueNode nextNode = dialogueGraph.GetNodeByID(currentNode.nextNodeID);
return nextNode != null && (nextNode.dialogueLines.Count > 0 || nextNode.nodeType != RuntimeDialogueNodeType.End);
}
// For WaitOnSlot nodes, check for lines based on current slot state
if (currentNode.nodeType == RuntimeDialogueNodeType.WaitOnSlot)
{
// Choose the appropriate line collection based on the current slot state
List<string> linesForState = currentNode.dialogueLines; // Default lines
switch (_currentSlotState)
{
case ItemSlotState.Incorrect:
// Use incorrect item lines if available, otherwise fall back to default lines
if (currentNode.incorrectItemLines != null && currentNode.incorrectItemLines.Count > 0)
linesForState = currentNode.incorrectItemLines;
break;
case ItemSlotState.Forbidden:
// Use forbidden item lines if available, otherwise fall back to default lines
if (currentNode.forbiddenItemLines != null && currentNode.forbiddenItemLines.Count > 0)
linesForState = currentNode.forbiddenItemLines;
break;
}
// Check if we have any lines for the current state
if (linesForState != null && linesForState.Count > 0)
{
// If we're not at the end of the lines or we loop through them
if (currentLineIndex < linesForState.Count - 1 || currentNode.loopThroughLines)
{
return true;
}
}
}
// For other node types, use the standard check
else if (currentNode.dialogueLines.Count > 0)
{
// If we're not at the end of the lines or we loop through them
if (currentLineIndex < currentNode.dialogueLines.Count - 1 || currentNode.loopThroughLines)
@@ -486,12 +614,7 @@ namespace Dialogue
}
}
// Special case for conditional nodes waiting on conditions
// if (IsWaitingForCondition())
// {
// return currentNode.dialogueLines.Count > 0;
// }
return false;
}