npcScript.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Xml.Serialization;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class npcScript : MonoBehaviour
  8. {
  9. public static npcScript activeNpc;
  10. public GameObject npcPanel;
  11. public GameObject textBtn;
  12. public TMP_Text npcText;
  13. // public string[] texts;
  14. public QuestScriptable[] questData;
  15. public int activeQuest=0;
  16. public int questLineIndex;
  17. //public GameObject questUI;
  18. public float textspeed = 0.15f;
  19. public bool isPlayerClose;
  20. [SerializeField] private BoxCollider2D rtrnActionCollider;
  21. void Update()
  22. {
  23. if (isPlayerClose)
  24. {
  25. }
  26. if (npcText.text == questData[activeQuest].questLines[questLineIndex])
  27. {
  28. textBtn.SetActive(true);
  29. }
  30. }
  31. bool isLoadingText = false;
  32. IEnumerator textLoad()
  33. {
  34. isLoadingText = true;
  35. Debug.Log($"Reading line {questLineIndex} from quest {activeQuest}", gameObject);
  36. foreach (char letter in questData[activeQuest].questLines[questLineIndex].ToCharArray())
  37. {
  38. npcText.text += letter;
  39. yield return new WaitForSecondsRealtime(textspeed);
  40. }
  41. isLoadingText = false;
  42. }
  43. Coroutine textLoopAsync;
  44. public void NextLine()
  45. {
  46. textBtn.SetActive(false);
  47. if (questLineIndex < questData[activeQuest].questLines.Length - 1)
  48. {
  49. questLineIndex++;
  50. npcText.text = "";
  51. LoadText();
  52. }
  53. else
  54. {
  55. ResetTexts();
  56. //Start quest
  57. // questUI.SetActive(true);
  58. playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().SetActiveQuest(questData[activeQuest]); // set the quest
  59. rtrnActionCollider.isTrigger = false;
  60. }
  61. }
  62. void ResetTexts(){
  63. npcText.text = "";
  64. npcPanel.SetActive(false);
  65. }
  66. private void OnTriggerEnter2D(Collider2D other)
  67. {
  68. if (other.CompareTag("Player"))
  69. {
  70. Debug.Log($"Player entered into {gameObject.name}",gameObject);
  71. if (other.transform == playerNetwork.localPlayerTransform)
  72. {
  73. playerNetwork pnet = playerNetwork.localPlayerTransform.GetComponent<playerNetwork>();
  74. if (pnet.currentQuest == questData[activeQuest]) //Already doing this quest
  75. {
  76. return;
  77. }
  78. // for (int i = 0; i < questData.Length; i++)
  79. // {
  80. // bool isFound = false;
  81. // foreach (string questName in playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().completedQuests)
  82. // {
  83. // if (questName == questData[i].name)
  84. // {
  85. // isFound = true;
  86. // }
  87. // }
  88. // if (!isFound)
  89. // {
  90. // activeQuest = i;
  91. // break;
  92. // }
  93. // }
  94. if(pnet.completedQuests.Contains(questData[activeQuest].questName)){ //Set next quest if current one is completed
  95. for(int i=0; i < questData.Length; i++){
  96. if(pnet.completedQuests.Contains(questData[i].questName)){
  97. continue;
  98. }
  99. activeQuest = i;
  100. break;
  101. }
  102. }
  103. isPlayerClose = true;
  104. activeNpc= this;
  105. if (npcPanel.activeInHierarchy)
  106. {
  107. ResetTexts();
  108. }
  109. else
  110. {
  111. questLineIndex=0;
  112. npcPanel.SetActive(true);
  113. LoadText();
  114. }
  115. }
  116. }
  117. }
  118. void LoadText()
  119. {
  120. if(!isPlayerClose){
  121. Debug.Log("This player aint in my area", gameObject);
  122. return;
  123. }
  124. npcText.text = "";
  125. if (textLoopAsync != null)
  126. {
  127. StopCoroutine(textLoopAsync);
  128. }
  129. Debug.Log("request quest line text load",gameObject);
  130. textLoopAsync = StartCoroutine(textLoad());
  131. }
  132. private void OnTriggerExit2D(Collider2D other)
  133. {
  134. if (other.CompareTag("Player"))
  135. {
  136. if (other.transform == playerNetwork.localPlayerTransform)
  137. {
  138. isPlayerClose = false;
  139. ResetTexts();
  140. }
  141. }
  142. }
  143. }