-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameStateManager.pde
63 lines (55 loc) · 1.84 KB
/
GameStateManager.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
/*
* Copyright 2017 Billy Brown
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
class GameStateManager {
// The different game states
private GameState[] states;
// The current game state index
private int state = 0;
public GameStateManager(final GameState[] states) {
this.states = states;
}
public void input(final Game game, final char key) {
// Pass the game and input on to the current state
states[state].input(game, key);
}
public void click(final Game game, final PVector position) {
// Pass the game and location on to the current state
states[state].click(game, position);
}
public void update(final Game game) {
// The state may transition
testTransition(game);
// Pass the game on to the current state
states[state].update(game);
}
public void draw(final Game game) {
// Pass the game on to the current state
states[state].draw(game);
}
private void testTransition(final Game game) {
// Test whether the current state is transitioning
if (states[state].willTransition()) {
// Transition to the next state
setState(states[state].transition());
// Tell the new state it has been entered
states[state].onEnter(game);
}
}
private void setState(final Class<? extends GameState> nextState) {
for (int i = 0; i < states.length; ++i) {
// Determine whether states[i] is an instance of the nextState class
if (nextState.isInstance(states[i])) {
// Reset the current state's transition
states[state].resetTransition();
// Set the current state
state = i;
return;
}
}
throw new java.util.NoSuchElementException("state not found");
}
}