-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassicgamewidget.cpp
795 lines (689 loc) · 21 KB
/
classicgamewidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
#include "classicgamewidget.h"
#include <QPainter>
#include <QPixmap>
#include <QPointF>
#include <QTimer>
#include "effectpainter.h"
#include "rules.h"
//#include "sixtyonegameboardinfo.h"
#include "connections.h"
#include "corecontroller.h"
#include "gesturecontroller.h"
#include "basicpainter.h"
#include "gamecommonitems.h"
#include "thirtysevengameboardinfo.h"
#include "othergameinit.h"
#include "resetwidget.h"
#include "gameoverwidget.h"
#include "publicgamesounds.h"
#include "ball.h"
#include "statistic.h"
extern Statistic statistic;
ClassicGameWidget::ClassicGameWidget(AbstractRule::Gesture gesture) :
frameCount(0),
endAnimCount(-1)
{
// Create the rule
if (gesture == AbstractRule::Swap)
rule = new SwapClassicGameRule();
else
rule = new RotateClassicGameRule();
// Create the gameboard info
gameboardInfo = new ThirtySevenGameBoardInfo();
// Load the game
OtherGameRecord *record =
OtherGameInit::loadOtherGame(getIndex());
// Create the controller
controller = new CoreController
(rule, gameboardInfo, record->balls);
// Move the balls to the correct position
// and avoid elimination at the beginning
controller->fillAllBlanks();
do
{
controller->setNeedTestStableEliminate(true);
for (int i = 0;i < gameboardInfo->totalBallCounts();++i)
if (controller->balls[i])
controller->balls[i]->moveToStablePos();
} while (controller->advance());
for (int i = 0;i < gameboardInfo->totalBallCounts();++i)
if (controller->balls[i])
controller->balls[i]->setState(Ball::JustCreated);
controller->autoRotate();
// Create the effect painter
effectPainter = new EffectPainter(gameboardInfo);
// Create the gesture controller
gestureController = new GestureController(rule,
gameboardInfo,
controller,
effectPainter);
// Create the items and initialize them
hightestScore = new IntegerItem(182);
hightestScore->setPos(QPointF(0.1, 0.1));
hightestScore->setValue(record->highestScore);
hightestScore->setHint("Highest Score");
myItems.push_back(hightestScore);
currentLevel = new IntegerItem(182);
currentLevel->setPos(QPointF(0.1, 0.25));
currentLevel->setValue(record->currentLevel);
currentLevel->setHint("Current Level");
myItems.push_back(currentLevel);
progressBar = new VerticalProgressBarItem();
progressBar->setPos(QPointF(0.25, 0.5));
progressBar->setCurrent(record->currentScore);
progressBar->setMin(record->minScore);
progressBar->setMax(record->maxScore);
myItems.push_back(progressBar);
flame = new FlameItem();
flame->setPos(QPointF(0.1, 0.4));
flame->setCurrent(record->flame);
myItems.push_back(flame);
star = new StarItem();
star->setPos(QPointF(0.1, 0.55));
star->setCurrent(record->star);
myItems.push_back(star);
hint = new ButtonItem("Hint");
hint->setPos(QPointF(0.1, 0.7));
myItems.push_back(hint);
resetItem = new ButtonItem("Reset");
resetItem->setPos(QPointF(0.1, 0.8));
myItems.push_back(resetItem);
exitItem = new ButtonItem("Exit");
exitItem->setPos(QPointF(0.1, 0.9));
myItems.push_back(exitItem);
// No items was chosen
itemAtPressPos = NULL;
// Release the space
delete record;
// Connect signals and slots
connect(controller,
SIGNAL(goodMove()),
this,
SLOT(goodMove()));
connect(controller,
SIGNAL(badMove()),
this,
SLOT(badMove()));
connect(controller,
SIGNAL(stableEliminateTested(Connections)),
this,
SLOT(dealStableEliminate(Connections)));
connect(controller,
SIGNAL(userMovingEliminateTested(Connections)),
this,
SLOT(dealUserMovingEliminate(Connections)));
// Create the timer and connect signals and slots
t = new QTimer();
t->setInterval(75);
connect(t, SIGNAL(timeout()), this, SLOT(advance()));
}
void ClassicGameWidget::makePixmap(
#ifdef USE_PIXMAP
QPixmap& pixmap,
#else
QPainter* painter,
#endif
int width,
int height)
{
#ifdef USE_PIXMAP
makeBasicPixmap(pixmap, width, height);
addEffect(pixmap, width, height);
#else
makeBasicPixmap(painter, width, height);
addEffect(painter, width, height);
#endif
}
ClassicGameWidget::~ClassicGameWidget()
{
// Stop the timer
t->stop();
// Release the space
delete t;
for (int i = 0;i < myItems.size();++i)
delete myItems[i];
delete controller;
delete gameboardInfo;
delete rule;
delete gestureController;
delete effectPainter;
}
void ClassicGameWidget::makeBasicPixmap(
#ifdef USE_PIXMAP
QPixmap& pixmap,
#else
QPainter* painter,
#endif
int width,
int height)
{
#ifdef USE_PIXMAP
pixmap = QPixmap(width, height);
// Fill the pixmap with black background
pixmap.fill(Qt::black);
// Get the painter
QPainter *painter = new QPainter(&pixmap);
#endif
// Get the balls
Ball **balls = controller->balls;
// Paint the background
BasicPainter::paintBackGround(BasicPainter::Game37,
painter,
width,
height,
frameCount);
// Paint the basic balls
BasicPainter::paintBasicBalls
(gameboardInfo,
balls,
gameboardInfo->totalBallCounts(),
painter,
width * 1.0 / gameboardInfo->width(),
height * 1.0 / gameboardInfo->height(),
frameCount);
// Paint the items
BasicPainter::paintItems(painter,
myItems,
width,
height,
frameCount);
#ifdef USE_PIXMAP
// End the paint and release the space
painter->end();
delete painter;
#endif
}
void ClassicGameWidget::addEffect(
#ifdef USE_PIXMAP
QPixmap& pixmap,
#else
QPainter* painter,
#endif
int width,
int height)
{
#ifdef USE_PIXMAP
// Get the painter
QPainter *painter = new QPainter(&pixmap);
#endif
// Calculte the bonus hint and show it
QPointF pos = currentPos;
pos.setX(currentPos.x() *
width /
gameboardInfo->width());
pos.setY(currentPos.y() *
height /
gameboardInfo->height());
effectPainter->clearBonusEliminationHints();
if (itemAtPressPos != NULL)
{
if (itemAtPressPos == flame && flame->notEmpty())
{
flame->paintLocatingIcon(painter, width, height, pos, frameCount);
int index =
gameboardInfo->indexOfPosition(currentPos);
flame->paintInfluencedArea(index,
gameboardInfo,
effectPainter,
frameCount);
}
else if (itemAtPressPos == star && star->notEmpty())
{
star->paintLocatingIcon(painter, width, height, pos, frameCount);
int index =
gameboardInfo->indexOfPosition(currentPos);
star->paintInfluencedArea(index,
gameboardInfo,
effectPainter,
frameCount);
}
}
// Paint the effects
effectPainter->paint(painter,
width *
1.0 /
gameboardInfo->width(),
height *
1.0 /
gameboardInfo->height());
#ifdef USE_PIXMAP
// End the paint and release the space
painter->end();
delete painter;
#endif
// Advance the effect painter
effectPainter->advance();
}
QPointF ClassicGameWidget::toScene(double xRate,
double yRate)
{
return QPointF(xRate * gameboardInfo->width(),
yRate * gameboardInfo->height());
}
void ClassicGameWidget::showHint()
{
// Get the index of the hint
int hintOnBoard = controller->hint();
// Show the hint if there is a hint
if (hintOnBoard >= 0)
effectPainter->hintAt(
gameboardInfo->positionOfIndex(hintOnBoard),
rule->gestureAllowed(AbstractRule::Rotate));
else if (flame->getCurrent() > 0)
// Else if there is a flame
effectPainter->hintAt(
toScene(flame->getPos().x(),
flame->getPos().y()),
false);
// Else if there is a star
else if (star->getCurrent() > 0)
effectPainter->hintAt(
toScene(star->getPos().x(),
star->getPos().y()),
false);
else
gameOver();
}
void ClassicGameWidget::preGameOver()
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::GameOver);
endAnimCount = 20;
int dxs[] = {1, 2, 3, 4, 4, 4, 4, 4};
double y = gameboardInfo->height() + gameboardInfo->ballR();
double centerX = gameboardInfo->centerPos().x();
for (int i = 0;i < gameboardInfo->totalBallCounts();++i)
if (controller->balls[i])
{
double cx = controller->balls[i]->pos().x();
double cy = controller->balls[i]->pos().y();
double dx = rand() % gameboardInfo->width() / 2 - gameboardInfo->width() / 4;
for (int j = 0; j < 6;++j)
{
int positive = 1;
if (j % 2 == i % 2)
positive = -1;
controller->translateABallTo(controller->balls[i],
QPointF(cx + positive * dxs[j], cy),
1,
true);
}
controller->translateABallTo(controller->balls[i],
QPointF(centerX + dx, y),
endAnimCount - 6,
false);
}
}
void ClassicGameWidget::gameOver()
{
// Clear the game record
OtherGameInit::clearGame(getIndex());
// Test the highest score
OtherGameInit::testHighest(getIndex(),
progressBar->getCurrent());
// Create the game over widget and give control to it
GameOverWidget *w = new GameOverWidget
(getIndex(),
progressBar->getCurrent());
emit giveControlTo(w, true);
delete this;
}
void ClassicGameWidget::quitGame()
{
// Make the record and save it
OtherGameRecord record;
record.currentLevel = currentLevel->getValue();
record.minScore = progressBar->getMin();
record.currentScore = progressBar->getCurrent();
record.maxScore = progressBar->getMax();
record.flame = flame->getCurrent();
record.star = star->getCurrent();
record.balls = controller->balls;
OtherGameInit::saveOtherGame
(&record,
getIndex(),
gameboardInfo->totalBallCounts());
emit giveControlTo(NULL, true);
delete this;
}
void ClassicGameWidget::dealPressed(QPointF mousePos,
Qt::MouseButton button)
{
if (endAnimCount != -1)
return;
// Choose the correct item at press position
currentPos = mousePos;
if (flame->in(mousePos, gameboardInfo->width(), gameboardInfo->height()))
itemAtPressPos = flame;
else if (star->in(mousePos, gameboardInfo->width(), gameboardInfo->height()))
itemAtPressPos = star;
else if (hint->in(mousePos, gameboardInfo->width(), gameboardInfo->height()))
itemAtPressPos = hint;
else if (resetItem->in(mousePos,
gameboardInfo->width(),
gameboardInfo->height()))
itemAtPressPos = resetItem;
else if (exitItem->in(mousePos,
gameboardInfo->width(),
gameboardInfo->height()))
itemAtPressPos = exitItem;
else
itemAtPressPos = NULL;
// Quit if it's a right button
// May be abandoned later
if (button == Qt::RightButton)
{
quitGame();
return;
}
// Let the gesture controller to deal the press event
gestureController->dealPressed(mousePos);
}
void ClassicGameWidget::dealMoved(QPointF mousePos,
Qt::MouseButton button)
{
if (endAnimCount != -1)
return;
// Record the current position
currentPos = mousePos;
// Clear user moving elimination hints
effectPainter->clearUserMovingEliminationHints();
// Let the gesture controller to deal the move event
gestureController->dealMoved(mousePos);
}
void ClassicGameWidget::dealReleased(QPointF mousePos, Qt::MouseButton button)
{
if (endAnimCount != -1)
return;
if (itemAtPressPos != NULL)
{
if (itemAtPressPos == flame && flame->notEmpty())
{
int index = gameboardInfo->indexOfPosition(mousePos);
if (index != -1)
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::UseFlame);
// Tell the controller to eliminate the balls
controller->flameAt(index);
// Add effects to effect painter
effectPainter->explodeAt(index);
effectPainter->flash();
// Minus the value of the flame
flame->minusOne();
statistic.changeStatistic(Statistic::FlameUsedCount, 1, true);
}
}
else if (itemAtPressPos == star && star->notEmpty())
{
int index = gameboardInfo->indexOfPosition(mousePos);
if (index != -1)
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::UseStar);
// Tell the controller to eliminate the balls
controller->starAt(index);
// Add effects to effect painter
effectPainter->lightningAt(index);
effectPainter->flash();
// Minus the value of the flame
star->minusOne();
statistic.changeStatistic(Statistic::StarUsedCount, 1, true);
}
}
else if (itemAtPressPos == hint &&
hint->in(mousePos,
gameboardInfo->width(),
gameboardInfo->height()))
{
// Reduce the score
int score = qMax(progressBar->getCurrent() - 10,
progressBar->getMin());
progressBar->setCurrent(score);
// Show the hint
showHint();
}
else if (itemAtPressPos == resetItem &&
resetItem->in(mousePos,
gameboardInfo->width(),
gameboardInfo->height()))
{
// Create the reset widget
ResetWidget *w = new ResetWidget();
// Connect
connect(w, SIGNAL(confirm()), this, SLOT(reset()));
// Give control to it
emit giveControlTo(w, false);
}
else if (itemAtPressPos == exitItem &&
exitItem->in(mousePos,
gameboardInfo->width(),
gameboardInfo->height()))
{
// Quit game
quitGame();
return;
}
}
// Clear user moving elimination hints
effectPainter->clearUserMovingEliminationHints();
itemAtPressPos = NULL;
// Let the gesture controller to deal the release event
gestureController->dealReleased(mousePos);
}
void ClassicGameWidget::getForcus()
{
// Start the timer
t->start();
}
void ClassicGameWidget::advance()
{
// Add the frame count
++frameCount;
if (endAnimCount == -1)
{
// Advance the controller
controller->advance();
// Go to next stage if the score has been reached
if (progressBar->getCurrent() >= progressBar->getMax())
{
bool allStable = true;
for (int i = 0;i < gameboardInfo->totalBallCounts();++i)
if ((!controller->balls[i]) ||
controller->balls[i]->getState() != Ball::Stable)
{
allStable = false;
break;
}
if (allStable)
nextStage();
return;
}
// Record the no solution count
if (flame->getCurrent() == 0 && star->getCurrent() == 0)
{
bool allStable = true;
for (int i = 0;i < gameboardInfo->totalBallCounts();++i)
if ((!controller->balls[i]) ||
controller->balls[i]->getState() != Ball::Stable)
{
allStable = false;
break;
}
if (allStable && controller->hint() < 0)
preGameOver();
}
}
else
{
--endAnimCount;
for (int i = 0;i < gameboardInfo->totalBallCounts();++i)
if (controller->balls[i])
controller->balls[i]->advance();
if (endAnimCount == 0)
gameOver();
}
}
void ClassicGameWidget::dealStableEliminate
(Connections connections)
{
// Calculate the points to add and the bonus
int pointsToAdd = 0;
for (int i = 0;i < gameboardInfo->totalBallCounts();++i)
{
QVector<QVector<int> *>& connect =
connections.connectionsOfIndex[i];
int connectionCountOfThePosition = 0;
for (int j = 0;j < 10;++j)
{
if (j == 3 || connect[j] == NULL)
continue;
++connectionCountOfThePosition;
}
if (connectionCountOfThePosition > 0)
effectPainter->highlightAt(i);
if (connectionCountOfThePosition > 1)
{
if (connectionCountOfThePosition >= 2)
effectPainter->flash();
if (connectionCountOfThePosition == 2)
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::GetFlame);
// Get a flame
flame->addOne();
statistic.changeStatistic(Statistic::FlameGetCount, 1, true);
}
if (connectionCountOfThePosition >= 3)
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::GetStar);
// Get a star
star->addOne();
statistic.changeStatistic(Statistic::StarGetCount, 1, true);
}
}
if (connectionCountOfThePosition > 0)
++pointsToAdd;
}
for (int i = 0;i < connections.connections.size();++i)
{
int size = connections.connections[i]->size();
QPointF pos1(gameboardInfo->positionOfIndex(
connections.connections[i]->at(0)));
QPointF pos2(gameboardInfo->positionOfIndex(
connections.connections[i]->at(size - 1)));
effectPainter->wordsAt(
QPointF((pos1.x() + pos2.x()) / 2,
(pos1.y() + pos2.y()) / 2),
tr("%1").arg(size),
size);
if (size >= 4)
effectPainter->flash();
if (size == 4)
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::GetFlame);
// Get a flame
flame->addOne();
statistic.changeStatistic(Statistic::FlameGetCount, 1, true);
}
if (size >= 5)
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::GetStar);
// Get a star
star->addOne();
statistic.changeStatistic(Statistic::StarGetCount, 1, true);
}
}
// Add sound effect if neccessary
if (pointsToAdd > 0)
PublicGameSounds::addEliminate(pointsToAdd);
// Set the score
progressBar->setCurrent(
progressBar->getCurrent() + pointsToAdd);
// // Reset the highest score if neccessary
// if (progressBar->getCurrent() >
// hightestScore->getValue())
// {
// OtherGameInit::testHighest(getIndex(),
// progressBar->getCurrent());
// hightestScore->setValue(progressBar->getCurrent());
// }
}
void ClassicGameWidget::dealUserMovingEliminate(
Connections connections)
{
// Add moving elimination hint if neccessary
if (rule->gestureAllowed(AbstractRule::Rotate))
{
effectPainter->clearUserMovingEliminationHints();
for (int i = 0;i < connections.connections.size();++i)
{
for (int j = 0;
j < connections.connections[i]->size();
++j)
effectPainter->userMovingEliminationHintAt(
connections.connections[i]->at(j));
}
}
}
void ClassicGameWidget::nextStage()
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::NextStage);
// Record the initial state of next stage
OtherGameRecord record;
record.currentLevel = currentLevel->getValue() + 1;
record.minScore = progressBar->getMax();
record.currentScore = progressBar->getCurrent();
record.maxScore = progressBar->getMax() * 2;
record.flame = flame->getCurrent();
record.star = star->getCurrent();
record.balls = NULL;
OtherGameInit::saveOtherGame(
&record,
getIndex(),
gameboardInfo->totalBallCounts());
// Create another widget of classic game and give control
// to it
ClassicGameWidget *nextStage;
if (rule->gestureAllowed(AbstractRule::Swap))
nextStage = new ClassicGameWidget(AbstractRule::Swap);
else
nextStage =
new ClassicGameWidget(AbstractRule::Rotate);
emit giveControlTo(nextStage, true);
delete this;
}
int ClassicGameWidget::getIndex()
{
return 0 + (rule->gestureAllowed(AbstractRule::Rotate) ?
1 :
0);
}
void ClassicGameWidget::reset()
{
OtherGameInit::clearGame(getIndex());
ClassicGameWidget *resetGame;
t->stop();
if (rule->gestureAllowed(AbstractRule::Swap))
resetGame = new ClassicGameWidget(AbstractRule::Swap);
else
resetGame = new ClassicGameWidget(AbstractRule::Rotate);
emit giveControlTo(resetGame, true);
delete this;
}
void ClassicGameWidget::goodMove()
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::GoodMove);
statistic.changeStatistic(Statistic::GoodMoveCount, 1, true);
}
void ClassicGameWidget::badMove()
{
// Add sound effect
PublicGameSounds::addSound(PublicGameSounds::BadMove);
statistic.changeStatistic(Statistic::BadMoveCount, 1, true);
}