add basic dialogue system and interactable interface
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class DialogueManager : MonoBehaviour
|
||||
{
|
||||
private Queue<string> sentences;
|
||||
private Dialogue currentDialogue;
|
||||
|
||||
public TextMeshProUGUI nameText;
|
||||
public TextMeshProUGUI dialogueText;
|
||||
|
||||
public Animator animator;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
sentences = new Queue<string>();
|
||||
}
|
||||
|
||||
public void StartDialogue(Dialogue dialogue)
|
||||
{
|
||||
animator.SetBool("IsOpen", true);
|
||||
|
||||
sentences.Clear();
|
||||
currentDialogue = dialogue;
|
||||
nameText.text = dialogue.charName;
|
||||
|
||||
foreach (string sentence in dialogue.sentences)
|
||||
{
|
||||
sentences.Enqueue(sentence);
|
||||
}
|
||||
|
||||
currentDialogue.isActive = true;
|
||||
DisplayNextSentence();
|
||||
}
|
||||
|
||||
public void DisplayNextSentence()
|
||||
{
|
||||
if (sentences.Count == 0)
|
||||
{
|
||||
EndDialogue();
|
||||
return;
|
||||
}
|
||||
|
||||
string sentence = sentences.Dequeue();
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(TypeSentence(sentence));
|
||||
}
|
||||
|
||||
public void EndDialogue()
|
||||
{
|
||||
animator.SetBool("IsOpen", false);
|
||||
|
||||
if (currentDialogue != null)
|
||||
{
|
||||
currentDialogue.isActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator TypeSentence(string sentence)
|
||||
{
|
||||
dialogueText.text = "";
|
||||
foreach (char c in sentence.ToCharArray())
|
||||
{
|
||||
dialogueText.text += c;
|
||||
yield return new WaitForSeconds(0.01f);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user