# HG changeset patch # User Matti Hamalainen # Date 1298491921 -7200 # Node ID fd10a9422b60d03dc55fffff1fa76c7c0e3e700a # Parent cde170f2f9800551b57721bbf5e5eda416636965 Cleanups, bugfixes, etc etc. diff -r cde170f2f980 -r fd10a9422b60 game/Engine.java --- a/game/Engine.java Wed Feb 23 21:43:07 2011 +0200 +++ b/game/Engine.java Wed Feb 23 22:12:01 2011 +0200 @@ -61,6 +61,7 @@ public static final int boardSize = 9; public static final int boardMiddle = 4; Piece[][] board; + float pscale; public boolean flagGameOver; public int gameScore; @@ -72,41 +73,61 @@ SoundManager soundManager; Sound sndPlaced; - public GameBoard(SoundManager smgr) + public GameBoard(IDMPoint pos, SoundManager smgr, float pscale) + { + super(pos); + this.pscale = pscale; + + soundManager = smgr; +// sndPlaced = soundManager.getSound("sounds/placed.wav"); + + startNewGame(); + } + + public void startNewGame() { board = new Piece[boardSize][boardSize]; - board[boardMiddle][boardMiddle] = new Piece(PieceType.START); currX = boardMiddle; currY = boardMiddle; currPoint = 0; - + + currPiece = null; nextPiece = new Piece(PieceType.ACTIVE); - pieceFinishTurn(); - + + pieceFinishTurn(); flagGameOver = false; gameScore = 0; - - soundManager = smgr; -// sndPlaced = soundManager.getSound("sounds/placed.wav"); } - public void paint(Graphics2D g, float sx, float sy, float scale) + public void paintBackPlate(Graphics2D g) + { + g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.2f)); + g.setStroke(new BasicStroke(5.0f)); + g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(), + boardSize * pscale, boardSize * pscale, + pscale / 5, pscale / 5)); + } + + public void paint(Graphics2D g) { for (int y = 0; y < boardSize; y++) for (int x = 0; x < boardSize; x++) if (board[x][y] != null) { - AffineTransform save = g.getTransform(); + board[x][y].paint(g, + getScaledX() + (x * pscale), + getScaledY() + (y * pscale), + pscale - pscale / 10); + } + } - board[x][y].paint(g, - sx + (x * scale), - sy + (y * scale), - scale - scale / 10); - - g.setTransform(save); - } + public boolean contains(Point where) + { + return (where.x >= getScaledX() && where.y >= getScaledY() && + where.x < getScaledX() + boardSize * pscale && + where.y < getScaledY() + boardSize * pscale); } public void animate(float time) @@ -121,8 +142,10 @@ public void pieceRotate(Piece.RotateDir dir) { - if (currPiece != null) + if (currPiece != null && !flagGameOver) + { currPiece.rotate(dir); + } } private void pieceMoveTo(int point) @@ -209,6 +232,11 @@ { boolean finished = false; int connections = 0; + + if (currPiece != null) + { + soundManager.play(sndPlaced); + } while (!finished) { @@ -233,6 +261,32 @@ System.out.print("GameOver!\n"); } } + + public boolean mouseWheelMoved(MouseWheelEvent e) + { + int notches = e.getWheelRotation(); + + if (notches < 0) + pieceRotate(Piece.RotateDir.LEFT); + else + pieceRotate(Piece.RotateDir.RIGHT); + + return true; + } + + public boolean mouseClicked(MouseEvent e) + { + if (flagGameOver) + return false; + + if (contains(e.getPoint())) + { + pieceFinishTurn(); + return true; + } + else + return false; + } public boolean keyPressed(KeyEvent e) { @@ -252,7 +306,6 @@ return true; case KeyEvent.VK_ENTER: - soundManager.play(sndPlaced); pieceFinishTurn(); return true; } @@ -262,7 +315,8 @@ public class Engine extends JPanel - implements Runnable, KeyListener, MouseListener + implements Runnable, KeyListener, + MouseListener, MouseWheelListener { long startTime; float gameClock, gameFrames; @@ -326,12 +380,16 @@ widgets.add(new BtnSwapPiece(0.75f, 0.60f)); widgets.add(new BtnNewGame(0.75f, 0.85f)); + lauta = new GameBoard(new IDMPoint(0.09f, 0.18f), soundManager, 63); + widgets.add(lauta); + // Game startNewGame(); // Initialize event listeners addKeyListener(this); addMouseListener(this); + addMouseWheelListener(this); // Get initial focus if (!hasFocus()) @@ -348,8 +406,7 @@ gameClock = 0; gameFrames = 0; startTime = new Date().getTime(); - - lauta = new GameBoard(soundManager); + lauta.startNewGame(); } public void paintComponent(Graphics g) @@ -363,10 +420,14 @@ g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + // Rescale if parent component size has changed Dimension dim = getSize(); if (lautaDim == null || !dim.equals(lautaDim)) { + // Rescale IDM GUI widgets + widgets.setScale(dim.width, dim.height); + // Rescale background image lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); Graphics2D gimg = lautaBGScaled.createGraphics(); @@ -374,32 +435,34 @@ RenderingHints.VALUE_INTERPOLATION_BICUBIC); gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); + lauta.paintBackPlate(gimg); lautaDim = dim; - // Rescale IDM GUI widgets - widgets.setScale(dim.width, dim.height); - System.out.print("scale changed\n"); } + // Get font metrics against current Graphics2D context if (metrics1 == null) metrics1 = g2.getFontMetrics(font1); if (metrics2 == null) metrics2 = g2.getFontMetrics(font2); - // Background image, pieces + + // Draw background image, pieces, widgets g2.drawImage(lautaBGScaled, 0, 0, null); - lauta.paint(g2, 90, 140, 65); + widgets.paint(g2); if (!lauta.flagGameOver) { + // Draw next piece AffineTransform save = g2.getTransform(); lauta.nextPiece.paint(g2, 830, 325, 90); g2.setTransform(save); } else { + // Game over text String text = "Game Over!"; int textWidth = metrics2.stringWidth(text); g2.setFont(font2); @@ -411,8 +474,6 @@ g2.setPaint(Color.white); g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); } - - widgets.paint(g2); // Scores, etc g2.setFont(font2); @@ -474,6 +535,15 @@ System.out.print("requesting focus\n"); requestFocus(); } + else + { + lauta.mouseClicked(e); + } + } + + public void mouseWheelMoved(MouseWheelEvent e) + { + lauta.mouseWheelMoved(e); } public void keyTyped(KeyEvent e) @@ -501,14 +571,11 @@ gameClock++; // Animate components - if (!lauta.flagGameOver) - { - lauta.animate(gameClock); - lauta.nextPiece.animate(gameClock); - } + lauta.animate(gameClock); + lauta.nextPiece.animate(gameClock); // Repaint with a frame limiter - if (gameClock % 3 == 1) + if (gameClock % 4 == 1) { repaint(); gameFrames++;