123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using UnityEngine;
- using System.Collections;
- public class ButtonSpermicidalWave : MonoBehaviour
- {
- private static Transform _transform;
- public static Transform GetTransform() { return _transform; }
- public UISprite sprite;
- public UILabel counter;
- bool _isEnabled;
- bool _hasBeenPressed;
- bool _waveInScene;
- int _usedWavesInThisLevel;
- #region Unity Callbacks
- void Awake()
- {
- _transform = transform;
- }
-
- void OnEnable()
- {
- NotificationCenter.AddListener(OnPickedUpSpermicidalWave, NotificationType.PickedUpSpermicidalWave);
- NotificationCenter.AddListener(OnPlaybackFireSpermicidalWave, NotificationType.PlaybackFireSpermicidalWave);
- NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
- NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.AddListener(OnSpermicidalWaveDespawned, NotificationType.SpermicidalWaveDespawned);
- UpdateCounterLabel();
- }
- void OnDisable()
- {
- NotificationCenter.RemoveListener(OnPickedUpSpermicidalWave, NotificationType.PickedUpSpermicidalWave);
- NotificationCenter.RemoveListener(OnPlaybackFireSpermicidalWave, NotificationType.PlaybackFireSpermicidalWave);
- NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
- NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.RemoveListener(OnSpermicidalWaveDespawned, NotificationType.SpermicidalWaveDespawned);
- }
- public void CustomFixedUpdate()
- {
- if (_hasBeenPressed)
- {
- _hasBeenPressed = false;
- NotificationCenter.Post(NotificationType.FireSpermicidalWave);
- InventoryManager.Instance.Consume(InventoryItem.specialwave);
- UpdateCounterLabel();
- }
- }
- #endregion
- #region Notification Callbacks
- void OnGameStart(Notification note)
- {
- _waveInScene = false;
- _usedWavesInThisLevel = 0;
- GetComponent<Collider>().enabled = false;
- }
- void OnStartSpawningEnemies(Notification note)
- {
- GetComponent<Collider>().enabled = true;
- SetButtonEnabled(HasInInventory());
- }
- void OnPickedUpSpermicidalWave(Notification note)
- {
- Logger.LogFlurryEvent("Picked up Spermicidal Wave");
- InventoryManager.Instance.Increment(InventoryItem.specialwave);
- UpdateCounterLabel();
- SetButtonEnabled(true);
- }
- void OnPlaybackFireSpermicidalWave(Notification note)
- {
- _hasBeenPressed = true;
- }
- void OnSpermicidalWaveDespawned(Notification note)
- {
- _waveInScene = false;
- }
- #endregion
- #region NGUI Callbacks
- void OnClick()
- {
- if (HasInInventory())
- {
- if (_usedWavesInThisLevel < GameConstants.MAX_WAVES_PER_LEVEL)
- {
- if (!_waveInScene &&
- !_hasBeenPressed)
- {
- _waveInScene = true;
- _hasBeenPressed = true;
- ++_usedWavesInThisLevel;
- Logger.LogFlurryEvent("Used Spermicidal Wave");
- if (!HasInInventory())
- {
- SetButtonEnabled(false);
- }
- }
- } else
- {
- Logger.LogFlurryEvent("Tried Spermicidal Wave but reached limit per level");
- NotificationCenter.Post(NotificationType.ShowGenericNotification, string.Format(Localization.instance.Get("spermicidalWave.limitPerLevelReached"), GameConstants.MAX_WAVES_PER_LEVEL));
- }
- } else
- {
- //NotificationCenter.Post(NotificationType.ShowGenericNotification, Localization.instance.Get("spermicidalWave.needToBuyNotification"));
- }
- }
- #endregion
- void SetButtonEnabled(bool isEnabled)
- {
- if (!isEnabled)
- {
- iTween.Stop(gameObject);
- CancelInvoke("Flash");
- sprite.alpha = 0.2f;
- } else
- //if it is not already enabled
- if (!_isEnabled)
- {
- //A bit hacky, but the alternative complicates life
- Invoke("Flash", Pickup.PICKUP_ANIMATION_TIME);
- }
- _isEnabled = isEnabled;
- }
- #region Flashing visual feedback
- int _flashesRemaining;
- protected virtual void Flash()
- {
- _flashesRemaining = 5;
- sprite.alpha = 1;
- AlphaDown();
- }
- void AlphaDown()
- {
- AnimateAlpha(1, 0);
- }
- void AlphaUp()
- {
- AnimateAlpha(0, 1);
- }
- void AnimateAlpha(float from, float to)
- {
- float flashTime = 0.2f;
- iTween.ValueTo(gameObject, iTween.Hash(
- "from", from,
- "to", to,
- "time", flashTime,
- "onupdate", "OniTweenUpdateSpriteAlpha",
- "onupdatetarget", gameObject,
- "oncomplete", "OniTweenFadeComplete",
- "oncompletetarget", gameObject));
- }
- void OniTweenUpdateSpriteAlpha(float newAlpha)
- {
- sprite.alpha = newAlpha;
- }
- void OniTweenFadeComplete()
- {
- --_flashesRemaining;
- if (_flashesRemaining > 0)
- {
- if (_flashesRemaining % 2 == 0)
- {
- AlphaDown();
- } else
- {
- AlphaUp();
- }
- }
- }
- #endregion
- void UpdateCounterLabel()
- {
- counter.text = GetCountInInventory().ToString();
- }
- bool HasInInventory()
- {
- return GetCountInInventory() > 0;
- }
- int GetCountInInventory()
- {
- return InventoryManager.Instance.GetItemCount(InventoryItem.specialwave);
- }
- }
|