-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStateManager.js
153 lines (133 loc) · 3.29 KB
/
StateManager.js
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
var StateManager = function() {
if(StateManager._instance) {
return StateManager._instance;
}
this.currentState = new State("root");
this.states = {};
this.states["root"] = this.currentState;
this.eventQueue = [];
this.currentEvent = null;
StateManager._instance = this;
return StateManager._instance;
};
StateManager.getInstance = function() {
return StateManager()._instance || new StateManager();
}
StateManager.addState = function(stateName) {
states[stateName] = new State(stateName);
}
StateManager.getState = function(stateName) {
return states[stateName];
}
StateManager.getCurrentState = function() {
return currentState;
}
StateManager.setState = function(state) {
currentState = state;
}
StateManager.apply = function(eventName) {
if(currentEvent && currentEvent.name == eventName) return;
var event = currentState.getEvent(eventName);
if(!currentEvent) {
if(event && currentState.getChild(event.name)) {
currentEvent = event;
event.run();
if (currentState.getChild(event.name)) currentState = currentState.getChild(event.name);
} else {
//console.log('This event is not a branch of the current state');
//console.log(currentState);
//console.log(event);
StateManager.finishedEvent();
}
} else {
eventQueue.push(eventName);
}
}
StateManager.stop = function() {
if(currentEvent) currentEvent.stop();
currentEvent = null;
}
StateManager.pause = function() {
if(currentEvent) currentEvent.pause();
}
StateManager.play = function() {
if(currentEvent) currentEvent.play();
}
StateManager.stopAll = function() {
if(currentEvent) {
currentEvent.pause();
currentEvent = null;
}
eventQueue = [];
}
StateManager.finishedEvent = function() {
currentEvent = null;
if(eventQueue.length > 0) StateManager.apply(eventQueue.shift());
}
class State {
constructor(_name) {
this.name = _name;
this.children = {};
this.events = {};
}
addChild(event, state) {
this.children[event.name] = state;
this.events[event.name] = event;
}
getChild(eventName) {
return this.children[eventName];
}
getEvent(eventName) {
return this.events[eventName];
}
}
class Event {
constructor(_name, _activity) {
this.name = _name;
this.activity = _activity;
}
run() {
this.activity.run();
}
stop() {
this.activity.stop();
}
pause() {
this.activity.pause();
}
play() {
this.activity.play();
}
}
class Activity {
constructor(_audio, _initfunc, _endfunc) {
this.audio = document.getElementById(_audio);
this.initfunc = _initfunc;
this.endfunc = function() {
if(_endfunc) _endfunc();
StateManager.finishedEvent();
}
if(this.audio) this.audio.addEventListener("ended", this.endfunc);
}
run() {
if (this.audio) {
this.audio.currentTime = 0;
this.audio.play();
}
if (this.initfunc) this.initfunc();
if (!this.audio) this.endfunc();
}
stop() {
if(this.audio) this.audio.pause();
this.endfunc();
}
pause() {
if(this.audio) this.audio.pause();
}
play() {
if(this.audio) this.audio.play();
}
}
StateManager.getInstance();
//// IMPORTANT NOTE: Make sure the init function disables all buttons or we could have two events running that conflict with each other == bad race condition stuff.
//// IMPORTANT NOTE 2: Each event must have an independent audio element or else we execute all the endfuncs that are attached to that audio.