-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
514 lines (469 loc) · 17.2 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
switchGameStatus(STARTMODE);
backgroundScene = new QGraphicsScene(this);//background scene
backgroundScene->setSceneRect(0, 0, 1861, 1061);
QPixmap backgroundImg(":/pics/background.png");
backgroundImg.scaled(1861,1061);
backgroundScene->setBackgroundBrush(QBrush(backgroundImg));
//test
// if(mario==nullptr){
// mario=new Hero();
// unitsList.push_front(mario);
// backgroundScene->addItem(mario);
// mario->setPos(STARTPOINT_X,STARTPOINT_Y);// init start location
// mario->grabKeyboard();// ensure keyboard event not be thrown
// mario->grabMouse();
// }
//test
ui->backgroundView->setScene(backgroundScene);
globalTimer = new QTimer(this);
globalTimer->setInterval(TIMER_INTERVAL);//可以调整间隔改变平滑度
connect(globalTimer, SIGNAL(timeout()), this, SLOT(allUpdate()));
connect(ui->startButton,&QPushButton::clicked,[=](){playOrEdit = PLAYMODE;
this->switchGameStatus(MAPSELECT);});
connect(ui->enterEditModeButton, &QPushButton::clicked, [=](){playOrEdit = EDITMODE;
this->switchGameStatus(MAPSELECT);});
connect(ui->map1Button_2, &QPushButton::clicked, [=](){
if(!ui->backgroundView->isVisible()){ //hiden in mapselect scene
if(playOrEdit==EDITMODE){
this->read("map1.json");
this->switchGameStatus(EDITMODE);
}
else{
this->read("map1.json");
this->switchGameStatus(PLAYMODE);
}
}
else{
this->write("map1.json");
ui->map1Button_2->hide();
ui->map2Button_2->hide();
}});
connect(ui->map2Button_2, &QPushButton::clicked, [=](){
if(!ui->backgroundView->isVisible()){
if(playOrEdit==EDITMODE){
this->read("map2.json");
this->switchGameStatus(EDITMODE);
}
else{
this->read("map2.json");
this->switchGameStatus(PLAYMODE);
}
}
else{
this->write("map2.json");
ui->map1Button_2->hide();
ui->map2Button_2->hide();
}});
connect(ui->saveButton, &QPushButton::clicked, [=](){
if(ui->backgroundView->isVisible()){
ui->map1Button_2->show();
ui->map2Button_2->show();
}});
connect(ui->backToMenuButton, &QPushButton::clicked, [=](){this->switchGameStatus(STARTMODE);});
connect(this, SIGNAL(heroDead()), this, SLOT(gameOver()));
}
MainWindow::~MainWindow(){
delete ui;
delete globalTimer;
for(auto i:unitsList){
if(i!=nullptr) delete i;
}
for(auto i:blocksList){
if(i!=nullptr) delete i;
}
}
void MainWindow::gameOver(){
endTime = QTime::currentTime();
globalTimer->stop();
QString playTime = QString::number(beginTime.secsTo(endTime));
QMessageBox msgBox;
msgBox.setText("Time:" + playTime + "\nScore:" + QString::number(mario->score));
msgBox.exec();
switchGameStatus(STARTMODE);
}
void MainWindow::switchGameStatus(int gameStatus){
if(gameStatus==STARTMODE){
qDebug()<<"Start"<<Qt::endl;
QPixmap backgroundImg = QPixmap(":/pics/background.png").scaled(1000,600);
QPalette palette(this->palette());
palette.setBrush(QPalette::Background, QBrush(backgroundImg));
this->setPalette(palette);
ui->startButton->show();
ui->enterEditModeButton->show();
ui->map1Button_2->hide();
ui->map2Button_2->hide();
ui->backgroundView->hide();
ui->backToMenuButton->hide();
ui->comboBox->setCurrentText("Null");
ui->comboBox->hide();
ui->elementsLabel->hide();
ui->mapProgressBar->hide();
ui->saveButton->hide();
}
if(gameStatus==MAPSELECT){//select maps, meanwhile, play or edit is recorded
ui->startButton->hide();
ui->enterEditModeButton->hide();
ui->map1Button_2->show();
ui->map2Button_2->show();
ui->backgroundView->hide();
ui->backToMenuButton->show();// implemented in construct function
ui->comboBox->setCurrentText("Null");
ui->comboBox->hide();
ui->elementsLabel->hide();
ui->mapProgressBar->hide();//todo:loading
ui->saveButton->hide();
}
if(gameStatus==PLAYMODE){
playOrEdit = PLAYMODE;
ui->startButton->hide();
ui->enterEditModeButton->hide();
ui->map1Button_2->hide();
ui->map2Button_2->hide();
ui->backgroundView->show();
ui->backToMenuButton->show();
ui->comboBox->setCurrentText("Null");
ui->comboBox->hide();
ui->elementsLabel->hide();
ui->mapProgressBar->hide();
ui->saveButton->hide();
ui->backToMenuButton->show();
globalTimer->start();
beginTime = QTime::currentTime();
if(mario!=nullptr){
mario->grabMouse();
mario->grabKeyboard();
}
}
if(gameStatus==EDITMODE){
playOrEdit = EDITMODE;
ui->startButton->hide();
ui->enterEditModeButton->hide();
ui->map1Button_2->hide();
ui->map2Button_2->hide();
ui->backgroundView->show();
ui->backToMenuButton->show();
ui->comboBox->setCurrentText("Null");
ui->comboBox->show();
ui->elementsLabel->show();
ui->mapProgressBar->hide();
ui->saveButton->show();
ui->backToMenuButton->show();
globalTimer->stop();
if(mario!=nullptr){
mario->ungrabMouse();
mario->ungrabKeyboard();
}
}
}
void MainWindow::checkObjsMove(){
for(auto u:unitsList){
if(u!=nullptr) u->move();
}
}
void MainWindow::checkObjsCollide(){
for(int u = 0; u < unitsList.size(); u++){
if(unitsList[u]!=nullptr){
if(unitsList[u]->gameType()!="spitter"){
unitsList[u]->monsterAttackHero(mario);
}
else{
Spitter *s=static_cast<Spitter *>(unitsList[u]);
s->farAttackHero(mario, backgroundScene, unitsList);
}
unitsList[u]->checkCollideDirection();
}
}
}
void MainWindow::allUpdate(){// check all things needed to be check periodically
checkObjsMove();
checkObjsCollide();
if(!mario->isVisible()){
emit heroDead();
return;
}
if(!blocksList.isEmpty()){// terrain damage
for(auto b = 0; b < blocksList.size(); b++){
blocksList[b]->collideHero(mario);// check terrain damage
if(!mario->isVisible()){//check if hero alive after every possible damage
emit heroDead();
return;
}
if(!blocksList[b]->isVisible()){
delete blocksList[b];
blocksList.removeAt(b);
}
}
}
if(!unitsList.isEmpty()){// clear dead units
for(int u = 0; u < unitsList.size(); u++){
if(!unitsList[u]->isVisible()){
delete unitsList[u];
unitsList.removeAt(u);
}
}
}
update();
}
void MainWindow::read(const QString &fileName)
{
for(auto u:unitsList){
if(u!=nullptr) delete u;
}
for(auto b:blocksList){
if(b!=nullptr) delete b;
}
unitsList.clear();
blocksList.clear();
ui->mapProgressBar->show();
for(int i=0;i<=100;i++){
QThread::msleep(10);
ui->mapProgressBar->setValue(i);
}
qDebug()<< QDir::currentPath() <<Qt::endl;
QFile mapfile(fileName);
if(!mapfile.exists()){
qDebug()<<"file not exist"<<Qt::endl;
}
if(!mapfile.open(QIODevice::ReadOnly)){
qDebug()<<"some error when read the file"<<Qt::endl;
return;
}
QByteArray allData=mapfile.readAll();
QJsonParseError jsonError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData,&jsonError));
if(jsonError.error!=QJsonParseError::NoError){
qDebug()<<"json parse error"<<Qt::endl;
return ;
}
QVariantList dataList=jsonDoc.toVariant().toList();
for(QVariant &data:dataList){
QVariantMap dataMap=data.toMap();
QString type=dataMap.value("type").toString();
int x=dataMap.value("x").toInt();
int y=dataMap.value("y").toInt();
if(type.isNull()){
qDebug()<<"Invalid data format";
}
if(type=="block"){// read every class
auto b = new Block(x, y);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(type=="breakable brick"){
auto b = new BreakableBlock(x, y);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
// else if(type=="move brick"){
// auto b = new MoveBlock(x, y);
// blocksList.push_back(b);
// backgroundScene->addItem(b);
// }
else if(type=="spring"){
auto b = new Spring(x, y);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(type=="destination"){
auto b = new Destination(x, y);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(type=="passive trap"){
auto b = new Trap(x, y, PASSIVE);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(type=="active trap"){
auto b = new Trap(x, y, NOT_TRIGGERED);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(type=="heart"){
auto b = new Heart(x, y);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(type=="mushroom"){
auto b = new Mushroom(x, y);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(type=="licker"){
auto u = new Licker;
u->setPos(x,y);
unitsList.push_back(u);
backgroundScene->addItem(u);
}
else if(type=="jumper"){
auto u = new Jumper;
u->setPos(x,y);
unitsList.push_back(u);
backgroundScene->addItem(u);
}
else if(type=="spitter"){
auto u = new Spitter;
u->setPos(x,y);
unitsList.push_back(u);
backgroundScene->addItem(u);
}
else if (type=="hero"){
if(!unitsList.isEmpty()&&unitsList.first()->gameType()=="hero"){
qDebug("read multiple hero!");
continue;//only one hero
}
auto h = new Hero();
h->setPos(x,y);
mario = h;
unitsList.push_front(h);
backgroundScene->addItem(h);
h->grabKeyboard();
h->grabMouse();
}
}
}
void MainWindow::write(const QString &fileName)
{
QFile mapfile(fileName);
QJsonArray jsonArray;
for(auto u:unitsList){
if(u->gameType()=="bullet"||!u->isVisible()){
continue;// bullets won't be saved
}
QJsonObject obj;
obj.insert("type",u->gameType());
obj.insert("x",u->x());
obj.insert("y",u->y());
jsonArray.append(obj);
}
for(auto b:blocksList){
if(!b->isVisible()){
continue;
}
QJsonObject obj;
obj.insert("type",b->gameType());
obj.insert("x",b->x());
obj.insert("y",b->y());
jsonArray.append(obj);
}
QJsonDocument doc(jsonArray);
QByteArray ba=doc.toJson(QJsonDocument::Indented);
if(!mapfile.open(QIODevice::WriteOnly)){
qDebug()<<"file write error"<<Qt::endl;
}
mapfile.write(ba);
mapfile.close();
QMessageBox msgBox;
msgBox.setText("The Map has been save successfully!");
msgBox.exec();
}
void MainWindow::mousePressEvent(QMouseEvent *event){
if(event->button()==Qt::LeftButton&&playOrEdit==EDITMODE&&ui->backgroundView->isVisible()&&event->x()<=ui->backgroundView->width()&&event->y()<=ui->backgroundView->height()){
if(ui->backgroundView->itemAt(event->x(), event->y())) return;
if(ui->comboBox->currentText()=="Null") {
return;
}
else if(ui->comboBox->currentText()=="Ground"){
Block *b=new Block(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(ui->comboBox->currentText()=="Breakable brick"){
BreakableBlock *b=new BreakableBlock(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
// else if(ui->comboBox->currentText()=="Move brick"){
// MoveBrick *b=new MoveBrick(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
// blocksList.push_back(b);
// backgroundScene->addItem(b);
// }
else if(ui->comboBox->currentText()=="Spring"){
Spring *b=new Spring(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(ui->comboBox->currentText()=="Destination"){
auto *b=new Destination(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(ui->comboBox->currentText()=="Passive trap"){
Trap *b=new Trap(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(ui->comboBox->currentText()=="Active trap"){
Trap *b=new Trap(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH, NOT_TRIGGERED);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(ui->comboBox->currentText()=="Heart"){
Heart *b=new Heart(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(ui->comboBox->currentText()=="Mushroom"){
Mushroom *b=new Mushroom(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
blocksList.push_back(b);
backgroundScene->addItem(b);
}
else if(ui->comboBox->currentText()=="Licker"){
Licker *u=new Licker;
u->setPos(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
unitsList.push_back(u);
backgroundScene->addItem(u);
}
else if(ui->comboBox->currentText()=="Jumper"){
Jumper *u=new Jumper;
u->setPos(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
unitsList.push_back(u);
backgroundScene->addItem(u);
}
else if(ui->comboBox->currentText()=="Spitter"){
Spitter *u=new Spitter;
u->setPos(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
unitsList.push_back(u);
backgroundScene->addItem(u);
}
else if (ui->comboBox->currentText()=="Hero"){
auto h = new Hero();
h->setPos(event->x()/BLOCK_LENGTH*BLOCK_LENGTH, event->y()/BLOCK_LENGTH*BLOCK_LENGTH);
mario = h;
if(!unitsList.isEmpty()&&unitsList.first()->gameType()=="hero"){
*unitsList.begin() = h;
}
else
unitsList.push_front(h);
backgroundScene->addItem(h);
// h->grabKeyboard();
// h->grabMouse();
}
repaint();
QThread::msleep(50);
}
// rightButton delete items
else if(event->button()==Qt::RightButton&&playOrEdit==EDITMODE&&ui->backgroundView->isVisible()&&event->x()<=ui->backgroundView->width()&&event->y()<=ui->backgroundView->height()){
auto item = ui->backgroundView->itemAt(event->x(), event->y());
if(item){
item->hide();
repaint();
}
}
// hero far attack
else if(event->button()==Qt::LeftButton&&playOrEdit==PLAYMODE&&ui->backgroundView->isVisible()&&mario->getIsBuffed()){
auto g = ui->backgroundView->itemAt(event->x(), event->y());
auto u = dynamic_cast<Unit *>(g);
if(u!=nullptr&&u->gameType()!="hero"){
u->hide();
}
}
}