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