Files
AppleHillsProduction/Assets/Scripts/Dialogue/DialogueComponent.cs

63 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Dialogue
{
public class DialogueComponent : MonoBehaviour
{
[SerializeField]
private RuntimeDialogueGraph runtimeGraph;
private Dictionary<string, RuntimeDialogueNode> _nodeLookup = new Dictionary<string, RuntimeDialogueNode>();
private RuntimeDialogueNode _currentNode;
private void Start()
{
foreach (var node in runtimeGraph.allNodes)
{
_nodeLookup[node.nodeID] = node;
}
if(string.IsNullOrEmpty(runtimeGraph.entryNodeID))
{
EndDialogue();
return;
}
ShowNode(runtimeGraph.entryNodeID);
}
private void Update()
{
if(Mouse.current.leftButton.wasPressedThisFrame && _currentNode != null)
{
if(string.IsNullOrEmpty(_currentNode.nextNodeID))
{
EndDialogue();
}
else
{
ShowNode(_currentNode.nextNodeID);
}
}
}
private void ShowNode(string nodeID)
{
if (!_nodeLookup.ContainsKey(nodeID))
{
EndDialogue();
return;
}
_currentNode = _nodeLookup[nodeID];
Debug.Log($"{runtimeGraph.speakerName}: {_currentNode.dialogueLine}");
}
private void EndDialogue()
{
Application.Quit();
}
}
}