-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
362 lines (301 loc) · 9.69 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "Game.h"
#include "Tower.h"
#include "MyApplication.h"
#include "EnemyBlueSlime.h"
#include "Wall.h"
#include "BuildWall.h"
#include "Tile.h"
#include "Path.h"
#include "bullet.h"
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsTextItem>
#include <QPixmap>
#include <QMouseEvent>
#include <QTimer>
#include <QDebug>
#include <QPoint>
#include <QMap>
#include <QMapIterator>
#include <QGraphicsRectItem>
#include <QBrush>
#include <QLineF>
#include <QPointF>
#include <qmath.h>
#include <QCursor>
#include <QMessageBox>
Game::Game()
{
setMouseTracking(true);
setMapTile(20,10,64); // but start from 0
createScene();
cursor = new QPixmap(":/util/assets/tank/crosshair_blue_large.png");
//cursor->setOffset(-25,-25);
//cursor->setZValue(999);
//QApplication::setOverrideCursor(Qt::BlankCursor);
setCursor(*cursor);
createMapTiles(":/floor/assets/floor/dirt.png");
//drawTilesOverlay(":/util/assets/util/sTrackBorder_0.png");
//drawTilesPoint();
//printAllTiles();
spawn1 = tiles.value(indexOfPoint(0,5));
spawn1->setPos(x_scene(0),y_scene(5));
dest1 = tiles.value(indexOfPoint(19,5));
dest1->setPos(x_scene(19),y_scene(5));
QGraphicsPixmapItem *keybind = new QGraphicsPixmapItem(QPixmap(":/menu/assets/menu/menu.png"));
scene->addItem(keybind);
keybind->setScale(0.6);
keybind->setOffset(400,430);
keybind->setZValue(99999);
QGraphicsPixmapItem *start = new QGraphicsPixmapItem(QPixmap(":/floor/assets/floor/start.png"));
start->setPos(x_scene(0),y_scene(5));
start->setScale(0.5);
scene->addItem(start);
QGraphicsPixmapItem *finish = new QGraphicsPixmapItem(QPixmap(":/floor/assets/floor/finish.png"));
finish->setPos(x_scene(19),y_scene(5));
finish->setScale(0.5);
scene->addItem(finish);
int index = indexOfPoint(19,4);
tower = new Tower(*this,this->tiles.value(index)->x(),this->tiles.value(index)->y());
tiles.value(indexOfPoint(19,4))->walkable = false;
base_reload_bar = new QGraphicsRectItem(0,0,map_tile_size-13,10);
base_reload_bar->setBrush(QBrush(Qt::red));
base_reload_bar->setZValue(1);
base_reload_bar->setPos(x_scene(19)+4,y_scene(4)-15);
scene->addItem(base_reload_bar);
reload_bar_placeholder = new QGraphicsRectItem(x_scene(19)+4,y_scene(4)-15,51,10);
reload_bar_placeholder->setBrush(QBrush(Qt::green));
reload_bar_placeholder->setZValue(999);
scene->addItem(reload_bar_placeholder);
buildwall = new BuildWall(*this);
score_text = new QGraphicsTextItem(QString("Skor : %1").arg(score));
score_text->setPos(1190,10);
score_text->setScale(1.5);
scene->addItem(score_text);
}
void Game::generatePath() {
if (!game_started) {
game_started = true;
path = new Path(*this,*spawn1,*dest1);
if (path->destfound){
buildwall->setZValue(-9999);
spawner_timer = new QTimer();
connect(spawner_timer,SIGNAL(timeout()),this,SLOT(spawnBlueSlime()));
spawner_timer->start(5000);
spawnBlueSlime();
}
}
}
void Game::createMapTiles(QString filename)
{
for(int x = 0; x < map_width_in_tiles; x++){
for(int y = 0; y < map_height_in_tiles; y++){
// set the floor sprite
Tile * tile = new Tile(*this, QPoint(x,y));
tile->setPixmap(QPixmap(filename));
tile->setScale(0.5);
tile->setPos(x_scene(x),y_scene(y));
tile->walkable = true;
tile->parent_tile = nullptr;
/* randomize wall
if ((y == 4) || (y == 5) || (y == 6) || (y == 7) || (y == 3)){
tile->setPixmap(QPixmap(":/wall/assets/wall/brick_red.png"));
tile->walkable = false;
}
if (x % 2 == 1){
tile->setPixmap(QPixmap(filename));
tile->walkable = true;
}*/
this->tiles.insert(indexOfPoint(x,y),tile);
scene->addItem(tile);
}
}
}
void Game::createScene()
{
int scene_height = map_height_in_tiles * map_tile_size;
int scene_width = map_width_in_tiles * map_tile_size;
// create a scene
scene = new QGraphicsScene(this);
// set the scene
setScene(scene);
scene->setSceneRect(0,0,scene_width,scene_height);
// can someone explain this? the size is not the same as the scene
setFixedSize(scene_width+2,scene_height+2);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void Game::setMapTile(int map_tiles_x, int map_tiles_y, int map_tile_size_)
{
map_width_in_tiles = map_tiles_x;
map_height_in_tiles = map_tiles_y;
map_tile_size = map_tile_size_;
}
void Game::spawnBlueSlime()
{
increase_spawner++;
BlueSlime * blueslime = new BlueSlime(*this, *path);
if (increase_spawner % 4== 0){
if (spawner_timer->interval() > 2000){
spawner_timer->setInterval(spawner_timer->interval() - 500);
}
}
}
int Game::getTileSize()
{
return this->map_tile_size;
}
QMap<int,Tile*> Game::getTiles()
{
return this->tiles;
}
int Game::x_scene(int x)
{
return x * this->map_tile_size;
}
int Game::y_scene(int y)
{
return y * this->map_tile_size;
}
void Game::restartScene()
{
Game *game = new Game();
game->show();
delete this;
}
int Game::indexOfPoint(int x, int y)
{
return x + map_width_in_tiles * y;
}
void Game::drawTilesOverlay(QString filename)
{
for(int i = 0; i < map_width_in_tiles; i++){
for(int n = 0; n < map_height_in_tiles; n++){
// set the floor overlay
QGraphicsPixmapItem * overlay = new QGraphicsPixmapItem();
overlay->setPixmap(QPixmap(filename));
overlay->setScale(1);
overlay->setZValue(100);
overlay->setPos(i*64,n*64);
scene->addItem(overlay);
}
}
}
void Game::drawTilesPoint()
{
QMapIterator<int,Tile*> tile(tiles);
while (tile.hasNext()){
tile.next();
QGraphicsTextItem *text = new QGraphicsTextItem();
int x = tile.value()->point.x();
int y = tile.value()->point.y();
text->setPlainText(QString("%1,%2").arg(x).arg(y));
text->setPos(x_scene(x),y_scene(y));
text->adjustSize();
scene->addItem(text);
}
}
void Game::printAllTiles()
{
QMapIterator<int,Tile*> tile(tiles);
while (tile.hasNext()){
tile.next();
}
}
void Game::mouseMoveEvent(QMouseEvent *event)
{
if (cursor !=nullptr){
//cursor->setPos(event->pos());
}
QLineF ln(QPointF(tower->x() + 5,tower->y() + 40),QPointF(event->pos()));
double angle = ((-1 * ln.angle()) + 90);
tower->setRotation(angle);
}
void Game::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton){
if (can_fire == true){
can_fire = false;
if (reload_bar_placeholder != nullptr){
scene->removeItem(reload_bar_placeholder);
reload_bar_placeholder = nullptr;
}
if (redraw_reload_bar == true){
delete reload_bar;
redraw_reload_bar = false;
}
// bullet
Bullet *bullet = new Bullet(*this);
QLineF ln(QPointF(tower->x() + 5,tower->y() + 40),QPointF(event->pos()));
bullet->setOffset(35,-10);
bullet->setPos(ln.x1(), ln.y1());
double angle = ((-1 * ln.angle()));
// QPointF *target = new QPointF(100,100);
// bullet->setTarget(target);
bullet->setRotation(angle);
bullet->setTarget(event->pos());
scene->addItem(bullet);
reload_bar = new QGraphicsRectItem(x_scene(19)+4,y_scene(4)-15,51,10);
reload_bar->setBrush(QBrush(Qt::green));
reload_bar->setZValue(999);
scene->addItem(reload_bar);
smoke_timer = new QTimer();
reload_timer = new QTimer();
reload_bar_timer = new QTimer();
connect(smoke_timer,SIGNAL(timeout()),this,SLOT(animateSmoke()));
//connect(reload_timer,SIGNAL(timeout()),this,SLOT(reloadTimer()));
connect(reload_bar_timer,SIGNAL(timeout()),this,SLOT(reloadBarTimer()));
smoke_timer->start(50);
reload_timer->start(3000);
reload_bar_timer->start(100);
// smoke on barrel
smoke = new QGraphicsPixmapItem(QPixmap(":/tank/assets/tank/smokeGrey4.png"));
smoke->setOffset(-25,-50);
smoke->setPos(ln.x1(), ln.y1());
smoke->setRotation(angle);
smoke->setZValue(199);
scene->addItem(smoke);
}
}
}
//void Game::keyPressEvent(QKeyEvent *ev)
//{
// switch (ev->key())
// {
// case Qt::Key_Return:
// buildwall = new BuildWall(*this);
// scene->setFocusItem(buildwall);
// break;
// }
//}
void Game::animateSmoke()
{
if (smoke->opacity() > 0) {
smoke->setOpacity(smoke->opacity() - 0.05);
}
else {
disconnect(smoke_timer,SIGNAL(timeout()),this,SLOT(animateSmoke()));
delete smoke;
}
}
void Game::reloadTimer()
{
disconnect(reload_timer,SIGNAL(timeout()),this,SLOT(reloadTimer()));
}
void Game::reloadBarTimer()
{
if (reload_bar_threshold == 2600) {
can_fire = true;
}
if (reload_bar_threshold < 2600) {
reload_bar_threshold += 100;
reload_bar->setRect(base_reload_bar->pos().x(),base_reload_bar->pos().y(),reload_bar_length,10);
reload_bar_length+=2;
}
else {
reload_bar_threshold = 0;
disconnect(reload_bar_timer,SIGNAL(timeout()),this,SLOT(reloadBarTimer()));
redraw_reload_bar = true;
reload_bar_length = 0;
}
}