| | | 1 | | using System; |
| | | 2 | | using TMPro; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using UnityEngine.SceneManagement; |
| | | 5 | | |
| | | 6 | | |
| | | 7 | | public class KeyDetector : MonoBehaviour |
| | | 8 | | { |
| | | 9 | | |
| | | 10 | | private TextMeshPro display; |
| | | 11 | | |
| | | 12 | | private KeyPadControll keyPadControll; |
| | | 13 | | |
| | | 14 | | [SerializeField] string displayTag; |
| | | 15 | | [SerializeField] string keypadTag; |
| | | 16 | | [SerializeField] string keypadButtonTag; |
| | | 17 | | |
| | | 18 | | // Start is called before the first frame update |
| | | 19 | | void Start() |
| | 12 | 20 | | { |
| | 12 | 21 | | Scene scene = SceneManager.GetActiveScene(); |
| | 12 | 22 | | if (scene.name == "EscapeRoom") |
| | 12 | 23 | | { |
| | 12 | 24 | | display = GameObject.FindGameObjectWithTag(displayTag).GetComponentInChildren<TextMeshPro>(); |
| | | 25 | | |
| | 12 | 26 | | keyPadControll = GameObject.FindGameObjectWithTag(keypadTag).GetComponent<KeyPadControll>(); |
| | | 27 | | |
| | 12 | 28 | | } |
| | 12 | 29 | | } |
| | | 30 | | |
| | | 31 | | private void OnTriggerEnter(Collider other) |
| | 36 | 32 | | { |
| | 36 | 33 | | if (other.CompareTag(keypadButtonTag)) |
| | 0 | 34 | | { |
| | 0 | 35 | | var key = other.GetComponentInChildren<TextMeshPro>(); |
| | 0 | 36 | | if (key != null) |
| | 0 | 37 | | { |
| | 0 | 38 | | var KeyFeedback = other.gameObject.GetComponent<KeyFeedback>(); |
| | | 39 | | |
| | 0 | 40 | | KeyFeedback.keyHit = true; |
| | | 41 | | |
| | 0 | 42 | | var accessGranted = false; |
| | 0 | 43 | | bool onlyNumbers = int.TryParse(display.text, out int value); |
| | | 44 | | |
| | 0 | 45 | | if (onlyNumbers == false) |
| | 0 | 46 | | { |
| | 0 | 47 | | display.text = ""; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | //make sure max 4 numbers |
| | 0 | 51 | | if(key.text == "Back") |
| | 0 | 52 | | { |
| | 0 | 53 | | if (display.text.Length > 0) |
| | 0 | 54 | | { |
| | 0 | 55 | | display.text = display.text.Remove(display.text.Length - 1, 1); |
| | 0 | 56 | | } |
| | 0 | 57 | | } |
| | 0 | 58 | | else if(key.text == "Confirm") |
| | 0 | 59 | | { |
| | 0 | 60 | | if (display.text.Length > 0) |
| | 0 | 61 | | { |
| | 0 | 62 | | accessGranted = keyPadControll.CheckIfCorrect(Convert.ToInt32(display.text)); |
| | 0 | 63 | | if (accessGranted) |
| | 0 | 64 | | { |
| | 0 | 65 | | display.text = "Correct"; |
| | 0 | 66 | | } |
| | | 67 | | else |
| | 0 | 68 | | { |
| | 0 | 69 | | display.text = "Retry"; |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | else |
| | 0 | 74 | | { |
| | 0 | 75 | | if (display.text.Length < 4) |
| | 0 | 76 | | { |
| | 0 | 77 | | display.text += key.text; |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | } |
| | 36 | 83 | | } |
| | | 84 | | } |