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