Scriptum/Assets/Scripts/NPCs' Scripts/NPCDialogue.cs
2022-12-06 02:39:33 +01:00

342 lines
9.4 KiB
C#

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 DialogueNpcTest dialogue;
//dialogue during quest
public DialogueNpcTest dialogueWQuest;
//dialotgue after quest
public DialogueNpcTest dialogueAQuest;
//first dialogue
public DialogueNpcTest dialoguePolish;
//dialogue during quest
public DialogueNpcTest dialogueWQuestPolish;
//dialotgue after quest
public DialogueNpcTest dialogueAQuestPolish;
private string language;
private Queue<string> sentences;
private Queue<string> sentencesWQuest;
private Queue<string> 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 = "Axe";
}
void Start()
{
sentences = new Queue<string>();
sentencesWQuest = new Queue<string>();
sentencesAQuest = new Queue<string>();
}
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)
{
var questItem = InventoryUIManager.Instance.FindItemInWarehouseByName(requiredItem);
if(!questItem.Equals(new KeyValuePair<int, Item>()))
{
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(DialogueNpcTest 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 = InventoryUIManager.Instance.FindItemInWarehouseByName(requiredItem);
if(!questItem.Equals(new KeyValuePair<int, Item>()))
{
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<NPCQuest>().IsTaskAccepted())
{
// 1. Add task to palyer quests list
Task myTask = gameObject.GetComponent<NPCQuest>().AcceptTask();
TaskUIManager.Instance.Add(myTask);
// 2. Drop Axe On Map
gameObject.GetComponent<NPCQuest>().DropItem();
} else {
Debug.Log("Ten task został juz rozpoczęty!");
}
}
public void FinishQuest()
{
// 1. Take item from palyer
var questItem = InventoryUIManager.Instance.FindItemInWarehouseByName(requiredItem);
InventoryUIManager.Instance.RemoveByPosition(questItem[0].Key);
// 3. remove quest from player list
var task = TaskUIManager.Instance.FindTaskByName(gameObject.GetComponent<NPCQuest>().quest.Title);
if (task.Count == 0)
Debug.LogError($"Task '{gameObject.GetComponent<NPCQuest>().quest.Title}' not found");
else
TaskUIManager.Instance.RemoveByName(gameObject.GetComponent<NPCQuest>().quest.Title);
// 3. Set as finished
isQuest = 3;
EndDialogue();
}
}