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