npcScript.cs 4.2 KB

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