TimerEffects.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine;
  2. using System.Collections;
  3. using TMPro;
  4. public class TimerEffects : MonoBehaviour
  5. {
  6. TextMeshProUGUI label;
  7. int lastPanicNumber;
  8. void Awake()
  9. {
  10. label = GetComponentInChildren<TextMeshProUGUI>();
  11. label.text = "";
  12. NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
  13. NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
  14. NotificationCenter.AddListener(OnTimeLeftUpdate, NotificationType.TimeLeftUpdate);
  15. }
  16. private void OnGameOver(Notification note)
  17. {
  18. //throw new System.NotImplementedException();
  19. }
  20. void OnDestroy()
  21. {
  22. NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
  23. NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
  24. NotificationCenter.RemoveListener(OnTimeLeftUpdate, NotificationType.TimeLeftUpdate);
  25. }
  26. void OnGameStart(Notification note)
  27. {
  28. label.text = "";
  29. }
  30. void OnTimeLeftUpdate(Notification note)
  31. {
  32. int timeLeft = (int)((float)note.data);
  33. label.text = timeLeft.ToString();
  34. if (timeLeft <= 10 && lastPanicNumber != timeLeft)
  35. {
  36. lastPanicNumber = timeLeft;
  37. Panic();
  38. }
  39. }
  40. public void Reset()
  41. {
  42. iTween.Stop(gameObject);
  43. transform.localScale = Vector3.one;
  44. label.color = Color.white;
  45. //label.MarkAsChangedLite();
  46. }
  47. void Panic()
  48. {
  49. SoundManager.Play(SoundEvent.countDownTick);
  50. iTween.Stop(gameObject);
  51. transform.localScale = Vector3.one * 3;
  52. iTween.ScaleTo(gameObject, iTween.Hash(
  53. "scale", Vector3.one,
  54. "time", 0.5f));
  55. iTween.ValueTo(gameObject, iTween.Hash(
  56. "from", 0,
  57. "to", 1,
  58. "time", 0.75f,
  59. "onupdate", "OniTweenUpdate",
  60. "onupdatetarget", gameObject));
  61. }
  62. void OniTweenUpdate(float newValue)
  63. {
  64. label.color = new Color(1, newValue, newValue);
  65. //label.MarkAsChangedLite();
  66. }
  67. }