Mercurial > hg > ristipolku
annotate game/Engine.java @ 40:a69103644bf6
Cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 18 Feb 2011 23:34:47 +0200 |
parents | e682b623aea9 |
children | a5fd4f74a767 |
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.*; |
37 | 18 import javax.sound.sampled.*; |
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"); | |
40 | 38 System.out.print(" outP="+out+", outX="+outX+", outY="+outY+"\n\n"); |
27 | 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); | |
40 | 203 curr.setConnectionState(curr.getRotatedPoint(point), true); |
27 | 204 } |
205 | |
206 // Get next piece | |
40 | 207 point = curr.getConnection(curr.getRotatedPoint(point)); |
27 | 208 switch (point) |
209 { | |
39 | 210 case 0: y--; point = 5; break; |
211 case 1: y--; point = 4; break; | |
37 | 212 |
39 | 213 case 2: x++; point = 7; break; |
214 case 3: x++; point = 6; break; | |
37 | 215 |
39 | 216 case 4: y++; point = 1; break; |
217 case 5: y++; point = 0; break; | |
37 | 218 |
39 | 219 case 6: x--; point = 3; break; |
220 case 7: x--; point = 2; break; | |
27 | 221 } |
222 } | |
223 } | |
224 else | |
225 { | |
226 // Outside of the board | |
227 return null; | |
228 } | |
7 | 229 } |
27 | 230 while (curr != null); |
231 | |
7 | 232 return new PathInfo(startPoint, startX, startY, point, x, y); |
233 } | |
234 | |
2 | 235 public void pieceFinishTurn() |
236 { | |
35 | 237 // Do we have a piece? |
2 | 238 if (current != null) |
7 | 239 { |
40 | 240 // Yes, start resolving path to next piece placement |
8 | 241 PathInfo i = resolvePath(moveX, moveY, movePoint, true); |
40 | 242 |
7 | 243 if (i != null) |
244 { | |
27 | 245 moveX = i.outX; |
246 moveY = i.outY; | |
247 movePoint = i.out; | |
7 | 248 } |
249 } | |
250 | |
35 | 251 // Create a new piece |
8 | 252 current = new Piece(PieceType.ACTIVE); |
35 | 253 |
254 // Find a place for it | |
7 | 255 if (isEmpty(moveX, moveY)) |
2 | 256 { |
35 | 257 // Current position is empty, use it |
8 | 258 board[moveX][moveY] = current; |
259 } | |
260 else | |
261 { | |
35 | 262 // Resolve path |
8 | 263 PathInfo i = resolvePath(moveX, moveY, movePoint, true); |
264 if (i != null) | |
40 | 265 { |
266 // Path found, place the piece | |
8 | 267 board[moveX][moveY] = current; |
40 | 268 } |
8 | 269 else |
27 | 270 { |
35 | 271 // Path ended up center/gameboard walls - it's game over, man |
27 | 272 System.out.print("pieceFinishTurn(): Game Over!\n"); |
8 | 273 flagGameOver = true; |
27 | 274 } |
2 | 275 } |
1 | 276 } |
277 } | |
278 | |
279 | |
9 | 280 public class Engine extends JPanel |
18 | 281 implements Runnable, KeyListener, MouseListener |
1 | 282 { |
25 | 283 long startTime; |
284 float gameClock, gameFrames; | |
8 | 285 Thread animThread; |
286 boolean animEnable = false; | |
25 | 287 |
288 Font fontMain, font1, font2; | |
4 | 289 GameBoard lauta = null; |
9 | 290 BufferedImage lautaBG = null, lautaBGScaled = null; |
25 | 291 Dimension lautaDim; |
22 | 292 |
25 | 293 |
37 | 294 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); |
295 SoundManager smgr; | |
296 Sound musa, placed; | |
1 | 297 |
9 | 298 public Engine() |
2 | 299 { |
25 | 300 // Initialize globals |
301 System.out.print("Engine() constructor\n"); | |
302 | |
303 gameClock = 0; | |
304 gameFrames = 0; | |
305 startTime = new Date().getTime(); | |
4 | 306 |
37 | 307 // Sound system |
308 smgr = new SoundManager(sfmt, 16); | |
9 | 309 |
25 | 310 // Load resources |
4 | 311 try |
312 { | |
24 | 313 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
18 | 314 lautaBG = ImageIO.read(res.getStream()); |
21 | 315 |
25 | 316 try { |
317 res = new ResourceLoader("graphics/font.ttf"); | |
318 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
319 font1 = fontMain.deriveFont(24f); | |
320 font2 = fontMain.deriveFont(32f); | |
321 } | |
322 catch (FontFormatException e) | |
323 { | |
324 System.out.print("Could not initialize fonts.\n"); | |
325 } | |
37 | 326 |
327 musa = smgr.getSound("sounds/gamemusic.wav"); | |
328 // placed = smgr.getSound("sounds/placed.wav"); | |
4 | 329 } |
330 catch (IOException e) | |
331 { | |
332 JOptionPane.showMessageDialog(null, | |
21 | 333 e.getMessage(), |
4 | 334 "Initialization error", |
335 JOptionPane.ERROR_MESSAGE); | |
37 | 336 |
21 | 337 System.out.print(e.getMessage()); |
18 | 338 } |
339 | |
37 | 340 // Initialize game components |
18 | 341 lauta = new GameBoard(); |
342 addKeyListener(this); | |
343 addMouseListener(this); | |
344 | |
345 // Get initial focus | |
346 if (!hasFocus()) | |
347 { | |
348 System.out.print("Engine(): requesting focus\n"); | |
349 requestFocus(); | |
4 | 350 } |
37 | 351 |
352 smgr.play(musa); | |
8 | 353 } |
354 | |
25 | 355 |
356 public void paintComponent(Graphics g) | |
357 { | |
358 Graphics2D g2 = (Graphics2D) g; | |
359 | |
360 // Use antialiasing when rendering the game elements | |
361 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
362 RenderingHints.VALUE_ANTIALIAS_ON); | |
363 | |
364 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | |
365 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
366 | |
367 // Rescale background if component size has changed | |
368 Dimension dim = getSize(); | |
369 if (lautaDim == null || !dim.equals(lautaDim)) | |
370 { | |
371 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); | |
372 Graphics2D gimg = lautaBGScaled.createGraphics(); | |
373 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | |
374 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
375 | |
376 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); | |
377 lautaDim = dim; | |
378 | |
379 System.out.print("scale changed\n"); | |
380 } | |
381 | |
382 // Background, pieces | |
383 g2.drawImage(lautaBGScaled, 0, 0, null); | |
384 lauta.paint(g2, 100, 150, 60); | |
385 | |
386 // Scores | |
387 g2.setFont(font1); | |
388 g2.setPaint(Color.white); | |
389 | |
390 | |
391 // Other elements | |
392 long currTime = new Date().getTime(); | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
393 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
25 | 394 } |
395 | |
8 | 396 public void startThreads() |
397 { | |
9 | 398 System.out.print("startThreads()\n"); |
8 | 399 if (animThread == null) |
400 { | |
401 animThread = new Thread(this); | |
402 animEnable = true; | |
403 animThread.start(); | |
404 } | |
405 } | |
406 | |
407 public void stopThreads() | |
408 { | |
9 | 409 System.out.print("stopThreads()\n"); |
37 | 410 |
411 // Stop animations | |
8 | 412 if (animThread != null) |
413 { | |
414 animThread.interrupt(); | |
415 animEnable = false; | |
416 animThread = null; | |
417 } | |
22 | 418 |
37 | 419 // Shut down sound manager |
420 smgr.close(); | |
2 | 421 } |
0 | 422 |
18 | 423 public void mousePressed(MouseEvent e) { } |
424 public void mouseEntered(MouseEvent e) { } | |
425 public void mouseExited(MouseEvent e) { } | |
426 public void mouseReleased(MouseEvent e) { } | |
427 | |
428 public void mouseClicked(MouseEvent e) | |
429 { | |
430 System.out.print("mouseClicked()\n"); | |
431 if (!hasFocus()) | |
432 { | |
433 System.out.print("requesting focus\n"); | |
434 requestFocus(); | |
435 } | |
436 } | |
437 | |
9 | 438 public void keyTyped(KeyEvent e) |
2 | 439 { |
440 } | |
1 | 441 |
2 | 442 public void keyReleased(KeyEvent e) |
443 { | |
444 } | |
1 | 445 |
9 | 446 public void keyPressed(KeyEvent e) |
2 | 447 { |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
448 // Handle keyboard input |
7 | 449 switch (e.getKeyCode()) |
2 | 450 { |
7 | 451 case KeyEvent.VK_LEFT: |
452 case KeyEvent.VK_UP: | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
453 lauta.pieceRotate(Piece.RotateDir.LEFT); |
2 | 454 break; |
0 | 455 |
7 | 456 case KeyEvent.VK_RIGHT: |
457 case KeyEvent.VK_DOWN: | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
458 lauta.pieceRotate(Piece.RotateDir.RIGHT); |
2 | 459 break; |
460 | |
7 | 461 case KeyEvent.VK_ENTER: |
37 | 462 smgr.play(placed); |
2 | 463 lauta.pieceFinishTurn(); |
464 break; | |
35 | 465 |
466 case KeyEvent.VK_ESCAPE: | |
467 break; | |
2 | 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 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
488 // Sleep for a moment |
9 | 489 try { |
490 Thread.sleep(10); | |
491 } | |
492 catch (InterruptedException x) { | |
493 } | |
8 | 494 } |
495 } | |
0 | 496 } |