using System.Collections; using System.Collections.Generic; using System.Xml.Serialization; using TMPro; using UnityEngine; using UnityEngine.UI; public class npcScript : MonoBehaviour { public GameObject npcPanel; public GameObject textBtn; public TMP_Text npcText; // public string[] texts; public QuestScriptable [] questData; public int activeQuest; private int index; //public GameObject questUI; public float textspeed = 0.10f; public bool isPlayerClose; void Update() { if(isPlayerClose){ // if(npcPanel.activeInHierarchy){ // ResetTexts(); // }else{ // npcPanel.SetActive(true); // StartCoroutine(textLoad()); // } } if(npcText.text == questData[activeQuest].questLines[index]){ textBtn.SetActive(true); } } public void ResetTexts(){ npcPanel.SetActive(false); npcText.text = ""; index = 0; } bool isLooping = false; IEnumerator textLoad(){ isLooping = true; foreach(char letter in questData[activeQuest].questLines[index].ToCharArray()){ npcText.text += letter; yield return new WaitForSecondsRealtime(textspeed); } isLooping=false; } Coroutine textLoopAsync ; public void NextLine(){ textBtn.SetActive(false); if(index < questData[activeQuest].questLines.Length -1 ){ index++; npcText.text = ""; LoadText(); }else{ ResetTexts(); //Start quest // questUI.SetActive(true); playerNetwork.localPlayerTransform.GetComponent().QuestFunction(questData[activeQuest]); } } private void OnTriggerEnter2D(Collider2D other) { if(other.CompareTag("Player")){ if(other.transform == playerNetwork.localPlayerTransform){ if(playerNetwork.localPlayerTransform.GetComponent().currentQuest == questData[activeQuest]){ return; } for(int i =0; i < questData.Length; i++){ bool isFound = false; foreach(string questName in playerNetwork.localPlayerTransform.GetComponent().completedQuests){ if(questName == questData[i].name){ isFound = true; } } if(!isFound){ activeQuest = i; break; } } isPlayerClose = true; if(npcPanel.activeInHierarchy){ ResetTexts(); }else{ npcPanel.SetActive(true); LoadText(); } } } } void LoadText(){ npcText.text = ""; if(textLoopAsync!=null){ StopCoroutine(textLoopAsync); } textLoopAsync = StartCoroutine(textLoad()); } private void OnTriggerExit2D(Collider2D other) { if(other.CompareTag("Player")){ if(other.transform == playerNetwork.localPlayerTransform){ isPlayerClose = false; ResetTexts(); } } } }