| 
									
										
										
										
											2021-04-15 20:31:09 +01:00
										 |  |  | using Ktyl.Util; | 
					
						
							| 
									
										
										
										
											2021-03-17 20:50:08 +00:00
										 |  |  | using System.Collections; | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  | using PathCreation; | 
					
						
							| 
									
										
										
										
											2021-03-17 20:50:08 +00:00
										 |  |  | using UnityEngine; | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  | #if UNITY_EDITOR | 
					
						
							|  |  |  | using UnityEditor; | 
					
						
							|  |  |  | #endif | 
					
						
							| 
									
										
										
										
											2021-03-17 20:50:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | public class RollingBoulder : MonoBehaviour | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |     // how long it takes to travel the length of the path | 
					
						
							|  |  |  |     [SerializeField] private float speed; | 
					
						
							|  |  |  |     [SerializeField] private Transform boulder; | 
					
						
							|  |  |  |     [SerializeField] private SerialFloat objectTimeScale; | 
					
						
							|  |  |  |     [SerializeField] private PathCreator path; | 
					
						
							|  |  |  |     [SerializeField] private Killbox _killbox; | 
					
						
							|  |  |  |     [SerializeField] private string _fmodKey; | 
					
						
							| 
									
										
										
										
											2021-05-16 20:00:39 +01:00
										 |  |  |     [SerializeField] private float _resetTime; | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |      | 
					
						
							|  |  |  |     private bool _triggered = false; | 
					
						
							|  |  |  |     private GameObject _endBoulder; | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |     private void Update() | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |         _killbox.gameObject.SetActive(objectTimeScale > 0.5f); | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private void OnTriggerEnter(Collider other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Checks to make sure other collider is the Player using tag. | 
					
						
							|  |  |  |         if (other.gameObject.CompareTag("Player")) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |             Trigger(); | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |     public void Trigger() | 
					
						
							| 
									
										
										
										
											2021-03-17 20:50:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |         if (_triggered) return; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         FMODUnity.RuntimeManager.PlayOneShot(_fmodKey); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         StartCoroutine(RollingStone()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private IEnumerator RollingStone() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         _triggered = true; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         var d = 0f; | 
					
						
							|  |  |  |         var pathLength = path.path.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (_triggered && d < pathLength) | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |             d += Time.deltaTime * objectTimeScale * speed; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             var pos = path.path.GetPointAtDistance(d); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             boulder.transform.position = pos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             yield return null; | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (_triggered) | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |             boulder.position = path.path.GetPointAtTime(0.999f); | 
					
						
							| 
									
										
										
										
											2021-03-17 22:07:33 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-16 20:00:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         var resetElapsed = 0f; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (resetElapsed < _resetTime) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             resetElapsed += Time.deltaTime * objectTimeScale; | 
					
						
							|  |  |  |             yield return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         Reset(); | 
					
						
							| 
									
										
										
										
											2021-03-17 20:50:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |     public void Reset() | 
					
						
							| 
									
										
										
										
											2021-03-17 20:50:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  |         boulder.transform.position = path.path.GetPointAtTime(0); | 
					
						
							|  |  |  |         _triggered = false; | 
					
						
							| 
									
										
										
										
											2021-03-17 20:50:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-05-16 17:08:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if UNITY_EDITOR | 
					
						
							|  |  |  | [CustomEditor(typeof(RollingBoulder))] | 
					
						
							|  |  |  | public class RollingBoulderEditor : Editor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private RollingBoulder _data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private void OnEnable() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         _data = target as RollingBoulder; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public override void OnInspectorGUI() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         base.OnInspectorGUI(); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         if (!Application.isPlaying) return; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         if (GUILayout.Button("Trigger")) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _data.Trigger(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (GUILayout.Button("Reset")) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _data.Reset(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif |