comparison Ristipolku.java @ 8:d8e7fd8f3ccf

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 22:21:45 +0200
parents 70714c229e23
children
comparison
equal deleted inserted replaced
7:70714c229e23 8:d8e7fd8f3ccf
13 import javax.imageio.*; 13 import javax.imageio.*;
14 import java.util.*; 14 import java.util.*;
15 import java.io.*; 15 import java.io.*;
16 import game.*; 16 import game.*;
17 17
18 import javax.sound.sampled.*;
19
18 20
19 class PathInfo 21 class PathInfo
20 { 22 {
21 public int in, inX, inY, out, outX, outY; 23 public int in, inX, inY, out, outX, outY;
22 24
30 this.outX = outX; 32 this.outX = outX;
31 this.outY = outY; 33 this.outY = outY;
32 } 34 }
33 } 35 }
34 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 */
35 74
36 class GameBoard 75 class GameBoard
37 { 76 {
38 public static final int boardSize = 9; 77 public static final int boardSize = 9;
39 public static final int boardMiddle = 4; 78 public static final int boardMiddle = 4;
40 Piece[][] board; 79 Piece[][] board;
41 Piece current; 80 Piece current;
81 public boolean flagGameOver;
42 82
43 int moveX, moveY, movePoint; 83 int moveX, moveY, movePoint;
44 84
45 public GameBoard() 85 public GameBoard()
46 { 86 {
49 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); 89 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
50 90
51 moveX = boardMiddle; 91 moveX = boardMiddle;
52 moveY = boardMiddle - 1; 92 moveY = boardMiddle - 1;
53 movePoint = 0; 93 movePoint = 0;
94
95
96
97 flagGameOver = false;
54 } 98 }
55 99
56 public void paint(Graphics2D g, int sx, int sy, float scale) 100 public void paint(Graphics2D g, int sx, int sy, float scale)
57 { 101 {
58 for (int y = 0; y < boardSize; y++) 102 for (int y = 0; y < boardSize; y++)
86 public void pieceRotate(boolean dir) 130 public void pieceRotate(boolean dir)
87 { 131 {
88 current.rotate(dir); 132 current.rotate(dir);
89 } 133 }
90 134
91 public PathInfo resolvePath(int startX, int startY, int startPoint) 135 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark)
92 { 136 {
93 int x = startX, y = startY; 137 int x = startX, y = startY;
94 int point; 138 int point = -1;
95 139
96 Piece curr = getPiece(startX, startY); 140 Piece curr = getPiece(startX, startY);
97 if (curr == null) 141 if (curr == null)
98 return null; 142 return null;
99 143
100 while (curr != null) 144 while (curr != null)
101 { 145 {
102 146 // curr.(true);
147 // elements.spawn("", );
103 } 148 }
104 149
105 return new PathInfo(startPoint, startX, startY, point, x, y); 150 return new PathInfo(startPoint, startX, startY, point, x, y);
106 } 151 }
107 152
108 public void pieceFinishTurn() 153 public void pieceFinishTurn()
109 { 154 {
110 if (current != null) 155 if (current != null)
111 { 156 {
112 current.setType(PieceType.LOCKED); 157 current.setType(PieceType.LOCKED);
113 PathInfo i = resolvePath(moveX, moveY, movePoint); 158 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
114 159
115 if (i != null) 160 if (i != null)
116 { 161 {
117
118 } 162 }
119 } 163 }
120 164
165 current = new Piece(PieceType.ACTIVE);
121 if (isEmpty(moveX, moveY)) 166 if (isEmpty(moveX, moveY))
122 { 167 {
123 current = new Piece(PieceType.ACTIVE); 168 board[moveX][moveY] = current;
124 lauta.setPiece(moveX, moveY, current); 169 }
170 else
171 {
172 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
173 if (i != null)
174 board[moveX][moveY] = current;
175 else
176 flagGameOver = true;
125 } 177 }
126 } 178 }
127 } 179 }
128 180
129 181
130 public class Ristipolku extends JApplet 182 public class Ristipolku extends JPanel
131 implements KeyListener 183 implements Runnable, KeyListener
132 { 184 {
185 Thread animThread;
186 boolean animEnable = false;
133 GameBoard lauta = null; 187 GameBoard lauta = null;
134 BufferedImage lautaBG = null; 188 BufferedImage lautaBG = null;
189 float clock;
135 190
136 public void init() 191 public void init()
137 { 192 {
138 getContentPane().setBackground(Color.white); 193 clock = 0;
139 194
140 try 195 try
141 { 196 {
142 lautaBG = ImageIO.read(new File("background.jpg")); 197 lautaBG = ImageIO.read(new File("board.png"));
143 } 198 }
144 199
145 catch (IOException e) 200 catch (IOException e)
146 { 201 {
147 JOptionPane.showMessageDialog(null, 202 JOptionPane.showMessageDialog(null,
148 "Could not load background image.", 203 "Could not load background image.",
149 "Initialization error", 204 "Initialization error",
150 JOptionPane.ERROR_MESSAGE); 205 JOptionPane.ERROR_MESSAGE);
151 } 206 }
152 207
208 startThreads();
209
153 lauta = new GameBoard(); 210 lauta = new GameBoard();
154 addKeyListener(this); 211 addKeyListener(this);
212
213 startThreads();
214 }
215
216 public void startThreads()
217 {
218 if (animThread == null)
219 {
220 animThread = new Thread(this);
221 animEnable = true;
222 animThread.start();
223 }
224 }
225
226 public void stopThreads()
227 {
228 if (animThread != null)
229 {
230 animThread.interrupt();
231 animEnable = false;
232 animThread = null;
233 }
155 } 234 }
156 235
157 public void paint(Graphics g) 236 public void paint(Graphics g)
158 { 237 {
159 Graphics2D g2 = (Graphics2D) g; 238 Graphics2D g2 = (Graphics2D) g;
189 case KeyEvent.VK_ENTER: 268 case KeyEvent.VK_ENTER:
190 lauta.pieceFinishTurn(); 269 lauta.pieceFinishTurn();
191 break; 270 break;
192 } 271 }
193 } 272 }
194 } 273
274 public void run()
275 {
276 while (animEnable)
277 {
278 clock++;
279
280 Thread.sleep(100);
281 }
282 }
283 }