123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using UnityEngine;
- using System.Collections;
- using ICTS.Localization;
- public class TimeManager : MonoBehaviour
- {
- #region Unity Singleton
- private static TimeManager _instance;
- public static TimeManager Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = FindObjectOfType(typeof(TimeManager)) as TimeManager;
- if (_instance == null)
- {
- 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));
- }
- }
- return _instance;
- }
- }
- void Awake()
- {
- if (_instance != null && _instance != this)
- {
- AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name));
- Destroy(gameObject);
- return;
- }
- _instance = this;
- DontDestroyOnLoad(gameObject);
- enabled = false;
- RegisterListeners();
- }
- void OnDestroy()
- {
- UnregisterListeners();
- }
- #endregion
- float _timeLeft;
- public bool IsReachingEndOfLevel { get { return _timeLeft <= 10; } }
- bool _isSpawningEnemies;
- public long StartServerTime { get; set; }
- public long EndServerTime { get; set; }
- public long PauseStartServerTime { private get; set; }
- public long PauseEndServerTime {
- set
- {
- StartServerTime += value - PauseStartServerTime;
- PauseStartServerTime = 0;
- }
- }
- void RegisterListeners()
- {
- NotificationCenter.AddListener(OnStartTimer, NotificationType.StartGameTimer);
- NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
- NotificationCenter.AddListener(OnTimeBonus, NotificationType.TimeBonus);
- DebugViewManager.OnDebugView += OnDebugView;
- }
- void UnregisterListeners()
- {
- NotificationCenter.RemoveListener(OnStartTimer, NotificationType.StartGameTimer);
- NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
- NotificationCenter.RemoveListener(OnTimeBonus, NotificationType.TimeBonus);
- DebugViewManager.OnDebugView -= OnDebugView;
- }
- void OnDebugView()
- {
- if (GUILayout.Button("Fake Time Up"))
- {
- TimeUp();
- }
- }
- public void CustomFixedUpdate()
- {
- if (!_isSpawningEnemies)
- {
- return;
- }
- _timeLeft -= Time.deltaTime;
- if (_timeLeft <= 0)
- {
- MenuManager._instance.IsLevelEnd = true;
- TimeUp();
- }
- NotificationCenter.Post(NotificationType.TimeLeftUpdate, _timeLeft);
- }
- void OnStartTimer(Notification note)
- {
- enabled = true;
- _isSpawningEnemies = false;
- MenuManager._instance.IsLevelEnd = false;
- _timeLeft = (int)note.data;
- }
- void OnStartSpawningEnemies(Notification note)
- {
- _isSpawningEnemies = true;
- }
- void OnGameOver(Notification note)
- {
- enabled = false;
- }
- void OnTimeBonus(Notification note)
- {
- int timeBonus = (int)note.data;
- _timeLeft += timeBonus;
- NotificationCenter.Post(NotificationType.Announcement, string.Format(Localization.instance.Get("timeBonus.text"), timeBonus));
- }
- void TimeUp()
- {
- GameOverData data = new GameOverData(GameOverData.Reason.TimeUp);
- NotificationCenter.Post(NotificationType.TransitToGameOver, data);
- _isSpawningEnemies = false;
- _timeLeft = 0;
- }
- }
|