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