comparison game/Engine.java @ 48:f13bab4cccd3

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 07:59:41 +0200
parents 695cf13c103a
children e6da5c71be28
comparison
equal deleted inserted replaced
47:695cf13c103a 48:f13bab4cccd3
16 import java.io.*; 16 import java.io.*;
17 import game.*; 17 import game.*;
18 import javax.sound.sampled.*; 18 import javax.sound.sampled.*;
19 19
20 20
21 class PathInfo
22 {
23 public int inPoint, inX, inY, outPoint, outX, outY;
24
25 public PathInfo(int inPoint, int inX, int inY, int outPoint, int outX, int outY)
26 {
27 this.inPoint = inPoint;
28 this.inX = inX;
29 this.inY = inY;
30
31 this.outPoint = outPoint;
32 this.outX = outX;
33 this.outY = outY;
34 }
35
36 public void print()
37 {
38 System.out.print("PathInfo: inP="+inPoint+", inX="+inX+", inY="+inY+"\n");
39 System.out.print(" outP="+outPoint+", outX="+outX+", outY="+outY+"\n\n");
40 }
41 }
42
43 /* 21 /*
44 class AnimatedElement 22 class AnimatedElement
45 { 23 {
46 float x, y, stime, value; 24 float x, y, stime, value;
47 Interpolate lerp; 25 Interpolate lerp;
78 } 56 }
79 */ 57 */
80 58
81 class IDMWidget 59 class IDMWidget
82 { 60 {
61 int keyCode;
62
83 public IDMWidget() 63 public IDMWidget()
84 { 64 {
65 keyCode = -1;
85 } 66 }
86 67
87 public void paint(Graphics2D g) 68 public void paint(Graphics2D g)
88 { 69 {
89 } 70 }
90 71
91 public boolean contains(float x, float y) 72 public boolean contains(Point pos)
92 { 73 {
93 return false; 74 return false;
94 } 75 }
95 76
77 public void mousePressed(MouseEvent e)
78 {
79 }
80
81 public void mouseReleased(MouseEvent e)
82 {
83 if (contains(e.getPoint()))
84 {
85 clicked();
86 }
87 }
88
89 // Generic key handler
90 public boolean keyHandler(KeyEvent e)
91 {
92 if (e.getKeyCode() == keyCode)
93 {
94 clicked();
95 return true;
96 }
97 else
98 return false;
99 }
100
96 public void clicked() 101 public void clicked()
97 { 102 {
98 } 103 }
99 } 104 }
100 105
101 class IDMButton 106 class IDMButton extends IDMWidget
102 { 107 {
103 enum State { FOCUSED, PRESSED, NORMAL } 108 enum State { FOCUSED, PRESSED, NORMAL }
104 State state; 109 State state;
105 BufferedImage imgUp, imgPressed, img; 110 BufferedImage imgUp, imgPressed, img;
106 Point pos; 111 Point pos;
107 112
108 public IDMButton(float x, float y, String text) 113 public IDMButton(float x, float y, int key, String text)
109 { 114 {
110 try 115 try
111 { 116 {
112 ResourceLoader res = new ResourceLoader("graphics/button1_up.png"); 117 ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
113 imgUp = ImageIO.read(res.getStream()); 118 imgUp = ImageIO.read(res.getStream());
118 catch (IOException e) 123 catch (IOException e)
119 { 124 {
120 System.out.print(e.getMessage()); 125 System.out.print(e.getMessage());
121 } 126 }
122 127
128 keyCode = key;
123 setState(State.NORMAL); 129 setState(State.NORMAL);
124 } 130 }
125 131
126 private void setState(State newState) 132 private void setState(State newState)
127 { 133 {
135 public void paint(Graphics2D g) 141 public void paint(Graphics2D g)
136 { 142 {
137 g.drawImage(img, pos.x, pos.y, null); 143 g.drawImage(img, pos.x, pos.y, null);
138 } 144 }
139 145
140 public boolean contains(float x, float y) 146 public boolean contains(Point where)
141 { 147 {
142 return (x >= pos.x && y >= pos.y && 148 return (where.x >= pos.x && where.y >= pos.y &&
143 x < pos.x + img.getWidth() && 149 where.x < pos.x + img.getWidth() &&
144 y < pos.y + img.getHeight()); 150 where.y < pos.y + img.getHeight());
145 } 151 }
146 152
147 public void clicked() 153 public void clicked()
148 { 154 {
149 } 155 }
150 } 156 }
151 157
152 158
153 class GameBoard 159 class GameBoard extends IDMWidget
154 { 160 {
155 public static final int boardSize = 9; 161 public static final int boardSize = 9;
156 public static final int boardMiddle = 4; 162 public static final int boardMiddle = 4;
157 Piece[][] board; 163 Piece[][] board;
158 164
159 public boolean flagGameOver; 165 public boolean flagGameOver;
160 166
161 Piece currPiece; 167 Piece currPiece;
162 int currX, currY, currPoint; 168 int currX, currY, currPoint;
163 169
170 int score;
171
164 SoundManager soundManager; 172 SoundManager soundManager;
165 Sound sndPlaced; 173 Sound sndPlaced;
166 174
167 public GameBoard(SoundManager smgr) 175 public GameBoard(SoundManager smgr)
168 { 176 {
175 currPoint = 0; 183 currPoint = 0;
176 184
177 pieceFinishTurn(); 185 pieceFinishTurn();
178 186
179 flagGameOver = false; 187 flagGameOver = false;
188 score = 0;
180 189
181 soundManager = smgr; 190 soundManager = smgr;
182 sndPlaced = soundManager.getSound("sounds/placed.wav"); 191 sndPlaced = soundManager.getSound("sounds/placed.wav");
183 } 192 }
184 193
208 board[x][y].animate(time); 217 board[x][y].animate(time);
209 } 218 }
210 219
211 } 220 }
212 221
213 private boolean isEmpty(int x, int y)
214 {
215 return (x >= 0 && x < boardSize && y >= 0 &&
216 y < boardSize && board[x][y] == null);
217 }
218
219 public void pieceRotate(Piece.RotateDir dir) 222 public void pieceRotate(Piece.RotateDir dir)
220 { 223 {
221 if (currPiece != null) 224 if (currPiece != null)
222 currPiece.rotate(dir); 225 currPiece.rotate(dir);
223 } 226 }
238 case 6: currX--; break; 241 case 6: currX--; break;
239 case 7: currX--; break; 242 case 7: currX--; break;
240 } 243 }
241 } 244 }
242 245
246 public boolean pieceCheck(Piece curr)
247 {
248 if (curr == null)
249 {
250 // Create new piece
251 currPiece = new Piece(PieceType.ACTIVE);
252 board[currX][currY] = currPiece;
253 return true;
254 }
255 else
256 if (curr.getType() == PieceType.START)
257 {
258 if (currPiece != null)
259 {
260 // Hit center starting piece, game over
261 flagGameOver = true;
262 currPiece = null;
263 System.out.print("GameOver!\n");
264 return true;
265 }
266 else
267 {
268 // Start piece as first piece means game is starting
269 pieceMoveTo(currPoint);
270 currPiece = new Piece(PieceType.ACTIVE);
271 board[currX][currY] = currPiece;
272 return true;
273 }
274 }
275
276 // Mark the current piece as locked
277 curr.setType(PieceType.LOCKED);
278
279 // Solve connection (with rotations) through the piece
280 currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint));
281
282 // Mark connection as active
283 curr.setConnectionState(currPoint, true);
284
285 // Solve exit point (with rotations)
286 currPoint = curr.getAntiRotatedPoint(curr.getConnection(currPoint));
287
288 // Move to next position accordingly
289 pieceMoveTo(currPoint);
290 return false;
291 }
292
243 public void pieceFinishTurn() 293 public void pieceFinishTurn()
244 { 294 {
245 while (true) 295 boolean finished = false;
296 while (!finished)
246 { 297 {
247 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) 298 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize)
248 { 299 {
249 Piece curr = board[currX][currY]; 300 finished = pieceCheck(board[currX][currY]);
250
251 if (curr == null)
252 {
253 // Create new piece
254 currPiece = new Piece(PieceType.ACTIVE);
255 board[currX][currY] = currPiece;
256 return;
257 }
258 else
259 if (curr.getType() == PieceType.START)
260 {
261 if (currPiece != null)
262 {
263 // Hit center starting piece, game over
264 flagGameOver = true;
265 currPiece = null;
266 System.out.print("GameOver!\n");
267 break;
268 }
269 else
270 {
271 // Start piece as first piece means game is starting
272 pieceMoveTo(currPoint);
273 currPiece = new Piece(PieceType.ACTIVE);
274 board[currX][currY] = currPiece;
275 return;
276 }
277 }
278 else
279 {
280 // Mark the current piece as locked
281 curr.setType(PieceType.LOCKED);
282
283 // Solve connection (with rotations) through the piece
284 currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint));
285
286 // Mark connection as active
287 curr.setConnectionState(currPoint, true);
288
289 // Solve exit point (with rotations)
290 currPoint = curr.getAntiRotatedPoint(curr.getConnection(currPoint));
291
292 // Move to next position accordingly
293 pieceMoveTo(currPoint);
294 }
295 } 301 }
296 else 302 else
297 { 303 {
298 // Outside of the board, game over 304 // Outside of the board, game over
305 finished = true;
299 flagGameOver = true; 306 flagGameOver = true;
300 currPiece = null; 307 currPiece = null;
301 System.out.print("GameOver!\n"); 308 System.out.print("GameOver!\n");
302 break; 309 }
303 } 310 }
304 }
305
306 } 311 }
307 312
308 public boolean keyHandler(KeyEvent e) 313 public boolean keyHandler(KeyEvent e)
309 { 314 {
310 switch (e.getKeyCode()) 315 switch (e.getKeyCode())
469 474
470 // Shut down sound manager 475 // Shut down sound manager
471 soundManager.close(); 476 soundManager.close();
472 } 477 }
473 478
474 public void mousePressed(MouseEvent e) { }
475 public void mouseEntered(MouseEvent e) { } 479 public void mouseEntered(MouseEvent e) { }
476 public void mouseExited(MouseEvent e) { } 480 public void mouseExited(MouseEvent e) { }
481 public void mousePressed(MouseEvent e) { }
477 public void mouseReleased(MouseEvent e) { } 482 public void mouseReleased(MouseEvent e) { }
478 483
479 public void mouseClicked(MouseEvent e) 484 public void mouseClicked(MouseEvent e)
480 { 485 {
481 System.out.print("mouseClicked()\n"); 486 System.out.print("mouseClicked()\n");