# HG changeset patch # User Matti Hamalainen # Date 1298351966 -7200 # Node ID 695cf13c103a67ace0356ac88c38b36178499475 # Parent 3e8d1c30f57341ecbda2336a94a02e4b2c290e71 Move some code. diff -r 3e8d1c30f573 -r 695cf13c103a game/Engine.java --- a/game/Engine.java Tue Feb 22 06:42:32 2011 +0200 +++ b/game/Engine.java Tue Feb 22 07:19:26 2011 +0200 @@ -161,7 +161,10 @@ Piece currPiece; int currX, currY, currPoint; - public GameBoard() + SoundManager soundManager; + Sound sndPlaced; + + public GameBoard(SoundManager smgr) { board = new Piece[boardSize][boardSize]; @@ -174,6 +177,9 @@ pieceFinishTurn(); flagGameOver = false; + + soundManager = smgr; + sndPlaced = soundManager.getSound("sounds/placed.wav"); } public void paint(Graphics2D g, int sx, int sy, float scale) @@ -206,7 +212,8 @@ private boolean isEmpty(int x, int y) { - return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); + return (x >= 0 && x < boardSize && y >= 0 && + y < boardSize && board[x][y] == null); } public void pieceRotate(Piece.RotateDir dir) @@ -255,6 +262,7 @@ { // Hit center starting piece, game over flagGameOver = true; + currPiece = null; System.out.print("GameOver!\n"); break; } @@ -289,12 +297,35 @@ { // Outside of the board, game over flagGameOver = true; + currPiece = null; System.out.print("GameOver!\n"); break; } } } + + public boolean keyHandler(KeyEvent e) + { + switch (e.getKeyCode()) + { + case KeyEvent.VK_LEFT: + case KeyEvent.VK_UP: + pieceRotate(Piece.RotateDir.LEFT); + return true; + + case KeyEvent.VK_RIGHT: + case KeyEvent.VK_DOWN: + pieceRotate(Piece.RotateDir.RIGHT); + return true; + + case KeyEvent.VK_ENTER: + soundManager.play(sndPlaced); + pieceFinishTurn(); + return true; + } + return false; + } } @@ -313,8 +344,8 @@ static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); - SoundManager smgr; - Sound musa, placed; + SoundManager soundManager; + Sound musa; public Engine() { @@ -326,7 +357,7 @@ startTime = new Date().getTime(); // Sound system - smgr = new SoundManager(sfmt, 16); + soundManager = new SoundManager(sfmt, 16); // Load resources try @@ -346,7 +377,6 @@ } // musa = smgr.getSound("sounds/gamemusic.wav"); -// placed = smgr.getSound("sounds/placed.wav"); } catch (IOException e) { @@ -359,7 +389,7 @@ } // Initialize game components - lauta = new GameBoard(); + lauta = new GameBoard(soundManager); addKeyListener(this); addMouseListener(this); @@ -370,7 +400,7 @@ requestFocus(); } - smgr.play(musa); + soundManager.play(musa); } @@ -438,7 +468,7 @@ } // Shut down sound manager - smgr.close(); + soundManager.close(); } public void mousePressed(MouseEvent e) { } @@ -467,23 +497,11 @@ public void keyPressed(KeyEvent e) { // Handle keyboard input + if (lauta.keyHandler(e)) + return; + switch (e.getKeyCode()) { - case KeyEvent.VK_LEFT: - case KeyEvent.VK_UP: - lauta.pieceRotate(Piece.RotateDir.LEFT); - break; - - case KeyEvent.VK_RIGHT: - case KeyEvent.VK_DOWN: - lauta.pieceRotate(Piece.RotateDir.RIGHT); - break; - - case KeyEvent.VK_ENTER: - smgr.play(placed); - lauta.pieceFinishTurn(); - break; - case KeyEvent.VK_ESCAPE: break; }