Mercurial > hg > ristipolku
annotate game/Engine.java @ 32:e480579cc460
More work on debugging the path resolving.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Feb 2011 18:22:16 +0200 |
parents | 60a4579a79df |
children | aa15b2c556b4 |
rev | line source |
---|---|
0 | 1 /* |
9 | 2 * Ristipolku Game Engine |
0 | 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
4 */ | |
9 | 5 package game; |
6 | |
0 | 7 import java.awt.*; |
8 import java.awt.geom.*; | |
1 | 9 import java.awt.event.*; |
7 | 10 import java.awt.image.*; |
11 import java.awt.event.*; | |
25 | 12 import java.awt.font.*; |
9 | 13 import javax.imageio.*; |
7 | 14 import javax.swing.*; |
1 | 15 import java.util.*; |
6 | 16 import java.io.*; |
1 | 17 import game.*; |
18 | |
21 | 19 |
1 | 20 class PathInfo |
21 { | |
2 | 22 public int in, inX, inY, out, outX, outY; |
1 | 23 |
2 | 24 public PathInfo(int in, int inX, int inY, int out, int outX, int outY) |
25 { | |
26 this.in = in; | |
27 this.inX = inX; | |
28 this.inY = inY; | |
29 | |
30 this.out = out; | |
31 this.outX = outX; | |
32 this.outY = outY; | |
33 } | |
27 | 34 |
35 public void print() | |
36 { | |
37 System.out.print("PathInfo: inP="+in+", inX="+inX+", inY="+inY+"\n"); | |
38 System.out.print(" outP="+out+", outX="+outX+", outY="+outY+"\n"); | |
39 } | |
1 | 40 } |
0 | 41 |
8 | 42 /* |
43 class AnimatedElement | |
44 { | |
45 float x, y, stime, value; | |
46 Interpolate lerp; | |
47 boolean active; | |
48 | |
49 public AnimatedElement(float x, float y, ) | |
50 { | |
51 stime = 0; | |
52 this.x = x; | |
53 this.y = y; | |
54 | |
55 } | |
56 | |
57 public animate(float time) | |
58 { | |
59 if (!active) | |
60 { | |
61 active = true; | |
62 stime = time; | |
63 } | |
64 | |
65 float t = (time - stime) / 10.0f; | |
66 if (t < 100) | |
67 value = lerp.getValue(t); | |
68 else | |
69 { | |
70 | |
71 } | |
72 } | |
73 | |
74 public paint(Graphics2D g, ); | |
75 { | |
76 } | |
77 } | |
78 */ | |
0 | 79 |
30 | 80 class IDMWidget |
81 { | |
82 public IDMWidget() | |
83 { | |
84 } | |
85 | |
86 public void paint(Graphics2D g) | |
87 { | |
88 } | |
89 | |
90 public boolean hit(float x, float y) | |
91 { | |
92 return false; | |
93 } | |
94 } | |
95 | |
96 class IDMButton | |
97 { | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
98 BufferedImage imgUp, imgPressed; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
99 |
30 | 100 public IDMButton(float x, float y, String text) |
101 { | |
102 } | |
103 | |
104 public void paint(Graphics2D g) | |
105 { | |
106 } | |
107 | |
108 public boolean hit(float x, float y) | |
109 { | |
110 return false; | |
111 } | |
112 } | |
113 | |
1 | 114 class GameBoard |
0 | 115 { |
2 | 116 public static final int boardSize = 9; |
117 public static final int boardMiddle = 4; | |
118 Piece[][] board; | |
119 Piece current; | |
8 | 120 public boolean flagGameOver; |
7 | 121 |
122 int moveX, moveY, movePoint; | |
9 | 123 |
2 | 124 public GameBoard() |
125 { | |
126 board = new Piece[boardSize][boardSize]; | |
127 | |
128 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | |
129 | |
130 moveX = boardMiddle; | |
131 moveY = boardMiddle - 1; | |
30 | 132 movePoint = 5; |
8 | 133 |
9 | 134 pieceFinishTurn(); |
8 | 135 |
136 flagGameOver = false; | |
2 | 137 } |
0 | 138 |
7 | 139 public void paint(Graphics2D g, int sx, int sy, float scale) |
2 | 140 { |
141 for (int y = 0; y < boardSize; y++) | |
142 for (int x = 0; x < boardSize; x++) | |
143 if (board[x][y] != null) | |
144 { | |
6 | 145 AffineTransform save = g.getTransform(); |
146 | |
4 | 147 board[x][y].paint(g, |
148 sx + (x * scale), | |
149 sy + (y * scale), | |
150 scale - scale / 10); | |
6 | 151 |
152 g.setTransform(save); | |
2 | 153 } |
154 } | |
9 | 155 |
156 public void animate(float time) | |
157 { | |
158 for (int y = 0; y < boardSize; y++) | |
159 for (int x = 0; x < boardSize; x++) | |
160 if (board[x][y] != null) | |
161 { | |
162 board[x][y].animate(time); | |
163 } | |
10 | 164 |
9 | 165 } |
1 | 166 |
2 | 167 private boolean isEmpty(int x, int y) |
168 { | |
169 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); | |
170 } | |
1 | 171 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
172 public void pieceRotate(Piece.RotateDir dir) |
2 | 173 { |
9 | 174 if (current != null) |
175 current.rotate(dir); | |
2 | 176 } |
1 | 177 |
8 | 178 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark) |
7 | 179 { |
180 int x = startX, y = startY; | |
27 | 181 int point = startPoint; |
182 Piece curr = board[x][y]; | |
9 | 183 |
27 | 184 do |
7 | 185 { |
27 | 186 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize) |
187 { | |
188 curr = board[x][y]; | |
189 if (curr == null) | |
190 break; | |
191 | |
192 if (curr.getType() == PieceType.START) | |
193 { | |
194 // Hit center starting piece | |
195 return null; | |
196 } | |
197 else | |
198 { | |
199 // Mark, if needed | |
200 if (mark) | |
201 { | |
202 curr.setType(PieceType.LOCKED); | |
203 curr.setActiveConnection(point); | |
204 } | |
205 | |
206 // Get next piece | |
207 point = curr.getConnection(point); | |
208 switch (point) | |
209 { | |
210 case 0: y--; break; | |
211 case 1: y--; break; | |
212 case 2: x++; break; | |
213 case 3: x++; break; | |
214 case 4: y++; break; | |
215 case 5: y++; break; | |
216 case 6: x--; break; | |
217 case 7: x--; break; | |
218 } | |
219 } | |
220 } | |
221 else | |
222 { | |
223 // Outside of the board | |
224 return null; | |
225 } | |
7 | 226 } |
27 | 227 while (curr != null); |
228 | |
7 | 229 return new PathInfo(startPoint, startX, startY, point, x, y); |
230 } | |
231 | |
2 | 232 public void pieceFinishTurn() |
233 { | |
234 if (current != null) | |
7 | 235 { |
6 | 236 current.setType(PieceType.LOCKED); |
8 | 237 PathInfo i = resolvePath(moveX, moveY, movePoint, true); |
7 | 238 |
239 if (i != null) | |
240 { | |
27 | 241 System.out.print("moveX="+moveX+", moveY="+moveY+", movePoint="+movePoint+"\n"); |
242 i.print(); | |
243 moveX = i.outX; | |
244 moveY = i.outY; | |
245 movePoint = i.out; | |
7 | 246 } |
247 } | |
248 | |
8 | 249 current = new Piece(PieceType.ACTIVE); |
7 | 250 if (isEmpty(moveX, moveY)) |
2 | 251 { |
8 | 252 board[moveX][moveY] = current; |
253 } | |
254 else | |
255 { | |
256 PathInfo i = resolvePath(moveX, moveY, movePoint, true); | |
257 if (i != null) | |
258 board[moveX][moveY] = current; | |
259 else | |
27 | 260 { |
261 System.out.print("pieceFinishTurn(): Game Over!\n"); | |
8 | 262 flagGameOver = true; |
27 | 263 } |
2 | 264 } |
1 | 265 } |
266 } | |
267 | |
268 | |
9 | 269 public class Engine extends JPanel |
18 | 270 implements Runnable, KeyListener, MouseListener |
1 | 271 { |
25 | 272 long startTime; |
273 float gameClock, gameFrames; | |
8 | 274 Thread animThread; |
275 boolean animEnable = false; | |
25 | 276 |
277 Font fontMain, font1, font2; | |
4 | 278 GameBoard lauta = null; |
9 | 279 BufferedImage lautaBG = null, lautaBGScaled = null; |
25 | 280 Dimension lautaDim; |
22 | 281 |
21 | 282 SoundElement[] sounds; |
25 | 283 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
284 Piece testi; int con = 0; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
285 |
21 | 286 public SoundElement snd(Sound snd) |
287 { | |
288 return sounds[snd.ordinal()]; | |
289 } | |
1 | 290 |
9 | 291 public Engine() |
2 | 292 { |
25 | 293 // Initialize globals |
294 System.out.print("Engine() constructor\n"); | |
295 | |
296 gameClock = 0; | |
297 gameFrames = 0; | |
298 startTime = new Date().getTime(); | |
4 | 299 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
300 testi = new Piece(PieceType.LOCKED); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
301 |
9 | 302 |
25 | 303 // Load resources |
4 | 304 try |
305 { | |
24 | 306 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
18 | 307 lautaBG = ImageIO.read(res.getStream()); |
21 | 308 |
25 | 309 try { |
310 res = new ResourceLoader("graphics/font.ttf"); | |
311 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
312 font1 = fontMain.deriveFont(24f); | |
313 font2 = fontMain.deriveFont(32f); | |
314 } | |
315 catch (FontFormatException e) | |
316 { | |
317 System.out.print("Could not initialize fonts.\n"); | |
318 } | |
319 | |
21 | 320 sounds = new SoundElement[16]; |
321 for (Sound s : Sound.values()) | |
322 { | |
323 System.out.print(s +" = "+ s.ordinal() +"\n"); | |
23
0741dc117808
Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
324 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isStreaming()); |
21 | 325 } |
4 | 326 } |
327 catch (IOException e) | |
328 { | |
18 | 329 /* |
4 | 330 JOptionPane.showMessageDialog(null, |
21 | 331 e.getMessage(), |
4 | 332 "Initialization error", |
333 JOptionPane.ERROR_MESSAGE); | |
18 | 334 */ |
21 | 335 System.out.print(e.getMessage()); |
18 | 336 } |
337 | |
338 lauta = new GameBoard(); | |
339 addKeyListener(this); | |
340 addMouseListener(this); | |
341 | |
342 // Get initial focus | |
343 if (!hasFocus()) | |
344 { | |
345 System.out.print("Engine(): requesting focus\n"); | |
346 requestFocus(); | |
4 | 347 } |
22 | 348 |
21 | 349 snd(Sound.MUSIC_GAME1).loop(-1); |
8 | 350 } |
351 | |
25 | 352 |
353 public void paintComponent(Graphics g) | |
354 { | |
355 Graphics2D g2 = (Graphics2D) g; | |
356 | |
357 // Use antialiasing when rendering the game elements | |
358 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
359 RenderingHints.VALUE_ANTIALIAS_ON); | |
360 | |
361 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | |
362 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
363 | |
364 // Rescale background if component size has changed | |
365 Dimension dim = getSize(); | |
366 if (lautaDim == null || !dim.equals(lautaDim)) | |
367 { | |
368 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); | |
369 Graphics2D gimg = lautaBGScaled.createGraphics(); | |
370 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | |
371 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
372 | |
373 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); | |
374 lautaDim = dim; | |
375 | |
376 System.out.print("scale changed\n"); | |
377 } | |
378 | |
379 // Background, pieces | |
380 g2.drawImage(lautaBGScaled, 0, 0, null); | |
381 lauta.paint(g2, 100, 150, 60); | |
382 | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
383 testi.paint(g2, 50f,50f,100f); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
384 |
25 | 385 // Scores |
386 g2.setFont(font1); | |
387 g2.setPaint(Color.white); | |
388 | |
389 | |
390 // Other elements | |
391 long currTime = new Date().getTime(); | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
392 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
393 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
394 g2.drawString("con = "+ con, 20, 30); |
25 | 395 } |
396 | |
8 | 397 public void startThreads() |
398 { | |
9 | 399 System.out.print("startThreads()\n"); |
8 | 400 if (animThread == null) |
401 { | |
402 animThread = new Thread(this); | |
403 animEnable = true; | |
404 animThread.start(); | |
405 } | |
406 } | |
407 | |
408 public void stopThreads() | |
409 { | |
9 | 410 System.out.print("stopThreads()\n"); |
8 | 411 if (animThread != null) |
412 { | |
413 animThread.interrupt(); | |
414 animEnable = false; | |
415 animThread = null; | |
416 } | |
22 | 417 |
418 for (Sound s : Sound.values()) | |
419 { | |
420 if (snd(s) != null) | |
421 snd(s).stop(); | |
422 } | |
2 | 423 } |
0 | 424 |
18 | 425 public void mousePressed(MouseEvent e) { } |
426 public void mouseEntered(MouseEvent e) { } | |
427 public void mouseExited(MouseEvent e) { } | |
428 public void mouseReleased(MouseEvent e) { } | |
429 | |
430 public void mouseClicked(MouseEvent e) | |
431 { | |
432 System.out.print("mouseClicked()\n"); | |
433 if (!hasFocus()) | |
434 { | |
435 System.out.print("requesting focus\n"); | |
436 requestFocus(); | |
437 } | |
438 } | |
439 | |
9 | 440 public void keyTyped(KeyEvent e) |
2 | 441 { |
442 } | |
1 | 443 |
2 | 444 public void keyReleased(KeyEvent e) |
445 { | |
446 } | |
1 | 447 |
9 | 448 public void keyPressed(KeyEvent e) |
2 | 449 { |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
450 // Handle keyboard input |
7 | 451 switch (e.getKeyCode()) |
2 | 452 { |
7 | 453 case KeyEvent.VK_LEFT: |
454 case KeyEvent.VK_UP: | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
455 lauta.pieceRotate(Piece.RotateDir.LEFT); |
2 | 456 break; |
0 | 457 |
7 | 458 case KeyEvent.VK_RIGHT: |
459 case KeyEvent.VK_DOWN: | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
460 lauta.pieceRotate(Piece.RotateDir.RIGHT); |
2 | 461 break; |
462 | |
7 | 463 case KeyEvent.VK_ENTER: |
2 | 464 lauta.pieceFinishTurn(); |
23
0741dc117808
Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
465 snd(Sound.PIECE_PLACED).stop(); |
0741dc117808
Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
466 snd(Sound.PIECE_PLACED).play(); |
2 | 467 break; |
468 } | |
469 } | |
8 | 470 |
471 public void run() | |
472 { | |
473 while (animEnable) | |
474 { | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
475 // Progress game animation clock |
25 | 476 gameClock++; |
9 | 477 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
478 // Animate components |
25 | 479 lauta.animate(gameClock); |
9 | 480 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
481 // Repaint with a frame limiter |
25 | 482 if (gameClock % 3 == 1) |
24 | 483 { |
10 | 484 repaint(); |
25 | 485 gameFrames++; |
24 | 486 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
487 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
488 // debugataan |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
489 con = ((int) (gameClock / 100)) & 7; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
490 testi.clearActiveConnections(); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
491 testi.setActiveConnection(con); |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
492 |
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
493 // Sleep for a moment |
9 | 494 try { |
495 Thread.sleep(10); | |
496 } | |
497 catch (InterruptedException x) { | |
498 } | |
8 | 499 } |
500 } | |
0 | 501 } |