| 
									
										
										
										
											2021-03-02 13:24:18 +00:00
										 |  |  | using System; | 
					
						
							|  |  |  | using System.Collections; | 
					
						
							|  |  |  | using System.Collections.Generic; | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  | using System.Configuration; | 
					
						
							| 
									
										
										
										
											2021-03-02 13:24:18 +00:00
										 |  |  | using UnityEngine; | 
					
						
							|  |  |  | using UnityEngine.Serialization; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public class ScriptedDialogueTrigger : MonoBehaviour | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |     [Serializable] | 
					
						
							|  |  |  |     private enum Speaker | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Radio, | 
					
						
							|  |  |  |         Baz | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2021-03-02 13:24:18 +00:00
										 |  |  |     [SerializeField] private DialogueSystem _dialogue; | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |      | 
					
						
							|  |  |  |     [SerializeField] private Speaker _speaker = Speaker.Radio; | 
					
						
							| 
									
										
										
										
											2021-05-17 14:31:13 +01:00
										 |  |  |     [SerializeField] private Transform _speakerTransform; | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |     [Obsolete] | 
					
						
							| 
									
										
										
										
											2021-03-02 13:24:18 +00:00
										 |  |  |     [SerializeField] private string _key; | 
					
						
							| 
									
										
										
										
											2021-04-27 00:57:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |     [SerializeField] private string[] _keys; | 
					
						
							|  |  |  |     private float[] _durations; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 00:57:07 +01:00
										 |  |  |     [SerializeField] private bool _log; | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private void Awake() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (_keys.Length == 0) return; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         _durations = new float[_keys.Length]; | 
					
						
							|  |  |  |         for (int i = 0; i < _keys.Length; i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _durations[i] = _dialogue.GetLineDuration(_keys[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (_speaker == Speaker.Radio) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 // fudge because radio has a bit of a delay before starting the clip | 
					
						
							|  |  |  |                 _durations[i] += 0.3f; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 13:24:18 +00:00
										 |  |  |     private void OnTriggerEnter(Collider other) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-05-12 20:10:58 +01:00
										 |  |  |         if ( !other.CompareTag( "Player" ) ) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-04-27 00:57:07 +01:00
										 |  |  |         if (_log) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             Debug.Log($"{other} triggered dialogue {_key}"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (_keys.Length == 0) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-05-17 14:31:13 +01:00
										 |  |  |             PlayLine(_key, _speakerTransform); | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             StartCoroutine(PlayKeys()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private IEnumerator PlayKeys() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         var idx = 0; | 
					
						
							|  |  |  |         var elapsed = 0f; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // play first line | 
					
						
							| 
									
										
										
										
											2021-05-17 14:31:13 +01:00
										 |  |  |         PlayLine(_keys[0], _speakerTransform); | 
					
						
							| 
									
										
										
										
											2021-04-27 00:57:07 +01:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |         while (idx < _keys.Length) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             elapsed += Time.deltaTime; | 
					
						
							|  |  |  |             if (elapsed > _durations[idx]) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 elapsed = 0; | 
					
						
							| 
									
										
										
										
											2021-05-17 03:29:00 +01:00
										 |  |  |                  | 
					
						
							| 
									
										
										
										
											2021-05-15 15:08:58 +01:00
										 |  |  |                 idx++; | 
					
						
							| 
									
										
										
										
											2021-05-17 14:48:46 +01:00
										 |  |  |                  | 
					
						
							| 
									
										
										
										
											2021-05-17 03:29:00 +01:00
										 |  |  |                 if (idx >= _keys.Length) break; | 
					
						
							|  |  |  |                  | 
					
						
							| 
									
										
										
										
											2021-05-17 14:31:13 +01:00
										 |  |  |                 PlayLine(_keys[idx], _speakerTransform); | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             yield return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 14:31:13 +01:00
										 |  |  |     private void PlayLine(string key, Transform speakerObject) | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         switch (_speaker) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             case Speaker.Radio: | 
					
						
							|  |  |  |                 _dialogue.PlayLineRadio(key); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             case Speaker.Baz: | 
					
						
							| 
									
										
										
										
											2021-05-17 14:31:13 +01:00
										 |  |  |                 _dialogue.PlayLineBaz(key, speakerObject); | 
					
						
							| 
									
										
										
										
											2021-05-14 19:08:27 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-02 13:24:18 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |