comparison game/Engine.java @ 59:fd10a9422b60

Cleanups, bugfixes, etc etc.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Feb 2011 22:12:01 +0200
parents 1435e9d7fd1a
children 4a984e3b27d2
comparison
equal deleted inserted replaced
58:cde170f2f980 59:fd10a9422b60
59 class GameBoard extends IDMWidget 59 class GameBoard extends IDMWidget
60 { 60 {
61 public static final int boardSize = 9; 61 public static final int boardSize = 9;
62 public static final int boardMiddle = 4; 62 public static final int boardMiddle = 4;
63 Piece[][] board; 63 Piece[][] board;
64 float pscale;
64 65
65 public boolean flagGameOver; 66 public boolean flagGameOver;
66 public int gameScore; 67 public int gameScore;
67 68
68 public Piece currPiece, nextPiece; 69 public Piece currPiece, nextPiece;
70 71
71 72
72 SoundManager soundManager; 73 SoundManager soundManager;
73 Sound sndPlaced; 74 Sound sndPlaced;
74 75
75 public GameBoard(SoundManager smgr) 76 public GameBoard(IDMPoint pos, SoundManager smgr, float pscale)
77 {
78 super(pos);
79 this.pscale = pscale;
80
81 soundManager = smgr;
82 // sndPlaced = soundManager.getSound("sounds/placed.wav");
83
84 startNewGame();
85 }
86
87 public void startNewGame()
76 { 88 {
77 board = new Piece[boardSize][boardSize]; 89 board = new Piece[boardSize][boardSize];
78
79 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); 90 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
80 91
81 currX = boardMiddle; 92 currX = boardMiddle;
82 currY = boardMiddle; 93 currY = boardMiddle;
83 currPoint = 0; 94 currPoint = 0;
84 95
96 currPiece = null;
85 nextPiece = new Piece(PieceType.ACTIVE); 97 nextPiece = new Piece(PieceType.ACTIVE);
86 pieceFinishTurn(); 98
87 99 pieceFinishTurn();
88 flagGameOver = false; 100 flagGameOver = false;
89 gameScore = 0; 101 gameScore = 0;
90 102 }
91 soundManager = smgr; 103
92 // sndPlaced = soundManager.getSound("sounds/placed.wav"); 104 public void paintBackPlate(Graphics2D g)
93 } 105 {
94 106 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.2f));
95 public void paint(Graphics2D g, float sx, float sy, float scale) 107 g.setStroke(new BasicStroke(5.0f));
108 g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(),
109 boardSize * pscale, boardSize * pscale,
110 pscale / 5, pscale / 5));
111 }
112
113 public void paint(Graphics2D g)
96 { 114 {
97 for (int y = 0; y < boardSize; y++) 115 for (int y = 0; y < boardSize; y++)
98 for (int x = 0; x < boardSize; x++) 116 for (int x = 0; x < boardSize; x++)
99 if (board[x][y] != null) 117 if (board[x][y] != null)
100 { 118 {
101 AffineTransform save = g.getTransform();
102
103 board[x][y].paint(g, 119 board[x][y].paint(g,
104 sx + (x * scale), 120 getScaledX() + (x * pscale),
105 sy + (y * scale), 121 getScaledY() + (y * pscale),
106 scale - scale / 10); 122 pscale - pscale / 10);
107 123 }
108 g.setTransform(save); 124 }
109 } 125
126 public boolean contains(Point where)
127 {
128 return (where.x >= getScaledX() && where.y >= getScaledY() &&
129 where.x < getScaledX() + boardSize * pscale &&
130 where.y < getScaledY() + boardSize * pscale);
110 } 131 }
111 132
112 public void animate(float time) 133 public void animate(float time)
113 { 134 {
114 for (int y = 0; y < boardSize; y++) 135 for (int y = 0; y < boardSize; y++)
119 } 140 }
120 } 141 }
121 142
122 public void pieceRotate(Piece.RotateDir dir) 143 public void pieceRotate(Piece.RotateDir dir)
123 { 144 {
124 if (currPiece != null) 145 if (currPiece != null && !flagGameOver)
146 {
125 currPiece.rotate(dir); 147 currPiece.rotate(dir);
148 }
126 } 149 }
127 150
128 private void pieceMoveTo(int point) 151 private void pieceMoveTo(int point)
129 { 152 {
130 switch (point) 153 switch (point)
207 230
208 public void pieceFinishTurn() 231 public void pieceFinishTurn()
209 { 232 {
210 boolean finished = false; 233 boolean finished = false;
211 int connections = 0; 234 int connections = 0;
235
236 if (currPiece != null)
237 {
238 soundManager.play(sndPlaced);
239 }
212 240
213 while (!finished) 241 while (!finished)
214 { 242 {
215 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) 243 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize)
216 { 244 {
231 { 259 {
232 currPiece = null; 260 currPiece = null;
233 System.out.print("GameOver!\n"); 261 System.out.print("GameOver!\n");
234 } 262 }
235 } 263 }
264
265 public boolean mouseWheelMoved(MouseWheelEvent e)
266 {
267 int notches = e.getWheelRotation();
268
269 if (notches < 0)
270 pieceRotate(Piece.RotateDir.LEFT);
271 else
272 pieceRotate(Piece.RotateDir.RIGHT);
273
274 return true;
275 }
276
277 public boolean mouseClicked(MouseEvent e)
278 {
279 if (flagGameOver)
280 return false;
281
282 if (contains(e.getPoint()))
283 {
284 pieceFinishTurn();
285 return true;
286 }
287 else
288 return false;
289 }
236 290
237 public boolean keyPressed(KeyEvent e) 291 public boolean keyPressed(KeyEvent e)
238 { 292 {
239 if (flagGameOver) 293 if (flagGameOver)
240 return false; 294 return false;
250 case KeyEvent.VK_DOWN: 304 case KeyEvent.VK_DOWN:
251 pieceRotate(Piece.RotateDir.RIGHT); 305 pieceRotate(Piece.RotateDir.RIGHT);
252 return true; 306 return true;
253 307
254 case KeyEvent.VK_ENTER: 308 case KeyEvent.VK_ENTER:
255 soundManager.play(sndPlaced);
256 pieceFinishTurn(); 309 pieceFinishTurn();
257 return true; 310 return true;
258 } 311 }
259 return false; 312 return false;
260 } 313 }
261 } 314 }
262 315
263 316
264 public class Engine extends JPanel 317 public class Engine extends JPanel
265 implements Runnable, KeyListener, MouseListener 318 implements Runnable, KeyListener,
319 MouseListener, MouseWheelListener
266 { 320 {
267 long startTime; 321 long startTime;
268 float gameClock, gameFrames; 322 float gameClock, gameFrames;
269 Thread animThread; 323 Thread animThread;
270 boolean animEnable = false; 324 boolean animEnable = false;
324 widgets = new IDMContainer(); 378 widgets = new IDMContainer();
325 379
326 widgets.add(new BtnSwapPiece(0.75f, 0.60f)); 380 widgets.add(new BtnSwapPiece(0.75f, 0.60f));
327 widgets.add(new BtnNewGame(0.75f, 0.85f)); 381 widgets.add(new BtnNewGame(0.75f, 0.85f));
328 382
383 lauta = new GameBoard(new IDMPoint(0.09f, 0.18f), soundManager, 63);
384 widgets.add(lauta);
385
329 // Game 386 // Game
330 startNewGame(); 387 startNewGame();
331 388
332 // Initialize event listeners 389 // Initialize event listeners
333 addKeyListener(this); 390 addKeyListener(this);
334 addMouseListener(this); 391 addMouseListener(this);
392 addMouseWheelListener(this);
335 393
336 // Get initial focus 394 // Get initial focus
337 if (!hasFocus()) 395 if (!hasFocus())
338 { 396 {
339 System.out.print("Engine(): requesting focus\n"); 397 System.out.print("Engine(): requesting focus\n");
346 public void startNewGame() 404 public void startNewGame()
347 { 405 {
348 gameClock = 0; 406 gameClock = 0;
349 gameFrames = 0; 407 gameFrames = 0;
350 startTime = new Date().getTime(); 408 startTime = new Date().getTime();
351 409 lauta.startNewGame();
352 lauta = new GameBoard(soundManager);
353 } 410 }
354 411
355 public void paintComponent(Graphics g) 412 public void paintComponent(Graphics g)
356 { 413 {
357 Graphics2D g2 = (Graphics2D) g; 414 Graphics2D g2 = (Graphics2D) g;
361 RenderingHints.VALUE_ANTIALIAS_ON); 418 RenderingHints.VALUE_ANTIALIAS_ON);
362 419
363 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 420 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
364 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 421 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
365 422
423
366 // Rescale if parent component size has changed 424 // Rescale if parent component size has changed
367 Dimension dim = getSize(); 425 Dimension dim = getSize();
368 if (lautaDim == null || !dim.equals(lautaDim)) 426 if (lautaDim == null || !dim.equals(lautaDim))
369 { 427 {
428 // Rescale IDM GUI widgets
429 widgets.setScale(dim.width, dim.height);
430
370 // Rescale background image 431 // Rescale background image
371 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); 432 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
372 Graphics2D gimg = lautaBGScaled.createGraphics(); 433 Graphics2D gimg = lautaBGScaled.createGraphics();
373 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 434 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
374 RenderingHints.VALUE_INTERPOLATION_BICUBIC); 435 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
375 436
376 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); 437 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
438 lauta.paintBackPlate(gimg);
377 lautaDim = dim; 439 lautaDim = dim;
378 440
379 // Rescale IDM GUI widgets
380 widgets.setScale(dim.width, dim.height);
381
382 System.out.print("scale changed\n"); 441 System.out.print("scale changed\n");
383 } 442 }
384 443
444 // Get font metrics against current Graphics2D context
385 if (metrics1 == null) 445 if (metrics1 == null)
386 metrics1 = g2.getFontMetrics(font1); 446 metrics1 = g2.getFontMetrics(font1);
387 447
388 if (metrics2 == null) 448 if (metrics2 == null)
389 metrics2 = g2.getFontMetrics(font2); 449 metrics2 = g2.getFontMetrics(font2);
390 450
391 // Background image, pieces 451
452 // Draw background image, pieces, widgets
392 g2.drawImage(lautaBGScaled, 0, 0, null); 453 g2.drawImage(lautaBGScaled, 0, 0, null);
393 lauta.paint(g2, 90, 140, 65); 454 widgets.paint(g2);
394 455
395 if (!lauta.flagGameOver) 456 if (!lauta.flagGameOver)
396 { 457 {
458 // Draw next piece
397 AffineTransform save = g2.getTransform(); 459 AffineTransform save = g2.getTransform();
398 lauta.nextPiece.paint(g2, 830, 325, 90); 460 lauta.nextPiece.paint(g2, 830, 325, 90);
399 g2.setTransform(save); 461 g2.setTransform(save);
400 } 462 }
401 else 463 else
402 { 464 {
465 // Game over text
403 String text = "Game Over!"; 466 String text = "Game Over!";
404 int textWidth = metrics2.stringWidth(text); 467 int textWidth = metrics2.stringWidth(text);
405 g2.setFont(font2); 468 g2.setFont(font2);
406 469
407 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); 470 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f));
409 472
410 double f = Math.sin(gameClock * 0.1) * 4.0; 473 double f = Math.sin(gameClock * 0.1) * 4.0;
411 g2.setPaint(Color.white); 474 g2.setPaint(Color.white);
412 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); 475 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f);
413 } 476 }
414
415 widgets.paint(g2);
416 477
417 // Scores, etc 478 // Scores, etc
418 g2.setFont(font2); 479 g2.setFont(font2);
419 g2.setPaint(Color.white); 480 g2.setPaint(Color.white);
420 481
472 if (!hasFocus()) 533 if (!hasFocus())
473 { 534 {
474 System.out.print("requesting focus\n"); 535 System.out.print("requesting focus\n");
475 requestFocus(); 536 requestFocus();
476 } 537 }
538 else
539 {
540 lauta.mouseClicked(e);
541 }
542 }
543
544 public void mouseWheelMoved(MouseWheelEvent e)
545 {
546 lauta.mouseWheelMoved(e);
477 } 547 }
478 548
479 public void keyTyped(KeyEvent e) 549 public void keyTyped(KeyEvent e)
480 { 550 {
481 } 551 }
499 { 569 {
500 // Progress game animation clock 570 // Progress game animation clock
501 gameClock++; 571 gameClock++;
502 572
503 // Animate components 573 // Animate components
504 if (!lauta.flagGameOver) 574 lauta.animate(gameClock);
505 { 575 lauta.nextPiece.animate(gameClock);
506 lauta.animate(gameClock);
507 lauta.nextPiece.animate(gameClock);
508 }
509 576
510 // Repaint with a frame limiter 577 // Repaint with a frame limiter
511 if (gameClock % 3 == 1) 578 if (gameClock % 4 == 1)
512 { 579 {
513 repaint(); 580 repaint();
514 gameFrames++; 581 gameFrames++;
515 } 582 }
516 583