-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
61 lines (53 loc) · 1.49 KB
/
bullet.cpp
File metadata and controls
61 lines (53 loc) · 1.49 KB
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
#include "bullet.h"
// Constructor
Bullet::Bullet(sf::Texture &bullet_texture, sf::Vector2f playerposition)
{
this->setTexture(bullet_texture);
this->setScale(0.625, 0.625); // 32 * 0.625 = 20
// Set position
this->setBulletPosition(playerposition);
}
// Set position for bullet
void Bullet::setBulletPosition(sf::Vector2f playerposition)
{
this->setPosition(playerposition.x +64/2 -this->getGlobalBounds().width/2, playerposition.y +5);
// 64/2 to center it wrt spaceship. +5 to hide bullet properly
}
// Setter for speed
void Bullet::setSpeed(int speedy)
{
speedy_ = speedy;
}
// Draw on window
void Bullet::drawBullet(sf::RenderWindow &window)
{
window.draw(*this);
}
// Bullet movement
void Bullet::animate(sf::Time elapsed, sf::Vector2f playerposition)
{
this->isAlive();
if(!this->fired) // If bullet is not yet fired
{
this->setBulletPosition(playerposition);
}
else if(this->fired) // If bullet is fired
{
this->move(0, -speedy_ * elapsed.asSeconds());
}
}
// Check if bullet is still in bounds and not collided
void Bullet::isAlive()
{
// Check if it is bounds of window i.e. not exited the top of the window
if(this->getGlobalBounds().top < 0.0)
{
// this->alive = false; // in case rapid bullets are implemented
this->fired = false; // So that bullet can be fired again
}
}
// Get bullet globalbounds to check for collision
sf::FloatRect Bullet::getGbounds()
{
return this->getGlobalBounds();
}