changeset 48:f13bab4cccd3

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 07:59:41 +0200
parents 695cf13c103a
children e6da5c71be28
files game/Engine.java
diffstat 1 files changed, 92 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/game/Engine.java	Tue Feb 22 07:19:26 2011 +0200
+++ b/game/Engine.java	Tue Feb 22 07:59:41 2011 +0200
@@ -18,28 +18,6 @@
 import javax.sound.sampled.*;
 
 
-class PathInfo
-{
-    public int inPoint, inX, inY, outPoint, outX, outY; 
-
-    public PathInfo(int inPoint, int inX, int inY, int outPoint, int outX, int outY)
-    {
-        this.inPoint = inPoint;
-        this.inX = inX;
-        this.inY = inY;
-
-        this.outPoint = outPoint;
-        this.outX = outX;
-        this.outY = outY;
-    }
-    
-    public void print()
-    {
-        System.out.print("PathInfo:  inP="+inPoint+", inX="+inX+", inY="+inY+"\n");
-        System.out.print("          outP="+outPoint+", outX="+outX+", outY="+outY+"\n\n");
-    }
-}
-
 /*
 class AnimatedElement
 {
@@ -80,32 +58,59 @@
 
 class IDMWidget
 {
+    int keyCode;
+    
     public IDMWidget()
     {
+        keyCode = -1;
     }
 
     public void paint(Graphics2D g)
     {
     }
     
-    public boolean contains(float x, float y)
+    public boolean contains(Point pos)
     {
         return false;
     }
     
+    public void mousePressed(MouseEvent e)
+    {
+    }
+
+    public void mouseReleased(MouseEvent e)
+    {
+        if (contains(e.getPoint()))
+        {
+            clicked();
+        }
+    }
+
+    // Generic key handler
+    public boolean keyHandler(KeyEvent e)
+    {
+        if (e.getKeyCode() == keyCode)
+        {
+            clicked();
+            return true;
+        }
+        else
+            return false;
+    }
+    
     public void clicked()
     {
     }
 }
 
-class IDMButton
+class IDMButton extends IDMWidget
 {
     enum State { FOCUSED, PRESSED, NORMAL }
     State state;
     BufferedImage imgUp, imgPressed, img;
     Point pos;
  
-    public IDMButton(float x, float y, String text)
+    public IDMButton(float x, float y, int key, String text)
     {
         try
         {
@@ -120,6 +125,7 @@
             System.out.print(e.getMessage());
         }
         
+        keyCode = key;
         setState(State.NORMAL);
     }
     
@@ -137,20 +143,20 @@
         g.drawImage(img, pos.x, pos.y, null);
     }
 
-    public boolean contains(float x, float y)
+    public boolean contains(Point where)
     {
-        return (x >= pos.x && y >= pos.y &&
-                x < pos.x + img.getWidth() &&
-                y < pos.y + img.getHeight());
+        return (where.x >= pos.x && where.y >= pos.y &&
+                where.x < pos.x + img.getWidth() &&
+                where.y < pos.y + img.getHeight());
     }
-     
+    
     public void clicked()
     {
     }
 }
 
 
-class GameBoard
+class GameBoard extends IDMWidget
 {
     public static final int boardSize = 9;
     public static final int boardMiddle = 4;
@@ -161,6 +167,8 @@
     Piece currPiece;
     int currX, currY, currPoint;
 
+    int score;
+
     SoundManager soundManager;
     Sound sndPlaced;
     
@@ -177,6 +185,7 @@
         pieceFinishTurn();        
         
         flagGameOver = false;
+        score = 0;
         
         soundManager = smgr;
         sndPlaced = soundManager.getSound("sounds/placed.wav");
@@ -210,12 +219,6 @@
 
     }
    
-    private boolean isEmpty(int x, int y)
-    {
-        return (x >= 0 && x < boardSize && y >= 0 &&
-                y < boardSize && board[x][y] == null);
-    }
-
     public void pieceRotate(Piece.RotateDir dir)
     {
         if (currPiece != null)
@@ -240,69 +243,71 @@
         }
     }
 
+    public boolean pieceCheck(Piece curr)
+    {
+        if (curr == null)
+        {
+            // Create new piece
+            currPiece = new Piece(PieceType.ACTIVE);
+            board[currX][currY] = currPiece;
+            return true;
+        }
+        else
+        if (curr.getType() == PieceType.START)
+        {
+            if (currPiece != null)
+            {
+                // Hit center starting piece, game over
+                flagGameOver = true;
+                currPiece = null;
+                System.out.print("GameOver!\n");
+                return true;
+            }
+            else
+            {
+                // Start piece as first piece means game is starting
+                pieceMoveTo(currPoint);
+                currPiece = new Piece(PieceType.ACTIVE);
+                board[currX][currY] = currPiece;
+                return true;
+            }
+        }
+
+        // Mark the current piece as locked
+        curr.setType(PieceType.LOCKED);
+
+        // Solve connection (with rotations) through the piece
+        currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint));
+
+        // Mark connection as active
+        curr.setConnectionState(currPoint, true);
+
+        // Solve exit point (with rotations)
+        currPoint = curr.getAntiRotatedPoint(curr.getConnection(currPoint));
+
+        // Move to next position accordingly
+        pieceMoveTo(currPoint);
+        return false;
+    }
+
     public void pieceFinishTurn()
     {
-        while (true)
+        boolean finished = false;
+        while (!finished)
         {
             if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize)
             {
-                Piece curr = board[currX][currY];
-
-                if (curr == null)
-                {
-                    // Create new piece
-                    currPiece = new Piece(PieceType.ACTIVE);
-                    board[currX][currY] = currPiece;
-                    return;
-                }
-                else
-                if (curr.getType() == PieceType.START)
-                {
-                    if (currPiece != null)
-                    {
-                        // Hit center starting piece, game over
-                        flagGameOver = true;
-                        currPiece = null;
-                        System.out.print("GameOver!\n");
-                        break;
-                    }
-                    else
-                    {
-                        // Start piece as first piece means game is starting
-                        pieceMoveTo(currPoint);
-                        currPiece = new Piece(PieceType.ACTIVE);
-                        board[currX][currY] = currPiece;
-                        return;
-                    }
-                }
-                else
-                {
-                    // Mark the current piece as locked
-                    curr.setType(PieceType.LOCKED);
-                    
-                    // Solve connection (with rotations) through the piece
-                    currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint));
-                    
-                    // Mark connection as active
-                    curr.setConnectionState(currPoint, true);
-                    
-                    // Solve exit point (with rotations)
-                    currPoint = curr.getAntiRotatedPoint(curr.getConnection(currPoint));
-                    
-                    // Move to next position accordingly
-                    pieceMoveTo(currPoint);
-                }
+                finished = pieceCheck(board[currX][currY]);
             }
             else
             {
                 // Outside of the board, game over
+                finished = true;
                 flagGameOver = true;
                 currPiece = null;
                 System.out.print("GameOver!\n");
-                break;
             }
         }
-
     }
 
     public boolean keyHandler(KeyEvent e)
@@ -471,9 +476,9 @@
         soundManager.close();
     }
 
-    public void mousePressed(MouseEvent e) { }
     public void mouseEntered(MouseEvent e) { }
     public void mouseExited(MouseEvent e) { }
+    public void mousePressed(MouseEvent e) { }
     public void mouseReleased(MouseEvent e) { }
 
     public void mouseClicked(MouseEvent e)