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

187 lines
4.0 KiB
C#
Raw 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;
2022-05-25 17:11:49 +02:00
public Text dialogueText;
public Dialogue dialogue;
public Dialogue dialogueWQuest;
public Dialogue dialogueAQuest;
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 GameObject QuestPanel;
2022-05-25 17:11:49 +02:00
bool triggered = false;
public int isQuest;
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
2022-05-25 17:11:49 +02:00
}
public void TriggerDialogue()
{
if (isQuest == 2)
{
StartDialogue(dialogueWQuest);
}
else if (isQuest == 3)
{
StartDialogue(dialogueAQuest);
}
else
{
StartDialogue(dialogue);
}
2022-05-25 17:11:49 +02:00
}
void OnTriggerExit2D(Collider2D collision)
{
if (Panel != null)
{
Panel.SetActive(false);
}
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)
{
StartDialogue(dialogueWQuest);
}
else if (isQuest == 2)
{
StartDialogue(dialogueAQuest);
}
else
{
StartDialogue(dialogue);
}
2022-05-25 17:11:49 +02:00
}
}
}
public void StartDialogue(Dialogue dialogue)
{
nameText.text = dialogue.name;
nameText1.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)
{
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 == 4)
{
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);
}
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
}
2022-05-25 17:11:49 +02:00
}