blob: ce99df5289bd969668ab38454fff7fa93ed97bf1 (
plain)
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.awt.Dimension;
import java.awt.GridLayout;
import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/* 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 ActionListener {
public static final Dimension WINDOW_SIZE = new Dimension(600, 400);
public final String MENU_CARD = "menu";
public final String SCENE_CARD = "scene";
private JPanel root;
private Scene scene = null;
private Thread sceneThread = null;
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);
// set up components
this.buildUi();
this.add(this.root, BorderLayout.CENTER);
this.pack();
this.setVisible(true);
// start Window Loop
this.loop();
}
private void buildUi() {
// set up a cardlayout
this.root = new JPanel(new CardLayout());
// TODO: menu should be a Scene
// build Menu card
JPanel menu = new JPanel();
menu.setLayout(new GridLayout(3, 1));
JButton editor = new JButton("Editor");
editor.setActionCommand("btn-editor");
editor.addActionListener(this);
JButton battle = new JButton("Battle");
battle.setActionCommand("btn-battle");
battle.addActionListener(this);
JButton exit = new JButton("Exit");
exit.setActionCommand("btn-exit");
exit.addActionListener(this);
menu.add(editor);
menu.add(battle);
menu.add(exit);
// add cards
this.root.add(menu, MENU_CARD);
}
// ovserver of this.game
private void loop() {
while (this.game.isRunning()) {
this.game.waitStateChange();
switch (this.game.getState()) {
case MENU:
this.loadMenu();
break;
case DREAM:
this.loadScene(this.scene);
break;
}
}
}
private void loadScene(Scene scene) {
// check to not reload the same scene
// TODO: this may not be necessary, ex reaload scene?
if (this.scene == scene)
return;
if (this.scene != null){
this.unloadScene();
}
// build thread
this.scene = scene;
this.sceneThread = new Thread(this.scene);
// for debugging
this.sceneThread.setName("Scene rendering Thread");
// add to UI
this.root.add(this.scene, SCENE_CARD);
((CardLayout) this.root.getLayout()).show(this.root, SCENE_CARD);
// start scene
this.sceneThread.start();
this.scene.updateCanvasSize(this.getSize());
}
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);
this.scene = null;
this.sceneThread = null;
}
// TODO: menu should become a Scene
private void loadMenu() {
if (this.scene != null)
this.scene.pause();
((CardLayout) this.root.getLayout()).show(this.root, MENU_CARD);
}
public void quit() {
this.setVisible(false);
if (this.scene != null) {
this.unloadScene();
}
this.dispose();
}
// Action Listener for menu
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().startsWith("btn")) {
if (e.getActionCommand().equals("btn-exit")) {
this.quit();
return;
}
if (e.getActionCommand().equals("btn-editor")) {
this.loadScene(new MapEditorScene());
} else if (e.getActionCommand().equals("btn-battle")) {
this.loadScene(new BattleScene(this.game));
this.game.start();
}
}
}
}
|