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
|
|
204 /*
|
21
|
205 SoundElement[] sounds;
|
|
206 public SoundElement snd(Sound snd)
|
|
207 {
|
|
208 return sounds[snd.ordinal()];
|
|
209 }
|
22
|
210 */
|
1
|
211
|
9
|
212 public Engine()
|
2
|
213 {
|
9
|
214 BufferedImage img;
|
8
|
215 clock = 0;
|
4
|
216
|
9
|
217 System.out.print("Engine() constructor\n");
|
|
218
|
4
|
219 try
|
|
220 {
|
18
|
221 ResourceLoader res = new ResourceLoader("graphics/board.png");
|
|
222 lautaBG = ImageIO.read(res.getStream());
|
21
|
223
|
22
|
224 /*
|
21
|
225 sounds = new SoundElement[16];
|
|
226 for (Sound s : Sound.values())
|
|
227 {
|
|
228 System.out.print(s +" = "+ s.ordinal() +"\n");
|
|
229 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isEffect());
|
|
230 }
|
22
|
231 */
|
4
|
232 }
|
|
233 catch (IOException e)
|
|
234 {
|
18
|
235 /*
|
4
|
236 JOptionPane.showMessageDialog(null,
|
21
|
237 e.getMessage(),
|
4
|
238 "Initialization error",
|
|
239 JOptionPane.ERROR_MESSAGE);
|
18
|
240 */
|
21
|
241 System.out.print(e.getMessage());
|
18
|
242 }
|
|
243
|
|
244 lauta = new GameBoard();
|
|
245 addKeyListener(this);
|
|
246 addMouseListener(this);
|
|
247
|
|
248 // Get initial focus
|
|
249 if (!hasFocus())
|
|
250 {
|
|
251 System.out.print("Engine(): requesting focus\n");
|
|
252 requestFocus();
|
4
|
253 }
|
22
|
254
|
|
255 /*
|
21
|
256 snd(Sound.MUSIC_GAME1).loop(-1);
|
22
|
257 */
|
8
|
258 }
|
|
259
|
|
260 public void startThreads()
|
|
261 {
|
9
|
262 System.out.print("startThreads()\n");
|
8
|
263 if (animThread == null)
|
|
264 {
|
|
265 animThread = new Thread(this);
|
|
266 animEnable = true;
|
|
267 animThread.start();
|
|
268 }
|
|
269 }
|
|
270
|
|
271 public void stopThreads()
|
|
272 {
|
9
|
273 System.out.print("stopThreads()\n");
|
8
|
274 if (animThread != null)
|
|
275 {
|
|
276 animThread.interrupt();
|
|
277 animEnable = false;
|
|
278 animThread = null;
|
|
279 }
|
22
|
280
|
|
281 /*
|
|
282 for (Sound s : Sound.values())
|
|
283 {
|
|
284 if (snd(s) != null)
|
|
285 snd(s).stop();
|
|
286 }
|
|
287 */
|
2
|
288 }
|
0
|
289
|
18
|
290 public void mousePressed(MouseEvent e) { }
|
|
291 public void mouseEntered(MouseEvent e) { }
|
|
292 public void mouseExited(MouseEvent e) { }
|
|
293 public void mouseReleased(MouseEvent e) { }
|
|
294
|
|
295 public void mouseClicked(MouseEvent e)
|
|
296 {
|
|
297 System.out.print("mouseClicked()\n");
|
|
298 if (!hasFocus())
|
|
299 {
|
|
300 System.out.print("requesting focus\n");
|
|
301 requestFocus();
|
|
302 }
|
|
303 }
|
|
304
|
9
|
305 public void paintComponent(Graphics g)
|
2
|
306 {
|
|
307 Graphics2D g2 = (Graphics2D) g;
|
10
|
308
|
|
309 // Use antialiasing when rendering the game elements
|
0
|
310 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
311 RenderingHints.VALUE_ANTIALIAS_ON);
|
4
|
312
|
10
|
313 // Rescale background if component size has changed
|
9
|
314 Dimension dim = getSize();
|
|
315 if (oldDim == null || !dim.equals(oldDim))
|
|
316 {
|
|
317 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
|
|
318 Graphics2D gimg = lautaBGScaled.createGraphics();
|
|
319 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
|
|
320 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
10
|
321
|
|
322 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
|
9
|
323 oldDim = dim;
|
|
324 System.out.print("scale changed\n");
|
|
325 }
|
|
326
|
10
|
327 // Background, pieces
|
9
|
328 g2.drawImage(lautaBGScaled, 0, 0, null);
|
7
|
329 lauta.paint(g2, 100, 150, 60);
|
9
|
330
|
10
|
331 // Other elements
|
0
|
332 }
|
|
333
|
9
|
334 public void keyTyped(KeyEvent e)
|
2
|
335 {
|
|
336 }
|
1
|
337
|
2
|
338 public void keyReleased(KeyEvent e)
|
|
339 {
|
|
340 }
|
1
|
341
|
9
|
342 public void keyPressed(KeyEvent e)
|
2
|
343 {
|
7
|
344 switch (e.getKeyCode())
|
2
|
345 {
|
7
|
346 case KeyEvent.VK_LEFT:
|
|
347 case KeyEvent.VK_UP:
|
2
|
348 lauta.pieceRotate(false);
|
|
349 break;
|
0
|
350
|
7
|
351 case KeyEvent.VK_RIGHT:
|
|
352 case KeyEvent.VK_DOWN:
|
2
|
353 lauta.pieceRotate(true);
|
|
354 break;
|
|
355
|
7
|
356 case KeyEvent.VK_ENTER:
|
2
|
357 lauta.pieceFinishTurn();
|
22
|
358 // snd(Sound.PIECE_PLACED).stop();
|
|
359 // snd(Sound.PIECE_PLACED).play();
|
2
|
360 break;
|
|
361 }
|
|
362 }
|
8
|
363
|
|
364 public void run()
|
|
365 {
|
|
366 while (animEnable)
|
|
367 {
|
|
368 clock++;
|
9
|
369
|
|
370 lauta.animate(clock);
|
|
371
|
10
|
372 if (clock % 2 == 1)
|
|
373 repaint();
|
9
|
374
|
|
375 try {
|
|
376 Thread.sleep(10);
|
|
377 }
|
|
378
|
|
379 catch (InterruptedException x) {
|
|
380 }
|
8
|
381 }
|
|
382 }
|
0
|
383 }
|