| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using TMPro; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | public class DigicodeHandler : MonoBehaviour |
| | | 7 | | { |
| | 12 | 8 | | [SerializeField] private string correctCode = "1234"; |
| | 12 | 9 | | private string currentCode = ""; |
| | | 10 | | |
| | | 11 | | [SerializeField] private GameObject displayText; |
| | | 12 | | private TextMeshProUGUI displayTextMechPro; |
| | | 13 | | |
| | | 14 | | [SerializeField] private AudioSource audioCorrectCode; |
| | | 15 | | [SerializeField] private AudioSource audioWrongCode; |
| | | 16 | | [SerializeField] private AudioSource openPotionTubeSound; |
| | 12 | 17 | | private bool soundAlreadyPlayed = false; |
| | | 18 | | |
| | | 19 | | [SerializeField] private Animator dropPotionAnim; |
| | | 20 | | |
| | | 21 | | |
| | | 22 | | // Start is called before the first frame update |
| | | 23 | | void Start() |
| | 6 | 24 | | { |
| | 6 | 25 | | displayTextMechPro = displayText.GetComponent<TextMeshProUGUI>(); |
| | 6 | 26 | | UpdateDisplay(); |
| | 6 | 27 | | } |
| | | 28 | | |
| | | 29 | | private void UpdateDisplay() |
| | 6 | 30 | | { |
| | 6 | 31 | | displayTextMechPro.text = currentCode; |
| | 6 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void PressButton(int number) |
| | 0 | 35 | | { |
| | 0 | 36 | | currentCode += number.ToString(); |
| | 0 | 37 | | UpdateDisplay(); |
| | 0 | 38 | | if (currentCode.Length == correctCode.Length) |
| | 0 | 39 | | { |
| | 0 | 40 | | StartCoroutine(CheckCurrentCode()); |
| | 0 | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | private IEnumerator CheckCurrentCode() |
| | 0 | 45 | | { |
| | 0 | 46 | | if (currentCode == correctCode) |
| | 0 | 47 | | { |
| | 0 | 48 | | displayTextMechPro.color = Color.green; |
| | 0 | 49 | | audioCorrectCode.Play(); |
| | | 50 | | //Code is correct |
| | 0 | 51 | | if(dropPotionAnim != null) |
| | 0 | 52 | | { |
| | 0 | 53 | | dropPotionAnim.SetTrigger("TriggerDropPotion"); |
| | 0 | 54 | | StartCoroutine(PlayOpenTubeSound()); |
| | 0 | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | } |
| | | 58 | | else |
| | 0 | 59 | | { |
| | 0 | 60 | | displayTextMechPro.color = Color.red; |
| | 0 | 61 | | audioWrongCode.Play(); |
| | | 62 | | //BEEEP code incorrect |
| | 0 | 63 | | } |
| | 0 | 64 | | yield return new WaitForSeconds(1); |
| | 0 | 65 | | currentCode = ""; |
| | 0 | 66 | | UpdateDisplay(); |
| | 0 | 67 | | displayTextMechPro.color = Color.white; |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | private IEnumerator PlayOpenTubeSound() |
| | 0 | 71 | | { |
| | 0 | 72 | | yield return new WaitForSeconds(1); |
| | 0 | 73 | | if (!soundAlreadyPlayed) |
| | 0 | 74 | | { |
| | 0 | 75 | | soundAlreadyPlayed = true; |
| | 0 | 76 | | openPotionTubeSound.Play(); |
| | 0 | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | } |