TimeManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using UnityEngine;
  2. using System.Collections;
  3. using ICTS.Localization;
  4. public class TimeManager : MonoBehaviour
  5. {
  6. #region Unity Singleton
  7. private static TimeManager _instance;
  8. public static TimeManager Instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. {
  14. _instance = FindObjectOfType(typeof(TimeManager)) as TimeManager;
  15. if (_instance == null)
  16. {
  17. AVDebug.LogError(string.Format("No gameObject with {0} component exists. Make sure to create a gameObject with {0} component", new System.Diagnostics.StackFrame().GetMethod().DeclaringType));
  18. }
  19. }
  20. return _instance;
  21. }
  22. }
  23. void Awake()
  24. {
  25. if (_instance != null && _instance != this)
  26. {
  27. AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name));
  28. Destroy(gameObject);
  29. return;
  30. }
  31. _instance = this;
  32. DontDestroyOnLoad(gameObject);
  33. enabled = false;
  34. RegisterListeners();
  35. }
  36. void OnDestroy()
  37. {
  38. UnregisterListeners();
  39. }
  40. #endregion
  41. float _timeLeft;
  42. public bool IsReachingEndOfLevel { get { return _timeLeft <= 10; } }
  43. bool _isSpawningEnemies;
  44. public long StartServerTime { get; set; }
  45. public long EndServerTime { get; set; }
  46. public long PauseStartServerTime { private get; set; }
  47. public long PauseEndServerTime {
  48. set
  49. {
  50. StartServerTime += value - PauseStartServerTime;
  51. PauseStartServerTime = 0;
  52. }
  53. }
  54. void RegisterListeners()
  55. {
  56. NotificationCenter.AddListener(OnStartTimer, NotificationType.StartGameTimer);
  57. NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  58. NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
  59. NotificationCenter.AddListener(OnTimeBonus, NotificationType.TimeBonus);
  60. DebugViewManager.OnDebugView += OnDebugView;
  61. }
  62. void UnregisterListeners()
  63. {
  64. NotificationCenter.RemoveListener(OnStartTimer, NotificationType.StartGameTimer);
  65. NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  66. NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
  67. NotificationCenter.RemoveListener(OnTimeBonus, NotificationType.TimeBonus);
  68. DebugViewManager.OnDebugView -= OnDebugView;
  69. }
  70. void OnDebugView()
  71. {
  72. if (GUILayout.Button("Fake Time Up"))
  73. {
  74. TimeUp();
  75. }
  76. }
  77. public void CustomFixedUpdate()
  78. {
  79. if (!_isSpawningEnemies)
  80. {
  81. return;
  82. }
  83. _timeLeft -= Time.deltaTime;
  84. if (_timeLeft <= 0)
  85. {
  86. MenuManager._instance.IsLevelEnd = true;
  87. TimeUp();
  88. }
  89. NotificationCenter.Post(NotificationType.TimeLeftUpdate, _timeLeft);
  90. }
  91. void OnStartTimer(Notification note)
  92. {
  93. enabled = true;
  94. _isSpawningEnemies = false;
  95. MenuManager._instance.IsLevelEnd = false;
  96. _timeLeft = (int)note.data;
  97. }
  98. void OnStartSpawningEnemies(Notification note)
  99. {
  100. _isSpawningEnemies = true;
  101. }
  102. void OnGameOver(Notification note)
  103. {
  104. enabled = false;
  105. }
  106. void OnTimeBonus(Notification note)
  107. {
  108. int timeBonus = (int)note.data;
  109. _timeLeft += timeBonus;
  110. NotificationCenter.Post(NotificationType.Announcement, string.Format(Localization.instance.Get("timeBonus.text"), timeBonus));
  111. }
  112. void TimeUp()
  113. {
  114. GameOverData data = new GameOverData(GameOverData.Reason.TimeUp);
  115. NotificationCenter.Post(NotificationType.TransitToGameOver, data);
  116. _isSpawningEnemies = false;
  117. _timeLeft = 0;
  118. }
  119. }