ButtonSpermicidalWave.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ButtonSpermicidalWave : MonoBehaviour
  4. {
  5. private static Transform _transform;
  6. public static Transform GetTransform() { return _transform; }
  7. public UISprite sprite;
  8. public UILabel counter;
  9. bool _isEnabled;
  10. bool _hasBeenPressed;
  11. bool _waveInScene;
  12. int _usedWavesInThisLevel;
  13. #region Unity Callbacks
  14. void Awake()
  15. {
  16. _transform = transform;
  17. }
  18. void OnEnable()
  19. {
  20. NotificationCenter.AddListener(OnPickedUpSpermicidalWave, NotificationType.PickedUpSpermicidalWave);
  21. NotificationCenter.AddListener(OnPlaybackFireSpermicidalWave, NotificationType.PlaybackFireSpermicidalWave);
  22. NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
  23. NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  24. NotificationCenter.AddListener(OnSpermicidalWaveDespawned, NotificationType.SpermicidalWaveDespawned);
  25. UpdateCounterLabel();
  26. }
  27. void OnDisable()
  28. {
  29. NotificationCenter.RemoveListener(OnPickedUpSpermicidalWave, NotificationType.PickedUpSpermicidalWave);
  30. NotificationCenter.RemoveListener(OnPlaybackFireSpermicidalWave, NotificationType.PlaybackFireSpermicidalWave);
  31. NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
  32. NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  33. NotificationCenter.RemoveListener(OnSpermicidalWaveDespawned, NotificationType.SpermicidalWaveDespawned);
  34. }
  35. public void CustomFixedUpdate()
  36. {
  37. if (_hasBeenPressed)
  38. {
  39. _hasBeenPressed = false;
  40. NotificationCenter.Post(NotificationType.FireSpermicidalWave);
  41. InventoryManager.Instance.Consume(InventoryItem.specialwave);
  42. UpdateCounterLabel();
  43. }
  44. }
  45. #endregion
  46. #region Notification Callbacks
  47. void OnGameStart(Notification note)
  48. {
  49. _waveInScene = false;
  50. _usedWavesInThisLevel = 0;
  51. GetComponent<Collider>().enabled = false;
  52. }
  53. void OnStartSpawningEnemies(Notification note)
  54. {
  55. GetComponent<Collider>().enabled = true;
  56. SetButtonEnabled(HasInInventory());
  57. }
  58. void OnPickedUpSpermicidalWave(Notification note)
  59. {
  60. Logger.LogFlurryEvent("Picked up Spermicidal Wave");
  61. InventoryManager.Instance.Increment(InventoryItem.specialwave);
  62. UpdateCounterLabel();
  63. SetButtonEnabled(true);
  64. }
  65. void OnPlaybackFireSpermicidalWave(Notification note)
  66. {
  67. _hasBeenPressed = true;
  68. }
  69. void OnSpermicidalWaveDespawned(Notification note)
  70. {
  71. _waveInScene = false;
  72. }
  73. #endregion
  74. #region NGUI Callbacks
  75. void OnClick()
  76. {
  77. if (HasInInventory())
  78. {
  79. if (_usedWavesInThisLevel < GameConstants.MAX_WAVES_PER_LEVEL)
  80. {
  81. if (!_waveInScene &&
  82. !_hasBeenPressed)
  83. {
  84. _waveInScene = true;
  85. _hasBeenPressed = true;
  86. ++_usedWavesInThisLevel;
  87. Logger.LogFlurryEvent("Used Spermicidal Wave");
  88. if (!HasInInventory())
  89. {
  90. SetButtonEnabled(false);
  91. }
  92. }
  93. } else
  94. {
  95. Logger.LogFlurryEvent("Tried Spermicidal Wave but reached limit per level");
  96. NotificationCenter.Post(NotificationType.ShowGenericNotification, string.Format(Localization.instance.Get("spermicidalWave.limitPerLevelReached"), GameConstants.MAX_WAVES_PER_LEVEL));
  97. }
  98. } else
  99. {
  100. //NotificationCenter.Post(NotificationType.ShowGenericNotification, Localization.instance.Get("spermicidalWave.needToBuyNotification"));
  101. }
  102. }
  103. #endregion
  104. void SetButtonEnabled(bool isEnabled)
  105. {
  106. if (!isEnabled)
  107. {
  108. iTween.Stop(gameObject);
  109. CancelInvoke("Flash");
  110. sprite.alpha = 0.2f;
  111. } else
  112. //if it is not already enabled
  113. if (!_isEnabled)
  114. {
  115. //A bit hacky, but the alternative complicates life
  116. Invoke("Flash", Pickup.PICKUP_ANIMATION_TIME);
  117. }
  118. _isEnabled = isEnabled;
  119. }
  120. #region Flashing visual feedback
  121. int _flashesRemaining;
  122. protected virtual void Flash()
  123. {
  124. _flashesRemaining = 5;
  125. sprite.alpha = 1;
  126. AlphaDown();
  127. }
  128. void AlphaDown()
  129. {
  130. AnimateAlpha(1, 0);
  131. }
  132. void AlphaUp()
  133. {
  134. AnimateAlpha(0, 1);
  135. }
  136. void AnimateAlpha(float from, float to)
  137. {
  138. float flashTime = 0.2f;
  139. iTween.ValueTo(gameObject, iTween.Hash(
  140. "from", from,
  141. "to", to,
  142. "time", flashTime,
  143. "onupdate", "OniTweenUpdateSpriteAlpha",
  144. "onupdatetarget", gameObject,
  145. "oncomplete", "OniTweenFadeComplete",
  146. "oncompletetarget", gameObject));
  147. }
  148. void OniTweenUpdateSpriteAlpha(float newAlpha)
  149. {
  150. sprite.alpha = newAlpha;
  151. }
  152. void OniTweenFadeComplete()
  153. {
  154. --_flashesRemaining;
  155. if (_flashesRemaining > 0)
  156. {
  157. if (_flashesRemaining % 2 == 0)
  158. {
  159. AlphaDown();
  160. } else
  161. {
  162. AlphaUp();
  163. }
  164. }
  165. }
  166. #endregion
  167. void UpdateCounterLabel()
  168. {
  169. counter.text = GetCountInInventory().ToString();
  170. }
  171. bool HasInInventory()
  172. {
  173. return GetCountInInventory() > 0;
  174. }
  175. int GetCountInInventory()
  176. {
  177. return InventoryManager.Instance.GetItemCount(InventoryItem.specialwave);
  178. }
  179. }