npcScript.cs 4.6 KB

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