Mercurial > hg > ristipolku
comparison game/Engine.java @ 117:973f567c7b9d dev-0_75
Change how gameboard is rendered, make it more OO-sensible.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 31 May 2011 18:53:37 +0300 |
parents | 858b9644a61e |
children | 52d79502447c |
comparison
equal
deleted
inserted
replaced
116:858b9644a61e | 117:973f567c7b9d |
---|---|
199 class GameBoard extends IDMWidget | 199 class GameBoard extends IDMWidget |
200 { | 200 { |
201 static final int boardSize = 9; | 201 static final int boardSize = 9; |
202 static final int boardMiddle = 4; | 202 static final int boardMiddle = 4; |
203 Piece[][] board; | 203 Piece[][] board; |
204 float pscale; | 204 float pscale, ptime; |
205 | 205 |
206 public boolean flagGameOver; | 206 public boolean flagGameOver; |
207 public int gameScore; | 207 public int gameScore; |
208 | 208 |
209 public Piece currPiece, nextPiece; | 209 public Piece currPiece, nextPiece; |
256 board[x][y].paint(g, | 256 board[x][y].paint(g, |
257 getScaledX() + (x * pscale), | 257 getScaledX() + (x * pscale), |
258 getScaledY() + (y * pscale), | 258 getScaledY() + (y * pscale), |
259 pscale - pscale / 10); | 259 pscale - pscale / 10); |
260 } | 260 } |
261 | |
262 if (!flagGameOver) | |
263 { | |
264 if (nextPiece != null) | |
265 { | |
266 // Draw next piece | |
267 AffineTransform save = g.getTransform(); | |
268 nextPiece.paint(g, 830, 325, 90); | |
269 g.setTransform(save); | |
270 } | |
271 } | |
272 else | |
273 { | |
274 // Game over text | |
275 String text = "Game Over!"; | |
276 int textWidth = G.metrics[2].stringWidth(text); | |
277 g.setFont(G.fonts[2]); | |
278 | |
279 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); | |
280 g.drawString(text, (G.screenDim.width - textWidth) / 2 + 5, G.screenDim.height / 2 + 5); | |
281 | |
282 double f = Math.sin(ptime * 0.1) * 4.0; | |
283 g.setPaint(Color.white); | |
284 g.drawString(text, (G.screenDim.width - textWidth) / 2 + (float) f, G.screenDim.height / 2 + (float) f); | |
285 } | |
286 | |
287 // Score | |
288 g.setFont(G.fonts[2]); | |
289 g.setPaint(Color.white); | |
290 g.drawString(""+ String.format("%05d", gameScore), G.screenDim.width - 230, 220); | |
291 | |
261 } | 292 } |
262 | 293 |
263 public boolean contains(float x, float y) | 294 public boolean contains(float x, float y) |
264 { | 295 { |
265 return (x >= getScaledX() && | 296 return (x >= getScaledX() && |
268 y < getScaledY() + boardSize * pscale); | 299 y < getScaledY() + boardSize * pscale); |
269 } | 300 } |
270 | 301 |
271 public void animate(float time) | 302 public void animate(float time) |
272 { | 303 { |
304 ptime = time; | |
273 for (int y = 0; y < boardSize; y++) | 305 for (int y = 0; y < boardSize; y++) |
274 for (int x = 0; x < boardSize; x++) | 306 for (int x = 0; x < boardSize; x++) |
275 if (board[x][y] != null) | 307 if (board[x][y] != null) |
276 { | 308 { |
277 board[x][y].animate(time); | 309 board[x][y].animate(time); |
483 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); | 515 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
484 G.lautaBG = ImageIO.read(res.getStream()); | 516 G.lautaBG = ImageIO.read(res.getStream()); |
485 | 517 |
486 try { | 518 try { |
487 res = new ResourceLoader("graphics/font.ttf"); | 519 res = new ResourceLoader("graphics/font.ttf"); |
488 | |
489 | 520 |
490 G.fonts = new Font[G.numFonts]; | 521 G.fonts = new Font[G.numFonts]; |
491 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | 522 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); |
492 G.fonts[1] = G.fonts[0].deriveFont(24f); | 523 G.fonts[1] = G.fonts[0].deriveFont(24f); |
493 G.fonts[2] = G.fonts[0].deriveFont(64f); | 524 G.fonts[2] = G.fonts[0].deriveFont(64f); |
513 } | 544 } |
514 | 545 |
515 // Create IDM GUI widgets | 546 // Create IDM GUI widgets |
516 widgets = new IDMContainer(); | 547 widgets = new IDMContainer(); |
517 | 548 |
549 lauta = new GameBoard(new IDMPoint(95, 130), 63); | |
550 widgets.add(lauta); | |
551 | |
518 widgets.add(new BtnSwapPiece(767f, 450f)); | 552 widgets.add(new BtnSwapPiece(767f, 450f)); |
519 widgets.add(new BtnAbout (767f, 570f)); | 553 widgets.add(new BtnAbout (767f, 570f)); |
520 widgets.add(new BtnNewGame (767f, 650f)); | 554 widgets.add(new BtnNewGame (767f, 650f)); |
521 | |
522 lauta = new GameBoard(new IDMPoint(95, 130), 63); | |
523 widgets.add(lauta); | |
524 | 555 |
525 aboutBox = new AboutBox(); | 556 aboutBox = new AboutBox(); |
526 | 557 |
527 // Game | 558 // Game |
528 startNewGame(); | 559 startNewGame(); |
594 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | 625 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
595 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | 626 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
596 | 627 |
597 widgets.paint(g2); | 628 widgets.paint(g2); |
598 | 629 |
599 if (!lauta.flagGameOver) | |
600 { | |
601 if (lauta.nextPiece != null) | |
602 { | |
603 // Draw next piece | |
604 AffineTransform save = g2.getTransform(); | |
605 lauta.nextPiece.paint(g2, 830, 325, 90); | |
606 g2.setTransform(save); | |
607 } | |
608 } | |
609 else | |
610 { | |
611 // Game over text | |
612 String text = "Game Over!"; | |
613 int textWidth = G.metrics[2].stringWidth(text); | |
614 g2.setFont(G.fonts[2]); | |
615 | |
616 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); | |
617 g2.drawString(text, (dim.width - textWidth) / 2 + 5, dim.height / 2 + 5); | |
618 | |
619 double f = Math.sin(gameClock * 0.1) * 4.0; | |
620 g2.setPaint(Color.white); | |
621 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); | |
622 } | |
623 | |
624 // Score | |
625 g2.setFont(G.fonts[2]); | |
626 g2.setPaint(Color.white); | |
627 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220); | |
628 | |
629 | 630 |
630 // Frames per second counter | 631 // Frames per second counter |
631 g2.setFont(G.fonts[1]); | 632 g2.setFont(G.fonts[1]); |
632 long currTime = new Date().getTime(); | 633 long currTime = new Date().getTime(); |
633 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); | 634 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), G.screenDim.width - 120, 20); |
634 | 635 |
635 gameFrames++; | 636 gameFrames++; |
636 } | 637 } |
637 | 638 |
638 public void startThreads() | 639 public void startThreads() |