forked from WuYufeng233/Tank3000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
missile.cpp
245 lines (204 loc) · 6.25 KB
/
missile.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
#include "missile.h"
#include<QDebug>
Missile::Missile(const Tank&tank)
{
dir=tank.dir;
pos.setX(tank.pos.x()+21);
pos.setY(tank.pos.y()+21);
calSphere();
step=tank.step+10;
kind=tank.kind;
attack=tank.attack;
group=tank.group;
bool tmp=false;
setDisappear(tmp);
}
void Missile::display(QPainter &paint)
{
if(this->isDisappeared())
return;
paint.drawImage(rectSphere,glb.missile);
}
void Missile::move()
{
switch(this->dir)
{
case Up:
pos.setY(pos.y()-step);
break;
case Down:
pos.setY(pos.y()+step);
break;
case Left:
pos.setX(pos.x()-step);
break;
case Right:
pos.setX(pos.x()+step);
break;
}
calSphere();
qDebug("before missile boom");
//导弹是否与地图块碰撞
for(int i=0;i<Row;++i)
{
for(int j=0;j<Col;++j)
{
if(glb.map->getSeg(i,j))
{
if(!glb.map->getSeg(i,j)->isPenetration2())
{
if(isCollision(*glb.map->getSeg(i,j)))
{
qDebug("missile hit wall!!!!!!!!");
glb.map->getSeg(i,j)->beAttacked(attack);
bool tmp=true;
setDisappear(tmp);//导弹消失
if(this->kind==glb.player->kind&&glb.player->getMissileNum()==1)
{
glb.player->setMissileNum();
}
for(int k=0;k<glb.enemytanks.count();++k)
{
if(this->kind==glb.enemytanks.at(k)->kind&&glb.enemytanks.at(k)->getMissileNum()==1)
{
glb.enemytanks.at(k)->setMissileNum();
break;
}
}
}
}
}
}
}
qDebug("missile 1-------");
//玩家导弹是否击中敌方坦克
for(int i=0;i<glb.enemytanks.count();++i)
{
if(glb.enemytanks.at(i)
&&glb.enemytanks.at(i)->group!=this->group
&&isCollision(*glb.enemytanks.at(i)))
{
bool tmp=true;
setDisappear(tmp);//导弹消失
glb.enemytanks.at(i)->beAttacked(attack);
if(glb.player->getMissileNum()==1&&this->kind==glb.player->kind)
{
glb.player->setMissileNum();
}
for(int k=0;k<glb.enemytanks.count();++k)
{
if(glb.enemytanks.at(k)->getMissileNum()==1&&this->kind==glb.enemytanks.at(k)->kind)
{
glb.enemytanks.at(k)->setMissileNum();
break;
}
}
qDebug("missile hit tank!!!!!!!!");
}
}
qDebug("missile 2-------");
if(glb.player->group!=this->group
&&isCollision(*glb.player))
{
bool tmp=true;
setDisappear(tmp);//导弹消失
glb.player->beAttacked(attack);
if(glb.player->getMissileNum()==1&&this->kind==glb.player->kind)
{
glb.player->setMissileNum();
}
for(int k=0;k<glb.enemytanks.count();++k)
{
if(glb.enemytanks.at(k)->getMissileNum()==1&&this->kind==glb.enemytanks.at(k)->kind)
{
glb.enemytanks.at(k)->setMissileNum();
break;
}
}
}
//敌方导弹是否击中玩家坦克
if(this->group==1)
{
for(int i=0;i<glb.enemytanks.count();++i)
{
if(glb.enemytanks.at(i)&&this->isCollision(*glb.player))
{
bool tmp=true;
this->setDisappear(tmp);
glb.player->beAttacked(this->attack);
for(int k=0;k<glb.enemytanks.count();++k)
{
if(glb.enemytanks.at(k)->getMissileNum()==1&&this->kind==glb.enemytanks.at(k)->kind)
{
glb.enemytanks.at(k)->setMissileNum();
break;
}
}
}
}
}
//导弹是否出界
if(pos.x()<0
||pos.x()>Width-5
||pos.y()<0
||pos.y()>Height-5)
{
bool tmp=true;
setDisappear(tmp);//导弹消失
if(glb.player->getMissileNum()==1&&this->kind==glb.player->kind)
{
glb.player->setMissileNum();
}
for(int k=0;k<glb.enemytanks.count();++k)
{
if(glb.enemytanks.at(k)->getMissileNum()==1&&this->kind==glb.enemytanks.at(k)->kind)
{
glb.enemytanks.at(k)->setMissileNum();
break;
}
}
qDebug("missile hit edge!!!!!!!!");
}
qDebug("missile 3------");
//导弹是否击中boss
if(this->isCollision1(*glb.boss))
{
bool tmp=true;
//导弹消失
setDisappear(tmp);
glb.boss->beAttacked(attack);
glb.boss->setDisappear(tmp);
if(glb.player->getMissileNum()==1&&this->kind==glb.player->kind)
{
glb.player->setMissileNum();
}
for(int k=0;k<glb.enemytanks.count();++k)
{
if(glb.enemytanks.at(k)->getMissileNum()==1&&this->kind==glb.enemytanks.at(k)->kind)
{
glb.enemytanks.at(k)->setMissileNum();
break;
}
}
}
}
int Missile::getKind()const
{
return this->kind;
}
void Missile::calSphere()
{
this->rectSphere.setRect(pos.x(),pos.y(),MissileWidth,MissileHeight);
}
bool Missile::isCollision1(Boss &boss)const
{
if(this->rectSphere.right()<boss.rectSphere.left()
||this->rectSphere.left()>boss.rectSphere.right()
||this->rectSphere.bottom()<boss.rectSphere.top()
||this->rectSphere.top()>boss.rectSphere.bottom())
{
return false;
}
else
return true;
}