Guest User

TimeManager.cs

a guest
Aug 6th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TimeManager : MonoBehaviour
  5. {
  6.     public int gameDayInMinutes;
  7.     private float m_DayInSeconds;
  8.     private float m_CurrentTime;
  9.  
  10.     private bool m_bIsPaused = false;
  11.     private bool m_bIsActive = true;
  12.     private bool m_bIsDayOver = false;
  13.  
  14.  
  15.     public void OnEnable()
  16.     {
  17.         m_DayInSeconds = gameDayInMinutes * 60.0f;
  18.  
  19.         EventManager.EndOfDayMethods += ResetGameClock;
  20.  
  21.         EnableGameClock();
  22.     }
  23.  
  24.     public void OnDisable()
  25.     {
  26.         KillGameClock();
  27.         EventManager.EndOfDayMethods -= ResetGameClock;
  28.     }
  29.  
  30.     private IEnumerator GameClock()
  31.     {
  32.         while(m_bIsActive)
  33.         {
  34.             if(m_bIsPaused)
  35.             {
  36.                 yield return null;
  37.             }
  38.             else
  39.             {
  40.                 m_CurrentTime++;
  41.  
  42.                 if(m_CurrentTime >= m_DayInSeconds)
  43.                 {
  44.                     //day is over
  45.                     m_bIsDayOver = true;
  46.                     EventManager.ResetDay();
  47.                 }
  48.                 yield return new WaitForSeconds(1.0f); //TODO: modifier here to manipulate game time speed
  49.             }
  50.         }
  51.     }
  52.  
  53.     public void EnableGameClock()
  54.     {
  55.         m_bIsPaused = false;
  56.         m_bIsActive = true;
  57.         StartCoroutine(GameClock());
  58.     }
  59.  
  60.     public void ResetGameClock()
  61.     {
  62.         m_bIsDayOver = true;
  63.         m_CurrentTime = 0.0f;
  64.         m_bIsDayOver = false;
  65.     }
  66.  
  67.     public void PauseGameClock()
  68.     {
  69.         Debug.LogWarning("Game Clock Paused");
  70.         m_bIsPaused = true;
  71.     }
  72.  
  73.     private void ResumeGameClock()
  74.     {
  75.         Debug.LogWarning("Game Clock Resumed");
  76.         m_bIsPaused = false;
  77.     }
  78.  
  79.     private void KillGameClock()
  80.     {
  81.         Debug.LogWarning("Game Clock Killed");
  82.         m_bIsActive = false;
  83.         m_bIsPaused = false;
  84.     }
  85. }
Add Comment
Please, Sign In to add comment