-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.java
More file actions
94 lines (82 loc) · 2.71 KB
/
Copy pathCounter.java
File metadata and controls
94 lines (82 loc) · 2.71 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
/**
* A simple counter that has a maximum and a minimum value. The counter is restrained
* to be within those values. If the counter underflows, it is set to the max. If it
* overflows, it is set to the minimum. (with behaviour set to true) Else, it caps out
* at the min and max.
*
* @author Jacky161
* @version v1.0-public
*/
public class Counter
{
private int value; // Default starting value
private int low; // Lowest value for the counter
private int max; // Highest value for the counter
private int incrementTime; // Minimum time between each increment (measured in act cycles)
private int actCycles; // Amount of act cycles passed since last increment
private boolean overflowBehaviour;
private boolean underflowBehaviour;
/**
* Create the counter, setting the appropriate instance variables
*/
public Counter(int value, int low, int max) {
this(value, low, max, 0, true, true);
}
public Counter(int value, int low, int max, int incrementTime) {
this(value, low, max, incrementTime, true, true);
}
public Counter(int value, int low, int max, int incrementTime, boolean overflowBehaviour, boolean underflowBehaviour) {
this.value = value;
this.low = low;
this.max = max;
this.incrementTime = incrementTime;
this.actCycles = incrementTime; // Counter can be incremented immediately
this.overflowBehaviour = overflowBehaviour;
this.underflowBehaviour = underflowBehaviour;
}
/** Increment # of actCycles passed. (NEEDS TO BE CALLED MANUALLY) */
public void act() {
actCycles++;
}
/** Increase by 1 */
public void increment() {
increment(1);
}
/** Increase by specified amount */
public void increment(int amount) {
if (actCycles < incrementTime) return;
else actCycles = 0;
value += amount;
constrain();
}
/** Decrease by 1 */
public void decrement() {
decrement(1);
}
/** Decrease by specified amount */
public void decrement(int amount) {
increment(-amount);
}
/** Retrieve and return the value */
public int getValue() {
return value;
}
/** Set the value */
public void setValue(int value) {
this.value = value;
constrain();
}
/** Constrain the value */
private void constrain() {
if (overflowBehaviour) {
if (value > max) value = low;
} else {
if (value > max) value = max;
}
if (underflowBehaviour) {
if (value < low) value = max;
} else {
if (value < low) value = low;
}
}
}