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