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 pathState.java
More file actions
68 lines (62 loc) · 1.78 KB
/
State.java
File metadata and controls
68 lines (62 loc) · 1.78 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
import java.awt.*;
/**
* Abstract class for a state of the program
*/
public abstract class State {
Keyboard keyboard;
Mouse mouse;
Messenger messenger;
private boolean active;
// The arguments that will be passed onto the next state
private Object[] nextArgs;
protected HashMap<String, Button> buttons;
State(Keyboard keyboard, Mouse mouse, Messenger messenger) {
this.keyboard = keyboard;
this.mouse = mouse;
this.messenger = messenger;
this.buttons = new HashMap<String, Button>();
this.active = false;
}
public boolean isActive() {
return this.active;
}
public Object[] getNextArgs() {
return this.nextArgs;
}
public void setNextArgs(Object[] nextArgs) {
this.nextArgs = nextArgs;
}
public void setup() {
this.active = true;
this.nextArgs = null;
}
public void close() {
this.buttons.clear();
this.active = false;
}
public abstract void setup(Object[] args);
// Process inputs
public void update() {
while (!this.messenger.isEmpty() && this.isActive()) {
this.message(this.messenger.poll());
}
while (this.keyboard.hasNext() && this.isActive()) {
this.type(this.keyboard.next());
}
while (this.mouse.hasNext() && this.isActive()) {
this.click(this.mouse.poll());
}
}
// On message receive
public abstract void message(String messageText);
// On key type
public abstract void type(char key);
// On mouse click
public abstract void click(Mouse.Click click);
public void draw(Graphics g) {
for (Button button: this.buttons.values()) {
button.draw(g);
}
}
}