Scriptum/Assets/Scripts/NPCs' Scripts/NPCDialogue.cs

342 lines
9.4 KiB
C#
Raw Permalink Normal View History

2022-05-25 17:11:49 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NPCDialogue : MonoBehaviour
{
public string name;
2022-05-25 17:11:49 +02:00
public Text nameText;
public Text nameText1;
public Text nameText2;
2022-05-25 17:11:49 +02:00
public Text dialogueText;
//first dialogue
2022-12-04 18:42:34 +01:00
public DialogueNpcTest dialogue;
//dialogue during quest
2022-12-04 18:42:34 +01:00
public DialogueNpcTest dialogueWQuest;
//dialotgue after quest
2022-12-04 18:42:34 +01:00
public DialogueNpcTest dialogueAQuest;
//first dialogue
2022-12-04 18:42:34 +01:00
public DialogueNpcTest dialoguePolish;
//dialogue during quest
2022-12-04 18:42:34 +01:00
public DialogueNpcTest dialogueWQuestPolish;
//dialotgue after quest
2022-12-04 18:42:34 +01:00
public DialogueNpcTest dialogueAQuestPolish;
private string language;
2022-05-25 17:11:49 +02:00
private Queue<string> sentences;
private Queue<string> sentencesWQuest;
private Queue<string> sentencesAQuest;
2022-05-25 17:11:49 +02:00
public GameObject Panel;
public string requiredItem;
public GameObject QuestPanel;
public GameObject FinishQuestPanel;
2022-05-25 17:11:49 +02:00
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
2022-06-20 22:31:48 +02:00
void Awake()
{
2022-12-06 01:58:32 +01:00
requiredItem = "Axe";
2022-06-20 22:31:48 +02:00
}
2022-05-25 17:11:49 +02:00
void Start()
{
sentences = new Queue<string>();
sentencesWQuest = new Queue<string>();
sentencesAQuest = new Queue<string>();
2022-05-25 17:11:49 +02:00
}
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);
}
}
2022-05-25 17:11:49 +02:00
}
void OnTriggerExit2D(Collider2D collision)
{
if (Panel != null)
{
Panel.SetActive(false);
QuestPanel.SetActive(false);
FinishQuestPanel.SetActive(false);
2022-05-25 17:11:49 +02:00
}
triggered = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
triggered = true;
}
}
void Update()
{
2022-05-25 17:11:49 +02:00
if (triggered)
{
if (Input.GetKeyDown(KeyCode.E))
{
if (Panel != null)
{
Panel.SetActive(true);
}
if (isQuest == 2)
{
var questItem = InventoryUIManager.Instance.FindItemInWarehouseByName(requiredItem);
2022-06-20 22:31:48 +02:00
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);
}
2022-06-20 22:31:48 +02:00
}
}
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);
}
}
2022-05-25 17:11:49 +02:00
}
}
}
2022-12-04 18:42:34 +01:00
public void StartDialogue(DialogueNpcTest dialogue)
2022-05-25 17:11:49 +02:00
{
nameText.text = dialogue.name;
nameText1.text = dialogue.name;
nameText2.text = dialogue.name;
2022-05-25 17:11:49 +02:00
sentences.Clear();
foreach(string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
2022-05-25 17:11:49 +02:00
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (isQuest == 2)
2022-05-25 17:11:49 +02:00
{
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
{
2022-06-20 22:31:48 +02:00
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;
2022-05-25 17:11:49 +02:00
}
}
public void EndDialogue()
2022-05-25 17:11:49 +02:00
{
Panel.SetActive(false);
QuestPanel.SetActive(false);
FinishQuestPanel.SetActive(false);
}
2022-06-13 23:33:19 +02:00
public void QuestAccepted()
{
isQuest = 2;
QuestPanel.SetActive(false);
2022-06-13 23:33:19 +02:00
if(!gameObject.GetComponent<NPCQuest>().IsTaskAccepted())
{
// 1. Add task to palyer quests list
2022-06-13 23:33:19 +02:00
Task myTask = gameObject.GetComponent<NPCQuest>().AcceptTask();
2022-10-22 18:04:21 +02:00
TaskUIManager.Instance.Add(myTask);
// 2. Drop Axe On Map
gameObject.GetComponent<NPCQuest>().DropItem();
2022-06-13 23:33:19 +02:00
} else {
Debug.Log("Ten task został juz rozpoczęty!");
}
2022-05-25 17:11:49 +02:00
}
public void FinishQuest()
{
2022-06-20 22:31:48 +02:00
// 1. Take item from palyer
var questItem = InventoryUIManager.Instance.FindItemInWarehouseByName(requiredItem);
2022-06-20 22:31:48 +02:00
InventoryUIManager.Instance.RemoveByPosition(questItem[0].Key);
2022-06-20 22:31:48 +02:00
2022-11-27 21:28:55 +01:00
// 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();
}
2022-05-25 17:11:49 +02:00
}