-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStateManager.js
50 lines (40 loc) · 1016 Bytes
/
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
(function() {
function StateManager() {
this.states = {};
this._currentState = null;
}
function isState(obj) {
if(obj instanceof Game.State) {
return true;
}
return false;
}
StateManager.prototype.state = function(name, stateObj) {
var state;
if(isState(name)) {
state = name;
} else {
state = new Game.State(name, stateObj);
}
this.states[state.name] = state;
}
StateManager.prototype.setCurrentState = function(stateName) {
if(isState(stateName)) {
this._currentState = stateName
} else {
var stateToSet = this.states[stateName];
if(stateToSet) {
this._currentState = stateToSet;
} else {
throw new Error("State not found.");
}
}
}
StateManager.prototype.renderCurrentState = function() {
this._currentState.render();
}
StateManager.prototype.updateCurrentState = function() {
this._currentState.update();
}
Game.StateManager = new StateManager();
})();