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