-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamecontrol.cpp
607 lines (549 loc) · 22.6 KB
/
gamecontrol.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
#include "gamecontrol.h"
#include "assassin.h"
#include "bullet.h"
#include "choicebutton.h"
#include "gameview.h"
#include "nenecchi.h"
#include "protagonist.h"
#include "pushbutton.h"
#include "settings.h"
#include "spikeweed.h"
#include "welcome.h"
#include <QApplication>
#include <QDebug>
#include <QDesktopWidget>
#include <QGraphicsProxyWidget>
#include <QGraphicsScene>
#include <QGroupBox>
#include <QLabel>
#include <QMessageBox>
#include <QPalette>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
#include <QSettings>
#include <QSlider>
#include <QSoundEffect>
#include <QTimer>
#include <QVBoxLayout>
GameControl::GameControl(QObject* parent)
: QObject(parent), scene(new QGraphicsScene), view(new GameView(scene)),
clock(new QTimer), nene_clock(new QTimer), assassin_clock(new QTimer),
settings(new Settings), bgm(new QSoundEffect) {
clock->setInterval(20);
nene_clock->setInterval(3000);
assassin_clock->setInterval(5000);
setScene();
setBgm(":/background/titlebgm");
}
void GameControl::setScene() {
setWelcome();
setSettings();
}
void GameControl::setWelcome() {
welcome = new Welcome;
QGraphicsProxyWidget* t = scene->addWidget(welcome);
const QRectF availableGeometry = view->sceneRect();
t->resize(availableGeometry.width(), availableGeometry.height());
t->setPos(availableGeometry.x(), availableGeometry.y());
welcome->buttLayout->setAlignment(Qt::AlignHCenter);
welcome->layout->setAlignment(Qt::AlignHCenter);
QPalette pal = welcome->palette();
pal.setBrush(QPalette::Background,
QBrush(QPixmap(":/background/yuki")
.scaled(availableGeometry.width(),
availableGeometry.height())));
welcome->setPalette(pal);
QObject::connect(welcome->new_game, &PushButton::clicked, this,
&GameControl::newGame);
QObject::connect(welcome->settings, &PushButton::clicked, this,
&GameControl::showSettings);
QObject::connect(welcome->exit, &PushButton::clicked, &QApplication::exit);
}
void GameControl::setSettings() {
settings = new Settings;
set = scene->addWidget(settings);
// set it on top of the scene
set->setZValue(10);
setSettingsPos();
set->setVisible(false);
QObject::connect(settings->fullscreen, &ChoiceButton::toggled, this,
&GameControl::resizeFullscreen);
QObject::connect(settings->normal, &ChoiceButton::toggled, this,
&GameControl::resizeWindow);
QObject::connect(settings->volume, &QSlider::valueChanged, this,
&GameControl::setVolume);
QObject::connect(settings->showShape, &ChoiceButton::toggled, this,
&GameControl::showShape);
QObject::connect(settings->noShowShape, &ChoiceButton::toggled, this,
&GameControl::noShowShape);
QObject::connect(settings->confirm, &QPushButton::clicked, this,
&GameControl::changeSettings);
QObject::connect(settings->cancel, &PushButton::clicked, this,
&GameControl::restoreSettings);
}
void GameControl::newGame() {
if (!welcome) {
if (QMessageBox::No ==
QMessageBox::question(
view, tr("新游戏"),
tr("<font size=26 "
"face='华康少女文字W5-A'>真的要重新开始游戏吗?</"
"font>")))
return;
delete protangonist;
delete nenecchi;
nenecchi = nullptr;
if (assassin) delete assassin;
delete spikeweed;
} else {
welcome->setVisible(false);
welcome->destroy();
welcome = nullptr;
}
emit inGame(true);
setBgm(":/background/gamebgm");
// scene background
const QRectF availableGeometry = view->sceneRect();
scene->setBackgroundBrush(QBrush(
QPixmap(":/background/gamebackground")
.scaled(availableGeometry.width(), availableGeometry.height())));
// protagonist
protangonist = new Protagonist;
scene->addItem(protangonist);
protangonist->setPos(10, scene->height() / 3);
protangonist->setFocus();
const bool showShape = settings->remark->value("showShape").toBool();
protangonist->showShape = showShape;
// nenecchi
nenecchi = new Nenecchi;
scene->addItem(nenecchi);
nenecchi->setPos(scene->width() / 7 * 5, scene->height() / 4);
nenecchi->showShape = showShape;
const int volume = settings->remark->value("volume").toInt();
for (auto& s : nenecchi->sounds) {
s->setVolume(volume);
}
QObject::connect(nene_clock, &QTimer::timeout, this, &GameControl::neneAct);
ani_nene_group = new QSequentialAnimationGroup;
/*QGraphicsObjects cannot set geometry*/
// QPropertyAnimation* ani_nene_up =
// new QPropertyAnimation(nenecchi, "geometry");
QPropertyAnimation* ani_nene_up = new QPropertyAnimation(nenecchi, "pos");
QPointF nenePos = nenecchi->pos();
ani_nene_up->setDuration(1000);
// ani_nene_up->setStartValue(QRectF(nenePos.x(), nenePos.y(),
// nenecchi->boundingRect().width(),
// nenecchi->boundingRect().height()));
// ani_nene_up->setEndValue(QRectF(nenePos.x(), nenePos.y() - 100,
// nenecchi->boundingRect().width(),
// nenecchi->boundingRect().height()));
ani_nene_up->setStartValue(nenePos);
ani_nene_up->setEndValue(QPointF(nenePos.x(), nenePos.y() - 100));
ani_nene_up->setEasingCurve(QEasingCurve::InOutCubic);
// QPropertyAnimation* ani_nene_down =
// new QPropertyAnimation(nenecchi, "geometry");
QPropertyAnimation* ani_nene_down = new QPropertyAnimation(nenecchi, "pos");
ani_nene_down->setDuration(1000);
// ani_nene_down->setStartValue(QRectF(nenePos.x(), nenePos.y() - 100,
// nenecchi->boundingRect().width(),
// nenecchi->boundingRect().height()));
// ani_nene_down->setEndValue(QRectF(nenePos.x(), nenePos.y(),
// nenecchi->boundingRect().width(),
// nenecchi->boundingRect().height()));
ani_nene_down->setStartValue(QPointF(nenePos.x(), nenePos.y() - 100));
ani_nene_down->setEndValue(nenePos);
ani_nene_down->setEasingCurve(QEasingCurve::InOutCubic);
ani_nene_group->addAnimation(ani_nene_up);
ani_nene_group->addAnimation(ani_nene_down);
ani_nene_group->setLoopCount(-1);
ani_nene_group->start();
// assain
QObject::connect(assassin_clock, &QTimer::timeout, this,
&GameControl::emergeAssassin);
assassin_clock->start();
// spikeweed
spikeweed = new Spikeweed;
spikeweed->showShape = showShape;
scene->addItem(spikeweed);
spikeweed->setPos(scene->width() / 3, scene->height() / 3);
// sword
ani_sword = new QPropertyAnimation(&protangonist->sword, "rotation");
ani_sword->setDuration(500);
ani_sword->setKeyValueAt(0, 0);
ani_sword->setKeyValueAt(0.5, 90);
ani_sword->setKeyValueAt(1, 0);
ani_sword->setLoopCount(1);
QObject::connect(ani_sword, &QPropertyAnimation::finished,
[this]() { protangonist->sword.setVisible(false); });
QObject::connect(protangonist, &Protagonist::attack, this,
&GameControl::attack);
QObject::connect(clock, &QTimer::timeout, this, &GameControl::clockThings);
play();
}
void GameControl::reToTitle() {
if (QMessageBox::No ==
QMessageBox::question(
view, tr("返回标题画面"),
tr("<font size=26 "
"face='华康少女文字W5-A'>真的要返回标题画面吗?</"
"font>")))
return;
emit inGame(false);
setWelcome();
setBgm(":/background/titlebgm");
}
void GameControl::showShape(bool toggled) {
if (!toggled || !protangonist) return;
protangonist->showShape = true;
protangonist->sword.showShape = true;
nenecchi->showShape = true;
spikeweed->showShape = true;
for (auto b : nenecchi->bullets) {
if (*b) (*b)->showShape = true;
}
if (assassin) assassin->showShape = true;
scene->update(scene->sceneRect());
}
void GameControl::noShowShape(bool toggled) {
if (!toggled || !protangonist) return;
protangonist->showShape = false;
protangonist->sword.showShape = false;
nenecchi->showShape = false;
spikeweed->showShape = false;
for (auto b : nenecchi->bullets) {
if (*b) (*b)->showShape = false;
}
if (assassin) assassin->showShape = false;
scene->update(scene->sceneRect());
}
void GameControl::attack() {
protangonist->sword.setVisible(true);
ani_sword->start();
}
void GameControl::clockThings() {
scene->advance();
protangonist->setFocus();
crushThings();
}
void GameControl::crushThings() {
if (protangonist->sword.isVisible() && nenecchi->phase != Nenecchi::Hurt &&
nenecchi->phase != Nenecchi::Die &&
protangonist->sword.collidesWithItem(nenecchi)) {
nene_clock->start(3000);
nenecchi->phase = Nenecchi::Hurt;
nenecchi->son_hurt->play();
nenecchi->blood -= protangonist->attackBlood;
if (nenecchi->blood <= 0) {
nenecchi->son_die->play();
nenecchi->phase = Nenecchi::Die;
ani_nene_group->stop();
scene->update(scene->sceneRect());
}
}
if (!protangonist->hurt) {
for (auto b : nenecchi->bullets) {
if (*b && protangonist->collidesWithItem(*b)) {
protangonist->hurt = true;
QTimer::singleShot(3000,
[this]() { protangonist->hurt = false; });
}
}
if (assassin && protangonist->collidesWithItem(assassin)) {
protangonist->hurt = true;
QTimer::singleShot(3000, [this]() { protangonist->hurt = false; });
}
if (spikeweed && protangonist->collidesWithItem(spikeweed)) {
protangonist->hurt = true;
QTimer::singleShot(3000, [this]() { protangonist->hurt = false; });
}
}
}
void GameControl::neneAct() {
if (nenecchi->phase == Nenecchi::Die) return;
if (nenecchi->phase == Nenecchi::Hurt)
nenecchi->phase = Nenecchi::Attack;
else if (nenecchi->phase == Nenecchi::Normal)
nenecchi->phase = Nenecchi::Attack;
else
nenecchi->phase = Nenecchi::Normal;
switch (nenecchi->phase) {
case Nenecchi::Normal: {
// nenecchi->phase = Nenecchi::Attack;
break;
}
case Nenecchi::Attack: {
nenecchi->son_attack->play();
nenecchi->bullet1 = new Bullet;
nenecchi->bullet2 = new Bullet;
nenecchi->bullet3 = new Bullet;
for (auto b : nenecchi->bullets) {
const bool showShape =
settings->remark->value("showShape").toBool();
(*b)->showShape = showShape;
}
QPointF attackPos = nenecchi->mapToScene(0, 300);
scene->addItem(nenecchi->bullet1);
scene->addItem(nenecchi->bullet2);
scene->addItem(nenecchi->bullet3);
nenecchi->bullet1->setPos(attackPos);
nenecchi->bullet2->setPos(attackPos);
nenecchi->bullet3->setPos(attackPos);
const int DURATION = 200, TOTALDURATION = 2000;
QParallelAnimationGroup* ani_bullet1_group =
new QParallelAnimationGroup;
QPropertyAnimation* ani_bullet1_rotate =
new QPropertyAnimation(nenecchi->bullet1, "rotation");
ani_bullet1_rotate->setDuration(DURATION);
ani_bullet1_rotate->setStartValue(0);
ani_bullet1_rotate->setEndValue(360);
ani_bullet1_rotate->setLoopCount(TOTALDURATION / DURATION);
QPropertyAnimation* ani_bullet1_move =
new QPropertyAnimation(nenecchi->bullet1, "pos");
ani_bullet1_move->setDuration(TOTALDURATION);
ani_bullet1_move->setStartValue(attackPos);
ani_bullet1_move->setEndValue(
QPointF(attackPos.x() - 1000, attackPos.y()));
ani_bullet1_move->setLoopCount(1);
ani_bullet1_group->addAnimation(ani_bullet1_move);
ani_bullet1_group->addAnimation(ani_bullet1_rotate);
QParallelAnimationGroup* ani_bullet2_group =
new QParallelAnimationGroup;
QPropertyAnimation* ani_bullet2_rotate =
new QPropertyAnimation(nenecchi->bullet2, "rotation");
ani_bullet2_rotate->setDuration(DURATION);
ani_bullet2_rotate->setStartValue(0);
ani_bullet2_rotate->setEndValue(360);
ani_bullet2_rotate->setLoopCount(TOTALDURATION / DURATION);
QPropertyAnimation* ani_bullet2_move =
new QPropertyAnimation(nenecchi->bullet2, "pos");
ani_bullet2_move->setDuration(TOTALDURATION);
ani_bullet2_move->setStartValue(attackPos);
ani_bullet2_move->setEndValue(
QPointF(attackPos.x() - 1000, attackPos.y() - 500));
ani_bullet2_move->setLoopCount(1);
ani_bullet2_group->addAnimation(ani_bullet2_move);
ani_bullet2_group->addAnimation(ani_bullet2_rotate);
QParallelAnimationGroup* ani_bullet3_group =
new QParallelAnimationGroup;
QPropertyAnimation* ani_bullet3_rotate =
new QPropertyAnimation(nenecchi->bullet3, "rotation");
ani_bullet3_rotate->setDuration(DURATION);
ani_bullet3_rotate->setStartValue(0);
ani_bullet3_rotate->setEndValue(360);
ani_bullet3_rotate->setLoopCount(TOTALDURATION / DURATION);
QPropertyAnimation* ani_bullet3_move =
new QPropertyAnimation(nenecchi->bullet3, "pos");
ani_bullet3_move->setDuration(TOTALDURATION);
ani_bullet3_move->setStartValue(attackPos);
ani_bullet3_move->setEndValue(
QPointF(attackPos.x() - 1000, attackPos.y() + 500));
ani_bullet3_move->setLoopCount(1);
ani_bullet3_group->addAnimation(ani_bullet3_move);
ani_bullet3_group->addAnimation(ani_bullet3_rotate);
ani_bullet1_group->start(QAbstractAnimation::DeleteWhenStopped);
ani_bullet2_group->start(QAbstractAnimation::DeleteWhenStopped);
ani_bullet3_group->start(QAbstractAnimation::DeleteWhenStopped);
QObject::connect(ani_bullet1_group,
&QParallelAnimationGroup::destroyed,
[this, &ani_bullet1_group]() mutable {
nenecchi->bullet1->setVisible(false);
delete nenecchi->bullet1;
nenecchi->bullet1 = nullptr;
ani_bullet1_group = nullptr;
});
QObject::connect(ani_bullet2_group,
&QParallelAnimationGroup::destroyed,
[this, &ani_bullet2_group]() mutable {
nenecchi->bullet2->setVisible(false);
delete nenecchi->bullet2;
nenecchi->bullet2 = nullptr;
ani_bullet2_group = nullptr;
});
QObject::connect(ani_bullet3_group,
&QParallelAnimationGroup::destroyed,
[this, &ani_bullet3_group]() mutable {
nenecchi->bullet3->setVisible(false);
delete nenecchi->bullet3;
nenecchi->bullet3 = nullptr;
ani_bullet3_group = nullptr;
});
// nenecchi->phase = Nenecchi::Normal;
break;
}
case Nenecchi::Hurt: {
// things have done
break;
}
case Nenecchi::Die: {
// things have done
break;
}
}
}
void GameControl::emergeAssassin() {
assassin = new Assassin;
const bool showShape = settings->remark->value("showShape").toBool();
assassin->showShape = showShape;
scene->addItem(assassin);
assassin->setPos(scene->width() / 7 * 5, protangonist->pos().y());
ani_assassin = new QPropertyAnimation(assassin, "pos");
QTimer* assassin_phase = new QTimer(assassin);
assassin_phase->setInterval(500);
QObject::connect(assassin_phase, &QTimer::timeout, assassin,
&Assassin::switchPhase);
ani_assassin->setDuration(4500);
ani_assassin->setStartValue(assassin->pos());
ani_assassin->setEndValue(
QPointF(assassin->pos().x() - 1500, assassin->pos().y()));
ani_assassin->setLoopCount(1);
ani_assassin->start(QAbstractAnimation::DeleteWhenStopped);
assassin_phase->start();
QObject::connect(ani_assassin, &QPropertyAnimation::destroyed,
[&]() mutable {
if (assassin) assassin->setVisible(false);
delete assassin;
assassin = nullptr;
ani_assassin = nullptr;
});
}
// move settings to the centre of the view
void GameControl::setSettingsPos() {
const QSizeF size = set->size();
const QRectF availableGeometry = view->sceneRect();
set->setPos((availableGeometry.width() - size.width()) / 2,
(availableGeometry.height() - size.height()) / 2);
}
void GameControl::setBgm(const QString& file) {
bgm->setSource(QUrl::fromLocalFile(file));
bgm->setLoopCount(QSoundEffect::Infinite);
bgm->play();
}
void GameControl::restoreSettings() {
if (settings->hasChanged) {
if (QMessageBox::Yes ==
QMessageBox::question(
view, tr("关闭"),
tr("<font size=26 "
"face='华康少女文字W5-A'>设置已被改变,是否要抛弃设置?</"
"font>"))) {
if (settings->remark->value("isFullscreen").toBool()) {
settings->fullscreen->setChecked(true);
settings->normal->setChecked(false);
emit settings->fullscreen->toggled(true);
} else {
settings->fullscreen->setChecked(false);
settings->normal->setChecked(true);
emit settings->normal->toggled(true);
}
if (settings->remark->value("showShape").toBool()) {
settings->showShape->setChecked(true);
settings->noShowShape->setChecked(false);
emit settings->showShape->toggled(true);
} else {
settings->showShape->setChecked(false);
settings->noShowShape->setChecked(true);
emit settings->noShowShape->toggled(true);
}
int vol = settings->remark->value("volume").toInt();
settings->volume->setValue(vol);
emit settings->volume->valueChanged(vol);
} else
return;
}
settings->hasChanged = false;
hideSettings();
}
void GameControl::setVolume(int v) {
settings->hasChanged = true;
bgm->setVolume(static_cast<qreal>(v) / 100.0);
if (nenecchi) {
for (auto& s : nenecchi->sounds) {
s->setVolume(static_cast<qreal>(v) / 100.0);
}
}
}
void GameControl::play() {
clock->start();
nene_clock->start();
assassin_clock->start();
ani_nene_group->start();
if (ani_assassin) ani_assassin->start();
}
void GameControl::palse() {
clock->stop();
nene_clock->stop();
assassin_clock->stop();
if (ani_nene_group) ani_nene_group->stop();
if (ani_assassin) ani_assassin->stop();
}
void GameControl::resizeFullscreen(bool toggled) {
if (!toggled) return;
settings->hasChanged = true;
const QRect availableGeometry = QApplication::desktop()->screenGeometry();
view->resize(availableGeometry.width(), availableGeometry.height());
view->setSceneRect(availableGeometry);
if (welcome) {
welcome->resize(availableGeometry.width(), availableGeometry.height());
QPalette pal = welcome->palette();
pal.setBrush(QPalette::Background,
QBrush(QPixmap(":/background/yuki")
.scaled(availableGeometry.width(),
availableGeometry.height())));
welcome->setPalette(pal);
} else {
scene->setBackgroundBrush(
QBrush(QPixmap(":/background/gamebackground")
.scaled(availableGeometry.width(),
availableGeometry.height())));
}
setSettingsPos();
}
void GameControl::resizeWindow(bool toggled) {
if (!toggled) return;
settings->hasChanged = true;
const QSize availableGeometry = view->sizeHint();
view->resize(availableGeometry);
view->setSceneRect(0, 0, availableGeometry.width(),
availableGeometry.height());
if (welcome) {
welcome->resize(availableGeometry);
QPalette pal = welcome->palette();
pal.setBrush(QPalette::Background,
QBrush(QPixmap(":/background/yuki")
.scaled(availableGeometry.width(),
availableGeometry.height())));
welcome->setPalette(pal);
} else {
scene->setBackgroundBrush(
QBrush(QPixmap(":/background/gamebackground")
.scaled(availableGeometry.width(),
availableGeometry.height())));
}
setSettingsPos();
}
void GameControl::showSettings() {
emit notInSettings(false);
palse();
settings->setVisible(true);
if (welcome) welcome->setVisible(false);
}
void GameControl::hideSettings() {
emit notInSettings(true);
settings->setVisible(false);
if (welcome)
welcome->setVisible(true);
else
play();
}
void GameControl::changeSettings() {
const bool isFullscreen = settings->fullscreen->isChecked();
settings->remark->setValue("isFullscreen", isFullscreen);
const int volume = settings->volume->value();
settings->remark->setValue("volume", volume);
const bool showShape = settings->showShape->isChecked();
settings->remark->setValue("showShape", showShape);
settings->remark->sync();
settings->hasChanged = false;
hideSettings();
}