-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainPanel.java
355 lines (301 loc) · 10.1 KB
/
MainPanel.java
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class MainPanel extends JPanel implements KeyListener, Runnable, Common {
public static final int WIDTH = 640;
public static final int HEIGHT = 640;
// 20ms/frame = 50fps
private static final int PERIOD = 20;
// debug mode
private static final boolean DEBUG_MODE = true;
// map list
private Map[] maps;
// current map number
private int mapNo;
// our hero!
private Character hero;
// action keys
private ActionKey leftKey;
private ActionKey rightKey;
private ActionKey upKey;
private ActionKey downKey;
private ActionKey spaceKey;
private Thread gameLoop;
private Random rand = new Random();
private MessageWindow messageWindow;
private static Rectangle WND_RECT = new Rectangle(142, 480, 356, 140);
private MidiEngine midiEngine = new MidiEngine();
private WaveEngine waveEngine = new WaveEngine();
// BGM
// from TAM Music Factory http://www.tam-music.com/
private static final String[] bgmNames = {"castle", "field"};
// Sound Clip
private static final String[] soundNames = {"treasure", "door", "step"};
// double buffering
private Graphics dbg;
private Image dbImage = null;
public MainPanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
addKeyListener(this);
// create action keys
leftKey = new ActionKey();
rightKey = new ActionKey();
upKey = new ActionKey();
downKey = new ActionKey();
spaceKey = new ActionKey(ActionKey.DETECT_INITIAL_PRESS_ONLY);
// create map
maps = new Map[2];
maps[0] = new Map("map/castle.map", "event/castle.evt", "castle", this);
maps[1] = new Map("map/field.map", "event/field.evt", "field", this);
mapNo = 0; // initial map
// create character
hero = new Character(6, 6, 0, DOWN, 0, maps[mapNo]);
// add characters to the map
maps[mapNo].addCharacter(hero);
// create message window
messageWindow = new MessageWindow(WND_RECT);
// load BGM and sound clips
loadSound();
midiEngine.play(maps[mapNo].getBgmName());
// start game loop
gameLoop = new Thread(this);
gameLoop.start();
}
public void run() {
long beforeTime, timeDiff, sleepTime;
beforeTime = System.currentTimeMillis();
while (true) {
checkInput();
gameUpdate();
gameRender();
printScreen();
timeDiff = System.currentTimeMillis() - beforeTime;
sleepTime = PERIOD - timeDiff;
// sleep at least 5ms
if (sleepTime <= 0) {
sleepTime = 5;
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
beforeTime = System.currentTimeMillis();
}
}
private void checkInput() {
if (messageWindow.isVisible()) {
messageWindowCheckInput();
} else {
mainWindowCheckInput();
}
}
private void gameUpdate() {
if (!messageWindow.isVisible()) {
heroMove();
characterMove();
}
}
private void gameRender() {
if (dbImage == null) {
// buffer image
dbImage = createImage(WIDTH, HEIGHT);
if (dbImage == null) {
return;
} else {
// device context of buffer image
dbg = dbImage.getGraphics();
}
}
dbg.setColor(Color.WHITE);
dbg.fillRect(0, 0, WIDTH, HEIGHT);
// calculate offset so that the hero is in the center of a screen.
int offsetX = hero.getPX() - MainPanel.WIDTH / 2;
// do not scroll at the edge of the map
if (offsetX < 0) {
offsetX = 0;
} else if (offsetX > maps[mapNo].getWidth() - MainPanel.WIDTH) {
offsetX = maps[mapNo].getWidth() - MainPanel.WIDTH;
}
int offsetY = hero.getPY() - MainPanel.HEIGHT / 2;
// do not scroll at the edge of the map
if (offsetY < 0) {
offsetY = 0;
} else if (offsetY > maps[mapNo].getHeight() - MainPanel.HEIGHT) {
offsetY = maps[mapNo].getHeight() - MainPanel.HEIGHT;
}
// draw map
maps[mapNo].draw(dbg, offsetX, offsetY);
// draw message window
messageWindow.draw(dbg);
// display debug information
if (DEBUG_MODE) {
Font font = new Font("SansSerif", Font.BOLD, 16);
dbg.setFont(font);
dbg.setColor(Color.YELLOW);
dbg.drawString(maps[mapNo].getMapName() + " (" + maps[mapNo].getCol() + "," + maps[mapNo].getRow() + ")", 4, 16);
dbg.drawString("(" + hero.getX() + "," + hero.getY() + ") ", 4, 32);
dbg.drawString("(" + hero.getPX() + "," + hero.getPY() + ")", 4, 48);
dbg.drawString(maps[mapNo].getBgmName(), 4, 64);
}
}
private void printScreen() {
Graphics g = getGraphics();
if ((g != null) && (dbImage != null)) {
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync();
if (g != null) {
g.dispose();
}
}
private void mainWindowCheckInput() {
if (leftKey.isPressed()) {
if (!hero.isMoving()) {
hero.setDirection(LEFT);
hero.setMoving(true);
}
}
if (rightKey.isPressed()) {
if (!hero.isMoving()) {
hero.setDirection(RIGHT);
hero.setMoving(true);
}
}
if (upKey.isPressed()) {
if (!hero.isMoving()) {
hero.setDirection(UP);
hero.setMoving(true);
}
}
if (downKey.isPressed()) {
if (!hero.isMoving()) {
hero.setDirection(DOWN);
hero.setMoving(true);
}
}
if (spaceKey.isPressed()) {
// cannot open window if hero is moving
if (hero.isMoving()) {
return;
}
// search
TreasureEvent treasure = hero.search();
if (treasure != null) {
waveEngine.play("treasure");
messageWindow.setMessage("HERO DISCOVERED/" + treasure.getItemName());
messageWindow.show();
maps[mapNo].removeEvent(treasure);
return;
}
// door
DoorEvent door = hero.open();
if (door != null) {
waveEngine.play("door");
maps[mapNo].removeEvent(door);
return;
}
// talk
if (!messageWindow.isVisible()) {
Character c = hero.talkWith();
if (c != null) {
messageWindow.setMessage(c.getMessage());
messageWindow.show();
} else {
messageWindow.setMessage("THERE IS NO ONE/IN THAT DIRECTION");
messageWindow.show();
}
}
}
}
private void messageWindowCheckInput() {
if (spaceKey.isPressed()) {
if (messageWindow.nextPage()) {
messageWindow.hide();
}
}
}
private void heroMove() {
if (hero.isMoving()) {
if (hero.move()) {
Event event = maps[mapNo].checkEvent(hero.getX(), hero.getY());
if (event instanceof MoveEvent) {
waveEngine.play("step");
// move to another map
MoveEvent m = (MoveEvent)event;
maps[mapNo].removeCharacter(hero);
mapNo = m.destMapNo;
hero = new Character(m.destX, m.destY, 0, DOWN, 0, maps[mapNo]);
maps[mapNo].addCharacter(hero);
midiEngine.play(maps[mapNo].getBgmName());
}
}
}
}
private void characterMove() {
// get characters in the map
Vector<Character> characters = maps[mapNo].getCharacters();
// move each character
for (int i = 0; i < characters.size(); i++) {
Character c = characters.get(i);
if (c.getMoveType() == 1) {
if (c.isMoving()) {
c.move();
} else if (rand.nextDouble() < Character.PROB_MOVE) {
c.setDirection(rand.nextInt(4));
c.setMoving(true);
}
}
}
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
leftKey.press();
}
if (keyCode == KeyEvent.VK_RIGHT) {
rightKey.press();
}
if (keyCode == KeyEvent.VK_UP) {
upKey.press();
}
if (keyCode == KeyEvent.VK_DOWN) {
downKey.press();
}
if (keyCode == KeyEvent.VK_SPACE) {
spaceKey.press();
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
leftKey.release();
}
if (keyCode == KeyEvent.VK_RIGHT) {
rightKey.release();
}
if (keyCode == KeyEvent.VK_UP) {
upKey.release();
}
if (keyCode == KeyEvent.VK_DOWN) {
downKey.release();
}
if (keyCode == KeyEvent.VK_SPACE) {
spaceKey.release();
}
}
public void keyTyped(KeyEvent e) {
}
private void loadSound() {
// load midi files
for (int i = 0; i < bgmNames.length; i++) {
midiEngine.load(bgmNames[i], "bgm/" + bgmNames[i] + ".mid");
}
// load sound clip files
for (int i = 0; i < soundNames.length; i++) {
waveEngine.load(soundNames[i], "sound/" + soundNames[i] + ".wav");
}
}
}