-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimer.java
More file actions
203 lines (175 loc) · 5.01 KB
/
Timer.java
File metadata and controls
203 lines (175 loc) · 5.01 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Timer class.<br /><br />
*
* Stores an in-game timer, which records time intervals and reports
* when the timer is "triggered" by a certain time interval elapsing.
* Useful for timing events without using multithreading.
*
* @author Matthew Kloster
* @version 1.0.0
*/
public class Timer
{
/** Named id for the timer. */
private String id;
/** Period of execution */
private double period;
/** Current set period - when this is greater than or equal to period, this resets */
private double currentPeriod;
/** Max execution count */
private int maxExecCount;
/** Current execution count */
private int execCount;
/** Is this timer active? */
private boolean isActive;
/** Is this timer triggered? If set to true, timer will ignore the timer's actual period and simply think it's been triggered. */
private boolean isTriggered;
/**
* Default constructor. Contains all timer options.
* @param d_id Named ID for this timer.
* @param d_period Execution period for this timer.
* @param d_maxExecCount Max execution count for this timer.
* @param d_isActive <tt>true</tt> if the timer is active, <tt>false</tt> if the timer is inactive.
*/
public Timer(String d_id, double d_period, int d_maxExecCount, boolean d_isActive)
{
id = d_id;
period = d_period;
currentPeriod = 0;
maxExecCount = d_maxExecCount;
execCount = 0;
isActive = d_isActive;
isTriggered = false;
}
/**
* Constructor which only includes options for period and max execution count. Set to be active by default.
* @param d_id Named ID for this timer.
* @param d_period Execution period for this timer.
* @param d_maxExecCount Max execution count for this timer.
*/
public Timer(String d_id, double d_period, int d_maxExecCount)
{
this(d_id, d_period, d_maxExecCount,true);
}
/**
* Constructor which only includes options for period. Set to be active and infinitely executing by default.
* @param d_id Named ID for this timer.
* @param d_period Execution period for this timer.
*/
public Timer(String d_id, double d_period)
{
this (d_id, d_period, 0, true);
}
/** Accessor method for this timer's ID. */
public String getId()
{
return new String(id);
}
/** Accessor method for this timer's period. */
public double getPeriod()
{
return period;
}
/** Accessor method for this timer's current period. */
public double getCurrentPeriod()
{
return currentPeriod;
}
/** Accessor method for this timer's maximum execution count. */
public int getMaxExecCount()
{
return maxExecCount;
}
/** Accessor method for this timer's current execution count. */
public int getExecCount()
{
return execCount;
}
/** Accessor method for this timer's active status. */
public boolean isActive()
{
return isActive;
}
/** Mutator method for this timer's ID. */
public void setId(String d_id)
{
id = d_id;
}
/** Mutator method for this timer's period. */
public void setPeriod(double d_period)
{
period = d_period;
}
/** Mutator method for this timer's max execution count. */
public void setMaxExecCount(int d_maxExecCount)
{
maxExecCount = d_maxExecCount;
if (execCount >= maxExecCount && maxExecCount > 0)
{
// since we're over the max exec count, disable this timer
currentPeriod = 0.0;
isActive = false;
}
}
/** Mutator method for this timer's current execution count. */
public void setExecCount(int d_execCount)
{
execCount = d_execCount;
if (execCount < 0)
{
// can't have an exec count of less than 0
execCount = 0;
}
else if (execCount >= maxExecCount && maxExecCount > 0)
{
// since we're over the max exec count, disable this timer
currentPeriod = 0.0;
isActive = false;
}
}
/** Mutator method for this timer's active status. */
public void setIsActive(boolean d_isActive)
{
isActive = d_isActive;
}
/** Mutator method for this timer's triggered status. */
public void setIsTriggered(boolean d_isTriggered)
{
isTriggered = d_isTriggered;
}
/** Increment a certain amount of time to the current period. Should be done periodically to keep timer up to date. */
public void increment(double amount)
{
if (isActive) // only increment if our timer is current active
{
currentPeriod += amount;
}
}
/** Has this timer been triggered? */
public boolean triggered()
{
if ((isActive && currentPeriod >= period) || isTriggered)
{
isTriggered = false;
execCount++;
currentPeriod = Math.max(0.0,currentPeriod-period);
if (execCount >= maxExecCount && maxExecCount > 0)
{
// since we're over the max exec count, disable this timer
currentPeriod = 0.0;
isActive = false;
}
return true;
}
else
{
return false;
}
}
/** Resets the timer (ie: the period and execution counts) */
public void reset()
{
execCount = 0;
currentPeriod = 0.0;
}
}