StartCounter.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using TMPro;
  5. public class StartCounter : MonoBehaviour
  6. {
  7. int _count;
  8. TextMeshProUGUI _label;
  9. const int INITIAL_DELAY_TICKS = 1;
  10. const float TIME_PER_NUMBER = 0.5f;
  11. const int START_NUMBER = 3;
  12. int _nextAnimationTick;
  13. public bool HasPlayerGetLife = false;
  14. //Due to the processing time to create the sperms, the coming sound is not synched with 3-2-1 go sound
  15. //So we introduced this bool so that we play it once the FixedUpdates for start counter start rolling out.
  16. public bool HasPlayedStartSound = true;
  17. private int _lastRectTickCalculation;
  18. Vector3 originalPos;
  19. void Awake()
  20. {
  21. _label = GetComponentInChildren<TextMeshProUGUI>();
  22. originalPos = transform.localPosition;
  23. NotificationCenter.AddListener(OnCelebrityDisappeared, NotificationType.CelebrityDisappeared);
  24. NotificationCenter.AddListener(OnGetLife, NotificationType.GetLife);
  25. NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
  26. }
  27. private void OnGameOver(Notification note)
  28. {
  29. HasPlayerGetLife = false;
  30. HasPlayedStartSound = true;
  31. }
  32. void OnDestroy()
  33. {
  34. NotificationCenter.RemoveListener(OnCelebrityDisappeared, NotificationType.CelebrityDisappeared);
  35. NotificationCenter.RemoveListener(OnGetLife, NotificationType.GetLife);
  36. }
  37. private void OnGetLife(Notification note)
  38. {
  39. if (!MenuManager._instance.mainMenuScreen.isActiveAndEnabled)
  40. {
  41. HasPlayerGetLife = true;
  42. _label.enabled = true;
  43. _count = 4;
  44. iTween.Stop(gameObject); //just in case
  45. transform.localPosition = originalPos;
  46. _lastRectTickCalculation = RecordingManager.Ticks + 1;
  47. //Debug.Log("OnCelebrityDisappeared " + HasPlayedStartSound );
  48. HasPlayedStartSound = false;
  49. }
  50. }
  51. void OnEnable()
  52. {
  53. _label.enabled = false;
  54. }
  55. void OnCelebrityDisappeared(Notification note)
  56. {
  57. NotificationCenter.Post(NotificationType.EndTimerStart);
  58. _label.enabled = true;
  59. _count = START_NUMBER;
  60. iTween.Stop(gameObject); //just in case
  61. transform.localPosition = originalPos;
  62. _nextAnimationTick = RecordingManager.Ticks+1;
  63. //Debug.Log("OnCelebrityDisappeared " + HasPlayedStartSound );
  64. HasPlayedStartSound = false;
  65. }
  66. void FixedUpdate()
  67. {
  68. if (!HasPlayedStartSound)
  69. {
  70. HasPlayedStartSound = true;
  71. SoundManager.Play(SoundEvent.coming);
  72. }
  73. if (HasPlayerGetLife)
  74. {
  75. if (RecordingManager.Ticks == _lastRectTickCalculation)
  76. {
  77. if (_count > 0)
  78. {
  79. UpdateLabel();
  80. transform.localScale = Vector3.one * 4;
  81. iTween.ScaleTo(gameObject, iTween.Hash(
  82. "usefixedupdate", true,
  83. "scale", Vector3.one,
  84. //"delay", _count == START_NUMBER ? INITIAL_DELAY : 0,
  85. "time", TIME_PER_NUMBER,
  86. "onupdate", "OniTweenUpdate",
  87. "onupdatetarget", gameObject,
  88. "oncomplete", "OniTweenScaleComplete",
  89. "oncompletetarget", gameObject));
  90. --_count;
  91. }
  92. else
  93. {
  94. GameUpdateManager.Instance._isPlaying = true;
  95. BackgroundManager.Instance._isPlaying = true;
  96. MenuManager._instance.IsCelebrity = false;
  97. //SceneryGenerator.Instance._isPlaying = true;
  98. HasPlayerGetLife = false;
  99. iTween.MoveTo(gameObject, iTween.Hash(
  100. "islocal", true,
  101. "x", GameConstants.GAME_WIDTH,
  102. "time", 0.5f,
  103. "easetype", iTween.EaseType.easeInBack,
  104. "onupdate", "OniTweenUpdate",
  105. "onupdatetarget", gameObject));
  106. }
  107. }
  108. }
  109. else
  110. {
  111. if (RecordingManager.Ticks == _nextAnimationTick)
  112. {
  113. if (_count > 0)
  114. {
  115. UpdateLabel();
  116. transform.localScale = Vector3.one * 4;
  117. iTween.ScaleTo(gameObject, iTween.Hash(
  118. "usefixedupdate", true,
  119. "scale", Vector3.one,
  120. //"delay", _count == START_NUMBER ? INITIAL_DELAY : 0,
  121. "time", TIME_PER_NUMBER,
  122. "onupdate", "OniTweenUpdate",
  123. "onupdatetarget", gameObject,
  124. "oncomplete", "OniTweenScaleComplete",
  125. "oncompletetarget", gameObject));
  126. --_count;
  127. }
  128. else
  129. {
  130. NotificationCenter.Post(NotificationType.StartSpawningEnemies);
  131. iTween.MoveTo(gameObject, iTween.Hash(
  132. "islocal", true,
  133. "x", GameConstants.GAME_WIDTH,
  134. "time", 0.5f,
  135. "easetype", iTween.EaseType.easeInBack,
  136. "onupdate", "OniTweenUpdate",
  137. "onupdatetarget", gameObject));
  138. MenuManager._instance.IsCelebrity = false;
  139. }
  140. }
  141. }
  142. }
  143. //void UpdateTimerOnAddLife()
  144. //{
  145. // if (!HasPlayedStartSound)
  146. // {
  147. // HasPlayedStartSound = true;
  148. // SoundManager.Play(SoundEvent.coming);
  149. // }
  150. // if (RecordingManager.Ticks == _nextAnimationTick)
  151. // {
  152. // if (_count > 0)
  153. // {
  154. // UpdateLabel();
  155. // transform.localScale = Vector3.one * 4;
  156. // iTween.ScaleTo(gameObject, iTween.Hash(
  157. // "usefixedupdate", true,
  158. // "scale", Vector3.one,
  159. // //"delay", _count == START_NUMBER ? INITIAL_DELAY : 0,
  160. // "time", TIME_PER_NUMBER,
  161. // "onupdate", "OniTweenUpdate",
  162. // "onupdatetarget", gameObject,
  163. // "oncomplete", "OniTweenScaleComplete",
  164. // "oncompletetarget", gameObject));
  165. // --_count;
  166. // }
  167. // else
  168. // {
  169. // NotificationCenter.Post(NotificationType.GetLife);
  170. // iTween.MoveTo(gameObject, iTween.Hash(
  171. // "islocal", true,
  172. // "x", GameConstants.GAME_WIDTH,
  173. // "time", 0.5f,
  174. // "easetype", iTween.EaseType.easeInBack,
  175. // "onupdate", "OniTweenUpdate",
  176. // "onupdatetarget", gameObject));
  177. // }
  178. // }
  179. //}
  180. void UpdateLabel()
  181. {
  182. if (_count > 0)
  183. {
  184. _label.text = _count.ToString();
  185. }
  186. /*else
  187. if (_count == 0)
  188. {
  189. _label.text = "GO!";
  190. }*/
  191. }
  192. void OniTweenUpdate()
  193. {
  194. //_label.MarkAsChangedLite();
  195. }
  196. void OniTweenScaleComplete()
  197. {
  198. _nextAnimationTick = RecordingManager.Ticks+1;
  199. _lastRectTickCalculation = RecordingManager.Ticks + 1;
  200. }
  201. }