Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class NPC : MonoBehaviour
- {
- public bool playerInRange;
- public bool isTalkingWithPlayer;
- TextMeshProUGUI npcDialogueText;
- Button optionButton1;
- TextMeshProUGUI optionButton1Text;
- Button optionButton2;
- TextMeshProUGUI optionButton2Text;
- public List<Quest> quests;
- public Quest currentActiveQuest = null;
- public int activeQuestIndex = 0;
- public bool firstTimeInteraction = true;
- public int currentDialogue;
- private void Start()
- {
- npcDialogueText = DialogueSystem.Instance.dialogueText;
- optionButton1 = DialogueSystem.Instance.option1BTN;
- optionButton1Text = DialogueSystem.Instance.option1BTN.transform.Find("Text (TMP)").GetComponent<TextMeshProUGUI>();
- optionButton2 = DialogueSystem.Instance.option2BTN;
- optionButton2Text = DialogueSystem.Instance.option2BTN.transform.Find("Text (TMP)").GetComponent<TextMeshProUGUI>();
- }
- public void StartConversation()
- {
- isTalkingWithPlayer = true;
- LookAtPlayer();
- // Interacting with the NPC for the first time
- if (firstTimeInteraction)
- {
- firstTimeInteraction = false;
- currentActiveQuest = quests[activeQuestIndex]; // 0 at start
- StartQuestInitialDialogue();
- currentDialogue = 0;
- }
- else // Interacting with the NPC after the first time
- {
- // If we return after declining the quest
- if (currentActiveQuest.declined)
- {
- DialogueSystem.Instance.OpenDialogueUI();
- npcDialogueText.text = currentActiveQuest.info.comebackAfterDecline;
- SetAcceptAndDeclineOptions();
- }
- // If we return while the quest is still in progress
- if (currentActiveQuest.accepted && currentActiveQuest.isCompleted == false)
- {
- if (AreQuestRequirementsCompleted())
- {
- SubmitRequiredItems();
- DialogueSystem.Instance.OpenDialogueUI();
- npcDialogueText.text = currentActiveQuest.info.comebackCompleted;
- optionButton1Text.text = "Take Reward";
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- ReceiveRewardAndCompleteQuest();
- });
- }
- else
- {
- DialogueSystem.Instance.OpenDialogueUI();
- npcDialogueText.text = currentActiveQuest.info.comebackInProgress;
- optionButton1Text.text = "Bye";
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- DialogueSystem.Instance.CloseDialogueUI();
- isTalkingWithPlayer = false;
- });
- }
- }
- if (currentActiveQuest.isCompleted == true)
- {
- DialogueSystem.Instance.OpenDialogueUI();
- npcDialogueText.text = currentActiveQuest.info.finalWords;
- optionButton1Text.text = "Bye";
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- DialogueSystem.Instance.CloseDialogueUI();
- isTalkingWithPlayer = false;
- });
- }
- // If there is another quest available
- if (currentActiveQuest.initialDialogueCompleted == false)
- {
- StartQuestInitialDialogue();
- }
- }
- }
- private void SetAcceptAndDeclineOptions()
- {
- optionButton1Text.text = currentActiveQuest.info.acceptOption;
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- AcceptedQuest();
- });
- optionButton2.gameObject.SetActive(true);
- optionButton2Text.text = currentActiveQuest.info.declineOption;
- optionButton2.onClick.RemoveAllListeners();
- optionButton2.onClick.AddListener(() => {
- DeclinedQuest();
- });
- }
- private void SubmitRequiredItems()
- {
- string firstRequiredItem = currentActiveQuest.info.firstRequirementItem;
- int firstRequiredAmount = currentActiveQuest.info.firstRequirementAmount;
- if (firstRequiredItem != "")
- {
- InventorySystem.Instance.RemoveItem(firstRequiredItem, firstRequiredAmount);
- }
- string secondtRequiredItem = currentActiveQuest.info.secondRequirementItem;
- int secondRequiredAmount = currentActiveQuest.info.secondRequirementAmount;
- if (firstRequiredItem != "")
- {
- InventorySystem.Instance.RemoveItem(secondtRequiredItem, secondRequiredAmount);
- }
- }
- private bool AreQuestRequirementsCompleted()
- {
- print("Checking Requirements");
- // First Item Requirement
- string firstRequiredItem = currentActiveQuest.info.firstRequirementItem;
- int firstRequiredAmount = currentActiveQuest.info.firstRequirementAmount;
- var firstItemCounter = 0;
- foreach (string item in InventorySystem.Instance.itemList)
- {
- if (item == firstRequiredItem)
- {
- firstItemCounter++;
- }
- }
- // Second Item Requirement -- If we dont have a second item, just set it to 0
- string secondRequiredItem = currentActiveQuest.info.secondRequirementItem;
- int secondRequiredAmount = currentActiveQuest.info.secondRequirementAmount;
- var secondItemCounter = 0;
- foreach (string item in InventorySystem.Instance.itemList)
- {
- if (item == secondRequiredItem)
- {
- secondItemCounter++;
- }
- }
- if (firstItemCounter >= firstRequiredAmount && secondItemCounter >= secondRequiredAmount)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private void StartQuestInitialDialogue()
- {
- DialogueSystem.Instance.OpenDialogueUI();
- npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
- optionButton1Text.text = "Next";
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- currentDialogue++;
- CheckIfDialogueIsDone();
- });
- optionButton2.gameObject.SetActive(false);
- }
- private void CheckIfDialogueIsDone()
- {
- if (currentDialogue == currentActiveQuest.info.initialDialogue.Count - 1) // If its the last dialog
- {
- npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
- currentActiveQuest.initialDialogueCompleted = true;
- SetAcceptAndDeclineOptions();
- }
- else // If there are more dialogues
- {
- npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
- optionButton1Text.text = "Next";
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- currentDialogue++;
- CheckIfDialogueIsDone();
- });
- }
- }
- private void AcceptedQuest()
- {
- QuestManager.Instance.AddActiveQuest(currentActiveQuest);
- currentActiveQuest.accepted = true;
- currentActiveQuest.declined = false;
- if (currentActiveQuest.hasNoRequirements)
- {
- npcDialogueText.text = currentActiveQuest.info.comebackCompleted;
- optionButton1Text.text = "Take Reward";
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- ReceiveRewardAndCompleteQuest();
- });
- optionButton2.gameObject.SetActive(false);
- }
- else
- {
- npcDialogueText.text = currentActiveQuest.info.acceptAnswer;
- CloseDialogueUI();
- }
- }
- private void CloseDialogueUI()
- {
- optionButton1Text.text = "Bye";
- optionButton1.onClick.RemoveAllListeners();
- optionButton1.onClick.AddListener(() => {
- DialogueSystem.Instance.CloseDialogueUI();
- isTalkingWithPlayer = false;
- });
- optionButton2.gameObject.SetActive(false);
- }
- private void ReceiveRewardAndCompleteQuest()
- {
- QuestManager.Instance.MarkQuestCompleted(currentActiveQuest);
- currentActiveQuest.isCompleted = true;
- var coinsRecieved = currentActiveQuest.info.coinReward;
- print("You recieved " + coinsRecieved + " gold coins");
- if (currentActiveQuest.info.rewardItem1 != "")
- {
- InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem1);
- }
- if (currentActiveQuest.info.rewardItem2 != "")
- {
- InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem2);
- }
- activeQuestIndex++;
- // Start Next Quest
- if (activeQuestIndex < quests.Count)
- {
- currentActiveQuest = quests[activeQuestIndex];
- currentDialogue = 0;
- DialogueSystem.Instance.CloseDialogueUI();
- isTalkingWithPlayer = false;
- }
- else
- {
- DialogueSystem.Instance.CloseDialogueUI();
- isTalkingWithPlayer = false;
- print("No more quests");
- }
- }
- private void DeclinedQuest()
- {
- currentActiveQuest.declined = true;
- npcDialogueText.text = currentActiveQuest.info.declineAnswer;
- CloseDialogueUI();
- }
- public void LookAtPlayer()
- {
- var player = PlayerState.Instance.playerBody.transform;
- Vector3 direction = player.position - transform.position;
- transform.rotation = Quaternion.LookRotation(direction);
- var yRotation = transform.eulerAngles.y;
- transform.rotation = Quaternion.Euler(0, yRotation, 0);
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.CompareTag("Player"))
- {
- playerInRange = true;
- }
- }
- private void OnTriggerExit(Collider other)
- {
- if (other.CompareTag("Player"))
- {
- playerInRange = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement