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