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