Mercurial > hg > ristipolku
comparison game/Engine.java @ 131:67b2322fda91
Add spawning of animated score elements when player finishes a turn.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 24 Nov 2011 21:11:06 +0200 |
parents | fa1b86b632aa |
children | 881deac2daf8 |
comparison
equal
deleted
inserted
replaced
130:792bf87886e7 | 131:67b2322fda91 |
---|---|
11 import java.awt.event.*; | 11 import java.awt.event.*; |
12 import java.awt.font.*; | 12 import java.awt.font.*; |
13 import javax.imageio.*; | 13 import javax.imageio.*; |
14 import javax.swing.*; | 14 import javax.swing.*; |
15 import java.util.*; | 15 import java.util.*; |
16 import java.util.concurrent.locks.Lock; | |
17 import java.util.concurrent.locks.ReentrantReadWriteLock; | |
16 import java.io.*; | 18 import java.io.*; |
17 import game.*; | 19 import game.*; |
18 import javax.sound.sampled.*; | 20 import javax.sound.sampled.*; |
19 | 21 |
20 | 22 |
208 | 210 |
209 Piece currPiece, nextPiece; | 211 Piece currPiece, nextPiece; |
210 int currX, currY, currPoint; | 212 int currX, currY, currPoint; |
211 | 213 |
212 Sound sndPlaced; | 214 Sound sndPlaced; |
215 | |
216 private final ReentrantReadWriteLock pointLock = new ReentrantReadWriteLock(); | |
217 private ArrayList<AnimatedPointElement> pointElems; | |
213 | 218 |
214 public GameBoard(IDMPoint pos, float ps) | 219 public GameBoard(IDMPoint pos, float ps) |
215 { | 220 { |
216 super(pos); | 221 super(pos); |
217 pscale = ps; | 222 pscale = ps; |
218 | 223 |
219 // sndPlaced = G.smgr.getSound("sounds/placed.wav"); | 224 // sndPlaced = G.smgr.getSound("sounds/placed.wav"); |
225 | |
226 pointElems = new ArrayList<AnimatedPointElement>(); | |
220 | 227 |
221 startNewGame(); | 228 startNewGame(); |
222 } | 229 } |
223 | 230 |
224 public void startNewGame() | 231 public void startNewGame() |
257 getScaledX() + (x * pscale), | 264 getScaledX() + (x * pscale), |
258 getScaledY() + (y * pscale), | 265 getScaledY() + (y * pscale), |
259 pscale - pscale / 10); | 266 pscale - pscale / 10); |
260 } | 267 } |
261 | 268 |
269 Lock read = pointLock.readLock(); | |
270 read.lock(); | |
271 try | |
272 { | |
273 for (AnimatedPointElement elem : pointElems) | |
274 { | |
275 elem.paint(g); | |
276 } | |
277 } | |
278 finally | |
279 { | |
280 read.unlock(); | |
281 } | |
282 | |
283 | |
262 if (!flagGameOver) | 284 if (!flagGameOver) |
263 { | 285 { |
264 if (nextPiece != null) | 286 if (nextPiece != null) |
265 { | 287 { |
266 // Draw next piece | 288 // Draw next piece |
305 for (int y = 0; y < boardSize; y++) | 327 for (int y = 0; y < boardSize; y++) |
306 for (int x = 0; x < boardSize; x++) | 328 for (int x = 0; x < boardSize; x++) |
307 if (board[x][y] != null) | 329 if (board[x][y] != null) |
308 { | 330 { |
309 board[x][y].animate(time); | 331 board[x][y].animate(time); |
332 } | |
333 | |
334 Lock write = pointLock.writeLock(); | |
335 write.lock(); | |
336 try | |
337 { | |
338 ArrayList<AnimatedPointElement> tmp = new ArrayList<AnimatedPointElement>(); | |
339 | |
340 for (AnimatedPointElement elem : pointElems) | |
341 { | |
342 elem.animate(time); | |
343 if (elem.active) | |
344 tmp.add(elem); | |
345 } | |
346 | |
347 pointElems = tmp; | |
348 } | |
349 finally | |
350 { | |
351 write.unlock(); | |
310 } | 352 } |
311 } | 353 } |
312 | 354 |
313 public void pieceRotate(Piece.RotateDir dir) | 355 public void pieceRotate(Piece.RotateDir dir) |
314 { | 356 { |
421 { | 463 { |
422 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) | 464 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
423 { | 465 { |
424 connections++; | 466 connections++; |
425 finished = pieceCheck(board[currX][currY]); | 467 finished = pieceCheck(board[currX][currY]); |
468 | |
469 if (!finished) | |
470 { | |
471 Lock write = pointLock.writeLock(); | |
472 write.lock(); | |
473 try | |
474 { | |
475 pointElems.add(new AnimatedPointElement( | |
476 new IDMPoint( | |
477 getScaledX() + ((currX + 0.5f) * pscale), | |
478 getScaledY() + ((currY + 0.5f) * pscale)), | |
479 "" + connections)); | |
480 } | |
481 finally | |
482 { | |
483 write.unlock(); | |
484 } | |
485 } | |
426 } | 486 } |
427 else | 487 else |
428 { | 488 { |
429 // Outside of the board, game over | 489 // Outside of the board, game over |
430 finished = true; | 490 finished = true; |