Mercurial > hg > ristipolku
annotate game/Engine.java @ 47:695cf13c103a
Move some code.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 22 Feb 2011 07:19:26 +0200 |
parents | 3e8d1c30f573 |
children | f13bab4cccd3 |
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 |
47 | 164 SoundManager soundManager; |
165 Sound sndPlaced; | |
166 | |
167 public GameBoard(SoundManager smgr) | |
2 | 168 { |
169 board = new Piece[boardSize][boardSize]; | |
170 | |
171 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | |
172 | |
41 | 173 currX = boardMiddle; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
174 currY = boardMiddle; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
175 currPoint = 0; |
8 | 176 |
9 | 177 pieceFinishTurn(); |
8 | 178 |
179 flagGameOver = false; | |
47 | 180 |
181 soundManager = smgr; | |
182 sndPlaced = soundManager.getSound("sounds/placed.wav"); | |
2 | 183 } |
0 | 184 |
7 | 185 public void paint(Graphics2D g, int sx, int sy, float scale) |
2 | 186 { |
187 for (int y = 0; y < boardSize; y++) | |
188 for (int x = 0; x < boardSize; x++) | |
189 if (board[x][y] != null) | |
190 { | |
6 | 191 AffineTransform save = g.getTransform(); |
192 | |
4 | 193 board[x][y].paint(g, |
194 sx + (x * scale), | |
195 sy + (y * scale), | |
196 scale - scale / 10); | |
6 | 197 |
198 g.setTransform(save); | |
2 | 199 } |
200 } | |
9 | 201 |
202 public void animate(float time) | |
203 { | |
204 for (int y = 0; y < boardSize; y++) | |
205 for (int x = 0; x < boardSize; x++) | |
206 if (board[x][y] != null) | |
207 { | |
208 board[x][y].animate(time); | |
209 } | |
10 | 210 |
9 | 211 } |
1 | 212 |
2 | 213 private boolean isEmpty(int x, int y) |
214 { | |
47 | 215 return (x >= 0 && x < boardSize && y >= 0 && |
216 y < boardSize && board[x][y] == null); | |
2 | 217 } |
1 | 218 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
219 public void pieceRotate(Piece.RotateDir dir) |
2 | 220 { |
41 | 221 if (currPiece != null) |
222 currPiece.rotate(dir); | |
2 | 223 } |
1 | 224 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
225 private void pieceMoveTo(int point) |
7 | 226 { |
46 | 227 switch (point) |
228 { | |
229 case 0: currY--; break; | |
230 case 1: currY--; break; | |
231 | |
232 case 2: currX++; break; | |
233 case 3: currX++; break; | |
234 | |
235 case 4: currY++; break; | |
236 case 5: currY++; break; | |
237 | |
238 case 6: currX--; break; | |
239 case 7: currX--; break; | |
240 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
241 } |
9 | 242 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
243 public void pieceFinishTurn() |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
244 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
245 while (true) |
7 | 246 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
247 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
27 | 248 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
249 Piece curr = board[currX][currY]; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
250 |
27 | 251 if (curr == null) |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
252 { |
46 | 253 // Create new piece |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
254 currPiece = new Piece(PieceType.ACTIVE); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
255 board[currX][currY] = currPiece; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
256 return; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
257 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
258 else |
27 | 259 if (curr.getType() == PieceType.START) |
260 { | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
261 if (currPiece != null) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
262 { |
46 | 263 // Hit center starting piece, game over |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
264 flagGameOver = true; |
47 | 265 currPiece = null; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
266 System.out.print("GameOver!\n"); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
267 break; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
268 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
269 else |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
270 { |
44 | 271 // 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
|
272 pieceMoveTo(currPoint); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
273 currPiece = new Piece(PieceType.ACTIVE); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
274 board[currX][currY] = currPiece; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
275 return; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
276 } |
27 | 277 } |
278 else | |
279 { | |
43 | 280 // Mark the current piece as locked |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
281 curr.setType(PieceType.LOCKED); |
43 | 282 |
283 // Solve connection (with rotations) through the piece | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
284 currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint)); |
43 | 285 |
286 // Mark connection as active | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
287 curr.setConnectionState(currPoint, true); |
27 | 288 |
43 | 289 // Solve exit point (with rotations) |
290 currPoint = curr.getAntiRotatedPoint(curr.getConnection(currPoint)); | |
291 | |
292 // Move to next position accordingly | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
293 pieceMoveTo(currPoint); |
27 | 294 } |
295 } | |
296 else | |
297 { | |
46 | 298 // Outside of the board, game over |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
299 flagGameOver = true; |
47 | 300 currPiece = null; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
301 System.out.print("GameOver!\n"); |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
302 break; |
7 | 303 } |
304 } | |
305 | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
306 } |
47 | 307 |
308 public boolean keyHandler(KeyEvent e) | |
309 { | |
310 switch (e.getKeyCode()) | |
311 { | |
312 case KeyEvent.VK_LEFT: | |
313 case KeyEvent.VK_UP: | |
314 pieceRotate(Piece.RotateDir.LEFT); | |
315 return true; | |
316 | |
317 case KeyEvent.VK_RIGHT: | |
318 case KeyEvent.VK_DOWN: | |
319 pieceRotate(Piece.RotateDir.RIGHT); | |
320 return true; | |
321 | |
322 case KeyEvent.VK_ENTER: | |
323 soundManager.play(sndPlaced); | |
324 pieceFinishTurn(); | |
325 return true; | |
326 } | |
327 return false; | |
328 } | |
1 | 329 } |
330 | |
331 | |
9 | 332 public class Engine extends JPanel |
18 | 333 implements Runnable, KeyListener, MouseListener |
1 | 334 { |
25 | 335 long startTime; |
336 float gameClock, gameFrames; | |
8 | 337 Thread animThread; |
338 boolean animEnable = false; | |
25 | 339 |
340 Font fontMain, font1, font2; | |
4 | 341 GameBoard lauta = null; |
9 | 342 BufferedImage lautaBG = null, lautaBGScaled = null; |
25 | 343 Dimension lautaDim; |
22 | 344 |
25 | 345 |
37 | 346 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); |
47 | 347 SoundManager soundManager; |
348 Sound musa; | |
1 | 349 |
9 | 350 public Engine() |
2 | 351 { |
25 | 352 // Initialize globals |
353 System.out.print("Engine() constructor\n"); | |
354 | |
355 gameClock = 0; | |
356 gameFrames = 0; | |
357 startTime = new Date().getTime(); | |
4 | 358 |
37 | 359 // Sound system |
47 | 360 soundManager = new SoundManager(sfmt, 16); |
9 | 361 |
25 | 362 // Load resources |
4 | 363 try |
364 { | |
24 | 365 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
18 | 366 lautaBG = ImageIO.read(res.getStream()); |
21 | 367 |
25 | 368 try { |
369 res = new ResourceLoader("graphics/font.ttf"); | |
370 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
371 font1 = fontMain.deriveFont(24f); | |
372 font2 = fontMain.deriveFont(32f); | |
373 } | |
374 catch (FontFormatException e) | |
375 { | |
376 System.out.print("Could not initialize fonts.\n"); | |
377 } | |
37 | 378 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
379 // musa = smgr.getSound("sounds/gamemusic.wav"); |
4 | 380 } |
381 catch (IOException e) | |
382 { | |
383 JOptionPane.showMessageDialog(null, | |
21 | 384 e.getMessage(), |
4 | 385 "Initialization error", |
386 JOptionPane.ERROR_MESSAGE); | |
37 | 387 |
21 | 388 System.out.print(e.getMessage()); |
18 | 389 } |
390 | |
37 | 391 // Initialize game components |
47 | 392 lauta = new GameBoard(soundManager); |
18 | 393 addKeyListener(this); |
394 addMouseListener(this); | |
395 | |
396 // Get initial focus | |
397 if (!hasFocus()) | |
398 { | |
399 System.out.print("Engine(): requesting focus\n"); | |
400 requestFocus(); | |
4 | 401 } |
37 | 402 |
47 | 403 soundManager.play(musa); |
8 | 404 } |
405 | |
25 | 406 |
407 public void paintComponent(Graphics g) | |
408 { | |
409 Graphics2D g2 = (Graphics2D) g; | |
410 | |
411 // Use antialiasing when rendering the game elements | |
412 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
413 RenderingHints.VALUE_ANTIALIAS_ON); | |
414 | |
415 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | |
416 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
417 | |
418 // Rescale background if component size has changed | |
419 Dimension dim = getSize(); | |
420 if (lautaDim == null || !dim.equals(lautaDim)) | |
421 { | |
422 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); | |
423 Graphics2D gimg = lautaBGScaled.createGraphics(); | |
424 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | |
425 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
426 | |
427 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); | |
428 lautaDim = dim; | |
429 | |
430 System.out.print("scale changed\n"); | |
431 } | |
432 | |
433 // Background, pieces | |
434 g2.drawImage(lautaBGScaled, 0, 0, null); | |
435 lauta.paint(g2, 100, 150, 60); | |
436 | |
437 // Scores | |
438 g2.setFont(font1); | |
439 g2.setPaint(Color.white); | |
440 | |
441 | |
442 // Other elements | |
443 long currTime = new Date().getTime(); | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
444 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
25 | 445 } |
446 | |
8 | 447 public void startThreads() |
448 { | |
9 | 449 System.out.print("startThreads()\n"); |
8 | 450 if (animThread == null) |
451 { | |
452 animThread = new Thread(this); | |
453 animEnable = true; | |
454 animThread.start(); | |
455 } | |
456 } | |
457 | |
458 public void stopThreads() | |
459 { | |
9 | 460 System.out.print("stopThreads()\n"); |
37 | 461 |
462 // Stop animations | |
8 | 463 if (animThread != null) |
464 { | |
465 animThread.interrupt(); | |
466 animEnable = false; | |
467 animThread = null; | |
468 } | |
22 | 469 |
37 | 470 // Shut down sound manager |
47 | 471 soundManager.close(); |
2 | 472 } |
0 | 473 |
18 | 474 public void mousePressed(MouseEvent e) { } |
475 public void mouseEntered(MouseEvent e) { } | |
476 public void mouseExited(MouseEvent e) { } | |
477 public void mouseReleased(MouseEvent e) { } | |
478 | |
479 public void mouseClicked(MouseEvent e) | |
480 { | |
481 System.out.print("mouseClicked()\n"); | |
482 if (!hasFocus()) | |
483 { | |
484 System.out.print("requesting focus\n"); | |
485 requestFocus(); | |
486 } | |
487 } | |
488 | |
9 | 489 public void keyTyped(KeyEvent e) |
2 | 490 { |
491 } | |
1 | 492 |
2 | 493 public void keyReleased(KeyEvent e) |
494 { | |
495 } | |
1 | 496 |
9 | 497 public void keyPressed(KeyEvent e) |
2 | 498 { |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
499 // Handle keyboard input |
47 | 500 if (lauta.keyHandler(e)) |
501 return; | |
502 | |
7 | 503 switch (e.getKeyCode()) |
2 | 504 { |
35 | 505 case KeyEvent.VK_ESCAPE: |
506 break; | |
2 | 507 } |
508 } | |
8 | 509 |
510 public void run() | |
511 { | |
512 while (animEnable) | |
513 { | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
514 // Progress game animation clock |
25 | 515 gameClock++; |
9 | 516 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
517 // Animate components |
25 | 518 lauta.animate(gameClock); |
9 | 519 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
520 // Repaint with a frame limiter |
25 | 521 if (gameClock % 3 == 1) |
24 | 522 { |
10 | 523 repaint(); |
25 | 524 gameFrames++; |
24 | 525 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
526 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
527 // Sleep for a moment |
9 | 528 try { |
529 Thread.sleep(10); | |
530 } | |
531 catch (InterruptedException x) { | |
532 } | |
8 | 533 } |
534 } | |
0 | 535 } |