chenxin
2020-11-04 ce7efae14eb31bcdfa0d540da6a1ef1457e046ab
Assets/Scripts/Core/Utilities/Timer.cs
@@ -4,115 +4,116 @@
namespace Core.Utilities
{
   /// <summary>
   /// A timer data model. Consumed/process by the TimedBehaviour
   /// 塔防游戏的基础Timer包装类,设置一个time时间后要引发的操作,然后每一轮Tick这个Timer,触发后Tick返回true
   /// 由调用层进行相应的操作。
   /// </summary>
   public class Timer
   {
      /// <summary>
      /// Event fired on elapsing
      /// </summary>
      readonly Action m_Callback;
    /// <summary>
    /// A timer data model. Consumed/process by the TimedBehaviour
    /// 塔防游戏的基础Timer包装类,设置一个time时间后要引发的操作,然后每一轮Tick这个Timer,触发后Tick返回true
    /// 由调用层进行相应的操作。
    /// </summary>
    public class Timer
    {
        /// <summary>
        /// Event fired on elapsing
        /// </summary>
        readonly Action m_Callback;
      /// <summary>
      /// The time
      /// </summary>
      float m_Time, m_CurrentTime;
        /// <summary>
        /// The time
        /// </summary>
        float m_Time, m_CurrentTime;
      protected static int TIMER_IDSTART = 0;
        protected static int TIMER_IDSTART = 0;
      protected int mTimerID = 0;
        protected int mTimerID = 0;
      /// <summary>
      /// Normalized progress of the timer
      /// </summary>
      public float normalizedProgress
      {
         get { return Mathf.Clamp(m_CurrentTime / m_Time, 0f, 1f); }
      }
        /// <summary>
        /// Normalized progress of the timer
        /// </summary>
        public float normalizedProgress
        {
            get { return Mathf.Clamp(m_CurrentTime / m_Time, 0f, 1f); }
        }
      public float currentTime
        public float currentTime
        {
            get { return this.m_CurrentTime; }
        }
      public int timerID
        public int timerID
        {
         get { return this.mTimerID; }
            get { return this.mTimerID; }
        }
      /// <summary>
      /// Timer constructor
      /// </summary>
      /// <param name="newTime">the time that timer is counting</param>
      /// <param name="onElapsed">the event fired at the end of the timer elapsing</param>
      public Timer(float newTime, Action onElapsed = null)
      {
         SetTime(newTime);
        /// <summary>
        /// Timer constructor
        /// </summary>
        /// <param name="newTime">the time that timer is counting</param>
        /// <param name="onElapsed">the event fired at the end of the timer elapsing</param>
        public Timer(float newTime, Action onElapsed = null)
        {
            SetTime(newTime);
         m_CurrentTime = 0f;
         m_Callback += onElapsed;
            m_CurrentTime = 0f;
            m_Callback += onElapsed;
         mTimerID = TIMER_IDSTART++;
      }
            mTimerID = TIMER_IDSTART++;
        }
      /// <summary>
      /// Returns the result of AssessTime
      /// </summary>
      /// <param name="deltaTime">change in time between ticks</param>
      /// <returns>true if the timer has elapsed, false otherwise</returns>
      public virtual bool Tick(float deltaTime)
      {
         return AssessTime(deltaTime);
      }
        /// <summary>
        /// Returns the result of AssessTime
        /// </summary>
        /// <param name="deltaTime">change in time between ticks</param>
        /// <returns>true if the timer has elapsed, false otherwise</returns>
        public virtual bool Tick(float deltaTime)
        {
            return AssessTime(deltaTime);
        }
      /// <summary>
      /// Checks if the time has elapsed and fires the tick event
      /// </summary>
      /// <param name="deltaTime">the change in time between assessments</param>
      /// <returns>true if the timer has elapsed, false otherwise</returns>
      protected bool AssessTime(float deltaTime)
      {
         m_CurrentTime += deltaTime;
         if (m_CurrentTime >= m_Time)
         {
            FireEvent();
            return true;
         }
        /// <summary>
        /// Checks if the time has elapsed and fires the tick event
        /// </summary>
        /// <param name="deltaTime">the change in time between assessments</param>
        /// <returns>true if the timer has elapsed, false otherwise</returns>
        protected bool AssessTime(float deltaTime)
        {
            m_CurrentTime += deltaTime;
            if (m_CurrentTime >= m_Time)
            {
                FireEvent();
                return true;
            }
         return false;
      }
            return false;
        }
      /// <summary>
      /// Resets the current time to 0
      /// </summary>
      public void Reset()
      {
         m_CurrentTime = 0;
      }
        /// <summary>
        /// Resets the current time to 0
        /// </summary>
        public void Reset()
        {
            m_CurrentTime = 0;
        }
      /// <summary>
      /// Fires the associated timer event
      /// </summary>
      public void FireEvent()
      {
         m_Callback.Invoke();
      }
        /// <summary>
        /// Fires the associated timer event
        /// </summary>
        public void FireEvent()
        {
            if (m_Callback != null)
                m_Callback.Invoke();
        }
      /// <summary>
      /// Sets the elapsed time
      /// </summary>
      /// <param name="newTime">sets the time to a new value</param>
      public void SetTime(float newTime)
      {
         m_Time = newTime;
        /// <summary>
        /// Sets the elapsed time
        /// </summary>
        /// <param name="newTime">sets the time to a new value</param>
        public void SetTime(float newTime)
        {
            m_Time = newTime;
         if (newTime <= 0)
         {
            m_Time = 0.1f;
         }
      }
   }
            if (newTime <= 0)
            {
                m_Time = 0.1f;
            }
        }
    }
}