Mercurial > hg > ristipolku
annotate game/Engine.java @ 88:753156d64493
Bump version to 0.65.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Mar 2011 16:33:53 +0200 |
parents | 6afbf88c8bda |
children | e1d657e6c25b |
rev | line source |
---|---|
0 | 1 /* |
9 | 2 * Ristipolku Game Engine |
0 | 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
4 */ | |
9 | 5 package game; |
6 | |
0 | 7 import java.awt.*; |
8 import java.awt.geom.*; | |
1 | 9 import java.awt.event.*; |
7 | 10 import java.awt.image.*; |
11 import java.awt.event.*; | |
25 | 12 import java.awt.font.*; |
9 | 13 import javax.imageio.*; |
7 | 14 import javax.swing.*; |
1 | 15 import java.util.*; |
6 | 16 import java.io.*; |
1 | 17 import game.*; |
37 | 18 import javax.sound.sampled.*; |
21 | 19 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
20 |
75 | 21 class AboutBox extends IDMWidget |
8 | 22 { |
75 | 23 BufferedImage aboutImg; |
24 boolean aboutSecret; | |
25 | |
76 | 26 IDMPoint currPos, currOffs; |
27 Paint textPaint; | |
28 Font textFont; | |
29 FontMetrics textMetrics; | |
30 | |
31 | |
75 | 32 public AboutBox() |
33 { | |
34 try { | |
35 ResourceLoader res = new ResourceLoader("graphics/girl.jpg"); | |
36 aboutImg = ImageIO.read(res.getStream()); | |
37 } | |
38 catch (IOException e) | |
39 { | |
40 } | |
41 | |
42 aboutSecret = false; | |
43 | |
44 setPos(150f, 150f); | |
81
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
45 setSize(675f, 420f); |
75 | 46 } |
47 | |
48 public void setTextFont(Font font, FontMetrics metrics) | |
49 { | |
50 textFont = font; | |
51 textMetrics = metrics; | |
52 } | |
53 | |
54 public void setTextPaint(Paint paint) | |
8 | 55 { |
75 | 56 textPaint = paint; |
57 } | |
58 | |
59 public void setCurrPos(IDMPoint npos) | |
60 { | |
61 currPos = npos; | |
62 currOffs = new IDMPoint(0, 0); | |
63 } | |
64 | |
65 public void setCurrPos(float x, float y) | |
66 { | |
67 setCurrPos(new IDMPoint(x, y)); | |
8 | 68 } |
69 | |
75 | 70 public void drawString(Graphics2D g, String text) |
8 | 71 { |
75 | 72 Paint savePaint = g.getPaint(); |
73 g.setPaint(textPaint); | |
76 | 74 g.setFont(textFont); |
75 | 75 |
76 int i = 0; | |
77 while (i < text.length()) | |
8 | 78 { |
75 | 79 int p = text.indexOf("\n", i); |
80 boolean linefeed; | |
81 String str; | |
80
3d34258ccc2c
Render empty lines correctly.
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
82 if (p >= i) |
75 | 83 { |
84 str = text.substring(i, p); | |
85 i = p + 1; | |
86 linefeed = true; | |
87 } | |
88 else | |
89 { | |
90 str = text.substring(i); | |
91 i += str.length(); | |
92 linefeed = false; | |
93 } | |
94 | |
95 g.drawString(str, currPos.x + currOffs.x, currPos.y + currOffs.y); | |
96 | |
97 if (linefeed) | |
98 { | |
99 currOffs.x = 0; | |
76 | 100 currOffs.y += textMetrics.getHeight(); |
75 | 101 } |
102 else | |
103 { | |
104 currOffs.x += textMetrics.stringWidth(str); | |
105 } | |
8 | 106 } |
75 | 107 |
108 g.setPaint(savePaint); | |
109 } | |
110 | |
111 public void paint(Graphics2D g) | |
112 { | |
113 int x = getScaledX(), y = getScaledY(), | |
114 w = getScaledWidth(), h = getScaledHeight(); | |
115 | |
116 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.6f)); | |
117 g.fill(new RoundRectangle2D.Float(x, y, w, h, 10, 10)); | |
118 | |
76 | 119 setTextFont(G.fonts[3], G.metrics[3]); |
75 | 120 setTextPaint(Color.white); |
8 | 121 |
76 | 122 setCurrPos(x + 20, y + 30); |
88 | 123 drawString(g, "RistiPolku (CrossPaths) v0.65\n"); |
75 | 124 |
76 | 125 setTextFont(G.fonts[1], G.metrics[1]); |
75 | 126 if (aboutSecret) |
127 { | |
76 | 128 g.drawImage(aboutImg, x + 20, y + 55, aboutImg.getWidth(), aboutImg.getHeight(), null); |
75 | 129 |
76 | 130 setCurrPos(x + 225, y + 75); |
75 | 131 drawString(g, |
132 "Dedicated to my\n" + | |
76 | 133 "favorite woman\n" + |
75 | 134 "in the world."); |
135 | |
76 | 136 setCurrPos(x + 370, y + 175); |
75 | 137 drawString(g, "- Matti"); |
138 } | |
8 | 139 else |
140 { | |
75 | 141 setTextPaint(Color.yellow); |
142 drawString(g, "(c) Copyright 2011 Matti 'ccr' Hämäläinen\n"); | |
143 | |
144 setTextPaint(Color.white); | |
145 drawString(g, "Programming project for Java-course\n" + | |
81
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
146 "T740306 taught by Kari Laitinen." + |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
147 "\n \n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
148 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
149 setTextPaint(Color.red); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
150 drawString(g, "Controls:\n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
151 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
152 IDMPoint old = currOffs.copy(); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
153 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
154 setTextPaint(Color.white); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
155 drawString(g, |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
156 "Arrow keys / mouse wheel\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
157 "Enter / mouse click\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
158 "Space bar\n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
159 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
160 currPos.x += 330; |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
161 currOffs.y = old.y; |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
162 drawString(g, |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
163 "- Rotate piece\n" + |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
164 "- Place/lock piece\n" + |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
165 "- Swap piece\n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
166 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
167 currPos.x -= 330; |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
168 setTextPaint(Color.green); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
169 drawString(g, |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
170 "\nObjective: Create a path long as possible by rotating\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
171 "and placing pieces. More points will be awarded for\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
172 "advancing the path by several segments per turn." |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
173 ); |
8 | 174 } |
175 } | |
75 | 176 |
177 public boolean keyPressed(KeyEvent e) | |
178 { | |
179 if (e.getKeyCode() == KeyEvent.VK_L) | |
180 { | |
181 aboutSecret = true; | |
182 } | |
183 else | |
184 { | |
185 clicked(); | |
186 aboutSecret = false; | |
187 } | |
188 return true; | |
189 } | |
8 | 190 |
75 | 191 public void clicked() |
8 | 192 { |
75 | 193 parent.remove(this); |
8 | 194 } |
195 } | |
75 | 196 |
0 | 197 |
48 | 198 class GameBoard extends IDMWidget |
0 | 199 { |
75 | 200 static final int boardSize = 9; |
201 static final int boardMiddle = 4; | |
2 | 202 Piece[][] board; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
203 float pscale; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
204 |
8 | 205 public boolean flagGameOver; |
55 | 206 public int gameScore; |
54 | 207 |
208 public Piece currPiece, nextPiece; | |
41 | 209 int currX, currY, currPoint; |
9 | 210 |
47 | 211 Sound sndPlaced; |
212 | |
75 | 213 public GameBoard(IDMPoint pos, float ps) |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
214 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
215 super(pos); |
75 | 216 pscale = ps; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
217 |
75 | 218 // sndPlaced = smgr.getSound("sounds/placed.wav"); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
219 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
220 startNewGame(); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
221 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
222 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
223 public void startNewGame() |
2 | 224 { |
225 board = new Piece[boardSize][boardSize]; | |
226 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | |
227 | |
41 | 228 currX = boardMiddle; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
229 currY = boardMiddle; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
230 currPoint = 0; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
231 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
232 currPiece = null; |
54 | 233 nextPiece = new Piece(PieceType.ACTIVE); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
234 |
65
dc936dba124e
Set game over-flag correctly, so that we reset everything after a game over
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
235 flagGameOver = false; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
236 pieceFinishTurn(); |
55 | 237 gameScore = 0; |
2 | 238 } |
0 | 239 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
240 public void paintBackPlate(Graphics2D g) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
241 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
242 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.2f)); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
243 g.setStroke(new BasicStroke(5.0f)); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
244 g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(), |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
245 boardSize * pscale, boardSize * pscale, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
246 pscale / 5, pscale / 5)); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
247 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
248 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
249 public void paint(Graphics2D g) |
2 | 250 { |
251 for (int y = 0; y < boardSize; y++) | |
252 for (int x = 0; x < boardSize; x++) | |
253 if (board[x][y] != null) | |
254 { | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
255 board[x][y].paint(g, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
256 getScaledX() + (x * pscale), |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
257 getScaledY() + (y * pscale), |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
258 pscale - pscale / 10); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
259 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
260 } |
75 | 261 |
262 public boolean contains(float x, float y) | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
263 { |
76 | 264 return (x >= getScaledX() && |
75 | 265 y >= getScaledY() && |
266 x < getScaledX() + boardSize * pscale && | |
267 y < getScaledY() + boardSize * pscale); | |
2 | 268 } |
9 | 269 |
270 public void animate(float time) | |
271 { | |
272 for (int y = 0; y < boardSize; y++) | |
273 for (int x = 0; x < boardSize; x++) | |
274 if (board[x][y] != null) | |
275 { | |
276 board[x][y].animate(time); | |
277 } | |
278 } | |
1 | 279 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
280 public void pieceRotate(Piece.RotateDir dir) |
2 | 281 { |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
282 if (currPiece != null && !flagGameOver) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
283 { |
41 | 284 currPiece.rotate(dir); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
285 } |
2 | 286 } |
1 | 287 |
75 | 288 // Change coordinates based on the "outgoing" |
289 // piece connection point. | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
290 private void pieceMoveTo(int point) |
7 | 291 { |
46 | 292 switch (point) |
293 { | |
294 case 0: currY--; break; | |
295 case 1: currY--; break; | |
296 | |
297 case 2: currX++; break; | |
298 case 3: currX++; break; | |
299 | |
300 case 4: currY++; break; | |
301 case 5: currY++; break; | |
302 | |
303 case 6: currX--; break; | |
304 case 7: currX--; break; | |
305 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
306 } |
9 | 307 |
54 | 308 public void pieceCreateNew() |
309 { | |
310 currPiece = nextPiece; | |
311 nextPiece = new Piece(PieceType.ACTIVE); | |
312 } | |
313 | |
314 public void pieceSwapCurrent() | |
315 { | |
56 | 316 if (!flagGameOver) |
317 { | |
318 Piece tmp = currPiece; | |
319 currPiece = nextPiece; | |
320 nextPiece = tmp; | |
321 board[currX][currY] = currPiece; | |
322 } | |
54 | 323 } |
324 | |
75 | 325 // Check one piece, set connections, find the new placement |
326 // based on piece rotations etc. | |
327 private boolean pieceCheck(Piece piece) | |
48 | 328 { |
53 | 329 if (piece == null) |
48 | 330 { |
331 // Create new piece | |
56 | 332 pieceCreateNew(); |
48 | 333 board[currX][currY] = currPiece; |
334 return true; | |
335 } | |
336 else | |
53 | 337 if (piece.getType() == PieceType.START) |
48 | 338 { |
339 if (currPiece != null) | |
340 { | |
341 // Hit center starting piece, game over | |
342 flagGameOver = true; | |
343 return true; | |
344 } | |
345 else | |
346 { | |
347 // Start piece as first piece means game is starting | |
348 pieceMoveTo(currPoint); | |
54 | 349 pieceCreateNew(); |
48 | 350 board[currX][currY] = currPiece; |
351 return true; | |
352 } | |
353 } | |
354 | |
355 // Mark the current piece as locked | |
53 | 356 piece.setType(PieceType.LOCKED); |
48 | 357 |
358 // Solve connection (with rotations) through the piece | |
53 | 359 currPoint = piece.getRotatedPoint(piece.getMatchingPoint(currPoint)); |
48 | 360 |
361 // Mark connection as active | |
53 | 362 piece.setConnectionState(currPoint, true); |
48 | 363 |
364 // Solve exit point (with rotations) | |
53 | 365 currPoint = piece.getAntiRotatedPoint(piece.getConnection(currPoint)); |
48 | 366 |
367 // Move to next position accordingly | |
368 pieceMoveTo(currPoint); | |
369 return false; | |
370 } | |
371 | |
75 | 372 // Finish one move/turn of the game, resolve path and find placement |
373 // of the next piece, or set "game over" state if required. | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
374 public void pieceFinishTurn() |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
375 { |
48 | 376 boolean finished = false; |
55 | 377 int connections = 0; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
378 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
379 if (currPiece != null) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
380 { |
75 | 381 G.smgr.play(sndPlaced); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
382 } |
55 | 383 |
48 | 384 while (!finished) |
7 | 385 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
386 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
27 | 387 { |
55 | 388 connections++; |
48 | 389 finished = pieceCheck(board[currX][currY]); |
27 | 390 } |
391 else | |
392 { | |
46 | 393 // Outside of the board, game over |
48 | 394 finished = true; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
395 flagGameOver = true; |
7 | 396 } |
397 } | |
53 | 398 |
66 | 399 // Compute and add score |
55 | 400 gameScore += connections * connections; |
401 | |
66 | 402 // If game over, clear the game |
53 | 403 if (flagGameOver) |
404 { | |
405 currPiece = null; | |
406 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
407 } |
66 | 408 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
409 public boolean mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
410 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
411 int notches = e.getWheelRotation(); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
412 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
413 if (notches < 0) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
414 pieceRotate(Piece.RotateDir.LEFT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
415 else |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
416 pieceRotate(Piece.RotateDir.RIGHT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
417 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
418 return true; |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
419 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
420 |
75 | 421 public void clicked() |
422 { | |
77
d17db72ac770
Fix the game continuing after gameover bug again due to refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
423 if (!flagGameOver) |
d17db72ac770
Fix the game continuing after gameover bug again due to refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
424 pieceFinishTurn(); |
75 | 425 } |
47 | 426 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
427 public boolean keyPressed(KeyEvent e) |
47 | 428 { |
54 | 429 if (flagGameOver) |
430 return false; | |
431 | |
47 | 432 switch (e.getKeyCode()) |
433 { | |
434 case KeyEvent.VK_LEFT: | |
435 case KeyEvent.VK_UP: | |
436 pieceRotate(Piece.RotateDir.LEFT); | |
437 return true; | |
438 | |
439 case KeyEvent.VK_RIGHT: | |
440 case KeyEvent.VK_DOWN: | |
441 pieceRotate(Piece.RotateDir.RIGHT); | |
442 return true; | |
443 | |
444 case KeyEvent.VK_ENTER: | |
445 pieceFinishTurn(); | |
446 return true; | |
447 } | |
448 return false; | |
449 } | |
1 | 450 } |
451 | |
452 | |
9 | 453 public class Engine extends JPanel |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
454 implements Runnable, KeyListener, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
455 MouseListener, MouseWheelListener |
1 | 456 { |
25 | 457 long startTime; |
458 float gameClock, gameFrames; | |
75 | 459 |
8 | 460 Thread animThread; |
461 boolean animEnable = false; | |
25 | 462 |
4 | 463 GameBoard lauta = null; |
69
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
464 InputStream musa; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
465 IDMContainer widgets; |
75 | 466 AboutBox aboutBox; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
467 |
9 | 468 public Engine() |
2 | 469 { |
25 | 470 // Initialize globals |
471 System.out.print("Engine() constructor\n"); | |
472 | |
37 | 473 // Sound system |
75 | 474 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 4); |
9 | 475 |
25 | 476 // Load resources |
4 | 477 try |
478 { | |
24 | 479 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
75 | 480 G.lautaBG = ImageIO.read(res.getStream()); |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
481 |
25 | 482 try { |
483 res = new ResourceLoader("graphics/font.ttf"); | |
75 | 484 |
55 | 485 |
75 | 486 G.fonts = new Font[G.numFonts]; |
487 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
488 G.fonts[1] = G.fonts[0].deriveFont(24f); | |
489 G.fonts[2] = G.fonts[0].deriveFont(64f); | |
76 | 490 G.fonts[3] = G.fonts[0].deriveFont(32f); |
75 | 491 |
25 | 492 } |
493 catch (FontFormatException e) | |
494 { | |
495 System.out.print("Could not initialize fonts.\n"); | |
496 } | |
37 | 497 |
69
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
498 res = new ResourceLoader("sounds/gamemusic.wav"); |
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
499 musa = res.getStream(); |
4 | 500 } |
501 catch (IOException e) | |
502 { | |
503 JOptionPane.showMessageDialog(null, | |
21 | 504 e.getMessage(), |
4 | 505 "Initialization error", |
506 JOptionPane.ERROR_MESSAGE); | |
37 | 507 |
21 | 508 System.out.print(e.getMessage()); |
18 | 509 } |
510 | |
52 | 511 // Create IDM GUI widgets |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
512 widgets = new IDMContainer(); |
52 | 513 |
75 | 514 widgets.add(new BtnSwapPiece(767f, 450f)); |
515 widgets.add(new BtnAbout (767f, 570f)); | |
516 widgets.add(new BtnNewGame (767f, 650f)); | |
50 | 517 |
75 | 518 lauta = new GameBoard(new IDMPoint(95, 130), 63); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
519 widgets.add(lauta); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
520 |
75 | 521 aboutBox = new AboutBox(); |
522 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
523 // Game |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
524 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
525 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
526 // Initialize event listeners |
18 | 527 addKeyListener(this); |
528 addMouseListener(this); | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
529 addMouseWheelListener(this); |
18 | 530 |
531 // Get initial focus | |
532 if (!hasFocus()) | |
533 { | |
534 System.out.print("Engine(): requesting focus\n"); | |
535 requestFocus(); | |
4 | 536 } |
37 | 537 |
75 | 538 // G.smgr.play(musa); |
8 | 539 } |
540 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
541 public void startNewGame() |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
542 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
543 gameClock = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
544 gameFrames = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
545 startTime = new Date().getTime(); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
546 lauta.startNewGame(); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
547 } |
25 | 548 |
549 public void paintComponent(Graphics g) | |
550 { | |
551 Graphics2D g2 = (Graphics2D) g; | |
552 | |
52 | 553 // Rescale if parent component size has changed |
25 | 554 Dimension dim = getSize(); |
75 | 555 if (G.screenDim == null || !dim.equals(G.screenDim)) |
25 | 556 { |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
557 // Rescale IDM GUI widgets |
75 | 558 widgets.setScale(dim.width / 1024.0f, dim.height / 768.0f); |
559 G.screenDim = dim; | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
560 |
52 | 561 // Rescale background image |
87
6afbf88c8bda
Change background image type from RGBA to RGB only, improves rendering speed.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
562 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); |
75 | 563 Graphics2D gimg = G.lautaBGScaled.createGraphics(); |
25 | 564 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
565 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
566 | |
75 | 567 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
568 lauta.paintBackPlate(gimg); |
25 | 569 |
570 System.out.print("scale changed\n"); | |
571 } | |
572 | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
573 // Get font metrics against current Graphics2D context |
75 | 574 if (G.metrics == null) |
575 { | |
576 G.metrics = new FontMetrics[G.numFonts]; | |
577 for (int i = 0; i < G.numFonts; i++) | |
578 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); | |
579 } | |
55 | 580 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
581 // Draw background image, pieces, widgets |
75 | 582 g2.drawImage(G.lautaBGScaled, 0, 0, null); |
86
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
583 |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
584 // Use antialiasing when rendering the game elements |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
585 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
586 RenderingHints.VALUE_ANTIALIAS_ON); |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
587 |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
588 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
589 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
590 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
591 widgets.paint(g2); |
55 | 592 |
593 if (!lauta.flagGameOver) | |
594 { | |
62
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
595 if (lauta.nextPiece != null) |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
596 { |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
597 // Draw next piece |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
598 AffineTransform save = g2.getTransform(); |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
599 lauta.nextPiece.paint(g2, 830, 325, 90); |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
600 g2.setTransform(save); |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
601 } |
55 | 602 } |
603 else | |
604 { | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
605 // Game over text |
55 | 606 String text = "Game Over!"; |
75 | 607 int textWidth = G.metrics[2].stringWidth(text); |
608 g2.setFont(G.fonts[2]); | |
55 | 609 |
610 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); | |
611 g2.drawString(text, (dim.width - textWidth) / 2 + 5, dim.height / 2 + 5); | |
612 | |
613 double f = Math.sin(gameClock * 0.1) * 4.0; | |
614 g2.setPaint(Color.white); | |
615 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); | |
616 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
617 |
75 | 618 // Score |
619 g2.setFont(G.fonts[2]); | |
25 | 620 g2.setPaint(Color.white); |
55 | 621 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220); |
622 | |
75 | 623 |
624 // Frames per second counter | |
625 g2.setFont(G.fonts[1]); | |
25 | 626 long currTime = new Date().getTime(); |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
627 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
628 |
75 | 629 gameFrames++; |
25 | 630 } |
631 | |
8 | 632 public void startThreads() |
633 { | |
9 | 634 System.out.print("startThreads()\n"); |
8 | 635 if (animThread == null) |
636 { | |
637 animThread = new Thread(this); | |
638 animEnable = true; | |
639 animThread.start(); | |
640 } | |
641 } | |
642 | |
643 public void stopThreads() | |
644 { | |
9 | 645 System.out.print("stopThreads()\n"); |
37 | 646 |
647 // Stop animations | |
8 | 648 if (animThread != null) |
649 { | |
650 animThread.interrupt(); | |
651 animEnable = false; | |
652 animThread = null; | |
653 } | |
22 | 654 |
37 | 655 // Shut down sound manager |
75 | 656 G.smgr.close(); |
2 | 657 } |
0 | 658 |
75 | 659 public void mouseEntered(MouseEvent e) |
660 { | |
661 widgets.mouseEntered(e); | |
662 } | |
663 public void mouseExited(MouseEvent e) | |
664 { | |
665 widgets.mouseExited(e); | |
666 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
667 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
668 public void mousePressed(MouseEvent e) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
669 { |
75 | 670 if (widgets.containsObject(aboutBox)) |
671 aboutBox.mousePressed(e); | |
672 else | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
673 widgets.mousePressed(e); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
674 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
675 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
676 public void mouseReleased(MouseEvent e) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
677 { |
75 | 678 if (widgets.containsObject(aboutBox)) |
679 aboutBox.mouseReleased(e); | |
680 else | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
681 widgets.mouseReleased(e); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
682 } |
18 | 683 |
684 public void mouseClicked(MouseEvent e) | |
685 { | |
686 if (!hasFocus()) | |
687 { | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
688 System.out.print("Requesting focus\n"); |
18 | 689 requestFocus(); |
690 } | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
691 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
692 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
693 public void mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
694 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
695 lauta.mouseWheelMoved(e); |
18 | 696 } |
697 | |
75 | 698 public void keyTyped(KeyEvent e) { } |
699 public void keyReleased(KeyEvent e) { } | |
1 | 700 |
9 | 701 public void keyPressed(KeyEvent e) |
2 | 702 { |
75 | 703 // Handle keyboard input |
704 if (widgets.containsObject(aboutBox)) | |
705 aboutBox.keyPressed(e); | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
706 else |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
707 widgets.keyPressed(e); |
2 | 708 } |
8 | 709 |
710 public void run() | |
711 { | |
712 while (animEnable) | |
713 { | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
714 // Progress game animation clock |
25 | 715 gameClock++; |
9 | 716 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
717 // Animate components |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
718 lauta.animate(gameClock); |
62
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
719 if (lauta.nextPiece != null) |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
720 lauta.nextPiece.animate(gameClock); |
9 | 721 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
722 // Repaint with a frame limiter |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
723 if (gameClock % 4 == 1) |
10 | 724 repaint(); |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
725 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
726 // Sleep for a moment |
9 | 727 try { |
728 Thread.sleep(10); | |
729 } | |
730 catch (InterruptedException x) { | |
731 } | |
8 | 732 } |
733 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
734 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
735 class BtnNewGame extends IDMButton |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
736 { |
51 | 737 public BtnNewGame(float x, float y) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
738 { |
75 | 739 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "New Game"); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
740 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
741 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
742 public void clicked() |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
743 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
744 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
745 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
746 } |
50 | 747 |
748 class BtnSwapPiece extends IDMButton | |
749 { | |
51 | 750 public BtnSwapPiece(float x, float y) |
50 | 751 { |
75 | 752 super(x, y, KeyEvent.VK_SPACE, G.fonts[1], "Swap"); |
50 | 753 } |
754 | |
755 public void clicked() | |
756 { | |
54 | 757 lauta.pieceSwapCurrent(); |
50 | 758 } |
759 } | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
760 |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
761 class BtnAbout extends IDMButton |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
762 { |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
763 public BtnAbout(float x, float y) |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
764 { |
75 | 765 super(x, y, KeyEvent.VK_A, G.fonts[1], "About"); |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
766 } |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
767 |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
768 public void clicked() |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
769 { |
75 | 770 if (!widgets.containsObject(aboutBox)) |
771 widgets.add(aboutBox); | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
772 } |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
773 } |
0 | 774 } |