comparison game/Engine.java @ 47:695cf13c103a

Move some code.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 07:19:26 +0200
parents 3e8d1c30f573
children f13bab4cccd3
comparison
equal deleted inserted replaced
46:3e8d1c30f573 47:695cf13c103a
159 public boolean flagGameOver; 159 public boolean flagGameOver;
160 160
161 Piece currPiece; 161 Piece currPiece;
162 int currX, currY, currPoint; 162 int currX, currY, currPoint;
163 163
164 public GameBoard() 164 SoundManager soundManager;
165 Sound sndPlaced;
166
167 public GameBoard(SoundManager smgr)
165 { 168 {
166 board = new Piece[boardSize][boardSize]; 169 board = new Piece[boardSize][boardSize];
167 170
168 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); 171 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
169 172
172 currPoint = 0; 175 currPoint = 0;
173 176
174 pieceFinishTurn(); 177 pieceFinishTurn();
175 178
176 flagGameOver = false; 179 flagGameOver = false;
180
181 soundManager = smgr;
182 sndPlaced = soundManager.getSound("sounds/placed.wav");
177 } 183 }
178 184
179 public void paint(Graphics2D g, int sx, int sy, float scale) 185 public void paint(Graphics2D g, int sx, int sy, float scale)
180 { 186 {
181 for (int y = 0; y < boardSize; y++) 187 for (int y = 0; y < boardSize; y++)
204 210
205 } 211 }
206 212
207 private boolean isEmpty(int x, int y) 213 private boolean isEmpty(int x, int y)
208 { 214 {
209 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); 215 return (x >= 0 && x < boardSize && y >= 0 &&
216 y < boardSize && board[x][y] == null);
210 } 217 }
211 218
212 public void pieceRotate(Piece.RotateDir dir) 219 public void pieceRotate(Piece.RotateDir dir)
213 { 220 {
214 if (currPiece != null) 221 if (currPiece != null)
253 { 260 {
254 if (currPiece != null) 261 if (currPiece != null)
255 { 262 {
256 // Hit center starting piece, game over 263 // Hit center starting piece, game over
257 flagGameOver = true; 264 flagGameOver = true;
265 currPiece = null;
258 System.out.print("GameOver!\n"); 266 System.out.print("GameOver!\n");
259 break; 267 break;
260 } 268 }
261 else 269 else
262 { 270 {
287 } 295 }
288 else 296 else
289 { 297 {
290 // Outside of the board, game over 298 // Outside of the board, game over
291 flagGameOver = true; 299 flagGameOver = true;
300 currPiece = null;
292 System.out.print("GameOver!\n"); 301 System.out.print("GameOver!\n");
293 break; 302 break;
294 } 303 }
295 } 304 }
296 305
306 }
307
308 public boolean keyHandler(KeyEvent e)
309 {
310 switch (e.getKeyCode())
311 {
312 case KeyEvent.VK_LEFT:
313 case KeyEvent.VK_UP:
314 pieceRotate(Piece.RotateDir.LEFT);
315 return true;
316
317 case KeyEvent.VK_RIGHT:
318 case KeyEvent.VK_DOWN:
319 pieceRotate(Piece.RotateDir.RIGHT);
320 return true;
321
322 case KeyEvent.VK_ENTER:
323 soundManager.play(sndPlaced);
324 pieceFinishTurn();
325 return true;
326 }
327 return false;
297 } 328 }
298 } 329 }
299 330
300 331
301 public class Engine extends JPanel 332 public class Engine extends JPanel
311 BufferedImage lautaBG = null, lautaBGScaled = null; 342 BufferedImage lautaBG = null, lautaBGScaled = null;
312 Dimension lautaDim; 343 Dimension lautaDim;
313 344
314 345
315 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); 346 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false);
316 SoundManager smgr; 347 SoundManager soundManager;
317 Sound musa, placed; 348 Sound musa;
318 349
319 public Engine() 350 public Engine()
320 { 351 {
321 // Initialize globals 352 // Initialize globals
322 System.out.print("Engine() constructor\n"); 353 System.out.print("Engine() constructor\n");
324 gameClock = 0; 355 gameClock = 0;
325 gameFrames = 0; 356 gameFrames = 0;
326 startTime = new Date().getTime(); 357 startTime = new Date().getTime();
327 358
328 // Sound system 359 // Sound system
329 smgr = new SoundManager(sfmt, 16); 360 soundManager = new SoundManager(sfmt, 16);
330 361
331 // Load resources 362 // Load resources
332 try 363 try
333 { 364 {
334 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); 365 ResourceLoader res = new ResourceLoader("graphics/board.jpg");
344 { 375 {
345 System.out.print("Could not initialize fonts.\n"); 376 System.out.print("Could not initialize fonts.\n");
346 } 377 }
347 378
348 // musa = smgr.getSound("sounds/gamemusic.wav"); 379 // musa = smgr.getSound("sounds/gamemusic.wav");
349 // placed = smgr.getSound("sounds/placed.wav");
350 } 380 }
351 catch (IOException e) 381 catch (IOException e)
352 { 382 {
353 JOptionPane.showMessageDialog(null, 383 JOptionPane.showMessageDialog(null,
354 e.getMessage(), 384 e.getMessage(),
357 387
358 System.out.print(e.getMessage()); 388 System.out.print(e.getMessage());
359 } 389 }
360 390
361 // Initialize game components 391 // Initialize game components
362 lauta = new GameBoard(); 392 lauta = new GameBoard(soundManager);
363 addKeyListener(this); 393 addKeyListener(this);
364 addMouseListener(this); 394 addMouseListener(this);
365 395
366 // Get initial focus 396 // Get initial focus
367 if (!hasFocus()) 397 if (!hasFocus())
368 { 398 {
369 System.out.print("Engine(): requesting focus\n"); 399 System.out.print("Engine(): requesting focus\n");
370 requestFocus(); 400 requestFocus();
371 } 401 }
372 402
373 smgr.play(musa); 403 soundManager.play(musa);
374 } 404 }
375 405
376 406
377 public void paintComponent(Graphics g) 407 public void paintComponent(Graphics g)
378 { 408 {
436 animEnable = false; 466 animEnable = false;
437 animThread = null; 467 animThread = null;
438 } 468 }
439 469
440 // Shut down sound manager 470 // Shut down sound manager
441 smgr.close(); 471 soundManager.close();
442 } 472 }
443 473
444 public void mousePressed(MouseEvent e) { } 474 public void mousePressed(MouseEvent e) { }
445 public void mouseEntered(MouseEvent e) { } 475 public void mouseEntered(MouseEvent e) { }
446 public void mouseExited(MouseEvent e) { } 476 public void mouseExited(MouseEvent e) { }
465 } 495 }
466 496
467 public void keyPressed(KeyEvent e) 497 public void keyPressed(KeyEvent e)
468 { 498 {
469 // Handle keyboard input 499 // Handle keyboard input
500 if (lauta.keyHandler(e))
501 return;
502
470 switch (e.getKeyCode()) 503 switch (e.getKeyCode())
471 { 504 {
472 case KeyEvent.VK_LEFT:
473 case KeyEvent.VK_UP:
474 lauta.pieceRotate(Piece.RotateDir.LEFT);
475 break;
476
477 case KeyEvent.VK_RIGHT:
478 case KeyEvent.VK_DOWN:
479 lauta.pieceRotate(Piece.RotateDir.RIGHT);
480 break;
481
482 case KeyEvent.VK_ENTER:
483 smgr.play(placed);
484 lauta.pieceFinishTurn();
485 break;
486
487 case KeyEvent.VK_ESCAPE: 505 case KeyEvent.VK_ESCAPE:
488 break; 506 break;
489 } 507 }
490 } 508 }
491 509