This repository was archived by the owner on Nov 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachine.java
More file actions
47 lines (42 loc) · 1.34 KB
/
StateMachine.java
File metadata and controls
47 lines (42 loc) · 1.34 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
import java.awt.*;
/*
* Statemachine that controls the flow of the program
*/
public class StateMachine {
Keyboard keyboard;
Mouse mouse;
Messenger messenger;
int current;
State[] states;
StateMachine(Keyboard keyboard, Mouse mouse, Messenger messenger) {
this.keyboard = keyboard;
this.mouse = mouse;
this.messenger = messenger;
this.current = 0;
// Initialize states
this.states = new State[]{
new MenuState(this.keyboard, this.mouse, this.messenger),
new LoadState(this.keyboard, this.mouse, this.messenger),
new GameState(this.keyboard, this.mouse, this.messenger)
};
this.states[this.current].setup(new Object[]{});
}
public void update() {
// Update the current state
this.states[this.current].update();
// Switch to the next state if the current one has closed
if (!this.states[current].isActive()) {
this.nextState();
}
}
public void draw(Graphics g) {
// Render the current state
this.states[this.current].draw(g);
}
public void nextState() {
// Arguments passed from previous state to next state
Object[] args = this.states[current].getNextArgs();
this.current++;
this.states[current].setup(args);
}
}