Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.2 kB
6
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Ink.Runtime;
using UnityEngine.EventSystems;

public class DialogueManager : MonoBehaviour
{
	
	[Header("Dialogue UI")]
	[SerializeField] private GameObject dialoguePanel;
	[SerializeField] private TextMeshProUGUI dialogueText;
	
	[Header("Choices UI")]
	[SerializeField] private GameObject[] choices;
	
	private TextMeshProUGUI[] choicesText;
	
	private Story currentStory;
	
	public bool dialogueIsPlaying {get; private set; }
	
	private static DialogueManager instance;
	
	private void Awake()
	{
		if (instance != null)
		{
			Debug.LogWarning("Found more than one Dialogue Manager in the scene");
		}
		instance = this;
	}
	
	public static DialogueManager GetInstance()
	{
		return instance;
	}
	
	private void Start()
	{
		dialogueIsPlaying = false;
		dialoguePanel.SetActive(false);

		choicesText = new TextMeshProUGUI[choices.Length];
		int index = 0;
		foreach(GameObject choice in choices)
		{
			choicesText[index] = choice.GetComponentInChildren<TextMeshProUGUI>();
			index++;
		}
	}
	
	private void Update()
	{	
		if (!dialogueIsPlaying)
		{
			return;
		}
		
		if(currentStory.currentChoices.Count == 0 && Input.GetKeyDown(KeyCode.B))
		{
			ContinueStory();
		}
		
		
	}
	
	public void EnterdialogueMode(TextAsset inkJSON)
	{
		currentStory = new Story(inkJSON.text);
		dialogueIsPlaying = true;
		dialoguePanel.SetActive(true);
		
		ContinueStory();
	}
	
	private IEnumerator ExitDialogueMode()
	{
		yield return new WaitForSeconds(0.2f);
		
		dialogueIsPlaying = false;
		dialoguePanel.SetActive(false);
		dialogueText.text = "";
		Debug.Log("ExitDialogueMode");
	}
	
	private void ContinueStory()
	{
		Debug.Log("Continue Story Called");
		if(currentStory.canContinue)
		{
			dialogueText.text = currentStory.Continue();
			
			DisplayChoices();
		}
		else 
		{
			StartCoroutine(ExitDialogueMode());
			Debug.Log("StartCoroutine)");
		}
	}
	
	private void DisplayChoices()
	{
		List<Choice> currentChoices = currentStory.currentChoices;
		
		if (currentChoices.Count > choices.Length)
		{
			Debug.LogError("More choices were given than the UI can support. Number of choices given: " +  currentChoices.Count);
		}
		
		int index = 0;
		
		foreach(Choice choice in currentChoices)
		{
			choices[index].gameObject.SetActive(true);
			choicesText[index].text = choice.text;
			index++;
		}
		
		for (int i = index; i < choices.Length; i++)
		{
			choices[i].gameObject.SetActive(false);
		}
		
		StartCoroutine(SelectFirstChoice());
	}
	
	private IEnumerator SelectFirstChoice() 
	{
		// Event System requires we clear it first, then wait
		// for at least one frame before we set the current selected object.
		EventSystem.current.SetSelectedGameObject(null);
		yield return new WaitForEndOfFrame();
		EventSystem.current.SetSelectedGameObject(choices[0].gameObject);
	}
	
		public void MakeChoice(int choiceIndex)
	{
		currentStory.ChooseChoiceIndex(choiceIndex);
		Input.GetKeyDown(KeyCode.Return); // this is specific to my InputManager script
		ContinueStory();
	}
}