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