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