| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using UnityEngine; |
| | | 4 | | |
| | | 5 | | public class Door : MonoBehaviour |
| | | 6 | | { |
| | | 7 | | private enum State |
| | | 8 | | { |
| | | 9 | | Closed, |
| | | 10 | | Opening, |
| | | 11 | | Closing, |
| | | 12 | | Opened |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | [Header("Sounds")] |
| | 4 | 16 | | public AudioClip open_fail_sound = null; |
| | 4 | 17 | | public AudioClip open_success_sound = null; |
| | | 18 | | |
| | 4 | 19 | | private bool locked = true; |
| | 4 | 20 | | private State _state = State.Closed; |
| | 4 | 21 | | private float _slide_speed = 2.0f; |
| | 4 | 22 | | private float _opened_position_offset = 7.0f; |
| | | 23 | | private Vector3 _initial_position; |
| | 4 | 24 | | private Vector3 _slide_vector = new Vector3(0f, 1f, 0f); |
| | | 25 | | |
| | 1 | 26 | | void Awake() { |
| | | 27 | | // save initial door position |
| | 1 | 28 | | _initial_position = gameObject.transform.position; |
| | 1 | 29 | | } |
| | | 30 | | |
| | 6924 | 31 | | void Update() { |
| | 6924 | 32 | | switch (_state) |
| | | 33 | | { |
| | | 34 | | case State.Closed: |
| | 5210 | 35 | | break; |
| | | 36 | | |
| | | 37 | | case State.Opening: |
| | 65 | 38 | | if (gameObject.transform.position.y < _initial_position.y + _opened_position_offset) |
| | 64 | 39 | | { |
| | | 40 | | // slide the door to open |
| | 64 | 41 | | gameObject.transform.position = gameObject.transform.position + _slide_vector * _slide_speed * Time. |
| | 64 | 42 | | } else |
| | 1 | 43 | | { |
| | 1 | 44 | | _state = State.Opened; |
| | 1 | 45 | | } |
| | 65 | 46 | | break; |
| | | 47 | | |
| | | 48 | | case State.Closing: |
| | 64 | 49 | | if (gameObject.transform.position.y > _initial_position.y) |
| | 62 | 50 | | { |
| | | 51 | | // slide the door to open |
| | 62 | 52 | | gameObject.transform.position = gameObject.transform.position - _slide_vector * _slide_speed * Time. |
| | 62 | 53 | | } |
| | | 54 | | else |
| | 2 | 55 | | { |
| | 2 | 56 | | _state = State.Opened; |
| | 2 | 57 | | } |
| | 64 | 58 | | break; |
| | | 59 | | |
| | | 60 | | case State.Opened: |
| | 1585 | 61 | | break; |
| | | 62 | | } |
| | 6924 | 63 | | } |
| | | 64 | | |
| | | 65 | | /** |
| | | 66 | | * Method that opens the door |
| | | 67 | | */ |
| | | 68 | | public void Open() |
| | 6 | 69 | | { |
| | | 70 | | // only affects closed door |
| | 6 | 71 | | if (_state == State.Closed && !locked) |
| | 1 | 72 | | { |
| | | 73 | | // start opening |
| | 1 | 74 | | PlaySound(open_success_sound); |
| | 1 | 75 | | _state = State.Opening; |
| | 6 | 76 | | } else { |
| | | 77 | | // cannot open |
| | 5 | 78 | | PlaySound(open_fail_sound); |
| | 5 | 79 | | } |
| | 6 | 80 | | } |
| | | 81 | | |
| | | 82 | | /** |
| | | 83 | | * Method that closes the door |
| | | 84 | | */ |
| | | 85 | | public void Close() |
| | 2 | 86 | | { |
| | | 87 | | // only affects opened or opening door |
| | 2 | 88 | | if (_state == State.Opened || _state == State.Opening) |
| | 2 | 89 | | { |
| | | 90 | | // start closing |
| | 2 | 91 | | PlaySound(open_success_sound); |
| | 2 | 92 | | _state = State.Closing; |
| | 2 | 93 | | } |
| | 2 | 94 | | } |
| | | 95 | | |
| | | 96 | | /** |
| | | 97 | | * Unlock the door |
| | | 98 | | */ |
| | | 99 | | public void Unlock() |
| | 3 | 100 | | { |
| | 3 | 101 | | locked = false; |
| | 3 | 102 | | } |
| | | 103 | | |
| | | 104 | | /** |
| | | 105 | | * Utility method for convenience |
| | | 106 | | */ |
| | | 107 | | protected void PlaySound(AudioClip clip) |
| | 8 | 108 | | { |
| | 8 | 109 | | AudioSource player = gameObject.GetComponent<AudioSource>(); |
| | 8 | 110 | | player.clip = clip; |
| | 8 | 111 | | player.Play(); |
| | 8 | 112 | | } |
| | | 113 | | |
| | | 114 | | } |