River Jiang
2020-10-22 c0c1fa6faf74eb6c0f81014a3e812e055cff7c2e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;
 
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>
        /// The time
        /// </summary>
        float m_Time, m_CurrentTime;
 
        protected static int TIMER_IDSTART = 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); }
        }
 
        public float currentTime
        {
            get { return this.m_CurrentTime; }
        }
 
        public int timerID
        {
            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);
 
            m_CurrentTime = 0f;
            m_Callback += onElapsed;
 
            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>
        /// 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;
        }
 
        /// <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>
        /// 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;
            }
        }
    }
}