/
field.cpp
666 lines (583 loc) · 18.3 KB
/
field.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
#include "field.h"
#include "resourceloader.h"
#include "svgrendereritem.h"
#include <QParallelAnimationGroup>
#include <QSequentialAnimationGroup>
#include <QPropertyAnimation>
#ifdef QT_DEBUG
#include <QDateTime>
#endif
#define MOVE_DURATION 50
#define APPEAR_DURATION 100
#define GROW_DURATION 150
#define DISAPPEAR_DURATION 250
Field::Field(QGraphicsScene *scene) //: QObject()
{
m_scene = scene;
m_animating = false;
m_pathItems.fill(NULL, W * H);
ResourceLoader *loader = ResourceLoader::instance();
for (int i = 0; i < W; i++)
{
for (int j = 0; j < H; j++)
{
QGraphicsObject *item = new SvgRendererItem(loader->tile());
item->setPos(i * CELL_SIZE, j * CELL_SIZE);
#ifdef QT_DEBUG
//if (j % 2 && i % 2)
// item->setScale(0.5);
#endif
item->setZValue(0);
m_scene->addItem(item);
item = new SvgRendererItem(loader->pathmarker());
item->setPos(i * CELL_SIZE, j * CELL_SIZE);
item->setZValue(2);
item->setVisible(false);
m_scene->addItem(item);
setVectorAt(m_pathItems, i, j, item);
}
}
m_sel = QPoint(-1, -1);
m_selItem = new SvgRendererItem(loader->select());
m_selItem->setZValue(1);
m_selItem->setVisible(false);
m_scene->addItem(m_selItem);
m_field.fill(0, W * H);
m_fieldItems.fill(NULL, W * H);
m_next.fill({ QPoint(-1, -1), 0, NULL, NULL }, 3);
}
Field *Field::m_instance = NULL;
Field *Field::initInstance(QGraphicsScene *scene)
{
m_instance = new Field(scene);
return m_instance;
}
Field *Field::instance()
{
return m_instance;
}
Field::~Field()
{
}
bool Field::animating()
{
return m_animating;
}
void Field::setAnimating(bool value)
{
m_animating = value;
}
void Field::throwBalls(int num, bool directly)
{
QVector<QPoint> free;
free.fill(QPoint(0, 0), W * H);
int nFree = 0;
for (int i = 0; i < W; i++)
for (int j = 0; j < H; j++)
if (at(i, j) == 0)
{
free[nFree] = QPoint(i, j);
nFree++;
}
#ifdef QT_DEBUG
for (int i = 0; i < W * H; i++)
{
//qDebug("%d, %d", free[i].x(), free[i].y());
}
#endif
//return;
QList<SuggestedBall> ballsToThrow;
for (int n = 0; n < num; n++)
{
if (nFree > 0)
{
int at = qrand() % nFree;
ballsToThrow.append({ free[at], qrand() % COLORS + 1, NULL, NULL });
free.removeAt(at);
nFree--;
}
else
ballsToThrow.append({ QPoint(-1, -1), qrand() % COLORS + 1, NULL, NULL });
}
if (directly)
{
// This is needed on startup only, therefore do nothing except placing balls
foreach (auto ball, ballsToThrow)
{
setAt(ball.pt, ball.color);
QGraphicsObject *item = new SvgRendererItem(ResourceLoader::instance()->ball(ball.color));
item->setPos(ball.pt * CELL_SIZE);
item->setZValue(3);
m_scene->addItem(item);
setVectorAt(m_fieldItems, ball.pt, item);
}
}
else
{
for (int i = 0; i < num; i++)
{
auto ball = m_next[i];
if (ball.toolbarItem != NULL)
{
m_scene->removeItem(ball.toolbarItem);
ball.toolbarItem->deleteLater();
}
if (ball.fieldItem != NULL)
{
m_scene->removeItem(ball.fieldItem);
ball.fieldItem->deleteLater();
}
}
if (m_animating)
{
//lock();
QAnimationGroup *group = addAnimGroup(new QParallelAnimationGroup);
for (int i = 0; i < num; i++)
{
auto ball = ballsToThrow[i];
// TODO : add toolbar ball display
if (ball.pt.x() > -1)
{
QGraphicsObject *item = new SvgRendererItem(ResourceLoader::instance()->ball(ball.color));
item->setScale(0.0);
item->setPos((QPointF(0.5, 0.5) + ball.pt) * CELL_SIZE);
item->setZValue(2);
m_scene->addItem(item);
ball.fieldItem = item;
QPropertyAnimation *anim = new QPropertyAnimation(item, "scale");
anim->setEndValue(0.4);
anim->setDuration(APPEAR_DURATION);
group->addAnimation(anim);
anim = new QPropertyAnimation(item, "pos");
anim->setEndValue((QPointF(0.3, 0.3) + ball.pt) * CELL_SIZE);
anim->setDuration(APPEAR_DURATION);
group->addAnimation(anim);
}
m_next[i] = ball;
}
//if (!m_deleting) // Deletion animation lasts longer,
// so suppress unlock if deleting at the same time
//group->connect(group, SIGNAL(finished()), this, SLOT(unlock()));
group->start(QAbstractAnimation::DeleteWhenStopped);
}
else
{
for (int i = 0; i < num; i++)
{
auto ball = ballsToThrow[i];
// TODO : add toolbar ball display
QGraphicsObject *item = new SvgRendererItem(ResourceLoader::instance()->ball(ball.color));
item->setScale(0.4);
item->setPos((QPointF(0.3, 0.3) + ball.pt) * CELL_SIZE);
item->setZValue(2);
m_scene->addItem(item);
ball.fieldItem = item;
m_next[i] = ball;
}
}
}
}
void Field::initGame(GameType gameType)
{
setAnimating(false);
clear();
throwBalls(3, true);
throwBalls(3);
m_gameType = gameType;
setAnimating();
setScore(0);
unlock();
}
void Field::clear()
{
while (!m_activeAnimGroups.empty())
{
QAnimationGroup *group = m_activeAnimGroups.first();
group->disconnect();
group->stop();
m_activeAnimGroups.removeFirst();
}
for (int i = 0; i < W; i++)
for (int j = 0; j < H; j++)
if (at(i, j))
{
QGraphicsObject *item = vectorAt(m_fieldItems, i, j);
m_scene->removeItem(item);
item->deleteLater();
}
for (int k = 0; k < 3; k++)
{
auto ball = m_next[k];
if (ball.toolbarItem != NULL)
{
QGraphicsObject *item = ball.toolbarItem;
m_scene->removeItem(item);
item->deleteLater();
}
if (ball.fieldItem != NULL)
{
QGraphicsObject *item = ball.fieldItem;
m_scene->removeItem(item);
item->deleteLater();
}
}
m_field.fill(0, W * H);
m_fieldItems.fill(NULL, W * H);
m_next.fill({ QPoint(-1, -1), 0, NULL, NULL });
unselect();
}
bool Field::onField(int x, int y)
{
return x > -1 && y > -1 && x < W && y < H;
}
bool Field::onField(QPoint point)
{
return onField(point.x(), point.y());
}
void Field::activate(int x, int y)
{
activate(QPoint(x, y));
}
void Field::activate(QPoint point)
{
if (locked() || !onField(point))
return;
if (at(point))
{
if (m_sel != point)
ResourceLoader::instance()->selectSound()->play();
m_sel = point;
m_selItem->setPos(point * CELL_SIZE);
m_selItem->setVisible(true);
#ifdef QT_DEBUG
//for (int i = 0; i < W; i++)
// for (int j = 0; j < H; j++)
// vectorAt(m_pathItems, i, j)->setVisible(false);
#endif
}
else if (onField(m_sel) && at(m_sel))
{
moveSelected(point);
}
}
void Field::unselect()
{
m_sel = QPoint(-1, -1);
m_selItem->setVisible(false);
}
QPoint Field::selected()
{
return m_sel;
}
void Field::moveSelected(QPoint destination)
{
QVector<int> vector = m_field;
int step = 0;
setVectorAt(vector, m_sel, -1);
bool bContinue, bHasPath = false;
QPoint points[4];
points[0] = QPoint(1, 0);
points[1] = QPoint(0, 1);
points[2] = QPoint(-1, 0);
points[3] = QPoint(0, -1);
// Looking whether there is a path to destination
do
{
step--;
bContinue = false;
for (int i = 0; i < W; i++)
for (int j = 0; j < H; j++)
if (!vectorAt(vector, i, j))
for (int k = 0; k < 4; k++)
{
QPoint point = points[k] + QPoint(i, j);
if (onField(point) && vectorAt(vector, point) == step)
{
if (destination == QPoint(i, j))
bHasPath = true;
setVectorAt(vector, i, j, step - 1);
bContinue = !bHasPath;
break;
}
}
} while (bContinue);
#ifdef QT_DEBUG
//qDebug("has path: %d", (int)bHasPath);
#endif
if (!bHasPath)
{
ResourceLoader::instance()->error()->play();
return;
}
// Finding shortest
QList<QPoint> path;
for (QPoint current = destination; current != m_sel; )
{
path.prepend(current);
for (int k = 0; k < 4; k++)
{
QPoint point = points[k] + current;
if (onField(point) && vectorAt(vector, point) - vectorAt(vector, current) == 1)
{
current = point;
break;
}
}
}
#ifdef QT_DEBUG
//for (int i = 0; i < W; i++)
// for (int j = 0; j < H; j++)
// vectorAt(m_pathItems, i, j)->setVisible(path.contains(QPoint(i, j)));
#endif
path.prepend(m_sel);
int num = path.count() - 1;
for (int i = 1; i < num; i++)
{
if ((path[i - 1].x() == path[i].x() &&
path[i + 1].x() == path[i].x()) ||
(path[i - 1].y() == path[i].y() &&
path[i + 1].y() == path[i].y()))
{
path.removeAt(i);
num--;
i--;
}
}
num++;
QAnimationGroup *group = addAnimGroup(new QSequentialAnimationGroup);
for (int i = 1; i < num; i++)
{
QPropertyAnimation *anim = new QPropertyAnimation(vectorAt(m_fieldItems, m_sel), "pos");
anim->setEndValue(path[i] * CELL_SIZE);
anim->setDuration(MOVE_DURATION * (qAbs(path[i].x() - path[i - 1].x()) + qAbs(path[i].y() - path[i - 1].y())));
group->addAnimation(anim);
}
setAt(destination, at(m_sel));
setAt(m_sel, 0);
setVectorAt(m_fieldItems, destination, vectorAt(m_fieldItems, m_sel));
setVectorAt(m_fieldItems, m_sel, (QGraphicsObject *)NULL);
unselect();
if (checkForDeletion())
group->connect(group, SIGNAL(finished()), this, SLOT(deleteBalls()));
else
group->connect(group, SIGNAL(finished()), this, SLOT(growBalls()));
group->start(QAbstractAnimation::DeleteWhenStopped);
/*foreach (QPoint point, path)
{
QGraphicsObject *item = new SvgRendererItem(ResourceLoader::instance()->pathmarker());
item->setPos(point * CELL_SIZE);
item->setZValue(1);
m_scene->addItem(item);
}*/
}
bool Field::checkForDeletion()
{
m_deletionMap.fill(false, W * H);
bool res = false;
int cur;
switch (m_gameType)
{
case Lines:
case Squares:
case Blocks:
for (int i = 0; i < W - 1; i++)
for (int j = 0; j < H - 1; j++)
if ((cur = at(i, j)) &&
cur == at(i + 1, j) &&
cur == at(i, j + 1) &&
cur == at(i + 1, j + 1))
{
setVectorAt(m_deletionMap, i, j, true);
setVectorAt(m_deletionMap, i + 1, j, true);
setVectorAt(m_deletionMap, i, j + 1, true);
setVectorAt(m_deletionMap, i + 1, j + 1, true);
res = true;
}
break;
}
return res;
}
int Field::at(int x, int y)
{
return m_field[W * y + x];
}
int Field::at(QPoint point)
{
return m_field[W * point.y() + point.x()];
}
void Field::setAt(int x, int y, int value)
{
m_field[W * y + x] = value;
}
void Field::setAt(QPoint point, int value)
{
m_field[W * point.y() + point.x()] = value;
}
int Field::score() const
{
return m_score;
}
void Field::setScore(int score)
{
m_score = score;
emit scoreChanged(score);
}
QAnimationGroup *Field::addAnimGroup(QAnimationGroup *group)
{
m_activeAnimGroups.append(group);
group->connect(group, SIGNAL(finished()), this, SLOT(deleteAnimGroup()));
lock();
return group;
}
GameType Field::gameType() const
{
return m_gameType;
}
bool Field::locked() const
{
return m_locked;
}
void Field::lock()
{
m_locked = true;
}
void Field::unlock()
{
#ifdef QT_DEBUG
qDebug("unlock() called: %s", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toLocal8Bit().data());
#endif
m_locked = false;
}
void Field::deleteAnimGroup()
{
#ifdef QT_DEBUG
qDebug("deleteAnimGroup() called: %s", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toLocal8Bit().data());
#endif
QAnimationGroup *group = qobject_cast<QAnimationGroup *>(QObject::sender());
if (group == NULL)
return;
m_activeAnimGroups.removeOne(group);
if (m_activeAnimGroups.empty())
unlock();
}
void Field::throwBalls()
{
throwBalls(3);
}
void Field::deleteBalls()
{
QAnimationGroup *group = addAnimGroup(new QParallelAnimationGroup);
int count = 0;
for (int i = 0; i < W; i++)
for (int j = 0; j < H; j++)
if (vectorAt(m_deletionMap, i, j))
{
count++;
setAt(i, j, 0);
QGraphicsObject *item = vectorAt(m_fieldItems, i, j);
QPropertyAnimation *anim = new QPropertyAnimation(item, "scale");
anim->setEndValue(0.0);
anim->setDuration(DISAPPEAR_DURATION);
group->addAnimation(anim);
anim = new QPropertyAnimation(item, "pos");
anim->setEndValue((QPointF(0.5, 0.5) + QPoint(i, j)) * CELL_SIZE);
anim->setDuration(DISAPPEAR_DURATION);
group->addAnimation(anim);
}
int nBalls = (m_gameType == Lines) ? 5 : (m_gameType == Squares) ? 4 : 7;
#ifdef QT_DEBUG
nBalls = 4;
#endif
setScore(score() + count * (count - (nBalls - 1)));
group->connect(group, SIGNAL(finished()), this, SLOT(finishDeletion()));
ResourceLoader::instance()->pop()->play();
group->start(QAbstractAnimation::DeleteWhenStopped);
}
void Field::finishDeletion()
{
for (int i = 0; i < W; i++)
for (int j = 0; j < H; j++)
if (vectorAt(m_deletionMap, i, j))
{
QGraphicsObject *item = vectorAt(m_fieldItems, i, j);
m_scene->removeItem(item);
item->deleteLater();
setVectorAt(m_fieldItems, i, j, (QGraphicsObject *)NULL);
}
}
void Field::growBalls()
{
QAnimationGroup *group = addAnimGroup(new QParallelAnimationGroup);
bool gameOver = false;
// Moving balls whose positions were occupied and
// Those whose not were positioned to new positions
for (int i = 0; i < 3; i++)
{
SuggestedBall ball = m_next[i];
if (!onField(ball.pt) || at(ball.pt))
{
QVector<QPoint> free;
int nFree = 0;
free.resize(W * H);
QList<QPoint> points;
foreach (SuggestedBall b, m_next)
points.append(b.pt);
for (int i = 0; i < W; i++)
for (int j = 0; j < H; j++)
{
if (at(i, j) == 0 && !points.contains(QPoint(i, j)))
{
free[nFree] = QPoint(i, j);
nFree++;
}
}
if (nFree)
{
ball.pt = free[qrand() % nFree];
if (ball.fieldItem == NULL)
{
QGraphicsObject *item = new SvgRendererItem(ResourceLoader::instance()->ball(ball.color));
item->setScale(0.4);
item->setZValue(2);
m_scene->addItem(item);
ball.fieldItem = item;
}
ball.fieldItem->setPos((QPointF(0.3, 0.3) + ball.pt) * CELL_SIZE);
}
else
{
gameOver = true;
}
}
if (onField(ball.pt))
{
setAt(ball.pt, ball.color);
setVectorAt(m_fieldItems, ball.pt, ball.fieldItem);
QPropertyAnimation *anim = new QPropertyAnimation(ball.fieldItem, "scale");
anim->setEndValue(1.0);
anim->setDuration(GROW_DURATION);
group->addAnimation(anim);
anim = new QPropertyAnimation(ball.fieldItem, "pos");
anim->setEndValue(ball.pt * CELL_SIZE);
anim->setDuration(GROW_DURATION);
group->addAnimation(anim);
ball.fieldItem = NULL;
}
m_next[i] = ball;
}
if (!gameOver)
{
if (checkForDeletion())
{
group->connect(group, SIGNAL(finished()), this, SLOT(deleteBalls()));
group->connect(group, SIGNAL(finished()), this, SLOT(throwBalls()));
}
else if (m_field.contains(0))
group->connect(group, SIGNAL(finished()), this, SLOT(throwBalls()));
else
gameOver = true;
}
if (gameOver)
group->connect(group, SIGNAL(finished()), this, SIGNAL(gameOver())/*, Qt::QueuedConnection*/);
group->start(QAbstractAnimation::DeleteWhenStopped);
}