-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMissile.cpp
77 lines (76 loc) · 1.92 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
/*
No rights Reserved.
Amish Kumar Naidu
amhndu at gmail.com
*/
#include "Missile.h"
#include "Application.h"
#include "Land.h"
#include "Tank.h"
#include "utilities.h"
#include "WeaponPostEffects.h"
Missile::Missile(double x, double y) : velocity(0, 0) , acceleration(0, constants::gravity)
{
type = WorldObject::WeaponType;
selfDestruct = false;
projectile.setRadius(2.5);
projectile.setFillColor(sf::Color::Magenta);
projectile.setPosition(x, y);
Application::getGame().incCounter();
}
Missile::~Missile()
{
Application::getGame().decCounter();
}
void Missile::setVelocity(double x, double y)
{
velocity.x = x;
velocity.y = y;
}
void Missile::setAcceleration(double x, double y)
{
acceleration.x = x;
acceleration.y = y;
}
void Missile::handleCollision(WorldObject &b)
{
switch(b.type)
{
case WorldObject::TankType:
{
Tank &t = static_cast<Tank&>(b);
if(intersects(projectile,t.getTankRect()))
explode();
}
break;
case WorldObject::LandType:
{
Land &l = static_cast<Land&>(b);
if(projectile.getPosition().y + projectile.getRadius() > (constants::windowHeight - l.getHeight(projectile.getPosition().x)))
explode();
}
break;
default:
break;
}
}
void Missile::draw(sf::RenderTarget &target)
{
target.draw(projectile);
}
void Missile::step(float dt)
{
velocity += acceleration * dt;
projectile.move( velocity * dt );
if(projectile.getPosition().x >= constants::windowWidth || projectile.getPosition().x < 0
|| projectile.getPosition().y >= constants::windowHeight)
{
selfDestruct = true;
}
}
void Missile::explode()
{
Application::getMsgStream().sendMessage(Message("DestroyCircle",sf::Vector2i(projectile.getPosition()),30),"Land");
Application::getGame().addWorldObj(new Explosion(MissileExplosionA,projectile.getPosition(),30));
selfDestruct = true;
}