Advertisement
evelynshilosky

NPC - Part 31

Feb 2nd, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.48 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.     public void StartConversation()
  42.     {
  43.         isTalkingWithPlayer = true;
  44.  
  45.         LookAtPlayer();
  46.  
  47.         // Interacting with the NPC for the first time
  48.         if (firstTimeInteraction)
  49.         {
  50.             firstTimeInteraction = false;
  51.             currentActiveQuest = quests[activeQuestIndex]; // 0 at start
  52.             StartQuestInitialDialogue();
  53.             currentDialogue = 0;
  54.         }
  55.         else // Interacting with the NPC after the first time
  56.         {
  57.  
  58.             // If we return after declining the quest
  59.             if (currentActiveQuest.declined)
  60.             {
  61.  
  62.                 DialogueSystem.Instance.OpenDialogueUI();
  63.  
  64.                 npcDialogueText.text = currentActiveQuest.info.comebackAfterDecline;
  65.                 SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.comebackAfterDeclineClip);
  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.                     SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.comebackCompletedClip);
  83.  
  84.  
  85.                     optionButton1Text.text = "Take Reward";
  86.                     optionButton1.onClick.RemoveAllListeners();
  87.                     optionButton1.onClick.AddListener(() => {
  88.                         SoundManager.Instance.StopVoiceOvers(currentActiveQuest.info.comebackCompletedClip);
  89.                         ReceiveRewardAndCompleteQuest();
  90.                     });
  91.                 }
  92.                 else
  93.                 {
  94.                     DialogueSystem.Instance.OpenDialogueUI();
  95.  
  96.                     npcDialogueText.text = currentActiveQuest.info.comebackInProgress;
  97.                     SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.comebackInProgressClip);
  98.  
  99.  
  100.                     optionButton1Text.text = "Bye";
  101.                     optionButton1.onClick.RemoveAllListeners();
  102.                     optionButton1.onClick.AddListener(() => {
  103.                         SoundManager.Instance.StopVoiceOvers(currentActiveQuest.info.comebackInProgressClip);
  104.                         DialogueSystem.Instance.CloseDialogueUI();
  105.                         isTalkingWithPlayer = false;
  106.                     });
  107.                 }
  108.             }
  109.  
  110.             if (currentActiveQuest.isCompleted == true)
  111.             {
  112.                 DialogueSystem.Instance.OpenDialogueUI();
  113.  
  114.                 npcDialogueText.text = currentActiveQuest.info.finalWords;
  115.                 SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.finalWordsClip);
  116.  
  117.  
  118.                 optionButton1Text.text = "Bye";
  119.                 optionButton1.onClick.RemoveAllListeners();
  120.                 optionButton1.onClick.AddListener(() => {
  121.                     SoundManager.Instance.StopVoiceOvers(currentActiveQuest.info.finalWordsClip);
  122.                     DialogueSystem.Instance.CloseDialogueUI();
  123.                     isTalkingWithPlayer = false;
  124.                 });
  125.             }
  126.  
  127.             // If there is another quest available
  128.             if (currentActiveQuest.initialDialogueCompleted == false)
  129.             {
  130.                 StartQuestInitialDialogue();
  131.             }
  132.  
  133.         }
  134.  
  135.     }
  136.  
  137.     private void SetAcceptAndDeclineOptions()
  138.     {
  139.         optionButton1Text.text = currentActiveQuest.info.acceptOption;
  140.         optionButton1.onClick.RemoveAllListeners();
  141.         optionButton1.onClick.AddListener(() => {
  142.             AcceptedQuest();
  143.         });
  144.  
  145.         optionButton2.gameObject.SetActive(true);
  146.         optionButton2Text.text = currentActiveQuest.info.declineOption;
  147.         optionButton2.onClick.RemoveAllListeners();
  148.         optionButton2.onClick.AddListener(() => {
  149.             DeclinedQuest();
  150.         });
  151.     }
  152.  
  153.     private void SubmitRequiredItems()
  154.     {
  155.         string firstRequiredItem = currentActiveQuest.info.firstRequirementItem;
  156.         int firstRequiredAmount = currentActiveQuest.info.firstRequirementAmount;
  157.  
  158.         if (firstRequiredItem != "")
  159.         {
  160.             InventorySystem.Instance.RemoveItem(firstRequiredItem, firstRequiredAmount);
  161.         }
  162.  
  163.  
  164.         string secondtRequiredItem = currentActiveQuest.info.secondRequirementItem;
  165.         int secondRequiredAmount = currentActiveQuest.info.secondRequirementAmount;
  166.  
  167.         if (firstRequiredItem != "")
  168.         {
  169.             InventorySystem.Instance.RemoveItem(secondtRequiredItem, secondRequiredAmount);
  170.         }
  171.  
  172.     }
  173.  
  174.     private bool AreQuestRequirementsCompleted()
  175.     {
  176.  
  177.         //print("Checking Requirements");
  178.  
  179.         // First Item Requirement
  180.  
  181.         string firstRequiredItem = currentActiveQuest.info.firstRequirementItem;
  182.         int firstRequiredAmount = currentActiveQuest.info.firstRequirementAmount;
  183.  
  184.         var firstItemCounter = 0;
  185.  
  186.         foreach (string item in InventorySystem.Instance.itemList)
  187.         {
  188.             if (item == firstRequiredItem)
  189.             {
  190.                 firstItemCounter++;
  191.             }
  192.         }
  193.  
  194.         // Second Item Requirement -- If we dont have a second item, just set it to 0
  195.  
  196.         string secondRequiredItem = currentActiveQuest.info.secondRequirementItem;
  197.         int secondRequiredAmount = currentActiveQuest.info.secondRequirementAmount;
  198.  
  199.         var secondItemCounter = 0;
  200.  
  201.         foreach (string item in InventorySystem.Instance.itemList)
  202.         {
  203.             if (item == secondRequiredItem)
  204.             {
  205.                 secondItemCounter++;
  206.             }
  207.         }
  208.  
  209.  
  210.         SetQuestHasCheckpoints(currentActiveQuest);
  211.  
  212.         bool allCheckpointsCompleted = false;
  213.  
  214.         if (currentActiveQuest.info.hasCheckpoints)
  215.         {
  216.             foreach (Checkpoint cp in currentActiveQuest.info.checkpoints)
  217.             {
  218.                 if (cp.isCompleted == false)
  219.                 {
  220.                     allCheckpointsCompleted = false; // if atleast one is false, then return false
  221.                     break;
  222.                 }
  223.  
  224.                 allCheckpointsCompleted = true;
  225.             }
  226.         }
  227.  
  228.  
  229.  
  230.  
  231.         if (firstItemCounter >= firstRequiredAmount && secondItemCounter >= secondRequiredAmount)
  232.         {
  233.             if (currentActiveQuest.info.hasCheckpoints)
  234.             {
  235.                 if (allCheckpointsCompleted)
  236.                 {
  237.                     return true;
  238.                 }
  239.                 else
  240.                 {
  241.                     return false;
  242.                 }
  243.             }
  244.             else
  245.             {
  246.                 return true;
  247.             }
  248.         }
  249.         else
  250.         {
  251.             return false;
  252.         }
  253.     }
  254.  
  255.     private void SetQuestHasCheckpoints(Quest activeQuest)
  256.     {
  257.         if (activeQuest.info.checkpoints.Count > 0)
  258.         {
  259.             activeQuest.info.hasCheckpoints = true;
  260.         }
  261.         else
  262.         {
  263.             activeQuest.info.hasCheckpoints = false;
  264.         }
  265.     }
  266.  
  267.     private void StartQuestInitialDialogue()
  268.     {
  269.         DialogueSystem.Instance.OpenDialogueUI();
  270.  
  271.         npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
  272.         SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.initialDialogueClips[currentDialogue]);
  273.  
  274.  
  275.         optionButton1Text.text = "Next";
  276.         optionButton1.onClick.RemoveAllListeners();
  277.         optionButton1.onClick.AddListener(() => {
  278.             currentDialogue++;
  279.             CheckIfDialogueIsDone();
  280.         });
  281.  
  282.         optionButton2.gameObject.SetActive(false);
  283.     }
  284.  
  285.     private void CheckIfDialogueIsDone()
  286.     {
  287.         if (currentDialogue == currentActiveQuest.info.initialDialogue.Count - 1) // If its the last dialog
  288.         {
  289.             npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
  290.             SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.initialDialogueClips[currentDialogue]);
  291.             currentActiveQuest.initialDialogueCompleted = true;
  292.  
  293.             SetAcceptAndDeclineOptions();
  294.         }
  295.         else  // If there are more dialogues
  296.         {
  297.             npcDialogueText.text = currentActiveQuest.info.initialDialogue[currentDialogue];
  298.             SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.initialDialogueClips[currentDialogue]);
  299.             optionButton1Text.text = "Next";
  300.             optionButton1.onClick.RemoveAllListeners();
  301.             optionButton1.onClick.AddListener(() => {
  302.                 currentDialogue++;
  303.                 CheckIfDialogueIsDone();
  304.             });
  305.         }
  306.     }
  307.     private void AcceptedQuest()
  308.     {
  309.         QuestManager.Instance.AddActiveQuest(currentActiveQuest);
  310.  
  311.         currentActiveQuest.accepted = true;
  312.         currentActiveQuest.declined = false;
  313.  
  314.         if (currentActiveQuest.hasNoRequirements)
  315.         {
  316.             npcDialogueText.text = currentActiveQuest.info.comebackCompleted;
  317.             SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.comebackCompletedClip);
  318.  
  319.             optionButton1Text.text = "Take Reward";
  320.             optionButton1.onClick.RemoveAllListeners();
  321.             optionButton1.onClick.AddListener(() => {
  322.                 ReceiveRewardAndCompleteQuest();
  323.             });
  324.             optionButton2.gameObject.SetActive(false);
  325.         }
  326.         else
  327.         {
  328.             npcDialogueText.text = currentActiveQuest.info.acceptAnswer;
  329.             SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.acceptAnswerClip);
  330.  
  331.             CloseDialogueUI();
  332.         }
  333.  
  334.  
  335.  
  336.     }
  337.  
  338.     private void CloseDialogueUI()
  339.     {
  340.         optionButton1Text.text = "Bye";
  341.         optionButton1.onClick.RemoveAllListeners();
  342.         optionButton1.onClick.AddListener(() => {
  343.             SoundManager.Instance.StopVoiceOvers(currentActiveQuest.info.acceptAnswerClip);
  344.             SoundManager.Instance.StopVoiceOvers(currentActiveQuest.info.declineAnswerClip);
  345.             DialogueSystem.Instance.CloseDialogueUI();
  346.             isTalkingWithPlayer = false;
  347.         });
  348.         optionButton2.gameObject.SetActive(false);
  349.     }
  350.  
  351.     private void ReceiveRewardAndCompleteQuest()
  352.     {
  353.        
  354.         QuestManager.Instance.MarkQuestCompleted(currentActiveQuest);
  355.        
  356.         currentActiveQuest.isCompleted = true;
  357.  
  358.         var coinsRecieved = currentActiveQuest.info.coinReward;
  359.         print("You recieved " + coinsRecieved + " gold coins");
  360.  
  361.         if (currentActiveQuest.info.rewardItem1 != "")
  362.         {
  363.             InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem1);
  364.         }
  365.  
  366.         if (currentActiveQuest.info.rewardItem2 != "")
  367.         {
  368.             InventorySystem.Instance.AddToInventory(currentActiveQuest.info.rewardItem2);
  369.         }
  370.  
  371.         activeQuestIndex++;
  372.  
  373.         // Start Next Quest
  374.         if (activeQuestIndex < quests.Count)
  375.         {
  376.             currentActiveQuest = quests[activeQuestIndex];
  377.             currentDialogue = 0;
  378.             DialogueSystem.Instance.CloseDialogueUI();
  379.             isTalkingWithPlayer = false;
  380.         }
  381.         else
  382.         {
  383.             DialogueSystem.Instance.CloseDialogueUI();
  384.             isTalkingWithPlayer = false;
  385.             print("No more quests");
  386.         }
  387.  
  388.     }
  389.  
  390.     private void DeclinedQuest()
  391.     {
  392.         currentActiveQuest.declined = true;
  393.        
  394.         npcDialogueText.text = currentActiveQuest.info.declineAnswer;
  395.         SoundManager.Instance.PlayVoiceOvers(currentActiveQuest.info.declineAnswerClip);
  396.         CloseDialogueUI();
  397.     }
  398.  
  399.  
  400.  
  401.     public void LookAtPlayer()
  402.     {
  403.         var player = PlayerState.Instance.playerBody.transform;
  404.         Vector3 direction = player.position - transform.position;
  405.         transform.rotation = Quaternion.LookRotation(direction);
  406.  
  407.         var yRotation = transform.eulerAngles.y;
  408.         transform.rotation = Quaternion.Euler(0, yRotation, 0);
  409.  
  410.     }
  411.  
  412.  
  413.  
  414.     private void OnTriggerEnter(Collider other)
  415.     {
  416.         if (other.CompareTag("Player"))
  417.         {
  418.             playerInRange = true;
  419.         }
  420.     }
  421.  
  422.     private void OnTriggerExit(Collider other)
  423.     {
  424.         if (other.CompareTag("Player"))
  425.         {
  426.             playerInRange = false;
  427.         }
  428.     }
  429. }
  430.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement