Mercurial > hg > ristipolku
comparison game/Engine.java @ 9:a7751971c2a3
More work.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 29 Jan 2011 00:30:00 +0200 |
parents | Ristipolku.java@d8e7fd8f3ccf |
children | 4bacc98973f5 |
comparison
equal
deleted
inserted
replaced
8:d8e7fd8f3ccf | 9:a7751971c2a3 |
---|---|
1 /* | |
2 * Ristipolku Game Engine | |
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
4 */ | |
5 package game; | |
6 | |
7 import java.awt.*; | |
8 import java.awt.geom.*; | |
9 import java.awt.event.*; | |
10 import java.awt.image.*; | |
11 import java.awt.event.*; | |
12 import javax.imageio.*; | |
13 import javax.swing.*; | |
14 import java.util.*; | |
15 import java.io.*; | |
16 import game.*; | |
17 | |
18 import javax.sound.sampled.*; | |
19 | |
20 | |
21 class PathInfo | |
22 { | |
23 public int in, inX, inY, out, outX, outY; | |
24 | |
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 } | |
35 } | |
36 | |
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 */ | |
74 | |
75 class GameBoard | |
76 { | |
77 public static final int boardSize = 9; | |
78 public static final int boardMiddle = 4; | |
79 Piece[][] board; | |
80 Piece current; | |
81 public boolean flagGameOver; | |
82 | |
83 int moveX, moveY, movePoint; | |
84 | |
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; | |
93 movePoint = 0; | |
94 | |
95 pieceFinishTurn(); | |
96 | |
97 flagGameOver = false; | |
98 } | |
99 | |
100 public void paint(Graphics2D g, int sx, int sy, float scale) | |
101 { | |
102 for (int y = 0; y < boardSize; y++) | |
103 for (int x = 0; x < boardSize; x++) | |
104 if (board[x][y] != null) | |
105 { | |
106 AffineTransform save = g.getTransform(); | |
107 | |
108 board[x][y].paint(g, | |
109 sx + (x * scale), | |
110 sy + (y * scale), | |
111 scale - scale / 10); | |
112 | |
113 g.setTransform(save); | |
114 } | |
115 } | |
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 } | |
126 | |
127 private boolean isEmpty(int x, int y) | |
128 { | |
129 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); | |
130 } | |
131 | |
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 } | |
139 | |
140 public void pieceRotate(boolean dir) | |
141 { | |
142 if (current != null) | |
143 current.rotate(dir); | |
144 } | |
145 | |
146 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark) | |
147 { | |
148 int x = startX, y = startY; | |
149 int point = -1; | |
150 | |
151 Piece curr = getPiece(startX, startY); | |
152 if (curr == null) | |
153 return null; | |
154 | |
155 /* | |
156 while (curr != null) | |
157 { | |
158 // curr.(true); | |
159 // elements.spawn("", ); | |
160 } | |
161 */ | |
162 | |
163 return new PathInfo(startPoint, startX, startY, point, x, y); | |
164 } | |
165 | |
166 public void pieceFinishTurn() | |
167 { | |
168 if (current != null) | |
169 { | |
170 current.setType(PieceType.LOCKED); | |
171 PathInfo i = resolvePath(moveX, moveY, movePoint, true); | |
172 | |
173 if (i != null) | |
174 { | |
175 } | |
176 } | |
177 | |
178 current = new Piece(PieceType.ACTIVE); | |
179 if (isEmpty(moveX, moveY)) | |
180 { | |
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; | |
190 } | |
191 } | |
192 } | |
193 | |
194 | |
195 public class Engine extends JPanel | |
196 implements Runnable, KeyListener | |
197 { | |
198 Thread animThread; | |
199 boolean animEnable = false; | |
200 GameBoard lauta = null; | |
201 BufferedImage lautaBG = null, lautaBGScaled = null; | |
202 Dimension oldDim; | |
203 float clock; | |
204 | |
205 public Engine() | |
206 { | |
207 BufferedImage img; | |
208 clock = 0; | |
209 | |
210 System.out.print("Engine() constructor\n"); | |
211 | |
212 try | |
213 { | |
214 lautaBG = ImageIO.read(new File("board.png")); | |
215 } | |
216 catch (IOException e) | |
217 { | |
218 System.out.print("lol\n"); | |
219 JOptionPane.showMessageDialog(null, | |
220 "Could not load background image.", | |
221 "Initialization error", | |
222 JOptionPane.ERROR_MESSAGE); | |
223 } | |
224 | |
225 // Graphics2D g = img.getGraphics(); | |
226 // atrans = AffineTransform.getScaleInstance(img.getWidth(), 3); | |
227 | |
228 lauta = new GameBoard(); | |
229 addKeyListener(this); | |
230 } | |
231 | |
232 public void startThreads() | |
233 { | |
234 System.out.print("startThreads()\n"); | |
235 if (animThread == null) | |
236 { | |
237 animThread = new Thread(this); | |
238 animEnable = true; | |
239 animThread.start(); | |
240 } | |
241 } | |
242 | |
243 public void stopThreads() | |
244 { | |
245 System.out.print("stopThreads()\n"); | |
246 if (animThread != null) | |
247 { | |
248 animThread.interrupt(); | |
249 animEnable = false; | |
250 animThread = null; | |
251 } | |
252 } | |
253 | |
254 public void paintComponent(Graphics g) | |
255 { | |
256 Graphics2D g2 = (Graphics2D) g; | |
257 | |
258 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
259 RenderingHints.VALUE_ANTIALIAS_ON); | |
260 | |
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); | |
275 lauta.paint(g2, 100, 150, 60); | |
276 | |
277 if (!hasFocus()) | |
278 requestFocus(); | |
279 } | |
280 | |
281 public void keyTyped(KeyEvent e) | |
282 { | |
283 } | |
284 | |
285 public void keyReleased(KeyEvent e) | |
286 { | |
287 } | |
288 | |
289 public void keyPressed(KeyEvent e) | |
290 { | |
291 System.out.print("lol\n"); | |
292 switch (e.getKeyCode()) | |
293 { | |
294 case KeyEvent.VK_LEFT: | |
295 case KeyEvent.VK_UP: | |
296 lauta.pieceRotate(false); | |
297 break; | |
298 | |
299 case KeyEvent.VK_RIGHT: | |
300 case KeyEvent.VK_DOWN: | |
301 lauta.pieceRotate(true); | |
302 break; | |
303 | |
304 case KeyEvent.VK_ENTER: | |
305 lauta.pieceFinishTurn(); | |
306 break; | |
307 } | |
308 } | |
309 | |
310 public void run() | |
311 { | |
312 while (animEnable) | |
313 { | |
314 clock++; | |
315 | |
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 } | |
328 } | |
329 } | |
330 } |