Mercurial > hg > ristipolku
annotate game/Engine.java @ 59:fd10a9422b60
Cleanups, bugfixes, etc etc.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 23 Feb 2011 22:12:01 +0200 |
parents | 1435e9d7fd1a |
children | 4a984e3b27d2 |
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 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
99 pieceFinishTurn(); |
8 | 100 flagGameOver = false; |
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 |
55 | 256 gameScore += connections * connections; |
257 | |
53 | 258 if (flagGameOver) |
259 { | |
260 currPiece = null; | |
261 System.out.print("GameOver!\n"); | |
262 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
263 } |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
264 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
265 public boolean mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
266 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
267 int notches = e.getWheelRotation(); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
268 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
269 if (notches < 0) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
270 pieceRotate(Piece.RotateDir.LEFT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
271 else |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
272 pieceRotate(Piece.RotateDir.RIGHT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
273 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
274 return true; |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
275 } |
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 public boolean mouseClicked(MouseEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
278 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
279 if (flagGameOver) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
280 return false; |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
281 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
282 if (contains(e.getPoint())) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
283 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
284 pieceFinishTurn(); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
285 return true; |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
286 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
287 else |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
288 return false; |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
289 } |
47 | 290 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
291 public boolean keyPressed(KeyEvent e) |
47 | 292 { |
54 | 293 if (flagGameOver) |
294 return false; | |
295 | |
47 | 296 switch (e.getKeyCode()) |
297 { | |
298 case KeyEvent.VK_LEFT: | |
299 case KeyEvent.VK_UP: | |
300 pieceRotate(Piece.RotateDir.LEFT); | |
301 return true; | |
302 | |
303 case KeyEvent.VK_RIGHT: | |
304 case KeyEvent.VK_DOWN: | |
305 pieceRotate(Piece.RotateDir.RIGHT); | |
306 return true; | |
307 | |
308 case KeyEvent.VK_ENTER: | |
309 pieceFinishTurn(); | |
310 return true; | |
311 } | |
312 return false; | |
313 } | |
1 | 314 } |
315 | |
316 | |
9 | 317 public class Engine extends JPanel |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
318 implements Runnable, KeyListener, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
319 MouseListener, MouseWheelListener |
1 | 320 { |
25 | 321 long startTime; |
322 float gameClock, gameFrames; | |
8 | 323 Thread animThread; |
324 boolean animEnable = false; | |
25 | 325 |
326 Font fontMain, font1, font2; | |
55 | 327 FontMetrics metrics1, metrics2; |
328 | |
4 | 329 GameBoard lauta = null; |
9 | 330 BufferedImage lautaBG = null, lautaBGScaled = null; |
25 | 331 Dimension lautaDim; |
22 | 332 |
37 | 333 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); |
47 | 334 SoundManager soundManager; |
335 Sound musa; | |
1 | 336 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
337 IDMContainer widgets; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
338 |
9 | 339 public Engine() |
2 | 340 { |
25 | 341 // Initialize globals |
342 System.out.print("Engine() constructor\n"); | |
343 | |
37 | 344 // Sound system |
47 | 345 soundManager = new SoundManager(sfmt, 16); |
9 | 346 |
25 | 347 // Load resources |
4 | 348 try |
349 { | |
24 | 350 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
18 | 351 lautaBG = ImageIO.read(res.getStream()); |
21 | 352 |
25 | 353 try { |
354 res = new ResourceLoader("graphics/font.ttf"); | |
355 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
55 | 356 |
25 | 357 font1 = fontMain.deriveFont(24f); |
55 | 358 font2 = fontMain.deriveFont(64f); |
25 | 359 } |
360 catch (FontFormatException e) | |
361 { | |
362 System.out.print("Could not initialize fonts.\n"); | |
363 } | |
37 | 364 |
56 | 365 // musa = soundManager.getSound("sounds/gamemusic.wav"); |
4 | 366 } |
367 catch (IOException e) | |
368 { | |
369 JOptionPane.showMessageDialog(null, | |
21 | 370 e.getMessage(), |
4 | 371 "Initialization error", |
372 JOptionPane.ERROR_MESSAGE); | |
37 | 373 |
21 | 374 System.out.print(e.getMessage()); |
18 | 375 } |
376 | |
52 | 377 // 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
|
378 widgets = new IDMContainer(); |
52 | 379 |
380 widgets.add(new BtnSwapPiece(0.75f, 0.60f)); | |
381 widgets.add(new BtnNewGame(0.75f, 0.85f)); | |
50 | 382 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
383 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
|
384 widgets.add(lauta); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
385 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
386 // Game |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
387 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
388 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
389 // Initialize event listeners |
18 | 390 addKeyListener(this); |
391 addMouseListener(this); | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
392 addMouseWheelListener(this); |
18 | 393 |
394 // Get initial focus | |
395 if (!hasFocus()) | |
396 { | |
397 System.out.print("Engine(): requesting focus\n"); | |
398 requestFocus(); | |
4 | 399 } |
37 | 400 |
47 | 401 soundManager.play(musa); |
8 | 402 } |
403 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
404 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
|
405 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
406 gameClock = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
407 gameFrames = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
408 startTime = new Date().getTime(); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
409 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
|
410 } |
25 | 411 |
412 public void paintComponent(Graphics g) | |
413 { | |
414 Graphics2D g2 = (Graphics2D) g; | |
415 | |
416 // Use antialiasing when rendering the game elements | |
417 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
418 RenderingHints.VALUE_ANTIALIAS_ON); | |
419 | |
420 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | |
421 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
422 | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
423 |
52 | 424 // Rescale if parent component size has changed |
25 | 425 Dimension dim = getSize(); |
426 if (lautaDim == null || !dim.equals(lautaDim)) | |
427 { | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
428 // Rescale IDM GUI widgets |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
429 widgets.setScale(dim.width, dim.height); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
430 |
52 | 431 // Rescale background image |
25 | 432 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); |
433 Graphics2D gimg = lautaBGScaled.createGraphics(); | |
434 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | |
435 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
436 | |
437 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
|
438 lauta.paintBackPlate(gimg); |
25 | 439 lautaDim = dim; |
440 | |
441 System.out.print("scale changed\n"); | |
442 } | |
443 | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
444 // Get font metrics against current Graphics2D context |
55 | 445 if (metrics1 == null) |
446 metrics1 = g2.getFontMetrics(font1); | |
447 | |
448 if (metrics2 == null) | |
449 metrics2 = g2.getFontMetrics(font2); | |
450 | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
451 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
452 // Draw background image, pieces, widgets |
25 | 453 g2.drawImage(lautaBGScaled, 0, 0, null); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
454 widgets.paint(g2); |
55 | 455 |
456 if (!lauta.flagGameOver) | |
457 { | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
458 // Draw next piece |
57 | 459 AffineTransform save = g2.getTransform(); |
55 | 460 lauta.nextPiece.paint(g2, 830, 325, 90); |
57 | 461 g2.setTransform(save); |
55 | 462 } |
463 else | |
464 { | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
465 // Game over text |
55 | 466 String text = "Game Over!"; |
467 int textWidth = metrics2.stringWidth(text); | |
468 g2.setFont(font2); | |
469 | |
470 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); | |
471 g2.drawString(text, (dim.width - textWidth) / 2 + 5, dim.height / 2 + 5); | |
472 | |
473 double f = Math.sin(gameClock * 0.1) * 4.0; | |
474 g2.setPaint(Color.white); | |
475 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); | |
476 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
477 |
52 | 478 // Scores, etc |
55 | 479 g2.setFont(font2); |
25 | 480 g2.setPaint(Color.white); |
50 | 481 |
55 | 482 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220); |
483 | |
484 g2.setFont(font1); | |
25 | 485 long currTime = new Date().getTime(); |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
486 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
57 | 487 |
25 | 488 } |
489 | |
8 | 490 public void startThreads() |
491 { | |
9 | 492 System.out.print("startThreads()\n"); |
8 | 493 if (animThread == null) |
494 { | |
495 animThread = new Thread(this); | |
496 animEnable = true; | |
497 animThread.start(); | |
498 } | |
499 } | |
500 | |
501 public void stopThreads() | |
502 { | |
9 | 503 System.out.print("stopThreads()\n"); |
37 | 504 |
505 // Stop animations | |
8 | 506 if (animThread != null) |
507 { | |
508 animThread.interrupt(); | |
509 animEnable = false; | |
510 animThread = null; | |
511 } | |
22 | 512 |
37 | 513 // Shut down sound manager |
47 | 514 soundManager.close(); |
2 | 515 } |
0 | 516 |
18 | 517 public void mouseEntered(MouseEvent e) { } |
518 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
|
519 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
520 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
|
521 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
522 widgets.mousePressed(e); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
523 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
524 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
525 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
|
526 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
527 widgets.mouseReleased(e); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
528 } |
18 | 529 |
530 public void mouseClicked(MouseEvent e) | |
531 { | |
532 System.out.print("mouseClicked()\n"); | |
533 if (!hasFocus()) | |
534 { | |
535 System.out.print("requesting focus\n"); | |
536 requestFocus(); | |
537 } | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
538 else |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
539 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
540 lauta.mouseClicked(e); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
541 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
542 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
543 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
544 public void mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
545 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
546 lauta.mouseWheelMoved(e); |
18 | 547 } |
548 | |
9 | 549 public void keyTyped(KeyEvent e) |
2 | 550 { |
551 } | |
1 | 552 |
2 | 553 public void keyReleased(KeyEvent e) |
554 { | |
555 } | |
1 | 556 |
9 | 557 public void keyPressed(KeyEvent e) |
2 | 558 { |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
559 // Handle keyboard input |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
560 if (lauta.keyPressed(e)) |
47 | 561 return; |
562 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
563 widgets.keyPressed(e); |
2 | 564 } |
8 | 565 |
566 public void run() | |
567 { | |
568 while (animEnable) | |
569 { | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
570 // Progress game animation clock |
25 | 571 gameClock++; |
9 | 572 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
573 // Animate components |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
574 lauta.animate(gameClock); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
575 lauta.nextPiece.animate(gameClock); |
9 | 576 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
577 // Repaint with a frame limiter |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
578 if (gameClock % 4 == 1) |
24 | 579 { |
10 | 580 repaint(); |
25 | 581 gameFrames++; |
24 | 582 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
583 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
584 // Sleep for a moment |
9 | 585 try { |
586 Thread.sleep(10); | |
587 } | |
588 catch (InterruptedException x) { | |
589 } | |
8 | 590 } |
591 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
592 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
593 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
|
594 { |
51 | 595 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
|
596 { |
51 | 597 super(x, y, KeyEvent.VK_ESCAPE, font1, "Uusi peli"); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
598 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
599 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
600 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
|
601 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
602 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
603 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
604 } |
50 | 605 |
606 class BtnSwapPiece extends IDMButton | |
607 { | |
51 | 608 public BtnSwapPiece(float x, float y) |
50 | 609 { |
51 | 610 super(x, y, KeyEvent.VK_SPACE, font1, "Vaihda"); |
50 | 611 } |
612 | |
613 public void clicked() | |
614 { | |
54 | 615 lauta.pieceSwapCurrent(); |
50 | 616 } |
617 } | |
0 | 618 } |