using System.Collections; using System.Collections.Generic; using System.Xml.Serialization; using TMPro; using UnityEngine; using UnityEngine.UI; public class npcScript : MonoBehaviour { public static npcScript activeNpc; public GameObject npcPanel; public GameObject textBtn; public TMP_Text npcText; // public string[] texts; public QuestScriptable[] questData; public int activeQuest=0; public int questLineIndex; //public GameObject questUI; public float textspeed = 0.10f; public bool isPlayerClose; void Update() { if (isPlayerClose) { } if (npcText.text == questData[activeQuest].questLines[questLineIndex]) { textBtn.SetActive(true); } } bool isLoadingText = false; IEnumerator textLoad() { isLoadingText = true; Debug.Log($"Reading line {questLineIndex} from quest {activeQuest}", gameObject); foreach (char letter in questData[activeQuest].questLines[questLineIndex].ToCharArray()) { npcText.text += letter; yield return new WaitForSecondsRealtime(textspeed); } isLoadingText = false; } Coroutine textLoopAsync; public void NextLine() { textBtn.SetActive(false); if (questLineIndex < questData[activeQuest].questLines.Length - 1) { questLineIndex++; npcText.text = ""; LoadText(); } else { ResetTexts(); //Start quest // questUI.SetActive(true); playerNetwork.localPlayerTransform.GetComponent().SetActiveQuest(questData[activeQuest]); } } void ResetTexts(){ npcText.text = ""; npcPanel.SetActive(false); } private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { Debug.Log($"Player entered into {gameObject.name}",gameObject); if (other.transform == playerNetwork.localPlayerTransform) { playerNetwork pnet = playerNetwork.localPlayerTransform.GetComponent(); if (pnet.currentQuest == questData[activeQuest]) //Already doing this quest { 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; // } // } if(pnet.completedQuests.Contains(questData[activeQuest].questName)){ //Set next quest if current one is completed for(int i=0; i < questData.Length; i++){ if(pnet.completedQuests.Contains(questData[i].questName)){ continue; } activeQuest = i; break; } } isPlayerClose = true; activeNpc= this; if (npcPanel.activeInHierarchy) { ResetTexts(); } else { questLineIndex=0; npcPanel.SetActive(true); LoadText(); } } } } void LoadText() { if(!isPlayerClose){ Debug.Log("This player aint in my area", gameObject); return; } npcText.text = ""; if (textLoopAsync != null) { StopCoroutine(textLoopAsync); } Debug.Log("request quest line text load",gameObject); textLoopAsync = StartCoroutine(textLoad()); } private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Player")) { if (other.transform == playerNetwork.localPlayerTransform) { isPlayerClose = false; ResetTexts(); } } } }