Advertisement
evelynshilosky

NPC - Part 29

Jan 5th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.79 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.         QuestManager.Instance.AddActiveQuest(currentActiveQuest);
  249.  
  250.         currentActiveQuest.accepted = true;
  251.         currentActiveQuest.declined = false;
  252.  
  253.         if (currentActiveQuest.hasNoRequirements)
  254.         {
  255.             npcDialogueText.text = currentActiveQuest.info.comebackCompleted;
  256.             optionButton1Text.text = "Take Reward";
  257.             optionButton1.onClick.RemoveAllListeners();
  258.             optionButton1.onClick.AddListener(() => {
  259.                 ReceiveRewardAndCompleteQuest();
  260.             });
  261.             optionButton2.gameObject.SetActive(false);
  262.         }
  263.         else
  264.         {
  265.             npcDialogueText.text = currentActiveQuest.info.acceptAnswer;
  266.             CloseDialogueUI();
  267.         }
  268.  
  269.  
  270.  
  271.     }
  272.  
  273.     private void CloseDialogueUI()
  274.     {
  275.         optionButton1Text.text = "Bye";
  276.         optionButton1.onClick.RemoveAllListeners();
  277.         optionButton1.onClick.AddListener(() => {
  278.             DialogueSystem.Instance.CloseDialogueUI();
  279.             isTalkingWithPlayer = false;
  280.         });
  281.         optionButton2.gameObject.SetActive(false);
  282.     }
  283.  
  284.     private void ReceiveRewardAndCompleteQuest()
  285.     {
  286.        
  287.         QuestManager.Instance.MarkQuestCompleted(currentActiveQuest);
  288.        
  289.         currentActiveQuest.isCompleted = true;
  290.  
  291.         var coinsRecieved = currentActiveQuest.info.coinReward;
  292.         print("You recieved " + coinsRecieved + " gold coins");
  293.  
  294.         if (currentActiveQuest.info.rewardItem1 != "")
  295.         {
  296.             InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem1);
  297.         }
  298.  
  299.         if (currentActiveQuest.info.rewardItem2 != "")
  300.         {
  301.             InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem2);
  302.         }
  303.  
  304.         activeQuestIndex++;
  305.  
  306.         // Start Next Quest
  307.         if (activeQuestIndex < quests.Count)
  308.         {
  309.             currentActiveQuest = quests[activeQuestIndex];
  310.             currentDialogue = 0;
  311.             DialogueSystem.Instance.CloseDialogueUI();
  312.             isTalkingWithPlayer = false;
  313.         }
  314.         else
  315.         {
  316.             DialogueSystem.Instance.CloseDialogueUI();
  317.             isTalkingWithPlayer = false;
  318.             print("No more quests");
  319.         }
  320.  
  321.     }
  322.  
  323.     private void DeclinedQuest()
  324.     {
  325.         currentActiveQuest.declined = true;
  326.        
  327.         npcDialogueText.text = currentActiveQuest.info.declineAnswer;
  328.         CloseDialogueUI();
  329.     }
  330.  
  331.  
  332.  
  333.     public void LookAtPlayer()
  334.     {
  335.         var player = PlayerState.Instance.playerBody.transform;
  336.         Vector3 direction = player.position - transform.position;
  337.         transform.rotation = Quaternion.LookRotation(direction);
  338.  
  339.         var yRotation = transform.eulerAngles.y;
  340.         transform.rotation = Quaternion.Euler(0, yRotation, 0);
  341.  
  342.     }
  343.  
  344.  
  345.  
  346.     private void OnTriggerEnter(Collider other)
  347.     {
  348.         if (other.CompareTag("Player"))
  349.         {
  350.             playerInRange = true;
  351.         }
  352.     }
  353.  
  354.     private void OnTriggerExit(Collider other)
  355.     {
  356.         if (other.CompareTag("Player"))
  357.         {
  358.             playerInRange = false;
  359.         }
  360.     }
  361. }
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement