-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
488 lines (430 loc) · 16 KB
/
window.cpp
File metadata and controls
488 lines (430 loc) · 16 KB
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
/*#################################################################
BattleMage window.cpp
Copyright (C) 2012 Quentin Hsu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
To contact the author, email: haloqa@gmail.com
##################################################################*/
#include "window.h"
#include <QAction>
#include <QLayout>
#include <QMenu>
#include <QMenuBar>
#include <QStatusBar>
#include <QTextEdit>
#include <QSignalMapper>
#include <QApplication>
#include <QPainter>
#include <QMouseEvent>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QObject>
#include <QTimer>
#include <QRadialGradient>
#include <QDockWidget>
#include <QStyle>
#include <QFont>
//Window Class
Window::Window()
{
//menu bar
setMenuBar();
//central widget
game = new Map(1); //construct first stage of game
game->setSceneRect(0.0, 0.0, 640.0, 360.0); //set game screen sizes
timer = new QTimer(); //create a timer, not active yet
connect(timer, SIGNAL(timeout()), game, SLOT(advance())); //connect timeouts to scene updates
connect(game, SIGNAL(turret_battle()), this, SLOT(turrets())); //connect stage advance signal to turrets
connect(game, SIGNAL(boss_battle()), this, SLOT(boss())); //moves to boss battle
connect(game, SIGNAL(dead()), this, SLOT(die())); //connects stage death to die function here
view = new QGraphicsView(game); //allocate memory for graphicsview
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //turn off main window scroll bars
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setCentralWidget(view); //set graphicsview as central widget
//dock widgets
setDockWidgets(); //set dockwidget function
game->set_toggle(towerClick->blue); //update game's tower spawn variable
//prevent shortcuts except quit
disconnect(actionRestart, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionFirst, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionTurrets, SIGNAL(triggered()), this, SLOT(turrets()));
disconnect(actionBoss, SIGNAL(triggered()), this, SLOT(boss()));
//connect restart shortcut to start game
connect(actionRestart, SIGNAL(triggered()), this, SLOT(deleteInstructions()));
instructions(); //display instructions
}
Window::~Window()
{
//delete dynamic variables
delete file;
delete stages;
delete timer;
delete view;
}
void Window::restart() //restart game
{
timer->stop(); //stop the timer
delete timer; //remake timer
delete game; //clear old Map
game = new Map(1); //make new map
game->setSceneRect(0.0, 0.0, 640.0, 360.0);
timer = new QTimer();
connect(timer, SIGNAL(timeout()), game, SLOT(advance())); //reconnect signals
connect(game, SIGNAL(turret_battle()), this, SLOT(turrets()));
connect(game, SIGNAL(boss_battle()), this, SLOT(boss())); //moves to boss battle
connect(game, SIGNAL(dead()), this, SLOT(die()));
view->setScene(game); //change qgraphicsview's scene
//delete dockwidgets
removeDockWidget(info);
removeDockWidget(towerInfo);
delete info; //remake stats
delete towerInfo; //remake button
setDockWidgets(); //remake dockwidgets
game->set_toggle(towerClick->blue); //update tower activation
//display stage name
tempTimer = new QTimer();
QString x("First Stage");
connect(tempTimer, SIGNAL(timeout()), this, SLOT(deleteDisplay())); //connect tempTimer to manage display time
//prevent shortcuts except quit until stage name screen is over
disconnect(actionRestart, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionFirst, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionTurrets, SIGNAL(triggered()), this, SLOT(turrets()));
disconnect(actionBoss, SIGNAL(triggered()), this, SLOT(boss()));
setDisplay(x); //display the stage name
tempTimer->start(1500); //start tempTimer, game timer is in deleteDisplay function
}
void Window::turrets()
{
timer->stop();
//copy game stats to transfer to new scene
int temp_score = game->get_score();
int temp_lives = game->get_lives();
int temp_kills = game->get_kills();
int temp_tower = game->get_max_towers();
//mage location
QPointF temp_current_mage = game->get_current_mage();
delete game; //clear old Map
game = new Map(2); //make turret stage
game->setSceneRect(0.0, 0.0, 640.0, 360.0);
delete timer; //remake timer
timer = new QTimer();
connect(timer, SIGNAL(timeout()), game, SLOT(advance())); //reconnect signals
connect(game, SIGNAL(turret_battle()), this, SLOT(turrets()));
connect(game, SIGNAL(boss_battle()), this, SLOT(boss())); //moves to boss battle
connect(game, SIGNAL(dead()), this, SLOT(die()));
view->setScene(game); //change qgraphicsview's scene
//remake dockwidgets
removeDockWidget(info);
removeDockWidget(towerInfo);
delete info; //remake stats
delete towerInfo; //remake button
setDockWidgets();
//transfer backup stats to new scene
game->set_current_mage(temp_current_mage);
game->set_score(temp_score);
game->set_lives(temp_lives);
game->set_kills(temp_kills);
game->set_max_towers(temp_tower);
game->set_toggle(towerClick->blue);
//display stage name
tempTimer = new QTimer();
QString x("Turret Battle");
connect(tempTimer, SIGNAL(timeout()), this, SLOT(deleteDisplay()));
disconnect(actionRestart, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionFirst, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionTurrets, SIGNAL(triggered()), this, SLOT(turrets()));
disconnect(actionBoss, SIGNAL(triggered()), this, SLOT(boss()));
setDisplay(x);
tempTimer->start(1500);
}
void Window::boss()
{
//same as turret() function, changed to load boss scene
timer->stop();
int temp_score = game->get_score();
int temp_lives = game->get_lives();
int temp_kills = game->get_kills();
int temp_tower = game->get_max_towers();
QPointF temp_current_mage = game->get_current_mage();
delete game; //clear old Map
game = new Map(3); //make boss map
game->setSceneRect(0.0, 0.0, 640.0, 360.0);
delete timer; //remake timer
timer = new QTimer();
connect(timer, SIGNAL(timeout()), game, SLOT(advance())); //reconnect signals
connect(game, SIGNAL(turret_battle()), this, SLOT(turrets()));
connect(game, SIGNAL(boss_battle()), this, SLOT(boss())); //moves to boss battle
connect(game, SIGNAL(dead()), this, SLOT(die()));
view->setScene(game); //change qgraphicsview's scene
removeDockWidget(info);
removeDockWidget(towerInfo);
delete info; //remake stats
delete towerInfo; //remake button
setDockWidgets();
game->set_current_mage(temp_current_mage);
game->set_score(temp_score);
game->set_lives(temp_lives);
game->set_kills(temp_kills);
game->set_max_towers(temp_tower);
game->set_toggle(towerClick->blue);
//display stage name
tempTimer = new QTimer();
QString x("Boss Battle");
connect(tempTimer, SIGNAL(timeout()), this, SLOT(deleteDisplay()));
disconnect(actionRestart, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionFirst, SIGNAL(triggered()), this, SLOT(restart()));
disconnect(actionTurrets, SIGNAL(triggered()), this, SLOT(turrets()));
disconnect(actionBoss, SIGNAL(triggered()), this, SLOT(boss()));
setDisplay(x);
tempTimer->start(1500);
}
void Window::die()
{
//loads game over screen
timer->stop();
delete timer;
timer = new QTimer;
int score_ = game->get_score(); //backup score
delete game;
game = new Map(4, score_); //load dead scene
connect(game, SIGNAL(turret_battle()), this, SLOT(turrets()));
connect(game, SIGNAL(boss_battle()), this, SLOT(boss())); //moves to boss battle
connect(game, SIGNAL(dead()), this, SLOT(die()));
connect(game, SIGNAL(restart()), this, SLOT(restart())); //restart game
connect(game, SIGNAL(end()), qApp, SLOT(quit())); //quit application
view->setScene(game); //change qgraphicsview's scene
removeDockWidget(info);
removeDockWidget(towerInfo);
delete info; //remake stats
delete towerInfo; //remake button
setDockWidgets();
game->set_toggle(towerClick->blue);
}
void Window::setDisplay(QString x)
{
display_screen = new QGraphicsScene; //create temporary new graphics scene
QFont f("Arial", 20, QFont::Bold); //set font
QGraphicsTextItem *temp = display_screen->addText(x, f); //create graphicstextitem to display stage name
temp->setPos(250, 360/2); //position center of screen
view->setScene(display_screen); //display screen in central widget
view->show();
}
void Window::instructions()
{
//same as setDisplay, but displays instructions
display_screen = new QGraphicsScene;
QString x("Controls:\nArrow Keys - Move\nSpace - Fire\nTo activate towers that block 15 hits for you, click the tower button.\nWhen tower button says True, click on the screen to spawn your tower at that location.\nYou only have 3 towers to use for the whole game\n\nPress CTRL + R to begin playing!");
QFont f("Arial", 12);
QGraphicsTextItem *temp = display_screen->addText(x, f);
temp->setPos(0,0);
view->setScene(display_screen);
view->show();
}
void Window::deleteDisplay()
{
tempTimer->stop(); //stop temp timer
display_screen->clear(); //remove items from graphicsscene
delete display_screen;
delete tempTimer;
//reconnect shortcuts
connect(actionRestart, SIGNAL(triggered()), this, SLOT(restart()));
connect(actionFirst, SIGNAL(triggered()), this, SLOT(restart()));
connect(actionTurrets, SIGNAL(triggered()), this, SLOT(turrets()));
connect(actionBoss, SIGNAL(triggered()), this, SLOT(boss()));
view->setScene(game); //reload paused map
timer->start(10); //start game scene
}
void Window::deleteInstructions()
{
//same as deleteDisplay but used for deleting instructions
display_screen->clear();
delete display_screen;
disconnect(actionRestart, SIGNAL(triggered()), this, SLOT(deleteInstructions())); //reset different shortcut connection
connect(actionRestart, SIGNAL(triggered()), this, SLOT(restart()));
connect(actionFirst, SIGNAL(triggered()), this, SLOT(restart()));
connect(actionTurrets, SIGNAL(triggered()), this, SLOT(turrets()));
connect(actionBoss, SIGNAL(triggered()), this, SLOT(boss()));
view->setScene(game);
timer->start(10);
}
void Window::setMenuBar()
{
//file menu
file = new QMenu;
file = menuBar()->addMenu("&File"); //File Menu
actionRestart = new QAction("&Restart", this); //Restart action
actionQuit = new QAction("&Quit", this); //Quit action
actionRestart->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); //shortcut for actionbutton
connect(actionRestart, SIGNAL(triggered()), this, SLOT(restart())); //connect to restart scene
actionQuit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); //shortcut for actionbutton
connect(actionQuit, SIGNAL(triggered()), qApp, SLOT(quit())); //connect to quit app
file->addAction(actionRestart); // add action to menu
file->addAction(actionQuit); // add action to menu
menuBar()->addMenu(file); //add menu to menubar
//stages menu
stages = new QMenu;
stages = menuBar()->addMenu("&Stages"); //stages menu
actionFirst = new QAction("&First Stage", this); // first stage
actionTurrets = new QAction("&Second Stage", this); //skip to turret battle
actionBoss = new QAction("&Boss", this); //skip to boss battle
actionFirst->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F)); //shortcut for first stage
connect(actionFirst, SIGNAL(triggered()), this, SLOT(restart()));
actionTurrets->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T)); //shortcut for turret battle
connect(actionTurrets, SIGNAL(triggered()), this, SLOT(turrets()));
actionBoss->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B)); //shortcut for boss battle
connect(actionBoss, SIGNAL(triggered()), this, SLOT(boss()));
//add shortcuts to stages menu
stages->addAction(actionFirst);
stages->addAction(actionTurrets);
stages->addAction(actionBoss);
menuBar()->addMenu(stages);
}
void Window::setDockWidgets()
{
//stats widget
info = new QDockWidget("Stats", this);
stats = new statsWidget;
//connect score and lives to the game
connect(game, SIGNAL(scoreChanged(int)), this, SLOT(update_score(int)));
connect(game, SIGNAL(livesChanged(int)), this, SLOT(update_lives(int)));
connect(game, SIGNAL(killsChanged(int)), this, SLOT(update_kills(int)));
info->setWidget(stats); //set dockwidget
addDockWidget(Qt::BottomDockWidgetArea, info); //add dockwidget to bottom of main window
//click widget
towerInfo = new QDockWidget("Towers", this);
towerClick = new clickWidget;
//connect tower toggle
connect(towerClick->towerOn, SIGNAL(clicked()), this, SLOT(update_click()));
connect(towerClick->towerOn, SIGNAL(clicked()), game, SLOT(tower_toggle()));
towerInfo->setWidget(towerClick);
addDockWidget(Qt::TopDockWidgetArea, towerInfo); //add dockwidget to top of main window
//setStyleSheet("QDockWidget#info{subcontrol-position: top left; subcontrol-origin: margin; position: absolute; top: 0px; left: 0px; bottom: 0px; width: 14px;}");
//file->addAction(info->toggleViewAction());
}
//public slots to update labels in dockwidgets
void Window::update_score(int x)
{
QString temp;
(stats->score)->setText(temp.number(x));
}
void Window::update_lives(int x)
{
QString temp;
(stats->lives)->setText(temp.number(x));
}
void Window::update_kills(int x)
{
QString temp;
(stats->kills)->setText(temp.number(x));
}
void Window::update_click()
{
towerClick->blue = !(towerClick->blue);
if(towerClick->blue == true)
{
(towerClick->label)->setText("True");
}
else
{
(towerClick->label)->setText("False");
}
}
//end Window Class
//Dockwidgets
//statsWidget
statsWidget::statsWidget(QWidget *parent)
: QWidget(parent)
{
QFont f("Arial", 14);
//create lives label
QLabel *label = new QLabel;
label->setText("Lives");
f.setBold(true);
label->setFont(f);
//create lives number
lives = new QLabel;
lives->setText("10");
f.setPointSize(12);
f.setBold(false);
lives->setFont(f);
//add lives to the layout
layout = new QGridLayout;
layout->addWidget(label, 0, 0);
layout->addWidget(lives, 1, 0);
//create score label
label = new QLabel;
label->setText("Score");
f.setBold(true);
f.setPointSize(14);
label->setFont(f);
//create score number
score = new QLabel;
score->setText("0");
f.setBold(false);
f.setPointSize(12);
score->setFont(f);
//add score to layout
layout->addWidget(label, 0, 1);
layout->addWidget(score, 1, 1);
//create kills label
label = new QLabel;
label->setText("Kills");
f.setBold(true);
f.setPointSize(14);
label->setFont(f);
//create kills number
kills = new QLabel;
kills->setText("0");
f.setBold(false);
f.setPointSize(12);
kills->setFont(f);
//add kills to layout
layout->addWidget(label, 0, 2);
layout->addWidget(kills, 1, 2);
setLayout(layout);
}
statsWidget::~statsWidget()
{
delete lives;
delete score;
delete kills;
delete layout;
}
//end StatsWidget
//clickWidget
clickWidget::clickWidget(QWidget *parent) : QWidget(parent)
{
blue = false; //set initial tower placement to false
//create pushbutton
towerOn = new QPushButton("Tower", this);
towerOn->setFont(QFont("Arial", 10));
towerOn->setFixedSize(180, 40);
//create bool label
label = new QLabel;
label->setText("False");
QFont f("Arial", 12);
f.setBold(true);
label->setFont(f);
//make layout
layout = new QGridLayout;
layout->addWidget(towerOn, 0, 0);
layout->addWidget(label, 0, 1);
//set layout
setLayout(layout);
}
clickWidget::~clickWidget()
{
delete towerOn;
delete label;
delete layout;
}
//end ClickWidget