using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class NPCDialogue : MonoBehaviour { public string name; public Text nameText; public Text nameText1; public Text nameText2; public Text dialogueText; //first dialogue public Dialogue dialogue; //dialogue during quest public Dialogue dialogueWQuest; //dialotgue after quest public Dialogue dialogueAQuest; //first dialogue public Dialogue dialoguePolish; //dialogue during quest public Dialogue dialogueWQuestPolish; //dialotgue after quest public Dialogue dialogueAQuestPolish; private string language; private Queue sentences; private Queue sentencesWQuest; private Queue sentencesAQuest; public GameObject Panel; public string requiredItem; public GameObject QuestPanel; public GameObject FinishQuestPanel; bool triggered = false; public int isQuest; // 0 - NPC no quest // 1 - NPC with quest that hasnt been started // 2 - NPC with ongoing quest // 3 - NPC with quest that has been finished void Awake() { requiredItem = "Lumberjac Axe"; } void Start() { sentences = new Queue(); sentencesWQuest = new Queue(); sentencesAQuest = new Queue(); } public void TriggerDialogue() { language = PlayerPrefs.GetString("language"); if (isQuest == 2) { if (PlayerPrefs.HasKey("language")) { language = PlayerPrefs.GetString("language"); if (language == "English") { StartDialogue(dialogueWQuest); } else if (language == "Polish") { StartDialogue(dialogueWQuestPolish); } } else { StartDialogue(dialogueWQuest); } } else if (isQuest == 3) { if (PlayerPrefs.HasKey("language")) { language = PlayerPrefs.GetString("language"); if (language == "English") { StartDialogue(dialogueAQuest); } else if (language == "Polish") { StartDialogue(dialogueAQuestPolish); } } else { StartDialogue(dialogueAQuest); } } else { if (PlayerPrefs.HasKey("language")) { language = PlayerPrefs.GetString("language"); if (language == "English") { StartDialogue(dialogue); } else if (language == "Polish") { StartDialogue(dialoguePolish); } } else { StartDialogue(dialogue); } } } void OnTriggerExit2D(Collider2D collision) { if (Panel != null) { Panel.SetActive(false); QuestPanel.SetActive(false); FinishQuestPanel.SetActive(false); } triggered = false; } private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Player") { triggered = true; } } void Update() { if (triggered) { if (Input.GetKeyDown(KeyCode.E)) { if (Panel != null) { Panel.SetActive(true); } if (isQuest == 2) { Debug.Log("before"); var questItem = InventoryManager.Instance.FindItemInWarehouse(requiredItem); if(!questItem.Equals(new KeyValuePair())) { FinishQuestPanel.SetActive(true); Panel.SetActive(false); } else { if (PlayerPrefs.HasKey("language")) { language = PlayerPrefs.GetString("language"); if (language == "English") { StartDialogue(dialogueWQuest); } else if (language == "Polish") { StartDialogue(dialogueWQuestPolish); } } else { StartDialogue(dialogueWQuest); } } } else if (isQuest == 3) { if (PlayerPrefs.HasKey("language")) { language = PlayerPrefs.GetString("language"); if (language == "English") { StartDialogue(dialogueAQuest); } else if (language == "Polish") { StartDialogue(dialogueAQuestPolish); } } else { StartDialogue(dialogueAQuest); } } else { if (PlayerPrefs.HasKey("language")) { language = PlayerPrefs.GetString("language"); if (language == "English") { StartDialogue(dialogue); } else if (language == "Polish") { StartDialogue(dialoguePolish); } } else { StartDialogue(dialogue); } } } } } public void StartDialogue(Dialogue dialogue) { nameText.text = dialogue.name; nameText1.text = dialogue.name; nameText2.text = dialogue.name; sentences.Clear(); foreach(string sentence in dialogue.sentences) { sentences.Enqueue(sentence); } DisplayNextSentence(); } public void DisplayNextSentence() { if (isQuest == 2) { if (sentences.Count == 0) { var questItem = InventoryManager.Instance.FindItemInWarehouse(requiredItem); if(!questItem.Equals(new KeyValuePair())) { FinishQuestPanel.SetActive(true); Panel.SetActive(false); return; } else { EndDialogue(); return; } } string sentence = sentences.Dequeue(); dialogueText.text = sentence; } else if (isQuest == 0) { if (sentences.Count == 0) { EndDialogue(); return; } string sentence = sentences.Dequeue(); dialogueText.text = sentence; } else if (isQuest == 3) { if (sentences.Count == 0) { EndDialogue(); return; } string sentence = sentences.Dequeue(); dialogueText.text = sentence; } else if (isQuest == 1) { if (sentences.Count == 0) { QuestPanel.SetActive(true); Panel.SetActive(false); return; } string sentence = sentences.Dequeue(); dialogueText.text = sentence; } } public void EndDialogue() { Panel.SetActive(false); QuestPanel.SetActive(false); FinishQuestPanel.SetActive(false); } public void QuestAccepted() { isQuest = 2; QuestPanel.SetActive(false); if(!gameObject.GetComponent().IsTaskAccepted()) { // 1. Add task to palyer quests list Task myTask = gameObject.GetComponent().AcceptTask(); TaskManager.Instance.AddTask(myTask); // 2. Drop Axe On Map gameObject.GetComponent().DropItem(); } else { Debug.Log("Ten task został juz rozpoczęty!"); } } public void FinishQuest() { // 1. Take item from palyer var questItem = InventoryManager.Instance.FindItemInWarehouse(requiredItem); InventoryManager.Instance.RemoveItemFromPosition(questItem.Key); // 2. Set as finished isQuest = 3; EndDialogue(); } }