evelynshilosky

NPC - Part 28

Dec 14th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.64 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7.  
  8. public class NPC : MonoBehaviour
  9. {
  10.  
  11.     public bool playerInRange;
  12.     public bool isTalkingWithPlayer;
  13.  
  14.     TextMeshProUGUI npcDialogueText;
  15.  
  16.     Button optionButton1;
  17.     TextMeshProUGUI optionButton1Text;
  18.  
  19.     Button optionButton2;
  20.     TextMeshProUGUI optionButton2Text;
  21.  
  22.     public List<Quest> quests;
  23.     public Quest currentActiveQuest = null;
  24.     public int activeQuestIndex = 0;
  25.     public bool firstTimeInteraction = true;
  26.     public int currentDialogue;
  27.  
  28.  
  29.     private void Start()
  30.     {
  31.         npcDialogueText = DialogueSystem.Instance.dialogueText;
  32.  
  33.         optionButton1 = DialogueSystem.Instance.option1BTN;
  34.         optionButton1Text = DialogueSystem.Instance.option1BTN.transform.Find("Text (TMP)").GetComponent<TextMeshProUGUI>();
  35.  
  36.         optionButton2 = DialogueSystem.Instance.option2BTN;
  37.         optionButton2Text = DialogueSystem.Instance.option2BTN.transform.Find("Text (TMP)").GetComponent<TextMeshProUGUI>();
  38.  
  39.     }
  40.  
  41.  
  42.     public void StartConversation()
  43.     {
  44.         isTalkingWithPlayer = true;
  45.  
  46.         LookAtPlayer();
  47.  
  48.         // Interacting with the NPC for the first time
  49.         if (firstTimeInteraction)
  50.         {
  51.             firstTimeInteraction = false;
  52.             currentActiveQuest = quests[activeQuestIndex]; // 0 at start
  53.             StartQuestInitialDialogue();
  54.             currentDialogue = 0;
  55.         }
  56.         else // Interacting with the NPC after the first time
  57.         {
  58.  
  59.             // If we return after declining the quest
  60.             if (currentActiveQuest.declined)
  61.             {
  62.  
  63.                 DialogueSystem.Instance.OpenDialogueUI();
  64.  
  65.                 npcDialogueText.text = currentActiveQuest.info.comebackAfterDecline;
  66.  
  67.                 SetAcceptAndDeclineOptions();
  68.             }
  69.  
  70.  
  71.             // If we return while the quest is still in progress
  72.             if (currentActiveQuest.accepted && currentActiveQuest.isCompleted == false)
  73.             {
  74.                 if (AreQuestRequirementsCompleted())
  75.                 {
  76.  
  77.                     SubmitRequiredItems();
  78.  
  79.                     DialogueSystem.Instance.OpenDialogueUI();
  80.  
  81.                     npcDialogueText.text = currentActiveQuest.info.comebackCompleted;
  82.  
  83.                     optionButton1Text.text = "Take Reward";
  84.                     optionButton1.onClick.RemoveAllListeners();
  85.                     optionButton1.onClick.AddListener(() => {
  86.                         ReceiveRewardAndCompleteQuest();
  87.                     });
  88.                 }
  89.                 else
  90.                 {
  91.                     DialogueSystem.Instance.OpenDialogueUI();
  92.  
  93.                     npcDialogueText.text = currentActiveQuest.info.comebackInProgress;
  94.  
  95.                     optionButton1Text.text = "Bye";
  96.                     optionButton1.onClick.RemoveAllListeners();
  97.                     optionButton1.onClick.AddListener(() => {
  98.                         DialogueSystem.Instance.CloseDialogueUI();
  99.                         isTalkingWithPlayer = false;
  100.                     });
  101.                 }
  102.             }
  103.  
  104.             if (currentActiveQuest.isCompleted == true)
  105.             {
  106.                 DialogueSystem.Instance.OpenDialogueUI();
  107.  
  108.                 npcDialogueText.text = currentActiveQuest.info.finalWords;
  109.  
  110.                 optionButton1Text.text = "Bye";
  111.                 optionButton1.onClick.RemoveAllListeners();
  112.                 optionButton1.onClick.AddListener(() => {
  113.                     DialogueSystem.Instance.CloseDialogueUI();
  114.                     isTalkingWithPlayer = false;
  115.                 });
  116.             }
  117.  
  118.             // If there is another quest available
  119.             if (currentActiveQuest.initialDialogueCompleted == false)
  120.             {
  121.                 StartQuestInitialDialogue();
  122.             }
  123.  
  124.         }
  125.  
  126.     }
  127.  
  128.     private void SetAcceptAndDeclineOptions()
  129.     {
  130.         optionButton1Text.text = currentActiveQuest.info.acceptOption;
  131.         optionButton1.onClick.RemoveAllListeners();
  132.         optionButton1.onClick.AddListener(() => {
  133.             AcceptedQuest();
  134.         });
  135.  
  136.         optionButton2.gameObject.SetActive(true);
  137.         optionButton2Text.text = currentActiveQuest.info.declineOption;
  138.         optionButton2.onClick.RemoveAllListeners();
  139.         optionButton2.onClick.AddListener(() => {
  140.             DeclinedQuest();
  141.         });
  142.     }
  143.  
  144.     private void SubmitRequiredItems()
  145.     {
  146.         string firstRequiredItem = currentActiveQuest.info.firstRequirementItem;
  147.         int firstRequiredAmount = currentActiveQuest.info.firstRequirementAmount;
  148.  
  149.         if (firstRequiredItem != "")
  150.         {
  151.             InventorySystem.Instance.RemoveItem(firstRequiredItem, firstRequiredAmount);
  152.         }
  153.  
  154.  
  155.         string secondtRequiredItem = currentActiveQuest.info.secondRequirementItem;
  156.         int secondRequiredAmount = currentActiveQuest.info.secondRequirementAmount;
  157.  
  158.         if (firstRequiredItem != "")
  159.         {
  160.             InventorySystem.Instance.RemoveItem(secondtRequiredItem, secondRequiredAmount);
  161.         }
  162.  
  163.     }
  164.  
  165.     private bool AreQuestRequirementsCompleted()
  166.     {
  167.         print("Checking Requirements");
  168.  
  169.         // First Item Requirement
  170.  
  171.         string firstRequiredItem = currentActiveQuest.info.firstRequirementItem;
  172.         int firstRequiredAmount = currentActiveQuest.info.firstRequirementAmount;
  173.  
  174.         var firstItemCounter = 0;
  175.  
  176.         foreach (string item in InventorySystem.Instance.itemList)
  177.         {
  178.             if (item == firstRequiredItem)
  179.             {
  180.                 firstItemCounter++;
  181.             }
  182.         }
  183.  
  184.         // Second Item Requirement -- If we dont have a second item, just set it to 0
  185.  
  186.         string secondRequiredItem = currentActiveQuest.info.secondRequirementItem;
  187.         int secondRequiredAmount = currentActiveQuest.info.secondRequirementAmount;
  188.  
  189.         var secondItemCounter = 0;
  190.  
  191.         foreach (string item in InventorySystem.Instance.itemList)
  192.         {
  193.             if (item == secondRequiredItem)
  194.             {
  195.                 secondItemCounter++;
  196.             }
  197.         }
  198.  
  199.         if (firstItemCounter >= firstRequiredAmount && secondItemCounter >= secondRequiredAmount)
  200.         {
  201.             return true;
  202.         }
  203.         else
  204.         {
  205.             return false;
  206.         }
  207.     }
  208.  
  209.     private void StartQuestInitialDialogue()
  210.     {
  211.         DialogueSystem.Instance.OpenDialogueUI();
  212.  
  213.         npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
  214.         optionButton1Text.text = "Next";
  215.         optionButton1.onClick.RemoveAllListeners();
  216.         optionButton1.onClick.AddListener(() => {
  217.             currentDialogue++;
  218.             CheckIfDialogueIsDone();
  219.         });
  220.  
  221.         optionButton2.gameObject.SetActive(false);
  222.     }
  223.  
  224.     private void CheckIfDialogueIsDone()
  225.     {
  226.         if (currentDialogue == currentActiveQuest.info.initialDialogue.Count - 1) // If its the last dialog
  227.         {
  228.             npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
  229.  
  230.             currentActiveQuest.initialDialogueCompleted = true;
  231.  
  232.             SetAcceptAndDeclineOptions();
  233.         }
  234.         else  // If there are more dialogues
  235.         {
  236.             npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
  237.  
  238.             optionButton1Text.text = "Next";
  239.             optionButton1.onClick.RemoveAllListeners();
  240.             optionButton1.onClick.AddListener(() => {
  241.                 currentDialogue++;
  242.                 CheckIfDialogueIsDone();
  243.             });
  244.         }
  245.     }
  246.     private void AcceptedQuest()
  247.     {
  248.         currentActiveQuest.accepted = true;
  249.         currentActiveQuest.declined = false;
  250.  
  251.         if (currentActiveQuest.hasNoRequirements)
  252.         {
  253.             npcDialogueText.text = currentActiveQuest.info.comebackCompleted;
  254.             optionButton1Text.text = "Take Reward";
  255.             optionButton1.onClick.RemoveAllListeners();
  256.             optionButton1.onClick.AddListener(() => {
  257.                 ReceiveRewardAndCompleteQuest();
  258.             });
  259.             optionButton2.gameObject.SetActive(false);
  260.         }
  261.         else
  262.         {
  263.             npcDialogueText.text = currentActiveQuest.info.acceptAnswer;
  264.             CloseDialogueUI();
  265.         }
  266.  
  267.  
  268.  
  269.     }
  270.  
  271.     private void CloseDialogueUI()
  272.     {
  273.         optionButton1Text.text = "Bye";
  274.         optionButton1.onClick.RemoveAllListeners();
  275.         optionButton1.onClick.AddListener(() => {
  276.             DialogueSystem.Instance.CloseDialogueUI();
  277.             isTalkingWithPlayer = false;
  278.         });
  279.         optionButton2.gameObject.SetActive(false);
  280.     }
  281.  
  282.     private void ReceiveRewardAndCompleteQuest()
  283.     {
  284.         currentActiveQuest.isCompleted = true;
  285.  
  286.         var coinsRecieved = currentActiveQuest.info.coinReward;
  287.         print("You recieved " + coinsRecieved + " gold coins");
  288.  
  289.         if (currentActiveQuest.info.rewardItem1 != "")
  290.         {
  291.             InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem1);
  292.         }
  293.  
  294.         if (currentActiveQuest.info.rewardItem2 != "")
  295.         {
  296.             InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem2);
  297.         }
  298.  
  299.         activeQuestIndex++;
  300.  
  301.         // Start Next Quest
  302.         if (activeQuestIndex < quests.Count)
  303.         {
  304.             currentActiveQuest = quests[activeQuestIndex];
  305.             currentDialogue = 0;
  306.             DialogueSystem.Instance.CloseDialogueUI();
  307.             isTalkingWithPlayer = false;
  308.         }
  309.         else
  310.         {
  311.             DialogueSystem.Instance.CloseDialogueUI();
  312.             isTalkingWithPlayer = false;
  313.             print("No more quests");
  314.         }
  315.  
  316.     }
  317.  
  318.     private void DeclinedQuest()
  319.     {
  320.         currentActiveQuest.declined = true;
  321.        
  322.         npcDialogueText.text = currentActiveQuest.info.declineAnswer;
  323.         CloseDialogueUI();
  324.     }
  325.  
  326.  
  327.  
  328.     public void LookAtPlayer()
  329.     {
  330.         var player = PlayerState.Instance.playerBody.transform;
  331.         Vector3 direction = player.position - transform.position;
  332.         transform.rotation = Quaternion.LookRotation(direction);
  333.  
  334.         var yRotation = transform.eulerAngles.y;
  335.         transform.rotation = Quaternion.Euler(0, yRotation, 0);
  336.  
  337.     }
  338.  
  339.  
  340.  
  341.     private void OnTriggerEnter(Collider other)
  342.     {
  343.         if (other.CompareTag("Player"))
  344.         {
  345.             playerInRange = true;
  346.         }
  347.     }
  348.  
  349.     private void OnTriggerExit(Collider other)
  350.     {
  351.         if (other.CompareTag("Player"))
  352.         {
  353.             playerInRange = false;
  354.         }
  355.     }
  356. }
  357.  
Add Comment
Please, Sign In to add comment