StartCounter.cs 7.0 KB

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