Mercurial > hg > ristipolku
annotate game/Engine.java @ 46:3e8d1c30f573
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 22 Feb 2011 06:42:32 +0200 |
parents | 698c6bcc4aec |
children | 695cf13c103a |
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 |
1 | 21 class PathInfo |
22 { | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
23 public int inPoint, inX, inY, outPoint, outX, outY; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
24 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
25 public PathInfo(int inPoint, int inX, int inY, int outPoint, int outX, int outY) |
2 | 26 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
27 this.inPoint = inPoint; |
2 | 28 this.inX = inX; |
29 this.inY = inY; | |
30 | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
31 this.outPoint = outPoint; |
2 | 32 this.outX = outX; |
33 this.outY = outY; | |
34 } | |
27 | 35 |
36 public void print() | |
37 { | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
38 System.out.print("PathInfo: inP="+inPoint+", inX="+inX+", inY="+inY+"\n"); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
39 System.out.print(" outP="+outPoint+", outX="+outX+", outY="+outY+"\n\n"); |
27 | 40 } |
1 | 41 } |
0 | 42 |
8 | 43 /* |
44 class AnimatedElement | |
45 { | |
46 float x, y, stime, value; | |
47 Interpolate lerp; | |
48 boolean active; | |
49 | |
50 public AnimatedElement(float x, float y, ) | |
51 { | |
52 stime = 0; | |
53 this.x = x; | |
54 this.y = y; | |
55 | |
56 } | |
57 | |
58 public animate(float time) | |
59 { | |
60 if (!active) | |
61 { | |
62 active = true; | |
63 stime = time; | |
64 } | |
65 | |
66 float t = (time - stime) / 10.0f; | |
67 if (t < 100) | |
68 value = lerp.getValue(t); | |
69 else | |
70 { | |
71 | |
72 } | |
73 } | |
74 | |
75 public paint(Graphics2D g, ); | |
76 { | |
77 } | |
78 } | |
79 */ | |
0 | 80 |
30 | 81 class IDMWidget |
82 { | |
83 public IDMWidget() | |
84 { | |
85 } | |
86 | |
87 public void paint(Graphics2D g) | |
88 { | |
89 } | |
90 | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
91 public boolean contains(float x, float y) |
30 | 92 { |
93 return false; | |
94 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
95 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
96 public void clicked() |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
97 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
98 } |
30 | 99 } |
100 | |
101 class IDMButton | |
102 { | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
103 enum State { FOCUSED, PRESSED, NORMAL } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
104 State state; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
105 BufferedImage imgUp, imgPressed, img; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
106 Point pos; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
107 |
30 | 108 public IDMButton(float x, float y, String text) |
109 { | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
110 try |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
111 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
112 ResourceLoader res = new ResourceLoader("graphics/button1_up.png"); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
113 imgUp = ImageIO.read(res.getStream()); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
114 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
115 res = new ResourceLoader("graphics/button1_down.png"); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
116 imgPressed = ImageIO.read(res.getStream()); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
117 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
118 catch (IOException e) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
119 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
120 System.out.print(e.getMessage()); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
121 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
122 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
123 setState(State.NORMAL); |
30 | 124 } |
125 | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
126 private void setState(State newState) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
127 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
128 state = newState; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
129 if (state == State.PRESSED) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
130 img = imgPressed; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
131 else |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
132 img = imgUp; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
133 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
134 |
30 | 135 public void paint(Graphics2D g) |
136 { | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
137 g.drawImage(img, pos.x, pos.y, null); |
30 | 138 } |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
139 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
140 public boolean contains(float x, float y) |
30 | 141 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
142 return (x >= pos.x && y >= pos.y && |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
143 x < pos.x + img.getWidth() && |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
144 y < pos.y + img.getHeight()); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
145 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
146 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
147 public void clicked() |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
148 { |
30 | 149 } |
150 } | |
151 | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
152 |
1 | 153 class GameBoard |
0 | 154 { |
2 | 155 public static final int boardSize = 9; |
156 public static final int boardMiddle = 4; | |
157 Piece[][] board; | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
158 |
8 | 159 public boolean flagGameOver; |
7 | 160 |
41 | 161 Piece currPiece; |
162 int currX, currY, currPoint; | |
9 | 163 |
2 | 164 public GameBoard() |
165 { | |
166 board = new Piece[boardSize][boardSize]; | |
167 | |
168 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | |
169 | |
41 | 170 currX = boardMiddle; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
171 currY = boardMiddle; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
172 currPoint = 0; |
8 | 173 |
9 | 174 pieceFinishTurn(); |
8 | 175 |
176 flagGameOver = false; | |
2 | 177 } |
0 | 178 |
7 | 179 public void paint(Graphics2D g, int sx, int sy, float scale) |
2 | 180 { |
181 for (int y = 0; y < boardSize; y++) | |
182 for (int x = 0; x < boardSize; x++) | |
183 if (board[x][y] != null) | |
184 { | |
6 | 185 AffineTransform save = g.getTransform(); |
186 | |
4 | 187 board[x][y].paint(g, |
188 sx + (x * scale), | |
189 sy + (y * scale), | |
190 scale - scale / 10); | |
6 | 191 |
192 g.setTransform(save); | |
2 | 193 } |
194 } | |
9 | 195 |
196 public void animate(float time) | |
197 { | |
198 for (int y = 0; y < boardSize; y++) | |
199 for (int x = 0; x < boardSize; x++) | |
200 if (board[x][y] != null) | |
201 { | |
202 board[x][y].animate(time); | |
203 } | |
10 | 204 |
9 | 205 } |
1 | 206 |
2 | 207 private boolean isEmpty(int x, int y) |
208 { | |
209 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); | |
210 } | |
1 | 211 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
212 public void pieceRotate(Piece.RotateDir dir) |
2 | 213 { |
41 | 214 if (currPiece != null) |
215 currPiece.rotate(dir); | |
2 | 216 } |
1 | 217 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
218 private void pieceMoveTo(int point) |
7 | 219 { |
46 | 220 switch (point) |
221 { | |
222 case 0: currY--; break; | |
223 case 1: currY--; break; | |
224 | |
225 case 2: currX++; break; | |
226 case 3: currX++; break; | |
227 | |
228 case 4: currY++; break; | |
229 case 5: currY++; break; | |
230 | |
231 case 6: currX--; break; | |
232 case 7: currX--; break; | |
233 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
234 } |
9 | 235 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
236 public void pieceFinishTurn() |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
237 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
238 while (true) |
7 | 239 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
240 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
27 | 241 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
242 Piece curr = board[currX][currY]; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
243 |
27 | 244 if (curr == null) |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
245 { |
46 | 246 // Create new piece |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
247 currPiece = new Piece(PieceType.ACTIVE); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
248 board[currX][currY] = currPiece; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
249 return; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
250 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
251 else |
27 | 252 if (curr.getType() == PieceType.START) |
253 { | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
254 if (currPiece != null) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
255 { |
46 | 256 // Hit center starting piece, game over |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
257 flagGameOver = true; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
258 System.out.print("GameOver!\n"); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
259 break; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
260 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
261 else |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
262 { |
44 | 263 // Start piece as first piece means game is starting |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
264 pieceMoveTo(currPoint); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
265 currPiece = new Piece(PieceType.ACTIVE); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
266 board[currX][currY] = currPiece; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
267 return; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
268 } |
27 | 269 } |
270 else | |
271 { | |
43 | 272 // Mark the current piece as locked |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
273 curr.setType(PieceType.LOCKED); |
43 | 274 |
275 // Solve connection (with rotations) through the piece | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
276 currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint)); |
43 | 277 |
278 // Mark connection as active | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
279 curr.setConnectionState(currPoint, true); |
27 | 280 |
43 | 281 // Solve exit point (with rotations) |
282 currPoint = curr.getAntiRotatedPoint(curr.getConnection(currPoint)); | |
283 | |
284 // Move to next position accordingly | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
285 pieceMoveTo(currPoint); |
27 | 286 } |
287 } | |
288 else | |
289 { | |
46 | 290 // Outside of the board, game over |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
291 flagGameOver = true; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
292 System.out.print("GameOver!\n"); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
293 break; |
7 | 294 } |
295 } | |
296 | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
297 } |
1 | 298 } |
299 | |
300 | |
9 | 301 public class Engine extends JPanel |
18 | 302 implements Runnable, KeyListener, MouseListener |
1 | 303 { |
25 | 304 long startTime; |
305 float gameClock, gameFrames; | |
8 | 306 Thread animThread; |
307 boolean animEnable = false; | |
25 | 308 |
309 Font fontMain, font1, font2; | |
4 | 310 GameBoard lauta = null; |
9 | 311 BufferedImage lautaBG = null, lautaBGScaled = null; |
25 | 312 Dimension lautaDim; |
22 | 313 |
25 | 314 |
37 | 315 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); |
316 SoundManager smgr; | |
317 Sound musa, placed; | |
1 | 318 |
9 | 319 public Engine() |
2 | 320 { |
25 | 321 // Initialize globals |
322 System.out.print("Engine() constructor\n"); | |
323 | |
324 gameClock = 0; | |
325 gameFrames = 0; | |
326 startTime = new Date().getTime(); | |
4 | 327 |
37 | 328 // Sound system |
329 smgr = new SoundManager(sfmt, 16); | |
9 | 330 |
25 | 331 // Load resources |
4 | 332 try |
333 { | |
24 | 334 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
18 | 335 lautaBG = ImageIO.read(res.getStream()); |
21 | 336 |
25 | 337 try { |
338 res = new ResourceLoader("graphics/font.ttf"); | |
339 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
340 font1 = fontMain.deriveFont(24f); | |
341 font2 = fontMain.deriveFont(32f); | |
342 } | |
343 catch (FontFormatException e) | |
344 { | |
345 System.out.print("Could not initialize fonts.\n"); | |
346 } | |
37 | 347 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
348 // musa = smgr.getSound("sounds/gamemusic.wav"); |
37 | 349 // placed = smgr.getSound("sounds/placed.wav"); |
4 | 350 } |
351 catch (IOException e) | |
352 { | |
353 JOptionPane.showMessageDialog(null, | |
21 | 354 e.getMessage(), |
4 | 355 "Initialization error", |
356 JOptionPane.ERROR_MESSAGE); | |
37 | 357 |
21 | 358 System.out.print(e.getMessage()); |
18 | 359 } |
360 | |
37 | 361 // Initialize game components |
18 | 362 lauta = new GameBoard(); |
363 addKeyListener(this); | |
364 addMouseListener(this); | |
365 | |
366 // Get initial focus | |
367 if (!hasFocus()) | |
368 { | |
369 System.out.print("Engine(): requesting focus\n"); | |
370 requestFocus(); | |
4 | 371 } |
37 | 372 |
373 smgr.play(musa); | |
8 | 374 } |
375 | |
25 | 376 |
377 public void paintComponent(Graphics g) | |
378 { | |
379 Graphics2D g2 = (Graphics2D) g; | |
380 | |
381 // Use antialiasing when rendering the game elements | |
382 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
383 RenderingHints.VALUE_ANTIALIAS_ON); | |
384 | |
385 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | |
386 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
387 | |
388 // Rescale background if component size has changed | |
389 Dimension dim = getSize(); | |
390 if (lautaDim == null || !dim.equals(lautaDim)) | |
391 { | |
392 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); | |
393 Graphics2D gimg = lautaBGScaled.createGraphics(); | |
394 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | |
395 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
396 | |
397 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); | |
398 lautaDim = dim; | |
399 | |
400 System.out.print("scale changed\n"); | |
401 } | |
402 | |
403 // Background, pieces | |
404 g2.drawImage(lautaBGScaled, 0, 0, null); | |
405 lauta.paint(g2, 100, 150, 60); | |
406 | |
407 // Scores | |
408 g2.setFont(font1); | |
409 g2.setPaint(Color.white); | |
410 | |
411 | |
412 // Other elements | |
413 long currTime = new Date().getTime(); | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
414 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
25 | 415 } |
416 | |
8 | 417 public void startThreads() |
418 { | |
9 | 419 System.out.print("startThreads()\n"); |
8 | 420 if (animThread == null) |
421 { | |
422 animThread = new Thread(this); | |
423 animEnable = true; | |
424 animThread.start(); | |
425 } | |
426 } | |
427 | |
428 public void stopThreads() | |
429 { | |
9 | 430 System.out.print("stopThreads()\n"); |
37 | 431 |
432 // Stop animations | |
8 | 433 if (animThread != null) |
434 { | |
435 animThread.interrupt(); | |
436 animEnable = false; | |
437 animThread = null; | |
438 } | |
22 | 439 |
37 | 440 // Shut down sound manager |
441 smgr.close(); | |
2 | 442 } |
0 | 443 |
18 | 444 public void mousePressed(MouseEvent e) { } |
445 public void mouseEntered(MouseEvent e) { } | |
446 public void mouseExited(MouseEvent e) { } | |
447 public void mouseReleased(MouseEvent e) { } | |
448 | |
449 public void mouseClicked(MouseEvent e) | |
450 { | |
451 System.out.print("mouseClicked()\n"); | |
452 if (!hasFocus()) | |
453 { | |
454 System.out.print("requesting focus\n"); | |
455 requestFocus(); | |
456 } | |
457 } | |
458 | |
9 | 459 public void keyTyped(KeyEvent e) |
2 | 460 { |
461 } | |
1 | 462 |
2 | 463 public void keyReleased(KeyEvent e) |
464 { | |
465 } | |
1 | 466 |
9 | 467 public void keyPressed(KeyEvent e) |
2 | 468 { |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
469 // Handle keyboard input |
7 | 470 switch (e.getKeyCode()) |
2 | 471 { |
7 | 472 case KeyEvent.VK_LEFT: |
473 case KeyEvent.VK_UP: | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
474 lauta.pieceRotate(Piece.RotateDir.LEFT); |
2 | 475 break; |
0 | 476 |
7 | 477 case KeyEvent.VK_RIGHT: |
478 case KeyEvent.VK_DOWN: | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
479 lauta.pieceRotate(Piece.RotateDir.RIGHT); |
2 | 480 break; |
481 | |
7 | 482 case KeyEvent.VK_ENTER: |
37 | 483 smgr.play(placed); |
2 | 484 lauta.pieceFinishTurn(); |
485 break; | |
35 | 486 |
487 case KeyEvent.VK_ESCAPE: | |
488 break; | |
2 | 489 } |
490 } | |
8 | 491 |
492 public void run() | |
493 { | |
494 while (animEnable) | |
495 { | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
496 // Progress game animation clock |
25 | 497 gameClock++; |
9 | 498 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
499 // Animate components |
25 | 500 lauta.animate(gameClock); |
9 | 501 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
502 // Repaint with a frame limiter |
25 | 503 if (gameClock % 3 == 1) |
24 | 504 { |
10 | 505 repaint(); |
25 | 506 gameFrames++; |
24 | 507 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
508 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
509 // Sleep for a moment |
9 | 510 try { |
511 Thread.sleep(10); | |
512 } | |
513 catch (InterruptedException x) { | |
514 } | |
8 | 515 } |
516 } | |
0 | 517 } |