Mercurial > hg > ristipolku
annotate game/Engine.java @ 23:0741dc117808
Remove sound code for later refactoring.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 01 Feb 2011 09:55:09 +0200 |
parents | afde253ec705 |
children | 1be98362e5e9 |
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.*; | |
9 | 12 import javax.imageio.*; |
7 | 13 import javax.swing.*; |
1 | 14 import java.util.*; |
6 | 15 import java.io.*; |
1 | 16 import game.*; |
17 | |
21 | 18 |
1 | 19 class PathInfo |
20 { | |
2 | 21 public int in, inX, inY, out, outX, outY; |
1 | 22 |
2 | 23 public PathInfo(int in, int inX, int inY, int out, int outX, int outY) |
24 { | |
25 this.in = in; | |
26 this.inX = inX; | |
27 this.inY = inY; | |
28 | |
29 this.out = out; | |
30 this.outX = outX; | |
31 this.outY = outY; | |
32 } | |
1 | 33 } |
0 | 34 |
8 | 35 /* |
36 class AnimatedElement | |
37 { | |
38 float x, y, stime, value; | |
39 Interpolate lerp; | |
40 boolean active; | |
41 | |
42 public AnimatedElement(float x, float y, ) | |
43 { | |
44 stime = 0; | |
45 this.x = x; | |
46 this.y = y; | |
47 | |
48 } | |
49 | |
50 public animate(float time) | |
51 { | |
52 if (!active) | |
53 { | |
54 active = true; | |
55 stime = time; | |
56 } | |
57 | |
58 float t = (time - stime) / 10.0f; | |
59 if (t < 100) | |
60 value = lerp.getValue(t); | |
61 else | |
62 { | |
63 | |
64 } | |
65 } | |
66 | |
67 public paint(Graphics2D g, ); | |
68 { | |
69 } | |
70 } | |
71 */ | |
0 | 72 |
1 | 73 class GameBoard |
0 | 74 { |
2 | 75 public static final int boardSize = 9; |
76 public static final int boardMiddle = 4; | |
77 Piece[][] board; | |
78 Piece current; | |
8 | 79 public boolean flagGameOver; |
7 | 80 |
81 int moveX, moveY, movePoint; | |
9 | 82 |
2 | 83 public GameBoard() |
84 { | |
85 board = new Piece[boardSize][boardSize]; | |
86 | |
87 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | |
88 | |
89 moveX = boardMiddle; | |
90 moveY = boardMiddle - 1; | |
7 | 91 movePoint = 0; |
8 | 92 |
9 | 93 pieceFinishTurn(); |
8 | 94 |
95 flagGameOver = false; | |
2 | 96 } |
0 | 97 |
7 | 98 public void paint(Graphics2D g, int sx, int sy, float scale) |
2 | 99 { |
100 for (int y = 0; y < boardSize; y++) | |
101 for (int x = 0; x < boardSize; x++) | |
102 if (board[x][y] != null) | |
103 { | |
6 | 104 AffineTransform save = g.getTransform(); |
105 | |
4 | 106 board[x][y].paint(g, |
107 sx + (x * scale), | |
108 sy + (y * scale), | |
109 scale - scale / 10); | |
6 | 110 |
111 g.setTransform(save); | |
2 | 112 } |
113 } | |
9 | 114 |
115 public void animate(float time) | |
116 { | |
117 for (int y = 0; y < boardSize; y++) | |
118 for (int x = 0; x < boardSize; x++) | |
119 if (board[x][y] != null) | |
120 { | |
121 board[x][y].animate(time); | |
122 } | |
10 | 123 |
9 | 124 } |
1 | 125 |
2 | 126 private boolean isEmpty(int x, int y) |
127 { | |
128 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); | |
129 } | |
1 | 130 |
2 | 131 private Piece getPiece(int x, int y) |
132 { | |
133 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize) | |
134 return board[x][y]; | |
135 else | |
136 return null; | |
137 } | |
7 | 138 |
2 | 139 public void pieceRotate(boolean dir) |
140 { | |
9 | 141 if (current != null) |
142 current.rotate(dir); | |
2 | 143 } |
1 | 144 |
8 | 145 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark) |
7 | 146 { |
147 int x = startX, y = startY; | |
8 | 148 int point = -1; |
7 | 149 |
150 Piece curr = getPiece(startX, startY); | |
151 if (curr == null) | |
152 return null; | |
9 | 153 |
154 /* | |
7 | 155 while (curr != null) |
156 { | |
8 | 157 // curr.(true); |
158 // elements.spawn("", ); | |
7 | 159 } |
9 | 160 */ |
161 | |
7 | 162 return new PathInfo(startPoint, startX, startY, point, x, y); |
163 } | |
164 | |
2 | 165 public void pieceFinishTurn() |
166 { | |
167 if (current != null) | |
7 | 168 { |
6 | 169 current.setType(PieceType.LOCKED); |
8 | 170 PathInfo i = resolvePath(moveX, moveY, movePoint, true); |
7 | 171 |
172 if (i != null) | |
173 { | |
174 } | |
175 } | |
176 | |
8 | 177 current = new Piece(PieceType.ACTIVE); |
7 | 178 if (isEmpty(moveX, moveY)) |
2 | 179 { |
8 | 180 board[moveX][moveY] = current; |
181 } | |
182 else | |
183 { | |
184 PathInfo i = resolvePath(moveX, moveY, movePoint, true); | |
185 if (i != null) | |
186 board[moveX][moveY] = current; | |
187 else | |
188 flagGameOver = true; | |
2 | 189 } |
1 | 190 } |
191 } | |
192 | |
193 | |
9 | 194 public class Engine extends JPanel |
18 | 195 implements Runnable, KeyListener, MouseListener |
1 | 196 { |
8 | 197 Thread animThread; |
198 boolean animEnable = false; | |
4 | 199 GameBoard lauta = null; |
9 | 200 BufferedImage lautaBG = null, lautaBGScaled = null; |
201 Dimension oldDim; | |
8 | 202 float clock; |
22 | 203 |
21 | 204 SoundElement[] sounds; |
205 public SoundElement snd(Sound snd) | |
206 { | |
207 return sounds[snd.ordinal()]; | |
208 } | |
1 | 209 |
9 | 210 public Engine() |
2 | 211 { |
9 | 212 BufferedImage img; |
8 | 213 clock = 0; |
4 | 214 |
9 | 215 System.out.print("Engine() constructor\n"); |
216 | |
4 | 217 try |
218 { | |
18 | 219 ResourceLoader res = new ResourceLoader("graphics/board.png"); |
220 lautaBG = ImageIO.read(res.getStream()); | |
21 | 221 |
222 sounds = new SoundElement[16]; | |
223 for (Sound s : Sound.values()) | |
224 { | |
225 System.out.print(s +" = "+ s.ordinal() +"\n"); | |
23
0741dc117808
Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
226 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isStreaming()); |
21 | 227 } |
4 | 228 } |
229 catch (IOException e) | |
230 { | |
18 | 231 /* |
4 | 232 JOptionPane.showMessageDialog(null, |
21 | 233 e.getMessage(), |
4 | 234 "Initialization error", |
235 JOptionPane.ERROR_MESSAGE); | |
18 | 236 */ |
21 | 237 System.out.print(e.getMessage()); |
18 | 238 } |
239 | |
240 lauta = new GameBoard(); | |
241 addKeyListener(this); | |
242 addMouseListener(this); | |
243 | |
244 // Get initial focus | |
245 if (!hasFocus()) | |
246 { | |
247 System.out.print("Engine(): requesting focus\n"); | |
248 requestFocus(); | |
4 | 249 } |
22 | 250 |
21 | 251 snd(Sound.MUSIC_GAME1).loop(-1); |
8 | 252 } |
253 | |
254 public void startThreads() | |
255 { | |
9 | 256 System.out.print("startThreads()\n"); |
8 | 257 if (animThread == null) |
258 { | |
259 animThread = new Thread(this); | |
260 animEnable = true; | |
261 animThread.start(); | |
262 } | |
263 } | |
264 | |
265 public void stopThreads() | |
266 { | |
9 | 267 System.out.print("stopThreads()\n"); |
8 | 268 if (animThread != null) |
269 { | |
270 animThread.interrupt(); | |
271 animEnable = false; | |
272 animThread = null; | |
273 } | |
22 | 274 |
275 for (Sound s : Sound.values()) | |
276 { | |
277 if (snd(s) != null) | |
278 snd(s).stop(); | |
279 } | |
2 | 280 } |
0 | 281 |
18 | 282 public void mousePressed(MouseEvent e) { } |
283 public void mouseEntered(MouseEvent e) { } | |
284 public void mouseExited(MouseEvent e) { } | |
285 public void mouseReleased(MouseEvent e) { } | |
286 | |
287 public void mouseClicked(MouseEvent e) | |
288 { | |
289 System.out.print("mouseClicked()\n"); | |
290 if (!hasFocus()) | |
291 { | |
292 System.out.print("requesting focus\n"); | |
293 requestFocus(); | |
294 } | |
295 } | |
296 | |
9 | 297 public void paintComponent(Graphics g) |
2 | 298 { |
299 Graphics2D g2 = (Graphics2D) g; | |
10 | 300 |
301 // Use antialiasing when rendering the game elements | |
0 | 302 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
303 RenderingHints.VALUE_ANTIALIAS_ON); | |
4 | 304 |
10 | 305 // Rescale background if component size has changed |
9 | 306 Dimension dim = getSize(); |
307 if (oldDim == null || !dim.equals(oldDim)) | |
308 { | |
309 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); | |
310 Graphics2D gimg = lautaBGScaled.createGraphics(); | |
311 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | |
312 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
10 | 313 |
314 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); | |
9 | 315 oldDim = dim; |
316 System.out.print("scale changed\n"); | |
317 } | |
318 | |
10 | 319 // Background, pieces |
9 | 320 g2.drawImage(lautaBGScaled, 0, 0, null); |
7 | 321 lauta.paint(g2, 100, 150, 60); |
9 | 322 |
10 | 323 // Other elements |
0 | 324 } |
325 | |
9 | 326 public void keyTyped(KeyEvent e) |
2 | 327 { |
328 } | |
1 | 329 |
2 | 330 public void keyReleased(KeyEvent e) |
331 { | |
332 } | |
1 | 333 |
9 | 334 public void keyPressed(KeyEvent e) |
2 | 335 { |
7 | 336 switch (e.getKeyCode()) |
2 | 337 { |
7 | 338 case KeyEvent.VK_LEFT: |
339 case KeyEvent.VK_UP: | |
2 | 340 lauta.pieceRotate(false); |
341 break; | |
0 | 342 |
7 | 343 case KeyEvent.VK_RIGHT: |
344 case KeyEvent.VK_DOWN: | |
2 | 345 lauta.pieceRotate(true); |
346 break; | |
347 | |
7 | 348 case KeyEvent.VK_ENTER: |
2 | 349 lauta.pieceFinishTurn(); |
23
0741dc117808
Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
350 snd(Sound.PIECE_PLACED).stop(); |
0741dc117808
Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
351 snd(Sound.PIECE_PLACED).play(); |
2 | 352 break; |
353 } | |
354 } | |
8 | 355 |
356 public void run() | |
357 { | |
358 while (animEnable) | |
359 { | |
360 clock++; | |
9 | 361 |
362 lauta.animate(clock); | |
363 | |
10 | 364 if (clock % 2 == 1) |
365 repaint(); | |
9 | 366 |
367 try { | |
368 Thread.sleep(10); | |
369 } | |
370 | |
371 catch (InterruptedException x) { | |
372 } | |
8 | 373 } |
374 } | |
0 | 375 } |