Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Quest NPC / TalkQuest.cs
@Rackday Rackday on 18 Aug 2024 4 KB Project Added
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class TalkQuest : MonoBehaviour
  6. {
  7. public GameObject chatPrefab;
  8. public GameObject chatBoxPrefab;
  9. public GameObject buttonPrefab;
  10. private Quest quest;
  11. private RangedArea rangedArea;
  12. private RectTransform WorldcanvasTransform;
  13. private RectTransform UIcanvasTransform;
  14. private GameObject chat;
  15. private GameObject chatBox;
  16. private GameObject button;
  17. private GameObject button2;
  18. private bool declined;
  19. [HideInInspector] public bool accepted;
  20. [HideInInspector] public bool NPCchatEnabled;
  21. private string[] npcPhrases;
  22. private int npcPhrasesIndex;
  23. private Vector3 pos;
  24. // Start is called before the first frame update
  25. void Start()
  26. {
  27. rangedArea = FindObjectOfType<RangedArea>();
  28. WorldcanvasTransform = GameObject.FindGameObjectWithTag("WorldCanvas").GetComponent<RectTransform>();
  29. UIcanvasTransform = GameObject.FindGameObjectWithTag("UICanvas").GetComponent<RectTransform>();
  30. npcPhrasesIndex = 0;
  31. NPCchatEnabled = false;
  32. accepted = false;
  33. declined = false;
  34. //Cursor.visible = false;
  35. }
  36. private void chatTalk()
  37. {
  38. //Debug.Log(quest.questActivated);
  39. pos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
  40. if ((Input.GetKeyDown(KeyCode.E)) && (rangedArea.playerInRange == true) && (NPCchatEnabled == false) /*(quest.questActivated == false)*/)
  41. {
  42. chatBox = Instantiate(chatBoxPrefab, new Vector3(pos.x, pos.y + 2, pos.z), Quaternion.identity, WorldcanvasTransform);
  43. chat = Instantiate(chatPrefab, new Vector3(pos.x, pos.y + 2.3f, pos.z), Quaternion.identity, WorldcanvasTransform);
  44. button = Instantiate(buttonPrefab, new Vector3(pos.x + 2, pos.y + 1.5f, pos.z), Quaternion.identity, WorldcanvasTransform);
  45. npcPhrases = new string[] { "Hello, i need your help!",
  46. "There is a huge monster in the forest.",
  47. "can you kill him for me?"};
  48. chat.GetComponent<TypeWriting>().textToWrite = npcPhrases[npcPhrasesIndex];
  49. button.GetComponentInChildren<Text>().text = "-->";
  50. NPCchatEnabled = true;
  51. //Cursor.visible = true;
  52. button.GetComponent<Button>().onClick.AddListener(TestArray);
  53. }
  54. else if ((rangedArea.playerInRange == false) || (Input.GetKeyDown(KeyCode.E) && NPCchatEnabled == true) || declined == true || accepted == true)
  55. {
  56. Object.Destroy(chat);
  57. Object.Destroy(chatBox);
  58. Object.Destroy(button);
  59. Object.Destroy(button2);
  60. NPCchatEnabled = false;
  61. declined = false;
  62. //Cursor.visible = false;
  63. }
  64. }
  65. private void TestArray()
  66. {
  67. if (npcPhrasesIndex < npcPhrases.Length - 1)
  68. {
  69. Destroy(chat);
  70. chat = Instantiate(chatPrefab, new Vector3(pos.x, pos.y + 2.3f, pos.z), Quaternion.identity, WorldcanvasTransform);
  71. npcPhrasesIndex++;
  72. chat.GetComponent<TypeWriting>().textToWrite = npcPhrases[npcPhrasesIndex];
  73. chat.GetComponent<Text>().text = npcPhrases[npcPhrasesIndex];
  74. Debug.Log(npcPhrasesIndex);
  75. }
  76. if (npcPhrasesIndex == 2)
  77. {
  78. Destroy(button);
  79. button = Instantiate(buttonPrefab, new Vector3(pos.x + 2, pos.y + 1.5f, pos.z), Quaternion.identity, WorldcanvasTransform);
  80. button2 = Instantiate(buttonPrefab, new Vector3(pos.x - 2, pos.y + 1.5f, pos.z), Quaternion.identity, WorldcanvasTransform);
  81. button.GetComponent<Button>().onClick.AddListener(AcceptQuest);
  82. button2.GetComponent<Button>().onClick.AddListener(DeclineQuest);
  83. button.GetComponentInChildren<Text>().text = "Yes";
  84. button2.GetComponentInChildren<Text>().text = "No";
  85. }
  86. }
  87. private void AcceptQuest()
  88. {
  89. accepted = true;
  90. }
  91. private void DeclineQuest()
  92. {
  93. declined = true;
  94. npcPhrasesIndex = 0;
  95. }
  96. void Update()
  97. {
  98. chatTalk();
  99. }
  100. }