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().enabled = false; } void OnStartSpawningEnemies(Notification note) { GetComponent().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); } }