-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
346 lines (261 loc) · 8.87 KB
/
Game.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
/** @file Game.cpp
@brief This file contains member function definitions for the Game class.
The Game constructor sets up the scene, health, score, tower icons,
background music enemy path, and spawns enemies.
*/
#include "Game.h"
#include "Tobi.h"
#include <QGraphicsScene>
#include "NarutoTower.h"
#include "Projectile.h"
#include "Enemy.h"
#include "BuildNarutoTowerIcon.h"
#include "BuildHinataTowerIcon.h"
#include "BuildInoTowerIcon.h"
#include "BuildTentenTowerIcon.h"
#include "BuildKibaTowerIcon.h"
#include "BuildSaiTowerIcon.h"
#include "BuildRockleeTowerIcon.h"
#include "BuildShikamaruTowerIcon.h"
#include "BuildNejiTowerIcon.h"
#include <QTimer>
#include <QGraphicsLineItem>
#include <QPen>
#include <QGraphicsLineItem>
#include <QMediaPlayer>
#include <QBrush>
#include <QImage>
#include <QGraphicsTextItem>
#include <QFont>
#include <QLabel>
Game::Game(): QGraphicsView()
{
//wave_value = 0;
// create a scene
scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,1600,850);
//set the scene
setScene(scene);
// make the background an image
setBackgroundBrush(QBrush(QImage(":/images/background.png")));
//create a tower
//Tower* t = new Tower();
//t->setPos(250,250);
//add the tower to scene
//scene->addItem(t);
//set cursor
cursor = nullptr;
building = nullptr;
setMouseTracking(true);
//alter window
setFixedSize(1600,850);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// create the score
score = new Score();
score->setPos(score->x()+1050,score->y()+10);
scene->addItem(score);
health = new Health();
health->setPos(health->x()+10,health->y()+140);
scene->addItem(health);
// // draw the text
// QLabel* cost = new QLabel("Level: ");
// setDefaultTextColor(Qt::darkGreen);
// setFont(QFont("Comic Sans MS",15));
// cost->setPos(100,100);
// scene->addItem(cost);
//label the costs
// QLabel *label = new QLabel(this);
// label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
// label->setText("first line\nsecond line");
// label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
//label the costs
QGraphicsTextItem* costText = new QGraphicsTextItem(QString("$1000 \n\n"
"$2000 \n\n"
"$3000 \n\n"
"$4000 \n\n"
"$5000 \n\n"
"$6000 \n\n"
"$7000 \n\n"
"$8000 \n\n"
"$9000 \n\n"));
QFont costFont("comic sans", 25);
costText->setFont(costFont);
int txPos = 1400;
int tyPos = 15;
costText->setPos(txPos,tyPos);
costText->setDefaultTextColor(Qt::darkYellow);
scene->addItem(costText);
// inot->setPos(x()+1500,y()+10);
// tentent->setPos(x()+1500,y()+90);
// hinatat->setPos(x()+1500,y()+170);
// kibat->setPos(x()+1500,y()+250);
// sait->setPos(x()+1500,y()+330);
// rockleet->setPos(x()+1500,y()+410);
// shikamarut->setPos(x()+1500,y()+490);
// nejit->setPos(x()+1500,y()+570);
// narutot->setPos(x()+1500,y()+650);
// create the player
player = new Player();
player->setPos(420,700);
// make the player focusable and set it to be the current focus
player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus();
// add the player to the scene
scene->addItem(player);
// spawn Tobis
QTimer * timer = new QTimer();
QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
timer->start(9000);
//create enemy
//Enemy*= new Enemy();
//scene->addItem(enemy);
spawnTimer = new QTimer(this);
waveTimer = new QTimer(this);
enemiesSpawned = 0;
maxNumberOfEnemies = 0;
pointsToFollow << QPointF(1550, 810)
<< QPointF(1100,810)
<< QPointF(1100,500)
<< QPointF(1330,500)
<< QPointF(1330,300)
<< QPointF(900,300)
<< QPointF(900,780)
<< QPointF(530,780)
<< QPointF(530,700)
<< QPointF(530,300)
<< QPointF(5,300)
<< QPointF(580,90)
<< QPointF(-100,90);
//<< QPointF(500,415)
// << QPointF(-100,120);
//<< QPointF(1,400)
// << QPointF(500,80)
//<< QPointF(1,230)
// create road
createRoad();
setCursor(":/images/leaf_symbol.png");
//sidebar icons
BuildInoTowerIcon * inot = new BuildInoTowerIcon();
BuildTentenTowerIcon * tentent = new BuildTentenTowerIcon();
BuildHinataTowerIcon * hinatat = new BuildHinataTowerIcon();
BuildKibaTowerIcon * kibat = new BuildKibaTowerIcon();
BuildSaiTowerIcon * sait = new BuildSaiTowerIcon();
BuildRockleeTowerIcon * rockleet = new BuildRockleeTowerIcon();
BuildShikamaruTowerIcon * shikamarut = new BuildShikamaruTowerIcon();
BuildNejiTowerIcon * nejit = new BuildNejiTowerIcon();
BuildNarutoTowerIcon * narutot = new BuildNarutoTowerIcon();
inot->setPos(x()+1500,y()+10);
tentent->setPos(x()+1500,y()+90);
hinatat->setPos(x()+1500,y()+170);
kibat->setPos(x()+1500,y()+250);
sait->setPos(x()+1500,y()+330);
rockleet->setPos(x()+1500,y()+410);
shikamarut->setPos(x()+1500,y()+490);
nejit->setPos(x()+1500,y()+570);
narutot->setPos(x()+1500,y()+650);
scene->addItem(narutot);
scene->addItem(hinatat);
scene->addItem(inot);
scene->addItem(tentent);
scene->addItem(kibat);
scene->addItem(sait);
scene->addItem(rockleet);
scene->addItem(nejit);
scene->addItem(shikamarut);
// play background music
QMediaPlayer * music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/bgsound.mp3"));
music->play();
// int level = 1;
// if(level ==2)
// createEnemies(10);
//QTimer::singleShot(30,this,SLOT(releaseWave()));
// QTimer* wave_timer = new QTimer(this);
// connect(wave_timer, SIGNAL(timeout()),this,SLOT(releaseWave()));
// wave_timer->start(1000);
// QTimer *timer = new QTimer(this);
// connect(timer, SIGNAL(timeout()), this, SLOT(releaseWave()));
// timer->start(1000);
// connect(waveTimer, SIGNAL(timeout()),this, SLOT(releaseWave()));
// waveTimer->start(3000); //spawn rate
//releaseWave();
//newWave();
createEnemies(90);
}
//void Game::releaseWave(){
// createEnemies(5);
//}
void Game::setCursor(QString filename)
{
if(cursor){ //if cursor is not null, remove it
scene->removeItem(cursor);
delete cursor;
}
cursor = new QGraphicsPixmapItem();
cursor->setPixmap(QPixmap(QPixmap(filename)));
scene->addItem(cursor);
}
void Game::mouseMoveEvent(QMouseEvent *event)
{
if(cursor){
cursor->setPos(event->pos());
}
}
void Game::mousePressEvent(QMouseEvent *event)
{
// if we are building
if (building){
// return if the cursor is colliding with a tower
QList<QGraphicsItem*> items = cursor->collidingItems();
for (size_t i=0, n=items.size(); i<n; ++i){
if (dynamic_cast<Tower*>(items[i])){
return;
}
}
//QList<QGraphicsItem*> items = cursor->collidingItems();
// if(cursor->collidingItems)
// return;
// otherwise, build at the clicked location
scene->addItem(building);
building->setPos(event->pos());
cursor = nullptr;
building = nullptr;
}
else {
QGraphicsView::mousePressEvent(event);
}
}
void Game::createEnemies(int numberOfEnemies)
{
enemiesSpawned = 0;
maxNumberOfEnemies = numberOfEnemies;
connect(spawnTimer, SIGNAL(timeout()),this, SLOT(spawnEnemy()));
spawnTimer->start(2000); //spawn rate
}
void Game::spawnEnemy()
{
// spawn an enemy
Enemy* enemy = new Enemy(pointsToFollow);
enemy->setPos(pointsToFollow[0]);
scene->addItem(enemy);
++enemiesSpawned;
if (enemiesSpawned >= maxNumberOfEnemies){
spawnTimer->disconnect();
}
}
void Game::createRoad()
{
for (size_t i = 0, n = pointsToFollow.size()-1; i < n; ++i){
// create a line connecting the two points
QLineF line(pointsToFollow[i],pointsToFollow[i+1]);
QGraphicsLineItem* lineItem = new QGraphicsLineItem(line);
QPen pen;
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
pen.setWidth(0);
pen.setColor(Qt::darkRed);
lineItem->setPen(pen);
scene->addItem(lineItem);
}
}