Mercurial > hg > ristipolku
annotate game/Piece.java @ 42:951a4d669af0
Initially working path solving algorithm.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 22 Feb 2011 05:58:55 +0200 |
parents | a69103644bf6 |
children | 9664bbb9d613 |
rev | line source |
---|---|
1 | 1 /* |
2 * Ristipolku | |
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
4 */ | |
5 package game; | |
6 | |
7 import java.awt.*; | |
7 | 8 import java.awt.geom.*; |
1 | 9 import java.util.*; |
6 | 10 import java.math.*; |
1 | 11 |
12 | |
13 public class Piece | |
14 { | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
15 public enum RotateDir { LEFT, RIGHT } |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
16 |
3 | 17 static final int numConnections = 8; |
9 | 18 static final float maxTime = 50.0f; |
19 | |
3 | 20 int currRotation; |
21 int[] connections; | |
40 | 22 boolean[] states; |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
23 PieceType type, prevType; |
1 | 24 |
3 | 25 boolean rotationChanged, rotationActive, |
9 | 26 typeChanged, typeActive, |
40 | 27 stateChanged, stateActive; |
7 | 28 float currAngle, newAngle, rotationTime, typeTime; |
6 | 29 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
30 float throbTime; |
7 | 31 Interpolate lerpRotation; |
1 | 32 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
33 |
3 | 34 public Piece(PieceType ptype) |
35 { | |
36 // Initialize | |
37 connections = new int[numConnections]; | |
40 | 38 states = new boolean[numConnections]; |
3 | 39 type = ptype; |
1 | 40 |
3 | 41 rotationChanged = false; |
6 | 42 rotationActive = false; |
43 currRotation = 0; | |
44 currAngle = 0; | |
45 | |
3 | 46 typeChanged = false; |
47 typeActive = false; | |
1 | 48 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
49 throbTime = 0; |
1 | 50 |
51 | |
3 | 52 // Initialize connections between endpoints of the paths inside the piece |
53 for (int i = 0; i < numConnections; i++) | |
54 connections[i] = -1; | |
1 | 55 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
56 |
6 | 57 // Randomize connections in the piece |
3 | 58 Random rnd = new Random(); |
59 for (int i = 0; i < numConnections; i++) | |
60 { | |
61 while (connections[i] < 0) | |
62 { | |
63 int tmp = rnd.nextInt(numConnections); | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
64 if (tmp != i && connections[tmp] < 0) |
3 | 65 { |
66 connections[i] = tmp; | |
67 connections[tmp] = i; | |
68 } | |
69 } | |
70 } | |
71 } | |
72 | |
73 public Piece() | |
74 { | |
75 this(PieceType.NONE); | |
76 } | |
1 | 77 |
3 | 78 public void setType(PieceType ptype) |
79 { | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
80 typeChanged = (prevType != ptype); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
81 prevType = type; |
3 | 82 type = ptype; |
83 } | |
1 | 84 |
40 | 85 public void clearStates() |
86 { | |
87 for (int i = 0; i < numConnections; i++) | |
88 states[i] = false; | |
89 stateChanged = true; | |
90 } | |
91 | |
92 public int getRotatedPoint(int in) | |
3 | 93 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
94 int point = (in - (currRotation * 2)) % 8; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
95 if (point < 0) point = 8 + point; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
96 return point; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
97 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
98 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
99 public int getAntiRotatedPoint(int in) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
100 { |
40 | 101 int point = (in + (currRotation * 2)) % 8; |
102 if (point < 0) point = 8 + point; | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
103 System.out.print("getAntiRotatedPoint("+ in +"): rot="+currRotation+", point="+point+"\n"); |
40 | 104 return point; |
105 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
106 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
107 public int getMatchingPoint(int point) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
108 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
109 switch (point) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
110 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
111 case 0: return 5; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
112 case 1: return 4; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
113 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
114 case 2: return 7; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
115 case 3: return 6; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
116 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
117 case 4: return 1; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
118 case 5: return 0; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
119 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
120 case 6: return 3; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
121 case 7: return 2; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
122 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
123 return -1; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
124 } |
37 | 125 |
40 | 126 public void setConnectionState(int point, boolean state) |
127 { | |
128 states[point] = state; | |
129 states[connections[point]] = state; | |
130 stateChanged = true; | |
131 } | |
132 | |
133 public int getConnection(int point) | |
134 { | |
135 return connections[point]; | |
3 | 136 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
137 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
138 public void rotate(RotateDir dir) |
3 | 139 { |
6 | 140 // Only normal |
141 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) | |
3 | 142 return; |
1 | 143 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
144 currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1)) % 4; |
11 | 145 newAngle = (float) ((currRotation * Math.PI) / 2.0f); |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
146 lerpRotation = new Interpolate(newAngle, currAngle, maxTime); |
3 | 147 rotationChanged = true; |
5 | 148 } |
1 | 149 |
7 | 150 public Point2D getPointCoords(float x, float y, float dim, int index) |
5 | 151 { |
7 | 152 float ox = 0, oy = 0; |
153 float step = dim / 10; | |
1 | 154 |
5 | 155 switch (index) { |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
156 // Normal line starting and ending points |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
157 case 0: ox = 3.0f; oy = 0.4f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
158 case 1: ox = 7.0f; oy = 0.4f; break; |
39 | 159 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
160 case 2: ox = 9.6f; oy = 3.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
161 case 3: ox = 9.6f; oy = 7.0f; break; |
39 | 162 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
163 case 4: ox = 7.0f; oy = 9.6f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
164 case 5: ox = 3.0f; oy = 9.6f; break; |
39 | 165 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
166 case 6: ox = 0.4f; oy = 7.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
167 case 7: ox = 0.4f; oy = 3.0f; break; |
1 | 168 |
39 | 169 |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
170 // Matching control points for each point above (+8) |
39 | 171 case 8: ox = 3.0f; oy = 2.5f; break; |
172 case 9: ox = 7.0f; oy = 2.5f; break; | |
173 | |
174 case 10: ox = 7.5f; oy = 3.0f; break; | |
175 case 11: ox = 7.5f; oy = 7.0f; break; | |
176 | |
177 case 12: ox = 7.0f; oy = 7.5f; break; | |
178 case 13: ox = 3.0f; oy = 7.5f; break; | |
179 | |
180 case 14: ox = 2.5f; oy = 7.0f; break; | |
181 case 15: ox = 2.5f; oy = 3.0f; break; | |
5 | 182 } |
1 | 183 |
7 | 184 return new Point2D.Float(x + ox * step, y + oy * step); |
5 | 185 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
186 |
27 | 187 public PieceType getType() |
188 { | |
189 return type; | |
190 } | |
9 | 191 |
7 | 192 public void animate(float time) |
5 | 193 { |
194 if (rotationChanged) | |
195 { | |
196 rotationTime = time; | |
197 rotationActive = true; | |
7 | 198 rotationChanged = false; |
5 | 199 } |
1 | 200 |
5 | 201 if (rotationActive) |
202 { | |
11 | 203 float t = (time - rotationTime) / 2; |
204 | |
9 | 205 if (t < maxTime) |
7 | 206 currAngle = lerpRotation.getValue(t); |
207 else | |
208 { | |
209 currAngle = newAngle; | |
210 rotationActive = false; | |
211 } | |
5 | 212 } |
6 | 213 |
214 if (typeChanged) | |
215 { | |
216 typeTime = time; | |
217 typeActive = true; | |
7 | 218 typeChanged = false; |
6 | 219 } |
220 | |
221 if (typeActive) | |
222 { | |
223 } | |
224 | |
40 | 225 if (stateChanged) |
9 | 226 { |
227 } | |
228 | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
229 throbTime = (time % 100) / 100.0f; |
5 | 230 } |
1 | 231 |
7 | 232 public void paint(Graphics2D g, float x, float y, float dim) |
5 | 233 { |
11 | 234 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); |
1 | 235 |
5 | 236 switch (type) { |
6 | 237 case LOCKED: g.setPaint(Color.green); break; |
5 | 238 case ACTIVE: g.setPaint(Color.red); break; |
239 case START: g.setPaint(Color.orange); break; | |
240 } | |
6 | 241 |
7 | 242 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
1 | 243 |
5 | 244 |
245 if (type == PieceType.START) | |
246 return; | |
247 | |
6 | 248 if (type == PieceType.ACTIVE) |
249 { | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
250 float offs1 = throbTime * 10.0f, |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
251 offs2 = throbTime * 20.0f; |
11 | 252 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
253 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throbTime) )); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
254 g.setStroke(new BasicStroke(2.0f + throbTime * 2.0f)); |
11 | 255 g.draw(new RoundRectangle2D.Float(x - offs1, y - offs1, dim + offs2, dim + offs2, dim / 10, dim / 10)); |
6 | 256 } |
257 | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
258 g.setStroke(new BasicStroke(5.0f)); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
259 CubicCurve2D c = new CubicCurve2D.Float(); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
260 // QuadCurve2D c = new QuadCurve2D.Float(); |
9 | 261 boolean[] drawn = new boolean[numConnections]; |
262 for (int i = 0; i < numConnections; i++) | |
263 if (!drawn[i]) | |
5 | 264 { |
265 Point2D start, cp1, cp2, end; | |
40 | 266 boolean isActive = states[i] || states[connections[i]]; |
27 | 267 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
268 g.setPaint(isActive ? Color.white : Color.black); |
9 | 269 |
6 | 270 start = getPointCoords(x, y, dim, i); |
271 end = getPointCoords(x, y, dim, connections[i]); | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
272 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
273 cp1 = getPointCoords(x, y, dim, i + 8); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
274 cp2 = getPointCoords(x, y, dim, connections[i] + 8); |
1 | 275 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
276 c.setCurve(start, cp1, cp2, end); |
9 | 277 g.draw(c); |
278 | |
279 drawn[i] = true; | |
280 drawn[connections[i]] = true; | |
5 | 281 } |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
282 |
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
283 g.setPaint(Color.black); |
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
284 g.setStroke(new BasicStroke(5.0f)); |
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
285 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
5 | 286 } |
1 | 287 } |