Debian Codegraph @

TetrisAttack/src/CPlayingField.cpp

raw
1 #include "tetris.h"
2
3 CPlayingField::CPlayingField(CVector pos, CBlockSet& set, CTetrisTheme* t, int speed)
4 : pos(pos), grav(SDL_GetTicks()), rect(CRect(pos.getX(), pos.getY(), 0, 0)),
5 cursor(0,0, FIELD_WIDTH, FIELD_HEIGHT, pos),
6 moveUpTimer(speed), moveUpOffset(0), blockSet(set), moves(1), numBlocks(0),
7 lStage(0), lIndex(0), gameOver(false), gameWon(false), txtColor(),
8 theme(t),
9 movesTxt(" ", CColor(), CTextManager::getDefaultFont( CTextManager::PLAIN ), CTextManager::getPtSize(2), CText::BOLD),
10 stageTxt(" ", CColor(), CTextManager::getDefaultFont( CTextManager::PLAIN ), CTextManager::getPtSize(2), CText::BOLD),
11 gameOverTxt("Press any key to retry level", CColor(255), CTextManager::getDefaultFont( CTextManager::PLAIN ), CTextManager::getPtSize(1.2), CText::BOLD)
12 {
13 //memset(blocks, 0, (FIELD_HEIGHT-1) * (FIELD_WIDTH-1) * sizeof(CBlock*));
14 for (int x = 0; x < FIELD_WIDTH; x++)
15 {
16 for (int y = 0; y < FIELD_HEIGHT; y++)
17 {
18 blocks[x][y] = NULL;
19 }
20 }
21 /*for (int i = 0; i < FIELD_WIDTH; i++)
22 {
23 addBlock(i, FIELD_HEIGHT - 1, static_cast<BlockType>(rand()%6));
24 blocks[i][FIELD_HEIGHT-1]->setActive(false);
25 }*/
26 }
27
28 CPlayingField::~CPlayingField()
29 {
30 for (int x = 0; x < FIELD_WIDTH; x++)
31 {
32 for (int y = 0; y < FIELD_HEIGHT; y++)
33 {
34 delete blocks[x][y];
35 }
36 }
37 // delete blockSet;
38 }
39
40 bool CPlayingField::checkBounds(int x, int y)
41 {
42 return (x < FIELD_WIDTH && x >= 0 && y < FIELD_HEIGHT && y >= 0);
43 }
44
45 bool CPlayingField::allActive()
46 {
47 for (int x = 0; x < FIELD_WIDTH; x++)
48 {
49 for (int y = 0; y < FIELD_HEIGHT; y++)
50 {
51 if (blockExists(x,y) && !blocks[x][y]->isActive())
52 {
53 return false;
54 }
55 }
56 }
57 return true;
58 }
59
60 bool CPlayingField::allFallen()
61 {
62 for (int x = 0; x < FIELD_WIDTH; x++)
63 {
64 for (int y = 0; y < FIELD_HEIGHT-1; y++)
65 {
66 if (blockExists(x,y) && !blockExists(x,y+1))
67 {
68 return false;
69 }
70 }
71 }
72 return true;
73 }
74
75 void CPlayingField::draw(CSurface& s)
76 {
77 /*for (int i = 0; i < FIELD_WIDTH; i++)
78 {
79 blocks[i][FIELD_HEIGHT-1]->setActive(false);
80 }*/
81 //moveUp();
82 //cout << "Execute applyGravity()" << endl;
83 //applyGravity(); // give floating blocks velocity downwards
84 s.setClippingRect(CRect(pos.getX(), pos.getY(), width*FIELD_WIDTH, height*FIELD_HEIGHT));
85 for (int x = FIELD_WIDTH-1; x >= 0; x--)
86 {
87 for (int y = FIELD_HEIGHT-1; y >= 0; y--)
88 {
89 if (blocks[x][y] != NULL)
90 {
91 blocks[x][y]->update();
92 }
93 }
94 }
95 checkBlocks(); // check for blocks to be cleared
96
97 for (int x = 0; x < FIELD_WIDTH; x++)
98 {
99 for (int y = 0; y < FIELD_HEIGHT; y++)
100 {
101 if (blocks[x][y] != NULL)
102 {
103 //blocks[x][y]->setPosRel(0, height - moveUpOffset);
104 blocks[x][y]->draw(s);
105 //blocks[x][y]->setPosRel(0, -height + moveUpOffset);
106 }
107 }
108 }
109 s.resetClippingRect();
110
111 for (list<CBlock*>::iterator i = freeBlocks.begin(); i != freeBlocks.end(); ++i)
112 {
113 //(*i)->setPosRel(0, height - moveUpOffset);
114 (*i)->draw(s);
115 //(*i)->setPosRel(0, -height + moveUpOffset);
116 }
117 //cursor.setPosRel(0, height - moveUpOffset);
118 cursor.update();
119 cursor.draw(s);
120 if (allActive() && allFallen())
121 {
122 if (isEmpty())
123 {
124 if (gameOver)
125 {
126 //CText t("GAME OVER", CTextManager::getDefaultFont(), 12);
127 //t.setPos(200,200);
128 //t.draw(s);
129 gameOverTxt.setPos(pos + CVector((width*FIELD_WIDTH-gameOverTxt.getWidth())/2, (height*FIELD_HEIGHT-gameOverTxt.getHeight())/2));
130 gameOverTxt.draw(s);
131 }
132 else
133 {
134 if (lIndex < 6)
135 load(lStage, lIndex+1);
136 else
137 {
138 if (lStage < 3)
139 {
140 load(lStage+1,1);
141 }
142 else
143 {
144 gameWon = true;
145 }
146 }
147 }
148 }
149 else if (moves <= 0)
150 {
151 // game over
152 gameOver = true;
153 //CSprite *death = new CSprite(ZERO_VECTOR, ZERO_VECTOR, "deadblock.png", "static", CRect(0,0,0,0));
154 for(int x = 0; x < FIELD_WIDTH; x++)
155 {
156 for(int y = 0; y < FIELD_HEIGHT; y++)
157 {
158 if (blockExists(x,y))
159 {
160 CSequencedEvent *e = new CBlockDeathEffect( *blocks[x][y], CImgLoader::loadImage("deadblock.png") );
161 blocks[x][y]->appendEvent( e );
162 e->update();
163 blocks[x][y]->appendEvent( new CPauseEvent(1) );
164 }
165 }
166 }
167
168 CSyncBlockerEvent *e = new CSyncBlockerEvent(0);
169 e->setTotal( addEventAllBlocks( e ) );
170
171 for(int x = 0; x < FIELD_WIDTH; x++)
172 {
173 for(int y = 0; y < FIELD_HEIGHT; y++)
174 {
175 if (blockExists(x,y))
176 {
177 blocks[x][y]->appendEvent( new CPauseEvent(1000) );
178 blocks[x][y]->appendEvent( new CRemoveBlockEvent(blocks[x][y], false) );
179 }
180 }
181 }
182
183 //addEventAllBlocks( new CLoadLvlEvent( *this, lStage, lIndex) );
184 // load(lStage, lIndex);
185 }
186 }
187 movesTxt.draw(s);
188 stageTxt.draw(s);
189 //cursor.setPosRel(0, -height + moveUpOffset);
190 }
191
192 int CPlayingField::addEventAllBlocks(CSequencedEvent *e)
193 {
194 int n = 0;
195 for (int x = 0; x < FIELD_WIDTH; x++)
196 {
197 for (int y = 0; y < FIELD_HEIGHT; y++)
198 {
199 if (blockExists(x,y))
200 {
201 e->plusRef();
202 blocks[x][y]->appendEvent(e);
203 n++;
204 }
205 }
206 }
207 return n;
208 }
209
210 void CPlayingField::applyGravity()
211 { // this is a little inefficient, i think
212 for (int x = 0; x < FIELD_WIDTH; x++)
213 {
214 for (int y = 0; y < FIELD_HEIGHT - 2; y++)
215 {
216 if (blocks[x][y] != NULL && blocks[x][y]->isActive() && blocks[x][y]->getVel().getX() == 0 && blocks[x][y+1] == NULL) // nothing holding up this column, so give it downward velocity (unless we've already given it one)
217 {
218 cout << "applying gravity to {" << blocks[x][y] << "}" << endl;
219 // move block into the null location
220 blocks[x][y+1] = blocks[x][y];
221 blocks[x][y+1]->moveRelBoardCords(0, 1, 300);
222 blocks[x][y+1]->update();
223 blocks[x][y] = NULL;
224 int i = 1;
225
226 if (y > 0)
227 {
228 CBlock* ptr = blocks[x][y-i];
229 while(ptr != NULL && y-i < FIELD_HEIGHT - 2 && y-i >= 0) // go up column until we find a null block, moving the blocks down
230 {
231 blocks[x][y-i+1] = blocks[x][y-i];
232 blocks[x][y-i]->moveRelBoardCords(0, 1, 300);
233 blocks[x][y-i] = NULL;
234 i++;
235 ptr = blocks[x][y-i];
236 }
237 }
238 y = y+i;
239 }
240 }
241 }
242 }
243
244 void CPlayingField::moveBlock(int x1, int y1, int x2, int y2)
245 {
246 if (blockExists(x1, y1) && checkBounds(x2,y2))
247 {
248 delete blocks[x2][y2];
249 blocks[x2][y2] = blocks[x1][y1];
250 blocks[x1][y1] = NULL;
251 }
252 }
253
254 void CPlayingField::moveUp()
255 {
256 if (moveUpTimer.isRinging())
257 {
258 if (moveUpOffset > height)
259 {
260 // move all blocks up one
261 for (int x = 0; x < FIELD_WIDTH; x++)
262 {
263 for (int y = 0; y < FIELD_HEIGHT; y++)
264 {
265 if (blockExists(x, y) && !blockExists(x,y-1))
266 {
267 if (y == 0)
268 {
269 delete blocks[x][y];
270 blocks[x][y] = NULL;
271 }
272 else if (y == FIELD_HEIGHT -1)
273 {
274 blocks[x][y]->setActive(true);
275 blocks[x][y-1] = blocks[x][y];
276 blocks[x][y-1]->moveRelBoardCords(0, -1);
277 blocks[x][y] = NULL;
278 }
279 else
280 {
281 if ((int)blocks[x][y]->getVel().getY() == 0)
282 {
283 blocks[x][y-1] = blocks[x][y];
284 blocks[x][y-1]->moveRelBoardCords(0, -1);
285 blocks[x][y] = NULL;
286 }
287 else
288 {
289 }
290 }
291 }
292 }
293 }
294
295 /*for (int i = 0; i < FIELD_WIDTH; i++)
296 {
297 addBlock(i, FIELD_HEIGHT - 1, static_cast<BlockType>(rand()%6));
298 blocks[i][FIELD_HEIGHT-1]->setActive(false);
299 }*/
300 moveUpOffset = 0;
301 moveCursor(0, -1, 0);
302 }
303 moveUpOffset++;
304 }
305 }
306
307 void CPlayingField::clear()
308 {
309 numBlocks = 0;
310 for(int x = 0; x < FIELD_WIDTH; x++)
311 {
312 for (int y = 0; y < FIELD_HEIGHT; y++)
313 {
314 delete blocks[x][y];
315 blocks[x][y] = NULL;
316 }
317 }
318 }
319
320 void CPlayingField::setMoves(int m)
321 {
322 moves = m;
323 ostringstream ss;
324 ss << "Moves Remaining: " << m;
325 movesTxt.setText(ss.str(),CText::BOLD, txtColor);
326 movesTxt.setPos(10,10);
327 }
328
329 void CPlayingField::load(int stage, int index)
330 {
331 if (stage == 2) txtColor = COLOR_WHITE;
332 if (stage == 1) txtColor = COLOR_BLACK;
333
334 ostringstream ss;
335 ss << CImgLoader::getPath() << "lvls/" << stage << "/" << index << ".txt";
336 ifstream lvl( ss.str().c_str() );
337 int x = 0, y = 0;
338
339 if (!lvl)
340 {
341 string s = ss.str();
342 _THROWEX(ex_Generic, "Failed to load lvl (" << stage << ", " << index << ") from file "
343 << s, "CPlayingField", "load", "stage = " << stage << ", index = " << index);
344 }
345
346
347 clear();
348 lvl >> moves;
349 setMoves(moves);
350 gameOver = false;
351 gameWon = false;
352 ostringstream ss2;
353 ss2 << "Level: " << stage << "-" << index;
354 stageTxt.setText(ss2.str(), CText::NONE, txtColor);
355 stageTxt.setPos(10 , movesTxt.getHeight() + 10);
356 lStage = stage;
357 lIndex = index;
358
359 while(!lvl.eof())
360 {
361 char c = lvl.get();
362 if (! (c == '\n' || c == '\r' || c == ' ' || c == '\t') )
363 {
364 switch(c - 47)
365 {
366 case 0:
367 // do nothing
368 break;
369 case 1:
370 addBlock(x,y,true,BLOCK_1);
371 break;
372 case 2:
373 addBlock(x,y,true,BLOCK_2);
374 break;
375 case 3:
376 addBlock(x,y,true,BLOCK_3);
377 break;
378 case 4:
379 addBlock(x,y,true,BLOCK_4);
380 break;
381 case 5:
382 addBlock(x,y,true,BLOCK_5);
383 break;
384 case 6:
385 addBlock(x,y,true,BLOCK_6);
386 break;
387 }
388 if (blockExists(x,y))
389 {
390 blocks[x][y]->appendEvent( new CReverseBlackEffect(1000, false, *blocks[x][y]->getCurrentFrame()) );
391 }
392
393 if ( ! (x+1 < FIELD_WIDTH) )
394 {
395 x = 0;
396 y++;
397 }
398 else
399 {
400 x++;
401 }
402 }
403 }
404 lvl.close();
405 }
406
407 bool CPlayingField::isEmpty()
408 {
409 return numBlocks == 0;
410 }
411
412 void CPlayingField::removeBlockEffect(CBlock* ptr, CSyncBlockerEvent* sync)
413 {
414 ptr->setActive(false);
415 ptr->setRect(CRect(0,0,0,0));
416
417 //ptr->appendEffect(1000, CEffect::BLACK);
418 CSequencedEvent *e = new CBlackEffect(1000, false, *ptr->getCurrentFrame());
419 ptr->appendEvent( e );
420 ptr->appendEvent(new CPauseEvent(200 * (sync->getRef() - 1)) );
421 ptr->appendEvent(new CRemoveSeqEvent( e, *ptr) );
422 //CRect(pos.getX(), pos.getY(), width*FIELD_WIDTH, height*FIELD_HEIGHT)
423 ptr->appendEvent(new CPlaySoundEvent(explode, CSoundPlayer, true) );
424 ptr->appendEvent(new CSpriteExplodeEffect(*ptr, 3000, 100, 250, CRect(0,0,0,0), 4, 4, false) );
425 ptr->appendEvent( sync );
426 ptr->appendEvent( new CRemoveBlockEvent(ptr, false) );
427 freeBlocks.push_back( ptr );
428
429 }
430
431 int CPlayingField::getBlockX(CBlock *b)
432 {
433 for (int x = 0; x < FIELD_WIDTH; x++)
434 {
435 for (int y = 0; y < FIELD_WIDTH; y++)
436 {
437 if (blocks[x][y] && blocks[x][y] == b)
438 return x;
439 }
440 }
441 return 0; // should throw exception here
442 }
443
444 int CPlayingField::getBlockY(CBlock *b)
445 {
446 for (int x = 0; x < FIELD_WIDTH; x++)
447 {
448 for (int y = 0; y < FIELD_WIDTH; y++)
449 {
450 if (blocks[x][y] == b)
451 return y;
452 }
453 }
454 return 0; // should throw exception here
455 }
456
457 void CPlayingField::removeList( list<CPoint> &toRemove, CSyncBlockerEvent* sync )
458 {
459 for(list<CPoint>::iterator i = toRemove.begin(); i != toRemove.end(); ++i)
460 {
461 CBlock* ptr = blocks[(int)(*i).getX()][(int)(*i).getY()];
462 if (ptr->isActive())
463 {
464 sync->plusRef();
465 removeBlockEffect(ptr, sync);
466 }
467 }
468 sync->minusRef();
469 //delete sync;
470 }
471
472 void CPlayingField::checkBlocks()
473 {
474 for (int x = 0; x < FIELD_WIDTH; x++)
475 {
476 for (int y = 0; y < FIELD_HEIGHT; y++)
477 {
478 if (blockExists(x,y))
479 {
480 if (blocks[x][y]->deleteMe())
481 {
482 removeBlock(x,y);
483 }
484 else
485 {
486 //CSyncBlockerEvent* sync = new CSyncBlockerEvent(
487 bool removedBlocks = false;
488 CBlock* ptr = blocks[x][y];
489 int i = 0;
490 int posY = 1;
491 int negY = 1;
492 int posX = 1;
493 int negX = 1;
494
495 i = 1;
496 list<CPoint> toRemove;
497 list<CPoint> tmp;
498 // first go up
499 while(ptr != NULL && blockExists(x, y-i) && ptr->sameType(*blocks[x][y-i]) && ptr->isActive() && blocks[x][y-i]->isActive())
500 {
501 tmp.push_back(CPoint(x, y-i));
502 negY++;
503 i++;
504 }
505 i--;
506 if (negY >= 3)
507 {
508 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
509 tmp.clear();
510
511 int y1 = y-i;
512 i = 1;
513 while(ptr != NULL && blockExists(x-i, y1) && ptr->sameType(*blocks[x-i][y1]) && ptr->isActive() && blocks[x-i][y1]->isActive())
514 {
515 tmp.push_back(CPoint(x-i, y1));
516 negX++;
517 i++;
518 }
519 if (negX >= 2)
520 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
521 tmp.clear();
522
523 i = 1;
524 while(ptr != NULL && blockExists(x+i, y1) && ptr->sameType(*blocks[x+i][y1]) && ptr->isActive() && blocks[x+i][y1]->isActive())
525 {
526 tmp.push_back(CPoint(x+i, y1));
527 posX++;
528 i++;
529 }
530 if (posX >= 2)
531 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
532 tmp.clear();
533 }
534
535 tmp.clear();
536
537 ptr = blocks[x][y];
538 i = 1;
539 // next go down
540 while(ptr != NULL && blockExists(x, y+i) && ptr->sameType(*blocks[x][y+i]) && ptr->isActive() && blocks[x][y+i]->isActive())
541 {
542 tmp.push_back(CPoint(x, y+i));
543 posY++;
544 i++;
545 }
546 i--;
547 if (posY >= 3)
548 {
549 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
550 tmp.clear();
551
552 int y1 = y+i;
553 i = 1;
554 while(ptr != NULL && blockExists(x-i, y1) && ptr->sameType(*blocks[x-i][y1]) && ptr->isActive() && blocks[x-i][y1]->isActive())
555 {
556 tmp.push_back(CPoint(x-i, y1));
557 negX++;
558 i++;
559 }
560 if (negX >= 2)
561 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
562 tmp.clear();
563
564 i = 1;
565 while(ptr != NULL && blockExists(x+i, y1) && ptr->sameType(*blocks[x+i][y1]) && ptr->isActive() && blocks[x+i][y1]->isActive())
566 {
567 tmp.push_back(CPoint(x+i, y1));
568 posX++;
569 i++;
570 }
571 if (posX >= 2)
572 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
573 tmp.clear();
574
575 }
576 tmp.clear();
577
578 ptr = blocks[x][y];
579 i = 1;
580 // now go left
581 while(ptr != NULL && blockExists(x-i, y) && ptr->sameType(*blocks[x-i][y]) && ptr->isActive() && blocks[x-i][y]->isActive())
582 {
583 tmp.push_back(CPoint(x-i, y));
584 negX++;
585 i++;
586 }
587 if (negX >= 3)
588 {
589 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
590 tmp.clear();
591
592 int x1 = x-i;
593 i = 1;
594 // go up
595 while(ptr != NULL && blockExists(x1, y-i) && ptr->sameType(*blocks[x1][y-i]) && ptr->isActive() && blocks[x1][y-i]->isActive())
596 {
597 tmp.push_back(CPoint(x1, y-i));
598 negY++;
599 i++;
600 }
601 if (negY >= 2)
602 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
603 }
604 tmp.clear();
605
606 ptr = blocks[x][y];
607 i = 1;
608 // finally go right
609 while(ptr != NULL && blockExists(x+i, y) && ptr->sameType(*blocks[x+i][y]) && ptr->isActive() && blocks[x+i][y]->isActive())
610 {
611 tmp.push_back(CPoint(x+i, y));
612 posX++;
613 i++;
614 }
615 i--;
616 if (posX >= 3)
617 {
618 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
619 tmp.clear();
620
621 int x1 = x+i;
622 i =1;
623 // go up
624 while(ptr != NULL && blockExists(x1, y-i) && ptr->sameType(*blocks[x1][y-i]) && ptr->isActive() && blocks[x1][y-i]->isActive())
625 {
626 tmp.push_back(CPoint(x1, y-i));
627 negY++;
628 i++;
629 }
630 if (negY >= 2)
631 toRemove.insert(toRemove.end(), tmp.begin(), tmp.end());
632 tmp.clear();
633 }
634
635 if (toRemove.size() > 0)
636 {
637 toRemove.push_back(CPoint(x,y));
638 removeList(toRemove, new CSyncBlockerEvent(toRemove.size()));
639 }
640
641 // clearedBlockLocations.push_back( CBlockLoc(x, y, negX, posX, negY, posY) ); // x and y are the center of the cleared block, the other numbers define its shape
642 }
643 }
644 }
645 }
646 }
647
648 void CPlayingField::swap()
649 {
650
651 if (cursor.getBoardX() + cursor.getBoardWidth() > FIELD_WIDTH || cursor.getBoardX() < 0) // something bad happened, the cursor is outside of the board
652 {
653 // throw some kind of exception
654 return;
655 }
656 if (cursor.getBoardY() + cursor.getBoardHeight() > FIELD_HEIGHT || cursor.getBoardY() < 0) // something bad happened, the cursor is outside of the board
657 {
658 // throw some kind of exception
659 return;
660 }
661
662 CBlock* tmp = blocks[ cursor.getBoardX() ][ cursor.getBoardY() ]; // swap the block (TODO: some kind of effect)
663 CBlock* tmp2 = blocks[ cursor.getBoardX() + 1 ][ cursor.getBoardY() ]; // swap the block (TODO: some kind of effect)
664 if (! (tmp || tmp2) )
665 {
666 return;
667 }
668 if ((tmp && !tmp->isActive()) || (tmp2 && !tmp2->isActive()))
669 { // bail out if either are inactive
670 return;
671 }
672
673 if(theme)
674 {
675 CSoundPlayer.playSound( theme->getSwapSound() );
676 }
677
678 if (! (moves > 0) )
679 return;
680 setMoves(moves - 1);
681
682 if (tmp && tmp2)
683 {
684 blocks[ cursor.getBoardX() ][ cursor.getBoardY() ] = blocks[ cursor.getBoardX() + 1 ][ cursor.getBoardY() ];
685 blocks[ cursor.getBoardX() + 1 ][ cursor.getBoardY() ] = tmp;
686 tmp->moveRelBoardCords(1,0,700);
687 tmp2->moveRelBoardCords(-1,0,700);
688 /*tmp->appendEvent( new CGotoAnimChangeEvent(*tmp) );
689 tmp2->appendEvent( new CGotoAnimChangeEvent(*tmp2) );*/
690 }
691 else if (tmp && !tmp2)
692 {
693 blocks[ cursor.getBoardX() + 1 ][ cursor.getBoardY() ] = tmp;
694 blocks[ cursor.getBoardX() ][ cursor.getBoardY() ] = NULL;
695 tmp->moveRelBoardCords(1,0,700);
696 tmp->appendEvent( new CGotoAnimChangeEvent(*tmp) );
697 if (!blockExists(cursor.getBoardX()+1, cursor.getBoardY()+1))
698 tmp->appendEvent( new CPauseEvent(200) );
699 }
700 else if (!tmp && tmp2)
701 {
702 blocks[ cursor.getBoardX() ][ cursor.getBoardY() ] = tmp2;
703 blocks[ cursor.getBoardX() + 1 ][ cursor.getBoardY() ] = NULL;
704 tmp2->moveRelBoardCords(-1,0,700);
705 tmp2->appendEvent( new CGotoAnimChangeEvent(*tmp2) );
706 if (!blockExists(cursor.getBoardX(), cursor.getBoardY()+1))
707 tmp2->appendEvent( new CPauseEvent(200) );
708 }
709 }
710
711 void CPlayingField::appendBlockEffect(int x, int y, int duration, CEffect::Effects effect)
712 {
713 if (blockExists(x,y))
714 {
715 blocks[x][y]->appendEffect(duration, effect);
716 }
717 else
718 {
719 // throw exception
720 }
721 }
722
723 void CPlayingField::appendBlockEvent(int x, int y, CSequencedEvent* e)
724 {
725 if (blockExists(x,y))
726 {
727 blocks[x][y]->appendEvent(e);
728 }
729 }
730
731 void CPlayingField::moveCursor(int dX, int dY, double sec)
732 {
733 cursor.moveRelBoardCords(dX, dY);
734 }
735
736 void CPlayingField::moveCursorAbs(int x, int y, double sec)
737 {
738 cursor.moveBoardCords(x, y);
739 }
740
741 void CPlayingField::setBackground(const string& fileName)
742 {
743 // background = CImgLoader::loadImage(fileName);
744 }
745
746 void CPlayingField::addBlock(int x, int y, BlockType type)
747 {
748 addBlock(x, y, true, type);
749 }
750
751 void CPlayingField::addBlock(int x, int y, bool active, BlockType type)
752 {
753 if (x < FIELD_WIDTH && y < FIELD_HEIGHT && y >= 0 && x >= 0)
754 {
755 delete blocks[x][y]; // okay to call delete even on null blocks
756 if (blocks[x][y] == NULL) numBlocks++;
757 blocks[x][y] = blockSet.getBlock(x, y, FIELD_WIDTH, FIELD_HEIGHT, pos, type);
758 blocks[x][y]->setActive(active);
759 blocks[x][y]->appendBehavior( new CApplyGravityEvent( *blocks[x][y], *this ) );
760 // blocks[x][y]->appendEvent(new CReverseBlackEffect(1000, false, *blocks[x][y]->getCurrentFrame()) );
761 width = blocks[x][y]->getWidth();
762 height = blocks[x][y]->getHeight();
763 //blocks[x][y]->setRect(CRect(x * width + pos.getX(), y * height + pos.getY(), width+50, 50+height * FIELD_HEIGHT));
764 //blocks[x][y]->setRect(CRect(0, 0, 640, 480));
765
766 rect.setWidth(width*FIELD_WIDTH);
767 rect.setHeight(height*FIELD_HEIGHT);
768 /*if (moveUpOffset == 0)
769 moveUpOffset = 0;*/
770 }
771 else
772 {
773 _THROWEX(ex_illegalArguments, "Block added outside allowed cords", "CPlayingField", "addBlock",
774 "x = " << x << ", y = " << y << ", type = " << type);
775 }
776 }
777
778 void CPlayingField::setBlockAnim(int x, int y, const string &name)
779 {
780 if (blocks[x][y])
781 {
782 cout << "setAnimation " << name << endl;
783 blocks[x][y]->setAnimation(name);
784 }
785 }
786
787 bool CPlayingField::boardCords(CVector p, int &x, int &y)
788 {
789 if (rect.contains(p.getX(), p.getY()))
790 {
791 CVector tmp = p - pos;
792 x = static_cast<int> (tmp.getX()/width);
793 y = static_cast<int> (tmp.getY()/height);
794 }
795 else
796 {
797 return false;
798 }
799 return true;
800 }
801
802 void CPlayingField::removeBlock(int x, int y)
803 {
804 if (blocks[x][y])
805 {
806 numBlocks--;
807 // LOG("removing block at (" << x << ", " << y << ")", 5, LOG_INFO);
808
809 blocks[x][y]->clear();
810
811 freeBlocks.remove(blocks[x][y]);
812 delete blocks[x][y];
813 blocks[x][y] = NULL;
814 }
815 }
816
817 bool CPlayingField::blockExists(int x, int y)
818 {
819 if (x < FIELD_WIDTH && x >= 0 && y < FIELD_HEIGHT && y >= 0)
820 return blocks[x][y] != NULL;
821 else
822 return false;
823 }