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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
package subconscious.graphics;
import subconscious.Game;
import java.util.Stack;
import java.util.AbstractMap.SimpleEntry;
import java.awt.Dimension;
import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
/* GameWindow
* This class manages the graphical part of the game, such as loading and
* unloading scenes, and the window itself
*/
@SuppressWarnings("serial")
public class GameWindow extends JFrame implements WindowListener {
public static final Dimension WINDOW_SIZE = new Dimension(600, 400);
private JPanel root;
// reference to the current scene which is NOT on the stack
private Scene scene = null;
private Thread sceneThread = null;
// stack of loaded scenes in the background that are paused
private Stack<SimpleEntry<Scene, Thread>> loadedScenes = new Stack<>();
private Game game;
// TODO: remove map editor, start directly on Battle mode
public GameWindow(Game g) {
super("Subconscious");
this.game = g;
// set up JFrame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WINDOW_SIZE);
this.setPreferredSize(WINDOW_SIZE);
this.setLocationRelativeTo(null);
this.addWindowListener(this);
// set up components
// set up a cardlayout
this.root = new JPanel(new CardLayout());
this.add(this.root, BorderLayout.CENTER);
this.pack();
this.setVisible(true);
// start Window Loop
this.loop();
}
// ovserver of this.game
private void loop() {
// load the first scene
this.loadScene(new MenuScene(this.game, "mainmenu"));
while (this.game.isRunning()) {
Game.State newState = this.game.waitStateChange();
switch (newState) {
case MENU:
// unload all scenes to get the first one => menu
this.unloadAllScenes();
break;
// TODO: rewrite the following part
case BATTLE:
this.loadScene(new BattleScene(this.game, "battle1"));
break;
case PAUSE:
this.loadScene(new PauseScene(this.game, "pause"));
try {
this.sceneThread.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
this.unloadScene();
break;
}
}
this.quit();
}
private void loadScene(Scene scene) {
// check to not reload the same scene
if (this.scene == scene)
return;
// if a scene is loaded push it onto the loadedScenes stack
if (this.scene != null){
this.scene.pauseThread();
this.loadedScenes.push(new SimpleEntry<Scene, Thread>(scene, sceneThread));
}
// build thread
this.scene = scene;
// add to UI
this.root.add(this.scene, this.scene.UNIQUE_NAME);
((CardLayout) this.root.getLayout()).show(this.root, this.scene.UNIQUE_NAME);
// start scene
this.sceneThread = new Thread(this.scene);
// for debugging
this.sceneThread.setName("Scene [" + scene.UNIQUE_NAME + "]");
this.sceneThread.start();
this.scene.updateCanvasSize(this.getSize());
}
private void unloadAllScenes() {
unloadScenes(this.loadedScenes.size() -1);
}
private void unloadScenes(int count) {
for (int i = 0; i < count; i++) {
this.unloadScene();
}
}
private void unloadScene() {
// close old scene
this.scene.stop();
try {
// wait for thread to die
this.sceneThread.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// remove card from UI
this.root.remove(scene);
// TODO: this should probably be an error
if (this.loadedScenes.empty()) {
this.scene = null;
this.sceneThread = null;
return;
}
// load the last scene
SimpleEntry<Scene, Thread> sceneEntry = this.loadedScenes.pop();
this.scene = sceneEntry.getKey();
this.sceneThread = sceneEntry.getValue();
((CardLayout) this.root.getLayout()).show(this.root, this.scene.UNIQUE_NAME);
this.scene.resumeThread();
}
public void quit() {
this.unloadAllScenes();
this.setVisible(false);
this.dispose();
}
/* window listener */
@Override public void windowActivated(WindowEvent e) {}
@Override public void windowClosed(WindowEvent e) {}
@Override public void windowClosing(WindowEvent e) {
this.quit();
}
@Override
public void windowDeactivated(WindowEvent e) {
this.game.pause();
}
@Override public void windowDeiconified(WindowEvent e) {}
@Override public void windowIconified(WindowEvent e) {}
@Override public void windowOpened(WindowEvent e) {}
}
|