Mercurial > hg > ristipolku
annotate game/Engine.java @ 108:ae66d21d4747 dev-0_70
Disable piece placement sound.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Mar 2011 22:29:54 +0200 |
parents | 41c6cca69d60 |
children | 8c3bc197310d |
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); |
105
75015dfd47ef
Move version number info to G.java.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
123 drawString(g, "RistiPolku (CrossPaths) v"+ G.version +"\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 |
108
ae66d21d4747
Disable piece placement sound.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
218 // sndPlaced = G.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; | |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
311 currPiece.changed(); |
54 | 312 nextPiece = new Piece(PieceType.ACTIVE); |
313 } | |
314 | |
315 public void pieceSwapCurrent() | |
316 { | |
56 | 317 if (!flagGameOver) |
318 { | |
319 Piece tmp = currPiece; | |
320 currPiece = nextPiece; | |
321 nextPiece = tmp; | |
322 board[currX][currY] = currPiece; | |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
323 currPiece.changed(); |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
324 nextPiece.changed(); |
56 | 325 } |
54 | 326 } |
327 | |
75 | 328 // Check one piece, set connections, find the new placement |
329 // based on piece rotations etc. | |
330 private boolean pieceCheck(Piece piece) | |
48 | 331 { |
53 | 332 if (piece == null) |
48 | 333 { |
334 // Create new piece | |
56 | 335 pieceCreateNew(); |
48 | 336 board[currX][currY] = currPiece; |
337 return true; | |
338 } | |
339 else | |
53 | 340 if (piece.getType() == PieceType.START) |
48 | 341 { |
342 if (currPiece != null) | |
343 { | |
344 // Hit center starting piece, game over | |
345 flagGameOver = true; | |
346 return true; | |
347 } | |
348 else | |
349 { | |
350 // Start piece as first piece means game is starting | |
351 pieceMoveTo(currPoint); | |
54 | 352 pieceCreateNew(); |
48 | 353 board[currX][currY] = currPiece; |
354 return true; | |
355 } | |
356 } | |
357 | |
358 // Mark the current piece as locked | |
53 | 359 piece.setType(PieceType.LOCKED); |
48 | 360 |
361 // Solve connection (with rotations) through the piece | |
53 | 362 currPoint = piece.getRotatedPoint(piece.getMatchingPoint(currPoint)); |
48 | 363 |
364 // Mark connection as active | |
53 | 365 piece.setConnectionState(currPoint, true); |
48 | 366 |
367 // Solve exit point (with rotations) | |
53 | 368 currPoint = piece.getAntiRotatedPoint(piece.getConnection(currPoint)); |
48 | 369 |
370 // Move to next position accordingly | |
371 pieceMoveTo(currPoint); | |
372 return false; | |
373 } | |
374 | |
75 | 375 // Finish one move/turn of the game, resolve path and find placement |
376 // 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
|
377 public void pieceFinishTurn() |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
378 { |
48 | 379 boolean finished = false; |
55 | 380 int connections = 0; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
381 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
382 if (currPiece != null) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
383 { |
75 | 384 G.smgr.play(sndPlaced); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
385 } |
55 | 386 |
48 | 387 while (!finished) |
7 | 388 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
389 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
27 | 390 { |
55 | 391 connections++; |
48 | 392 finished = pieceCheck(board[currX][currY]); |
27 | 393 } |
394 else | |
395 { | |
46 | 396 // Outside of the board, game over |
48 | 397 finished = true; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
398 flagGameOver = true; |
7 | 399 } |
400 } | |
53 | 401 |
66 | 402 // Compute and add score |
55 | 403 gameScore += connections * connections; |
404 | |
66 | 405 // If game over, clear the game |
53 | 406 if (flagGameOver) |
407 { | |
408 currPiece = null; | |
409 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
410 } |
66 | 411 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
412 public boolean mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
413 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
414 int notches = e.getWheelRotation(); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
415 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
416 if (notches < 0) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
417 pieceRotate(Piece.RotateDir.LEFT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
418 else |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
419 pieceRotate(Piece.RotateDir.RIGHT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
420 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
421 return true; |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
422 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
423 |
75 | 424 public void clicked() |
425 { | |
77
d17db72ac770
Fix the game continuing after gameover bug again due to refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
426 if (!flagGameOver) |
d17db72ac770
Fix the game continuing after gameover bug again due to refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
427 pieceFinishTurn(); |
75 | 428 } |
47 | 429 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
430 public boolean keyPressed(KeyEvent e) |
47 | 431 { |
54 | 432 if (flagGameOver) |
433 return false; | |
434 | |
47 | 435 switch (e.getKeyCode()) |
436 { | |
437 case KeyEvent.VK_LEFT: | |
438 case KeyEvent.VK_UP: | |
439 pieceRotate(Piece.RotateDir.LEFT); | |
440 return true; | |
441 | |
442 case KeyEvent.VK_RIGHT: | |
443 case KeyEvent.VK_DOWN: | |
444 pieceRotate(Piece.RotateDir.RIGHT); | |
445 return true; | |
446 | |
447 case KeyEvent.VK_ENTER: | |
448 pieceFinishTurn(); | |
449 return true; | |
450 } | |
451 return false; | |
452 } | |
1 | 453 } |
454 | |
455 | |
9 | 456 public class Engine extends JPanel |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
457 implements Runnable, KeyListener, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
458 MouseListener, MouseWheelListener |
1 | 459 { |
25 | 460 long startTime; |
461 float gameClock, gameFrames; | |
75 | 462 |
8 | 463 Thread animThread; |
464 boolean animEnable = false; | |
25 | 465 |
4 | 466 GameBoard lauta = null; |
69
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
467 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
|
468 IDMContainer widgets; |
75 | 469 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
|
470 |
9 | 471 public Engine() |
2 | 472 { |
25 | 473 // Initialize globals |
474 System.out.print("Engine() constructor\n"); | |
475 | |
37 | 476 // Sound system |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
477 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 1); |
9 | 478 |
25 | 479 // Load resources |
4 | 480 try |
481 { | |
24 | 482 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
75 | 483 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
|
484 |
25 | 485 try { |
486 res = new ResourceLoader("graphics/font.ttf"); | |
75 | 487 |
55 | 488 |
75 | 489 G.fonts = new Font[G.numFonts]; |
490 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
491 G.fonts[1] = G.fonts[0].deriveFont(24f); | |
492 G.fonts[2] = G.fonts[0].deriveFont(64f); | |
76 | 493 G.fonts[3] = G.fonts[0].deriveFont(32f); |
75 | 494 |
25 | 495 } |
496 catch (FontFormatException e) | |
497 { | |
498 System.out.print("Could not initialize fonts.\n"); | |
499 } | |
37 | 500 |
69
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
501 res = new ResourceLoader("sounds/gamemusic.wav"); |
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
502 musa = res.getStream(); |
4 | 503 } |
504 catch (IOException e) | |
505 { | |
506 JOptionPane.showMessageDialog(null, | |
21 | 507 e.getMessage(), |
4 | 508 "Initialization error", |
509 JOptionPane.ERROR_MESSAGE); | |
37 | 510 |
21 | 511 System.out.print(e.getMessage()); |
18 | 512 } |
513 | |
52 | 514 // 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
|
515 widgets = new IDMContainer(); |
52 | 516 |
75 | 517 widgets.add(new BtnSwapPiece(767f, 450f)); |
518 widgets.add(new BtnAbout (767f, 570f)); | |
519 widgets.add(new BtnNewGame (767f, 650f)); | |
50 | 520 |
75 | 521 lauta = new GameBoard(new IDMPoint(95, 130), 63); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
522 widgets.add(lauta); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
523 |
75 | 524 aboutBox = new AboutBox(); |
525 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
526 // Game |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
527 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
528 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
529 // Initialize event listeners |
18 | 530 addKeyListener(this); |
531 addMouseListener(this); | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
532 addMouseWheelListener(this); |
18 | 533 |
93 | 534 // Start playing background music |
535 G.smgr.play(musa); | |
536 | |
18 | 537 // Get initial focus |
538 if (!hasFocus()) | |
539 { | |
540 System.out.print("Engine(): requesting focus\n"); | |
541 requestFocus(); | |
4 | 542 } |
93 | 543 |
8 | 544 } |
545 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
546 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
|
547 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
548 gameClock = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
549 gameFrames = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
550 startTime = new Date().getTime(); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
551 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
|
552 } |
25 | 553 |
554 public void paintComponent(Graphics g) | |
555 { | |
556 Graphics2D g2 = (Graphics2D) g; | |
557 | |
52 | 558 // Rescale if parent component size has changed |
25 | 559 Dimension dim = getSize(); |
75 | 560 if (G.screenDim == null || !dim.equals(G.screenDim)) |
25 | 561 { |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
562 // Rescale IDM GUI widgets |
75 | 563 widgets.setScale(dim.width / 1024.0f, dim.height / 768.0f); |
564 G.screenDim = dim; | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
565 |
52 | 566 // 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
|
567 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); |
75 | 568 Graphics2D gimg = G.lautaBGScaled.createGraphics(); |
25 | 569 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
570 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
571 | |
75 | 572 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
|
573 lauta.paintBackPlate(gimg); |
25 | 574 |
575 System.out.print("scale changed\n"); | |
576 } | |
577 | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
578 // Get font metrics against current Graphics2D context |
75 | 579 if (G.metrics == null) |
580 { | |
581 G.metrics = new FontMetrics[G.numFonts]; | |
582 for (int i = 0; i < G.numFonts; i++) | |
583 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); | |
584 } | |
55 | 585 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
586 // Draw background image, pieces, widgets |
75 | 587 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
|
588 |
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 // 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
|
590 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
|
591 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
|
592 |
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
|
593 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
|
594 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
|
595 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
596 widgets.paint(g2); |
55 | 597 |
598 if (!lauta.flagGameOver) | |
599 { | |
62
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
600 if (lauta.nextPiece != null) |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
601 { |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
602 // Draw next piece |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
603 AffineTransform save = g2.getTransform(); |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
604 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
|
605 g2.setTransform(save); |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
606 } |
55 | 607 } |
608 else | |
609 { | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
610 // Game over text |
55 | 611 String text = "Game Over!"; |
75 | 612 int textWidth = G.metrics[2].stringWidth(text); |
613 g2.setFont(G.fonts[2]); | |
55 | 614 |
615 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); | |
616 g2.drawString(text, (dim.width - textWidth) / 2 + 5, dim.height / 2 + 5); | |
617 | |
618 double f = Math.sin(gameClock * 0.1) * 4.0; | |
619 g2.setPaint(Color.white); | |
620 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); | |
621 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
622 |
75 | 623 // Score |
624 g2.setFont(G.fonts[2]); | |
25 | 625 g2.setPaint(Color.white); |
55 | 626 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220); |
627 | |
75 | 628 |
629 // Frames per second counter | |
630 g2.setFont(G.fonts[1]); | |
25 | 631 long currTime = new Date().getTime(); |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
632 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
|
633 |
75 | 634 gameFrames++; |
25 | 635 } |
636 | |
8 | 637 public void startThreads() |
638 { | |
9 | 639 System.out.print("startThreads()\n"); |
8 | 640 if (animThread == null) |
641 { | |
642 animThread = new Thread(this); | |
643 animEnable = true; | |
644 animThread.start(); | |
645 } | |
646 } | |
647 | |
648 public void stopThreads() | |
649 { | |
9 | 650 System.out.print("stopThreads()\n"); |
37 | 651 |
652 // Stop animations | |
8 | 653 if (animThread != null) |
654 { | |
655 animThread.interrupt(); | |
656 animEnable = false; | |
657 animThread = null; | |
658 } | |
22 | 659 |
37 | 660 // Shut down sound manager |
75 | 661 G.smgr.close(); |
2 | 662 } |
0 | 663 |
75 | 664 public void mouseEntered(MouseEvent e) |
665 { | |
666 widgets.mouseEntered(e); | |
667 } | |
668 public void mouseExited(MouseEvent e) | |
669 { | |
670 widgets.mouseExited(e); | |
671 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
672 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
673 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
|
674 { |
75 | 675 if (widgets.containsObject(aboutBox)) |
676 aboutBox.mousePressed(e); | |
677 else | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
678 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
|
679 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
680 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
681 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
|
682 { |
75 | 683 if (widgets.containsObject(aboutBox)) |
684 aboutBox.mouseReleased(e); | |
685 else | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
686 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
|
687 } |
18 | 688 |
689 public void mouseClicked(MouseEvent e) | |
690 { | |
691 if (!hasFocus()) | |
692 { | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
693 System.out.print("Requesting focus\n"); |
18 | 694 requestFocus(); |
695 } | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
696 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
697 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
698 public void mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
699 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
700 lauta.mouseWheelMoved(e); |
18 | 701 } |
702 | |
75 | 703 public void keyTyped(KeyEvent e) { } |
704 public void keyReleased(KeyEvent e) { } | |
1 | 705 |
9 | 706 public void keyPressed(KeyEvent e) |
2 | 707 { |
75 | 708 // Handle keyboard input |
709 if (widgets.containsObject(aboutBox)) | |
710 aboutBox.keyPressed(e); | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
711 else |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
712 widgets.keyPressed(e); |
2 | 713 } |
8 | 714 |
715 public void run() | |
716 { | |
717 while (animEnable) | |
718 { | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
719 // Progress game animation clock |
25 | 720 gameClock++; |
9 | 721 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
722 // Animate components |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
723 lauta.animate(gameClock); |
62
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
724 if (lauta.nextPiece != null) |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
725 lauta.nextPiece.animate(gameClock); |
9 | 726 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
727 // Repaint with a frame limiter |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
728 if (gameClock % 4 == 1) |
10 | 729 repaint(); |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
730 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
731 // Sleep for a moment |
9 | 732 try { |
733 Thread.sleep(10); | |
734 } | |
735 catch (InterruptedException x) { | |
736 } | |
8 | 737 } |
738 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
739 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
740 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
|
741 { |
51 | 742 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
|
743 { |
75 | 744 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
|
745 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
746 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
747 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
|
748 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
749 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
750 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
751 } |
50 | 752 |
753 class BtnSwapPiece extends IDMButton | |
754 { | |
51 | 755 public BtnSwapPiece(float x, float y) |
50 | 756 { |
75 | 757 super(x, y, KeyEvent.VK_SPACE, G.fonts[1], "Swap"); |
50 | 758 } |
759 | |
760 public void clicked() | |
761 { | |
54 | 762 lauta.pieceSwapCurrent(); |
50 | 763 } |
764 } | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
765 |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
766 class BtnAbout extends IDMButton |
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 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
|
769 { |
75 | 770 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
|
771 } |
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 public void clicked() |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
774 { |
75 | 775 if (!widgets.containsObject(aboutBox)) |
776 widgets.add(aboutBox); | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
777 } |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
778 } |
0 | 779 } |