Mercurial > hg > ristipolku
annotate game/Piece.java @ 162:e8eeac403e5f
Backed out changeset fb33d3796942
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 01 Dec 2016 14:33:25 +0200 |
parents | src/Piece.java@fb33d3796942 |
children | 7bf508d363bd |
rev | line source |
---|---|
1 | 1 /* |
2 * Ristipolku | |
151 | 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
1 | 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 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
16 |
3 | 17 static final int numConnections = 8; |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
18 static final float maxTime = 50.0f; |
9 | 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, |
138
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
27 stateChanged, stateActive, |
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
28 active; |
6 | 29 |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
30 float currAngle, rotationTime, rotationSpeed, |
153 | 31 typeTime, typeValue, throbTime; |
91 | 32 |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
33 Interpolate lerpRotation, |
149
35374be74fdc
Add some bling to piece rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
34 lerpScale, |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
35 lerpType; |
1 | 36 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
37 |
3 | 38 public Piece(PieceType ptype) |
39 { | |
40 // Initialize | |
41 connections = new int[numConnections]; | |
40 | 42 states = new boolean[numConnections]; |
3 | 43 type = ptype; |
1 | 44 |
3 | 45 rotationChanged = false; |
6 | 46 rotationActive = false; |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
47 currRotation = 4 * 5000; |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
48 currAngle = getAngle(currRotation); |
6 | 49 |
3 | 50 typeChanged = false; |
51 typeActive = false; | |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
52 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
53 throbTime = 0; |
1 | 54 |
149
35374be74fdc
Add some bling to piece rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
55 lerpType = new Interpolate(1.0f, 0.0f, maxTime); |
35374be74fdc
Add some bling to piece rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
56 lerpScale = new Interpolate(0.0f, (float) Math.PI, maxTime); |
1 | 57 |
3 | 58 // Initialize connections between endpoints of the paths inside the piece |
59 for (int i = 0; i < numConnections; i++) | |
60 connections[i] = -1; | |
1 | 61 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
62 |
6 | 63 // Randomize connections in the piece |
3 | 64 Random rnd = new Random(); |
65 for (int i = 0; i < numConnections; i++) | |
66 { | |
67 while (connections[i] < 0) | |
68 { | |
69 int tmp = rnd.nextInt(numConnections); | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
70 if (tmp != i && connections[tmp] < 0) |
3 | 71 { |
72 connections[i] = tmp; | |
73 connections[tmp] = i; | |
74 } | |
75 } | |
76 } | |
77 } | |
78 | |
79 public Piece() | |
80 { | |
81 this(PieceType.NONE); | |
82 } | |
1 | 83 |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
84 |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
85 public void changed() |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
86 { |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
87 typeChanged = true; |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
88 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
89 |
3 | 90 public void setType(PieceType ptype) |
91 { | |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
92 // typeChanged = (prevType != ptype) && (ptype == PieceType.LOCKED); |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
93 prevType = type; |
3 | 94 type = ptype; |
95 } | |
1 | 96 |
40 | 97 public void clearStates() |
98 { | |
99 for (int i = 0; i < numConnections; i++) | |
100 states[i] = false; | |
101 stateChanged = true; | |
102 } | |
103 | |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
104 public int getRotation() |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
105 { |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
106 return currRotation % 4; |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
107 } |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
108 |
40 | 109 public int getRotatedPoint(int in) |
3 | 110 { |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
111 int point = (in - (getRotation() * 2)) % 8; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
112 if (point < 0) point = 8 + point; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
113 return point; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
114 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
115 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
116 public int getAntiRotatedPoint(int in) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
117 { |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
118 int point = (in + (getRotation() * 2)) % 8; |
40 | 119 if (point < 0) point = 8 + point; |
120 return point; | |
121 } | |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
122 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
123 public int getMatchingPoint(int point) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
124 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
125 switch (point) |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
126 { |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
127 case 0: return 5; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
128 case 1: return 4; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
129 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
130 case 2: return 7; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
131 case 3: return 6; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
132 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
133 case 4: return 1; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
134 case 5: return 0; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
135 |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
136 case 6: return 3; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
137 case 7: return 2; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
138 } |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
139 return -1; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
140 } |
37 | 141 |
40 | 142 public void setConnectionState(int point, boolean state) |
143 { | |
144 states[point] = state; | |
145 states[connections[point]] = state; | |
146 stateChanged = true; | |
147 } | |
148 | |
149 public int getConnection(int point) | |
150 { | |
151 return connections[point]; | |
3 | 152 } |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
153 |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
154 private float getAngle(float rotation) |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
155 { |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
156 return (float) ((rotation * Math.PI) / 2.0f); |
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
157 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
158 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
159 public void rotate(RotateDir dir) |
3 | 160 { |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
161 // Only normal |
6 | 162 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) |
3 | 163 return; |
1 | 164 |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
165 currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1)); |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
166 lerpRotation = new Interpolate(getAngle(currRotation), currAngle, maxTime); |
3 | 167 rotationChanged = true; |
5 | 168 } |
1 | 169 |
7 | 170 public Point2D getPointCoords(float x, float y, float dim, int index) |
5 | 171 { |
7 | 172 float ox = 0, oy = 0; |
173 float step = dim / 10; | |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
174 |
5 | 175 switch (index) { |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
176 // Normal line starting and ending points |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
177 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
|
178 case 1: ox = 7.0f; oy = 0.4f; break; |
39 | 179 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
180 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
|
181 case 3: ox = 9.6f; oy = 7.0f; break; |
39 | 182 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
183 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
|
184 case 5: ox = 3.0f; oy = 9.6f; break; |
39 | 185 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
186 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
|
187 case 7: ox = 0.4f; oy = 3.0f; break; |
1 | 188 |
39 | 189 |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
190 // Matching control points for each point above (+8) |
39 | 191 case 8: ox = 3.0f; oy = 2.5f; break; |
192 case 9: ox = 7.0f; oy = 2.5f; break; | |
193 | |
194 case 10: ox = 7.5f; oy = 3.0f; break; | |
195 case 11: ox = 7.5f; oy = 7.0f; break; | |
196 | |
197 case 12: ox = 7.0f; oy = 7.5f; break; | |
198 case 13: ox = 3.0f; oy = 7.5f; break; | |
199 | |
200 case 14: ox = 2.5f; oy = 7.0f; break; | |
201 case 15: ox = 2.5f; oy = 3.0f; break; | |
5 | 202 } |
1 | 203 |
7 | 204 return new Point2D.Float(x + ox * step, y + oy * step); |
5 | 205 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
206 |
27 | 207 public PieceType getType() |
208 { | |
209 return type; | |
210 } | |
9 | 211 |
7 | 212 public void animate(float time) |
5 | 213 { |
138
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
214 active = false; |
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
215 |
5 | 216 if (rotationChanged) |
217 { | |
218 rotationTime = time; | |
219 rotationActive = true; | |
7 | 220 rotationChanged = false; |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
221 rotationSpeed = 1.0f; |
54 | 222 } |
223 | |
224 if (typeChanged && type == PieceType.LOCKED) | |
225 { | |
104
eb2e72dd8cae
Change how piece rotation works.
Matti Hamalainen <ccr@tnsp.org>
parents:
92
diff
changeset
|
226 rotationSpeed = 1.5f; |
5 | 227 } |
1 | 228 |
5 | 229 if (rotationActive) |
230 { | |
54 | 231 float t = (time - rotationTime) * rotationSpeed; |
11 | 232 |
9 | 233 if (t < maxTime) |
149
35374be74fdc
Add some bling to piece rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
234 { |
7 | 235 currAngle = lerpRotation.getValue(t); |
149
35374be74fdc
Add some bling to piece rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
236 } |
7 | 237 else |
238 { | |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
239 currAngle = lerpRotation.start; |
7 | 240 rotationActive = false; |
241 } | |
138
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
242 |
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
243 active = true; |
5 | 244 } |
54 | 245 |
6 | 246 if (typeChanged) |
247 { | |
248 typeTime = time; | |
249 typeActive = true; | |
7 | 250 typeChanged = false; |
6 | 251 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
252 |
6 | 253 if (typeActive) |
254 { | |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
255 float t = (time - typeTime) * 2.0f; |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
256 |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
257 if (t < maxTime) |
152 | 258 { |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
259 typeValue = lerpType.getValue(t); |
152 | 260 } |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
261 else |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
262 { |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
263 typeValue = lerpType.start; |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
264 typeActive = false; |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
265 } |
138
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
266 |
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
267 |
9eb791e2fa17
Optimize board updating logic, so that the old placed tiles need not to be
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
268 active = true; |
6 | 269 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
270 |
40 | 271 if (stateChanged) |
9 | 272 { |
273 } | |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
274 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
275 throbTime = (time % 100) / 100.0f; |
5 | 276 } |
1 | 277 |
7 | 278 public void paint(Graphics2D g, float x, float y, float dim) |
5 | 279 { |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
280 AffineTransform save = g.getTransform(); |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
281 Composite csave = g.getComposite(); |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
282 |
149
35374be74fdc
Add some bling to piece rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
283 |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
284 // Change compositing alpha for the whole piece drawing |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
285 // when the piece is being "introduced". |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
286 if (typeActive) |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
287 { |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
288 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, typeValue)); |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
289 } |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
290 |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
291 |
45 | 292 // Transform drawing by current angle |
11 | 293 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); |
1 | 294 |
45 | 295 // Color piece by type |
5 | 296 switch (type) { |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
297 case LOCKED: g.setPaint(new Color(0.3f, 0.8f, 0.3f, 0.35f)); break; |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
298 case ACTIVE: g.setPaint(new Color(0.9f, 0.3f, 0.3f, 0.35f)); break; |
92
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
299 case START: g.setPaint(new Color(1.0f, 0.6f, 0.0f, 0.95f)); break; |
5 | 300 } |
6 | 301 |
153 | 302 float corner = dim / 10.0f; |
303 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, corner, corner)); | |
1 | 304 |
92
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
305 // Start pieces (center piece) have a different kind of border |
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
306 // and no connections drawn inside |
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
307 if (type == PieceType.START) |
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
308 { |
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
309 // Draw piece border |
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
310 g.setPaint(Color.black); |
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
311 g.setStroke(new BasicStroke(2.0f)); |
153 | 312 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, corner, corner)); |
92
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
313 } |
d5f51370617b
Change start piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
314 else |
5 | 315 { |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
316 // Active piece has a throbbing "ghost" border |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
317 if (type == PieceType.ACTIVE) |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
318 { |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
319 float offs1 = throbTime * 10.0f, |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
320 offs2 = throbTime * 20.0f; |
27 | 321 |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
322 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throbTime) )); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
323 g.setStroke(new BasicStroke(2.0f + throbTime * 2.0f)); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
324 g.draw(new RoundRectangle2D.Float( |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
325 x - offs1, y - offs1, |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
326 dim + offs2, dim + offs2, |
153 | 327 corner, corner)); |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
328 } |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
329 |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
330 // Draw piece border |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
331 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.6f)); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
332 g.setStroke(new BasicStroke(5.0f)); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
333 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
1 | 334 |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
335 // Draw the connections |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
336 g.setStroke(new BasicStroke(5.5f)); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
337 CubicCurve2D curve = new CubicCurve2D.Float(); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
338 boolean[] drawn = new boolean[numConnections]; |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
339 for (int i = 0; i < numConnections; i++) |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
340 if (!drawn[i]) |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
341 { |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
342 Point2D start, cp1, cp2, end; |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
343 boolean isActive = states[i] || states[connections[i]]; |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
344 |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
345 g.setPaint(isActive ? Color.white : Color.black); |
9 | 346 |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
347 start = getPointCoords(x, y, dim, i); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
348 end = getPointCoords(x, y, dim, connections[i]); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
349 cp1 = getPointCoords(x, y, dim, i + 8); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
350 cp2 = getPointCoords(x, y, dim, connections[i] + 8); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
351 |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
352 curve.setCurve(start, cp1, cp2, end); |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
353 g.draw(curve); |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
354 |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
355 // Mark connection drawn, so we don't overdraw |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
356 drawn[i] = true; |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
357 drawn[connections[i]] = true; |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
358 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
359 |
58
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
360 } // !PieceType.START |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
361 |
cde170f2f980
Clean up the piece rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
362 g.setTransform(save); |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
363 g.setComposite(csave); |
5 | 364 } |
1 | 365 } |