123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using TMPro;
- using ICTS.Localization;
- /// <summary>
- /// This component is responsible for updating the UI of the lives counter. This includes
- /// the number of current lives available, the time to replenish the live (if it's not full),
- /// and also a circular graphical representation of how much lives are left.
- /// It will also give visual feedback when a life charges or discharges
- /// </summary>
- public class LivesCounterUI : MonoBehaviour
- {
- public TextMeshProUGUI livesCountLabel;
- public TextMeshProUGUI timeLeftLabel;
- public Image livesCircle;
- public Transform feedbackHeart;
- const float FADE_TIME = 1;
- int _prevLives;
- Vector3 _originalHeartScale;
- Image _feedbackHeartSprite;
- void Awake()
- {
- _feedbackHeartSprite = feedbackHeart.GetComponent<Image>();
- _originalHeartScale = feedbackHeart.transform.localScale;
- NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
- NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
- DebugViewManager.OnDebugView += OnDebugView;
- _prevLives = LivesManager.Instance.Lives;
- }
- void OnDestroy()
- {
- NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
- DebugViewManager.OnDebugView -= OnDebugView;
- }
- void OnEnable()
- {
- UpdateLivesCounterAndLivesCircle();
- StartCoroutine(UpdateUICoroutine());
- StopCoroutine(DelayedHideCoroutine());
- }
- void OnDebugView()
- {
- if (GUILayout.Button("Reset Lives"))
- {
- for (int i = LivesManager.Instance.Lives; i < LivesManager.MAX_RECHARGABLE_LIVES; i++)
- {
- LivesManager.Instance.RechargeLife();
- }
- }
- if (GUILayout.Button("Zero Lives"))
- {
- while (LivesManager.Instance.Lives > 0)
- {
- LivesManager.Instance.ConsumeLife();
- }
- }
- }
- /// <summary>
- /// Updates the UI components every half a second ... that should be enough.
- /// Note that it's not ever 1 second since one might experience a "jump" of two seconds visually,
- /// due to WaitForSeconds not being accurate. With 0.5f this should be more unlike to occur
- /// </summary>
- IEnumerator UpdateUICoroutine()
- {
- while (true)
- {
- int numOfLives = LivesManager.Instance.Lives;
- if (_prevLives < numOfLives)
- {
- IncreaseLivesFeedback();
- }
- if (_prevLives != numOfLives)
- {
- UpdateLivesCounterAndLivesCircle();
- }
- _prevLives = numOfLives;
- if (LivesManager.Instance.IsFull)
- {
- timeLeftLabel.text = Localization.instance.Get("lifeBank.full");
- } else
- {
- timeLeftLabel.text = "";
- //timeLeftLabel.text = UIUtils.FormatSeconds(LivesManager.Instance.SecondsLeftToReplenishALife);
- }
- yield return new WaitForSeconds(0.5f);
- }
- }
- void UpdateLivesCounterAndLivesCircle()
- {
- int numOfLives = LivesManager.Instance.Lives;
- if (!PlayerPrefs.HasKey("Live"))
- {
- PlayerPrefs.SetInt("Live", LivesManager.MAX_RECHARGABLE_LIVES);
- numOfLives = LivesManager.MAX_RECHARGABLE_LIVES;
- PlayerPrefs.Save();
- LivesManager.Instance.Lives = LivesManager.MAX_RECHARGABLE_LIVES;
- }
-
- livesCountLabel.text = numOfLives.ToString();
- float newFilledAmount = numOfLives / (float)LivesManager.MAX_RECHARGABLE_LIVES;
- iTween.ValueTo(livesCircle.gameObject, iTween.Hash(
- "from", livesCircle.fillAmount,
- "to", newFilledAmount,
- "easeType", iTween.EaseType.easeOutCubic,
- "onupdate", "OniTweenUpdateLivesCircle",
- "onupdatetarget", gameObject));
- }
- void OnGameStart(Notification note)
- {
- AVDebug.Assert(LivesManager.Instance.CanPlay, "Trying to play when he doesn't have lives! Should never happen.");
- //LivesManager.Instance.ConsumeLife();
- DecreaseLivesFeedback();
- }
- void OnGameOver(Notification note)
- {
- AVDebug.Assert(LivesManager.Instance.CanPlay, "Trying to play when he doesn't have lives! Should never happen.");
- //LivesManager.Instance.ConsumeLife();
- //DecreaseLivesFeedback();
- }
- #region Visual Feedback
- void IncreaseLivesFeedback()
- {
- SoundManager.Play(SoundEvent.lifeIncrement);
- feedbackHeart.localScale = _originalHeartScale * 2;
- iTween.ScaleTo(feedbackHeart.gameObject, iTween.Hash(
- "scale", _originalHeartScale,
- "time", 1));
- _feedbackHeartSprite.color = new Color(_feedbackHeartSprite.color.r, _feedbackHeartSprite.color.g, _feedbackHeartSprite.color.b,0);
- FadeHeartTo(1);
- StartCoroutine(FadeOutIncreaseFeedbackHeartCoroutine());
- }
- IEnumerator FadeOutIncreaseFeedbackHeartCoroutine()
- {
- yield return new WaitForSeconds(FADE_TIME);
- FadeHeartTo(0);
- }
- void DecreaseLivesFeedback()
- {
- feedbackHeart.localScale = _originalHeartScale;
- iTween.ScaleTo(feedbackHeart.gameObject, iTween.Hash(
- "scale", _originalHeartScale * 2,
- "time", 1));
- _feedbackHeartSprite.color = new Color(_feedbackHeartSprite.color.r, _feedbackHeartSprite.color.g, _feedbackHeartSprite.color.b, 1);
- FadeHeartTo(0);
- if(gameObject.activeSelf)
- try
- {
- StartCoroutine(DelayedHideCoroutine());
- }
- catch (Exception e)
- {
-
- Debug.Log(e);
- }
-
- }
- void FadeHeartTo(float toAlpha)
- {
- iTween.ValueTo(_feedbackHeartSprite.gameObject, iTween.Hash(
- "from", _feedbackHeartSprite.color.a,
- "to", toAlpha,
- "time", FADE_TIME,
- "onupdate", "OniTweenUpdateSpriteAlpha",
- "onupdatetarget", gameObject));
- }
- void OniTweenUpdateSpriteAlpha(float newAlpha)
- {
- _feedbackHeartSprite.color = new Color(_feedbackHeartSprite.color.r, _feedbackHeartSprite.color.g, _feedbackHeartSprite.color.b, newAlpha);
- }
- void OniTweenUpdateLivesCircle(float newFilledAmount)
- {
- livesCircle.fillAmount = newFilledAmount;
- }
- #endregion
- IEnumerator DelayedHideCoroutine()
- {
- yield return new WaitForSeconds(FADE_TIME);
- transform.parent.SendMessageUpwards("Hide");
- }
- }
|