-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStateController.pde
67 lines (53 loc) · 1.48 KB
/
StateController.pde
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
interface StateActionCallback {
void callbackWith(StateSequenceController sc, State s, PGraphics g);
}
class State {
int stateId;
float durationInFrames;
Object owner;
State(Object owner, int id, float durationInFrames) {
this.stateId = id;
this.durationInFrames = durationInFrames;
this.owner = owner;
}
String toString() {
return "state id: "+ this.stateId;
}
}
class StateSequenceController {
List<State> states;
int stateIndex = 0;
float currentFrame = 0;
List<StateActionCallback> listeners;
StateSequenceController () {
states = new ArrayList();
listeners = new ArrayList();
}
void add(State s) {
states.add(s);
}
void addId(Object owner, int id, float durationInFrames) {
State s = new State(owner, id, durationInFrames);
this.add(s);
}
float getCurrentDx(){
State state = getCurrentState();
return state.durationInFrames;
}
State getCurrentState() {
return this.states.get(stateIndex);
}
void tick() {
//pdebug("state controller tick. currentFrame: " + currentFrame + ", current dx: " + this.getCurrentDx());
currentFrame++;
if (currentFrame >= this.getCurrentDx()) {
pdebug("state: " + stateIndex);
for(StateActionCallback s: listeners) {
s.callbackWith(this, getCurrentState(), g);
}
currentFrame = 0;
stateIndex = (stateIndex + 1) % this.states.size();
}
//pdebug("state: " + stateIndex +", currentFrame: " + currentFrame);
}
}