npcScript.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  15. private int index;
  16. //public GameObject questUI;
  17. public float textspeed = 0.10f;
  18. public bool isPlayerClose;
  19. void Update()
  20. {
  21. if(isPlayerClose){
  22. // if(npcPanel.activeInHierarchy){
  23. // ResetTexts();
  24. // }else{
  25. // npcPanel.SetActive(true);
  26. // StartCoroutine(textLoad());
  27. // }
  28. }
  29. if(npcText.text == questData[activeQuest].questLines[index]){
  30. textBtn.SetActive(true);
  31. }
  32. }
  33. public void ResetTexts(){
  34. npcPanel.SetActive(false);
  35. npcText.text = "";
  36. index = 0;
  37. }
  38. bool isLooping = false;
  39. IEnumerator textLoad(){
  40. isLooping = true;
  41. foreach(char letter in questData[activeQuest].questLines[index].ToCharArray()){
  42. npcText.text += letter;
  43. yield return new WaitForSecondsRealtime(textspeed);
  44. }
  45. isLooping=false;
  46. }
  47. Coroutine textLoopAsync ;
  48. public void NextLine(){
  49. textBtn.SetActive(false);
  50. if(index < questData[activeQuest].questLines.Length -1 ){
  51. index++;
  52. npcText.text = "";
  53. LoadText();
  54. }else{
  55. ResetTexts();
  56. //Start quest
  57. // questUI.SetActive(true);
  58. playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().QuestFunction(questData[activeQuest]);
  59. }
  60. }
  61. private void OnTriggerEnter2D(Collider2D other) {
  62. if(other.CompareTag("Player")){
  63. if(other.transform == playerNetwork.localPlayerTransform){
  64. if(playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().currentQuest == questData[activeQuest]){
  65. return;
  66. }
  67. for(int i =0; i < questData.Length; i++){
  68. bool isFound = false;
  69. foreach(string questName in playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().completedQuests){
  70. if(questName == questData[i].name){
  71. isFound = true;
  72. }
  73. }
  74. if(!isFound){
  75. activeQuest = i;
  76. break;
  77. }
  78. }
  79. isPlayerClose = true;
  80. if(npcPanel.activeInHierarchy){
  81. ResetTexts();
  82. }else{
  83. npcPanel.SetActive(true);
  84. LoadText();
  85. }
  86. }
  87. }
  88. }
  89. void LoadText(){
  90. npcText.text = "";
  91. if(textLoopAsync!=null){
  92. StopCoroutine(textLoopAsync);
  93. }
  94. textLoopAsync = StartCoroutine(textLoad());
  95. }
  96. private void OnTriggerExit2D(Collider2D other) {
  97. if(other.CompareTag("Player")){
  98. if(other.transform == playerNetwork.localPlayerTransform){
  99. isPlayerClose = false;
  100. ResetTexts();
  101. }
  102. }
  103. }
  104. }