-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameThread.java
More file actions
66 lines (51 loc) · 1.48 KB
/
GameThread.java
File metadata and controls
66 lines (51 loc) · 1.48 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
package thread;
import java.util.ArrayList;
import java.util.List;
import main.GameControl;
import main.Main;
import assets.AssetContainer;
public abstract class GameThread implements Runnable, ObservableThread {
protected Main mainApplication;
protected AssetContainer assContainer;
/** List of listeners that care about when the initial setup is complete. */
private List<ThreadObserver> setupObservers = new ArrayList<ThreadObserver>();
/** Pause between iterations. */
private int threadDelay;
public GameThread(AssetContainer assContainer, int threadDelay, Main mainApplication) {
this.assContainer = assContainer;
this.threadDelay = threadDelay;
this.mainApplication = mainApplication;
}
public void run() {
setup();
try {
while (GameControl.isPlaying()) {
gameLoop();
Thread.sleep(threadDelay);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
afterLoop();
}
}
protected void beforeLoop() {
// No default functionality.
System.out.println("beforeLoop: " + this.toString());
}
protected void afterLoop() {
// No default functionality.
System.out.println("afterLoop: " + this.toString());
}
protected abstract void gameLoop();
protected void setup() {
beforeLoop();
// Notify observers that setup is complete.
for (ThreadObserver observer : setupObservers) {
observer.setupDone(this);
}
}
public void registerSetupObserver(ThreadObserver observer) {
setupObservers.add(observer);
}
}