Mercurial > hg > ristipolku
comparison game/Piece.java @ 106:41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Mar 2011 21:21:35 +0200 |
parents | eb2e72dd8cae |
children | 4c0dec72e2f0 |
comparison
equal
deleted
inserted
replaced
105:75015dfd47ef | 106:41c6cca69d60 |
---|---|
24 | 24 |
25 boolean rotationChanged, rotationActive, | 25 boolean rotationChanged, rotationActive, |
26 typeChanged, typeActive, | 26 typeChanged, typeActive, |
27 stateChanged, stateActive; | 27 stateChanged, stateActive; |
28 | 28 |
29 float currAngle, newAngle, | 29 float currAngle, rotationTime, rotationSpeed, |
30 rotationTime, rotationSpeed, | 30 typeTime, typeValue, throbTime; |
31 typeTime, throbTime; | 31 |
32 | 32 Interpolate lerpRotation, |
33 Interpolate lerpRotation; | 33 lerpType; |
34 | 34 |
35 | 35 |
36 public Piece(PieceType ptype) | 36 public Piece(PieceType ptype) |
37 { | 37 { |
38 // Initialize | 38 // Initialize |
48 typeChanged = false; | 48 typeChanged = false; |
49 typeActive = false; | 49 typeActive = false; |
50 | 50 |
51 throbTime = 0; | 51 throbTime = 0; |
52 | 52 |
53 lerpType = new Interpolate(1.0f, 0.0f, maxTime); | |
53 | 54 |
54 // Initialize connections between endpoints of the paths inside the piece | 55 // Initialize connections between endpoints of the paths inside the piece |
55 for (int i = 0; i < numConnections; i++) | 56 for (int i = 0; i < numConnections; i++) |
56 connections[i] = -1; | 57 connections[i] = -1; |
57 | 58 |
75 public Piece() | 76 public Piece() |
76 { | 77 { |
77 this(PieceType.NONE); | 78 this(PieceType.NONE); |
78 } | 79 } |
79 | 80 |
81 | |
82 public void changed() | |
83 { | |
84 typeChanged = true; | |
85 } | |
86 | |
80 public void setType(PieceType ptype) | 87 public void setType(PieceType ptype) |
81 { | 88 { |
82 typeChanged = (prevType != ptype); | 89 // typeChanged = (prevType != ptype) && (ptype == PieceType.LOCKED); |
83 prevType = type; | 90 prevType = type; |
84 type = ptype; | 91 type = ptype; |
85 } | 92 } |
86 | 93 |
87 public void clearStates() | 94 public void clearStates() |
151 // Only normal | 158 // Only normal |
152 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) | 159 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) |
153 return; | 160 return; |
154 | 161 |
155 currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1)); | 162 currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1)); |
156 newAngle = getAngle(currRotation); | 163 lerpRotation = new Interpolate(getAngle(currRotation), currAngle, maxTime); |
157 lerpRotation = new Interpolate(newAngle, currAngle, maxTime); | |
158 rotationChanged = true; | 164 rotationChanged = true; |
159 } | 165 } |
160 | 166 |
161 public Point2D getPointCoords(float x, float y, float dim, int index) | 167 public Point2D getPointCoords(float x, float y, float dim, int index) |
162 { | 168 { |
221 | 227 |
222 if (t < maxTime) | 228 if (t < maxTime) |
223 currAngle = lerpRotation.getValue(t); | 229 currAngle = lerpRotation.getValue(t); |
224 else | 230 else |
225 { | 231 { |
226 currAngle = newAngle; | 232 currAngle = lerpRotation.start; |
227 rotationActive = false; | 233 rotationActive = false; |
228 } | 234 } |
229 } | 235 } |
230 | 236 |
231 if (typeChanged) | 237 if (typeChanged) |
235 typeChanged = false; | 241 typeChanged = false; |
236 } | 242 } |
237 | 243 |
238 if (typeActive) | 244 if (typeActive) |
239 { | 245 { |
246 float t = (time - typeTime) * 2.0f; | |
247 | |
248 if (t < maxTime) | |
249 typeValue = lerpType.getValue(t); | |
250 else | |
251 { | |
252 typeValue = lerpType.start; | |
253 typeActive = false; | |
254 } | |
240 } | 255 } |
241 | 256 |
242 if (stateChanged) | 257 if (stateChanged) |
243 { | 258 { |
244 } | 259 } |
247 } | 262 } |
248 | 263 |
249 public void paint(Graphics2D g, float x, float y, float dim) | 264 public void paint(Graphics2D g, float x, float y, float dim) |
250 { | 265 { |
251 AffineTransform save = g.getTransform(); | 266 AffineTransform save = g.getTransform(); |
267 Composite csave = g.getComposite(); | |
268 | |
269 // Change compositing alpha for the whole piece drawing | |
270 // when the piece is being "introduced". | |
271 if (typeActive) | |
272 { | |
273 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, typeValue)); | |
274 } | |
275 | |
252 | 276 |
253 // Transform drawing by current angle | 277 // Transform drawing by current angle |
254 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); | 278 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); |
255 | 279 |
256 // Color piece by type | 280 // Color piece by type |
318 } | 342 } |
319 | 343 |
320 } // !PieceType.START | 344 } // !PieceType.START |
321 | 345 |
322 g.setTransform(save); | 346 g.setTransform(save); |
347 g.setComposite(csave); | |
323 } | 348 } |
324 } | 349 } |