forked from RedDef/Tower_Defence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
196 lines (155 loc) · 5 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
#include "game.h"
#include "bullet.h"
#include "enemy.h"
#include "gridpoint.h"
#include "buildredtowericon.h"
#include "buildbluetowericon.h"
#include "buildgreentowericon.h"
#include "buildblacktowericon.h"
#include "buildwhitetowericon.h"
#include "buildyellowtowericon.h"
#include <QDebug>
#include <QTimer>
#include <QPen>
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <qmath.h>
Game::Game(): QGraphicsView(){
move_timer = new QTimer(this);
move_timer->start(20);
//create a scene
scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,920,720);
//set the scene
setScene(scene);
//set cursor
cursor = nullptr;
building = nullptr;
setMouseTracking(true);
//alter window
setFixedSize(920,720);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//split screen into playfield and menu
field_menu << QPointF(720,0) << QPointF(720,720);
for(size_t i=0, n = field_menu.size()-1; i < n; i++){
//create a line
QLineF line(field_menu[i],field_menu[i+1]);
QGraphicsLineItem * lineItem = new QGraphicsLineItem(line);
QPen pen;
pen.setWidth(5);
pen.setColor(Qt::black);
lineItem->setPen(pen);
scene->addItem(lineItem);
}
//create gridpoints
for(gPoint_x=0; gPoint_x<20; gPoint_x++){
for(gPoint_y=0; gPoint_y<20; gPoint_y++){
Gridpoint *gPoint = new Gridpoint;
gPoint->setPos(x()+12+(36*gPoint_x),y()+12+(36*gPoint_y));
scene->addItem(gPoint);
}
}
//creat enemy
spawnTimer = new QTimer(this);
enemiesSpawned = 0;
maxNumberOfEnemies = 0;
pointsToFollow << QPointF(18,18) << QPointF(18,414) << QPointF(198,414)<< QPointF(594,414) << QPointF(594,54) << QPointF(800,54)<< QPointF(594,54);
for(int i=0;i<=(pointsToFollow.length()-2);i++){
QPointF temp;
double test;
temp=((pointsToFollow[i+1])-(pointsToFollow[i]));
temp=temp;
direction[i][0]=temp.x()/(sqrt(pow(temp.x(),2)+pow(temp.y(),2)));
direction[i][1]=temp.y()/(sqrt(pow(temp.x(),2)+pow(temp.y(),2)));
}
createEnemies(20);
//create Road
createRoad();
//create Tower-Icon
BuildRedTowerIcon * rt = new BuildRedTowerIcon();
BuildGreenTowerIcon * gt = new BuildGreenTowerIcon();
BuildBlueTowerIcon * bt = new BuildBlueTowerIcon();
BuildBlackTowerIcon * bkt = new BuildBlackTowerIcon();
BuildWhiteTowerIcon * wt = new BuildWhiteTowerIcon();
BuildYellowTowerIcon * yt = new BuildYellowTowerIcon();
//move tower icons
rt->setPos(x()+850,y());
gt->setPos(x()+850,y()+50);
bt->setPos(x()+850,y()+100);
bkt->setPos(x()+850,y()+150);
yt->setPos(x()+850,y()+200);
wt->setPos(x()+850,y()+250);
scene->addItem(rt);
scene->addItem(gt);
scene->addItem(bt);
scene->addItem(bkt);
scene->addItem(yt);
scene->addItem(wt);
}
void Game::setCursor(QString filename){
if(cursor){
scene->removeItem(cursor);
delete cursor;
}
cursor = new QGraphicsPixmapItem();
cursor->setPixmap(QPixmap(filename));
scene->addItem(cursor);
}
void Game::mouseMoveEvent(QMouseEvent *event){
if(cursor){
cursor->setPos((event->pos().x()-18),(event->pos().y())-18);
}
}
void Game::mousePressEvent(QMouseEvent *event){
if(building){
qDebug() << "if build ";
//return if cursor is coliding with a tower
QList<QGraphicsItem *> items = cursor->collidingItems();
for(size_t i=0, n = items.size(); i<n; i++){
qDebug() << "for ";
Tower *tower = dynamic_cast<Tower *>(items[i]);
if(tower){
qDebug() << "if ";
return;
}else{
qDebug() << "else ";
}
}
//otherwise build at the clicked location
scene->addItem(building);
building->setPos((event->pos().x()-18),(event->pos().y()-18));
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(1173);
}
void Game::createRoad(){
/*for(size_t i=0, n = pointsToFollow.size()-1; i < n; i++){
//create a line
QLineF line(pointsToFollow[i],pointsToFollow[i+1]);
QGraphicsLineItem * lineItem = new QGraphicsLineItem(line);
QPen pen;
pen.setWidth(50);
pen.setColor(Qt::darkGray);
lineItem->setPen(pen);
scene->addItem(lineItem);
}*/
}
void Game::spawnEnemy(){
//spawn an enemy
Enemy * enemy = new Enemy(pointsToFollow);
enemy->setPos((pointsToFollow[0].x()-18),(pointsToFollow[0].y()-18));
scene->addItem(enemy);
enemiesSpawned += 1;
if(enemiesSpawned >= maxNumberOfEnemies){
spawnTimer->disconnect();
}
}