-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTank.cpp
208 lines (204 loc) · 6.83 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
199
200
201
202
203
204
205
206
207
208
#include "Tank.h"
#include "Application.h"
#include "Missile.h"
#include "utilities.h"
#include "WeaponPostEffects.h"
#define HMAP(n) Application::getGame().getLandHeight(n)
#define R_POINTDIST(n) (cachedSqrt(1 + (HMAP(n) - HMAP((n)+1))*(HMAP(n) - HMAP((n)+1)))+0.5)
#define L_POINTDIST(n) (-cachedSqrt(1+ (HMAP((n)-1) - HMAP(n))*(HMAP((n)-1) - HMAP(n)))-0.5)
#define TANK_WIDTH tank.getLocalBounds().width
#define TANK_HEIGHT tank.getLocalBounds().height
const float lvelocity = 150;
Tank::Tank() :
WorldObject(TankType) ,
myOwner(nullptr) ,
moving() ,
tank(Application::getTexture(TankTexture)) ,
turret(Application::getTexture(TurretTexture)),
lifeBg(sf::Vector2f(TANK_WIDTH,5)),
lifeFill(sf::Vector2f()),
freefall(true),
velocity(),
Name(),
fadingLife(true),
fadingTimer(1)
{
Application::getGame().incCounter();
tank.setOrigin(tank.getLocalBounds().width / 2, tank.getLocalBounds().height);
turret.setOrigin(0, turret.getLocalBounds().height / 2);
turret.setRotation(-45);
lifeBg.setOutlineColor(sf::Color::Black);
lifeBg.setOutlineThickness(1);
lifeBg.setFillColor(sf::Color::Yellow);
lifeFill.setFillColor(sf::Color::Red);
Name.setColor(sf::Color::Black);
Name.setCharacterSize(15);
Name.setFont(Application::getFont(UbuntuCondensed));
setPosition(sf::Vector2f());
}
Tank::~Tank()
{
if(freefall)
Application::getGame().decCounter();
}
sf::RectangleShape Tank::getTankRect()
{
sf::RectangleShape rs(sf::Vector2f(tank.getLocalBounds().width,tank.getLocalBounds().height));
rs.setPosition(tank.getPosition().x,tank.getPosition().y-TANK_HEIGHT/2);
return rs;
}
void Tank::weapAct(float dlife)
{
int newlife = std::max(0.0f,myOwner->getLife()-dlife);
assert(newlife<=Player::maxlife);
myOwner->setLife(newlife);
if(newlife == 0)
{
selfDestruct = true;
Application::getMsgStream().sendMessage(Message("TankDestroyed"),"GameState");
}
setLifeFill(newlife);
fadingLife = true;
fadingTimer = 1;
lifeBg.setFillColor(lifeBg.getFillColor()+sf::Color(0,0,0,255));
lifeBg.setOutlineColor(lifeBg.getOutlineColor()+sf::Color(0,0,0,255));
lifeFill.setFillColor(lifeFill.getFillColor()+sf::Color(0,0,0,255));
Name.setColor(Name.getColor()+sf::Color(0,0,0,255));
}
void Tank::setPlayer(Player *p)
{
myOwner = p;
Name.setString(p->getName());
setPosition(tank.getPosition());
setLifeFill(p->getLife());
}
void Tank::setLifeFill(int l)
{
lifeFill.setSize(sf::Vector2f(l*lifeBg.getSize().x/100,lifeBg.getSize().y));
}
void Tank::receiveMessage(const Message& msg)
{}
void Tank::handleCollision(WorldObject &b)
{
switch(b.type)
{
case LandType:
if(freefall)
{
int x0 = tank.getPosition().x , y0 = tank.getPosition().y;
int h = constants::windowHeight-HMAP(x0);
if(h <= y0)//collision
{
float ang = Application::getGame().getLandNormAng(x0,y0);
tank.setRotation(std::fmod(90 - TO_DEG(ang),360) );
freefall = false;
velocity = sf::Vector2f();
Application::getGame().decCounter();
setPosition(x0,h);
break;
}
}
break;
case WeaponType:
case WeaponPostEffectType:
b.handleCollision(*this);
break;
default:
break;
}
}
void Tank::draw(sf::RenderTarget &target)
{
target.draw(turret);
target.draw(tank);
if(lifeFill.getFillColor().a != 0)
{
target.draw(lifeBg);
target.draw(lifeFill);
target.draw(Name);
}
}
void Tank::setMovement(int val) { moving = val; }
void Tank::step(float dt)
{
if(freefall)
{
velocity.y += constants::gravity*dt;
sf::Vector2f ds = velocity*dt;
tank.move(ds);
turret.move(ds);
lifeBg.move(ds);
lifeFill.move(ds);
Name.move(ds);
}
else
{
if(tank.getPosition().y<constants::windowHeight-HMAP(tank.getPosition().x))
{
freefall = true;
Application::getGame().incCounter();
}
if(moving)
{
float phi = TO_RAD(tank.getRotation());
//xvel is the horizontal component of the velocity with the angle phi.
//we set the minimum velocity to 2 pixels/second
//(moving<0)?-1:1 multiplies xvel by -1 if moving in the left direction
float xvel = std::max(2.0f,lvelocity*std::cos(phi))*((moving<0)?-1:1);
sf::Vector2f ds(xvel*dt,0);
ds.y = constants::windowHeight-HMAP(tank.getPosition().x+ds.x)-tank.getPosition().y;
moving = 0;
if(ds.y < 0 && std::abs(ds.y/ds.x) > std::tan(TO_RAD(80))) //if ascend (going up) is too steep
return;
tank.move(ds);
turret.move(ds);
lifeBg.move(ds);
lifeFill.move(ds);
Name.move(ds);
float ang = Application::getGame().getLandNormAng(tank.getPosition().x,tank.getPosition().y);
tank.setRotation(std::fmod(90 - TO_DEG(ang),360) );
}
}
sf::Vector2f dist = sf::Vector2f(sf::Mouse::getPosition(Application::getWindow())) - tank.getPosition();
if(Application::getGame().isPlayerTurn() && sq(dist.x)+sq(dist.y) <= sq(50))
{
fadingLife = true;
fadingTimer = 1;
lifeBg.setFillColor(lifeBg.getFillColor()+sf::Color(0,0,0,255));
lifeBg.setOutlineColor(lifeBg.getOutlineColor()+sf::Color(0,0,0,255));
lifeFill.setFillColor(lifeFill.getFillColor()+sf::Color(0,0,0,255));
Name.setColor(Name.getColor()+sf::Color(0,0,0,255));
}
else if(fadingLife && (fadingTimer-=dt) < 0)
{
if(lifeBg.getFillColor().a != 0)
{
const float rate = 255.0/1;//fade out in 1 second
int dAlpha = rate*dt;
lifeFill.setFillColor(lifeFill.getFillColor()-sf::Color(0,0,0,dAlpha));
lifeBg.setFillColor(lifeBg.getFillColor()-sf::Color(0,0,0,dAlpha));
lifeBg.setOutlineColor(lifeBg.getOutlineColor()-sf::Color(0,0,0,dAlpha));
Name.setColor(Name.getColor()-sf::Color(0,0,0,dAlpha));
}
else
fadingLife = false;
}
}
void Tank::reset()
{
moving = 0;
turret.setRotation(-45);
if(!freefall)
{
freefall = true;
Application::getGame().incCounter();
}
}
void Tank::setPosition(const sf::Vector2f& pos)
{
tank.setPosition(pos);
turret.setPosition(pos-sf::Vector2f(0,TANK_HEIGHT/2));
lifeBg.setPosition(pos.x-lifeBg.getGlobalBounds().width/2,pos.y-std::max(TANK_HEIGHT,TANK_WIDTH)-turret.getLocalBounds().width);
lifeFill.setPosition(lifeBg.getPosition());
Name.setPosition(pos.x-Name.getLocalBounds().width/2,lifeBg.getPosition().y-lifeBg.getLocalBounds().height-Name.getLocalBounds().height);
}