-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tank.cpp
202 lines (173 loc) · 3.12 KB
/
Tank.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
#include "Tank.h"
#include "EnemyTank.h"
#include "TankWindow.h"
#include "Missile.h"
#include <QtGui>
#include <iostream>
#define NO_HIT_OTHER
using namespace std;
extern Map::map firstMap;
Tank::Tank(QPoint startPoint, TankWindow *tankWindow,
const TankType& type, Direction::Direction dir)
:MapObject(startPoint, pic_width, pic_height)
{
this->name = type.name;
this->speed = type.speed;
this->max_missile = type.missile;
position = startPoint;
this -> dir= dir;
this -> tankWindow = tankWindow;
alive = true;
missile_num = 0;
}
Tank::~Tank()
{
}
void Tank::moveUp()
{
move(Direction::up);
}
void Tank::moveDown()
{
move(Direction::down);
}
void Tank::moveLeft()
{
move(Direction::left);
}
void Tank::moveRight()
{
move(Direction::right);
}
bool Tank::move(Direction::Direction dir)
{
if (!alive)
return true;
old_position = position;
switch (dir)
{
case Direction::up:
position.setY(position.y() - speed);
break;
case Direction::down:
position.setY(position.y() + speed);
break;
case Direction::left:
position.setX(position.x() - speed);
break;
case Direction::right:
position.setX(position.x() + speed);
break;
default:
break;
}
this->dir = dir;
rect.moveTo(position);
/* if cannot move to there */
if (outOfMap() || hitBarrier() || hitOtherTank())
{
undo();
return false;
}
return true;
}
bool Tank::move()
{
return move(dir);
}
/*
* shoot missiles
*/
void Tank::shoot()
{
if (!alive || missile_num >= max_missile)
return;
PlayMusic("fire.wav");
missile_num++;
QPoint missilePoint;
int x = position.x();
int y = position.y();
int offset = (tank_width - missile_width) / 2;
switch (dir)
{
case Direction::up:
missilePoint.setX(x+offset);
missilePoint.setY(y-missile_height);
break;
case Direction::down:
missilePoint.setX(x+offset);
missilePoint.setY(y+tank_height);
break;
case Direction::right:
missilePoint.setX(x+tank_width);
missilePoint.setY(y+offset);
break;
case Direction::left:
missilePoint.setX(x-missile_width);
missilePoint.setY(y+offset);
break;
default:
break;
}
Missile *missile = new Missile(missilePoint,
dir, this);
tankWindow->addMissile(missile);
}
void Tank::downMissile()
{
missile_num -- ;
}
bool Tank::hitBarrier()
{
return tankWindow->hitBarrier(*this);
}
bool Tank::hitOtherTank()
{
#ifdef NO_HIT_OTHER
return false;
#endif
}
bool Tank::isAlive()
{
return alive;
}
bool Tank::kill()
{
alive = false;
return true;
}
void Tank::drawTank(QPainter &painter)
{
QImage tank;
switch(dir)
{
case Direction::left:
tank.load(QString(":/image/small/%1L.gif").arg(name));
break;
case Direction::right:
tank.load(QString(":/image/small/%1R.gif").arg(name));
break;
case Direction::up:
tank.load(QString(":/image/small/%1U.gif").arg(name));
break;
case Direction::down:
tank.load(QString(":/image/small/%1D.gif").arg(name));
break;
default:
return;
}
drawTankPict(painter, tank);
}
void Tank::drawTankPict(QPainter &painter, QImage& tank)
{
painter.drawImage(position, tank);
}
void Tank::undo()
{
position = old_position;
rect.moveTo(old_position);
}
void Tank::upgrade()
{
max_missile *= 1.5;
}