| | | 1 | | using UnityEngine; |
| | | 2 | | using System.Collections; |
| | | 3 | | using VRExplorer; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | |
| | | 6 | | public class Waypoint : MonoBehaviour, ITriggerableEntity |
| | | 7 | | { |
| | | 8 | | #region Entity |
| | | 9 | | [ExcludeFromCodeCoverage] public float TriggeringTime => 0.05f; |
| | | 10 | | [ExcludeFromCodeCoverage] public string Name => Str.Button; |
| | | 11 | | |
| | | 12 | | [ExcludeFromCodeCoverage] |
| | | 13 | | public void Triggerred() |
| | | 14 | | { |
| | | 15 | | Exit(); |
| | | 16 | | Click(); |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | [ExcludeFromCodeCoverage] |
| | | 20 | | public void Triggerring() |
| | | 21 | | { |
| | | 22 | | Enter(); |
| | | 23 | | } |
| | | 24 | | #endregion |
| | | 25 | | private enum State |
| | | 26 | | { |
| | | 27 | | Idle, |
| | | 28 | | Focused, |
| | | 29 | | Clicked, |
| | | 30 | | Approaching, |
| | | 31 | | Moving, |
| | | 32 | | Collect, |
| | | 33 | | Collected, |
| | | 34 | | Occupied, |
| | | 35 | | Open, |
| | | 36 | | Hidden |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | [SerializeField] |
| | 361 | 40 | | private State _state = State.Idle; |
| | 361 | 41 | | private Color _color_origional = new Color(0.0f, 1.0f, 0.0f, 0.5f); |
| | 361 | 42 | | private Color _color = Color.white; |
| | 361 | 43 | | private float _scale = 1.0f; |
| | 361 | 44 | | private float _displacement = 0.0f; |
| | 361 | 45 | | private Vector3 _displacementVector = new Vector3(0.0f, 1.0f, 0.0f); |
| | 361 | 46 | | private Vector3 _rotationVector = new Vector3(0.0f, 1.0f, 0.0f); |
| | 361 | 47 | | private float _animated_lerp = 1.0f; |
| | 361 | 48 | | private AudioSource _audio_source = null; |
| | 361 | 49 | | private Material _material = null; |
| | | 50 | | private Vector3 _initial_position; |
| | | 51 | | // camera should be slightly above |
| | 361 | 52 | | private Vector3 _cameraDisplacement = new Vector3(0f, 0.7f, 0f); |
| | | 53 | | private GameObject cam; |
| | | 54 | | |
| | | 55 | | [Header("Material")] |
| | 361 | 56 | | public Material material = null; |
| | 361 | 57 | | public Color color_hilight = new Color(0.8f, 0.8f, 1.0f, 0.125f); |
| | | 58 | | |
| | | 59 | | [Header("State Blend Speeds")] |
| | 361 | 60 | | public float lerp_idle = 0.0f; |
| | 361 | 61 | | public float lerp_focus = 0.0f; |
| | 361 | 62 | | public float lerp_hide = 0.0f; |
| | 361 | 63 | | public float lerp_clicked = 0.0f; |
| | | 64 | | |
| | | 65 | | [Header("State Animation Scales")] |
| | 361 | 66 | | public float scale_clicked_max = 0.0f; |
| | 361 | 67 | | public float scale_animation = 3.0f; |
| | 361 | 68 | | public float scale_idle_min = 0.0f; |
| | 361 | 69 | | public float scale_idle_max = 0.0f; |
| | 361 | 70 | | public float scale_focus_min = 0.0f; |
| | 361 | 71 | | public float scale_focus_max = 0.0f; |
| | 361 | 72 | | public float displacement_idle_min = -2f; |
| | 361 | 73 | | public float displacement_idle_max = 2f; |
| | | 74 | | |
| | | 75 | | [Header("Misc")] |
| | 361 | 76 | | public float rotation_speed = 2f; |
| | | 77 | | |
| | | 78 | | [Header("Sounds")] |
| | 361 | 79 | | public AudioClip clip_click = null; |
| | | 80 | | |
| | | 81 | | [Header("Hide Distance")] |
| | 361 | 82 | | public float threshold = 0.125f; |
| | | 83 | | |
| | | 84 | | |
| | | 85 | | |
| | | 86 | | void Awake() |
| | 361 | 87 | | { |
| | 361 | 88 | | _material = Instantiate(material); |
| | 361 | 89 | | _color_origional = _material.color; |
| | 361 | 90 | | _color = _color_origional; |
| | 361 | 91 | | _audio_source = gameObject.GetComponent<AudioSource>(); |
| | 361 | 92 | | _audio_source.clip = clip_click; |
| | 361 | 93 | | _audio_source.playOnAwake = false; |
| | 361 | 94 | | _initial_position = gameObject.transform.position; |
| | | 95 | | // modifying camera's position does not work on Android for some reason (but works in unity) |
| | | 96 | | // so i had to wrap camera in empty object and move this object |
| | 361 | 97 | | cam = GameObject.Find("Camera Holder"); |
| | 361 | 98 | | } |
| | | 99 | | |
| | | 100 | | |
| | | 101 | | void Update() |
| | 1990649 | 102 | | { |
| | 1990649 | 103 | | bool occupied = cam.transform.position == (_initial_position + _cameraDisplacement); |
| | | 104 | | |
| | 1990649 | 105 | | switch(_state) |
| | | 106 | | { |
| | | 107 | | case State.Idle: |
| | 1888454 | 108 | | Idle(); |
| | | 109 | | |
| | 1888454 | 110 | | _state = occupied ? State.Occupied : _state; |
| | 1888454 | 111 | | break; |
| | | 112 | | |
| | | 113 | | case State.Focused: |
| | 1560 | 114 | | Focus(); |
| | 1560 | 115 | | break; |
| | | 116 | | |
| | | 117 | | case State.Clicked: |
| | 781 | 118 | | Clicked(); |
| | 781 | 119 | | bool scaled = _scale >= scale_clicked_max * .95f; |
| | 781 | 120 | | _state = scaled ? State.Approaching : _state; |
| | 781 | 121 | | break; |
| | | 122 | | |
| | | 123 | | case State.Approaching: |
| | 478 | 124 | | Hide(); |
| | 478 | 125 | | Approach(); |
| | 478 | 126 | | _state = occupied ? State.Occupied : _state; |
| | 478 | 127 | | break; |
| | | 128 | | |
| | | 129 | | case State.Occupied: |
| | 99376 | 130 | | Hide(); |
| | 99376 | 131 | | _state = !occupied ? State.Idle : _state; |
| | 99376 | 132 | | break; |
| | | 133 | | |
| | | 134 | | case State.Hidden: |
| | 0 | 135 | | Hide(); |
| | 0 | 136 | | break; |
| | | 137 | | |
| | | 138 | | default: |
| | 0 | 139 | | break; |
| | | 140 | | } |
| | | 141 | | |
| | 1990649 | 142 | | gameObject.GetComponentInChildren<MeshRenderer>().material.color = _color; |
| | 1990649 | 143 | | gameObject.transform.localScale = Vector3.one * _scale; |
| | 1990649 | 144 | | _animated_lerp = Mathf.Abs(Mathf.Cos(Time.time * scale_animation)); |
| | | 145 | | |
| | | 146 | | // levitate |
| | 1990649 | 147 | | gameObject.transform.position = _initial_position + _displacementVector * _displacement; |
| | | 148 | | |
| | | 149 | | // rotate waypoint object |
| | 1990649 | 150 | | gameObject.transform.Rotate(Time.deltaTime * rotation_speed * _rotationVector); |
| | 1990649 | 151 | | } |
| | | 152 | | |
| | | 153 | | |
| | | 154 | | public void Enter() |
| | 241 | 155 | | { |
| | 241 | 156 | | _state = _state == State.Idle ? State.Focused : _state; |
| | 241 | 157 | | } |
| | | 158 | | |
| | | 159 | | |
| | | 160 | | public void Exit() |
| | 241 | 161 | | { |
| | | 162 | | // only go idle from focued state, to avoid non-working clicks |
| | 241 | 163 | | _state = _state == State.Focused ? State.Idle : _state; |
| | 241 | 164 | | } |
| | | 165 | | |
| | | 166 | | public void Approach() |
| | 478 | 167 | | { |
| | 478 | 168 | | cam.transform.localPosition = _initial_position + _cameraDisplacement; |
| | 478 | 169 | | cam.transform.rotation = Quaternion.identity; |
| | 478 | 170 | | } |
| | | 171 | | |
| | | 172 | | public void Click() |
| | 241 | 173 | | { |
| | 241 | 174 | | _state = State.Clicked; |
| | 241 | 175 | | _audio_source.Play(); |
| | 241 | 176 | | } |
| | | 177 | | |
| | | 178 | | |
| | | 179 | | private void Idle() |
| | 1888454 | 180 | | { |
| | 1888454 | 181 | | float scale = Mathf.Lerp(scale_idle_min, scale_idle_max, _animated_lerp); |
| | 1888454 | 182 | | float displacement = Mathf.Lerp(displacement_idle_min, displacement_idle_max, _animated_lerp); |
| | 1888454 | 183 | | Color color = Color.Lerp(_color_origional, color_hilight, _animated_lerp); |
| | | 184 | | |
| | 1888454 | 185 | | _scale = Mathf.Lerp(_scale, scale, lerp_idle); |
| | 1888454 | 186 | | _color = Color.Lerp(_color, color, lerp_idle); |
| | 1888454 | 187 | | _displacement = Mathf.Lerp(_displacement, displacement, lerp_idle); |
| | 1888454 | 188 | | } |
| | | 189 | | |
| | | 190 | | |
| | | 191 | | public void Focus() |
| | 1560 | 192 | | { |
| | 1560 | 193 | | float scale = Mathf.Lerp(scale_focus_min, scale_focus_max, _animated_lerp); |
| | 1560 | 194 | | float displacement = Mathf.Lerp(displacement_idle_min, displacement_idle_max, _animated_lerp); |
| | 1560 | 195 | | Color color = Color.Lerp( _color_origional, color_hilight, _animated_lerp); |
| | | 196 | | |
| | 1560 | 197 | | _scale = Mathf.Lerp(_scale, scale, lerp_focus); |
| | 1560 | 198 | | _color = Color.Lerp(_color, color, lerp_focus); |
| | 1560 | 199 | | _displacement = Mathf.Lerp(_displacement, displacement, lerp_focus); |
| | 1560 | 200 | | } |
| | | 201 | | |
| | | 202 | | |
| | | 203 | | public void Clicked() |
| | 781 | 204 | | { |
| | 781 | 205 | | _scale = Mathf.Lerp(_scale, scale_clicked_max, lerp_clicked); |
| | 781 | 206 | | _color = Color.Lerp(_color, color_hilight, lerp_clicked); |
| | 781 | 207 | | } |
| | | 208 | | |
| | | 209 | | |
| | | 210 | | public void Hide() |
| | 99854 | 211 | | { |
| | 99854 | 212 | | _scale = Mathf.Lerp(_scale, 0.0f, lerp_hide); |
| | 99854 | 213 | | _color = Color.Lerp(_color, Color.clear, lerp_hide); |
| | 99854 | 214 | | } |
| | | 215 | | } |