-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
47 lines (35 loc) · 1.18 KB
/
Copy pathbullet.cpp
File metadata and controls
47 lines (35 loc) · 1.18 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
#include "Bullet.hpp"
#include <cmath>
#include <SFML/Graphics.hpp>
#include "entity.hpp"
#include "constants.hpp"
const sf::Texture Bullet::BULLET_TEXTURE = Entity::loadTexture("../images/laser.png");
const float Bullet::BULLET_LIFE = 1.0f;
const float Bullet::BULLET_SPEED = 200.0f;
Bullet::Bullet(sf::Vector2f position, sf::Vector2f direction) :
Entity(position, acos(direction.x) / DEG2RAD, BULLET_LIFE)
{
direction_ = direction;
if (direction_.y < 0)
{
angle_ *= -1;
}
sprite_.setTexture(BULLET_TEXTURE);
sprite_.setOrigin(sprite_.getGlobalBounds().width / 2.0f, sprite_.getGlobalBounds().height / 2.0f);
sprite_.setPosition(position_);
sprite_.setScale(0.3f, 0.3f);
sprite_.setRotation(angle_);
}
void Bullet::update(sf::Time deltaTime)
{
sf::Vector2f distance = direction_ * deltaTime.asSeconds() * BULLET_SPEED;
position_.x += distance.x;
position_.y += distance.y;
float radius = sprite_.getLocalBounds().width / 2.5;
if (position_.x + radius < 0.0f || position_.x - radius > static_cast<float>(WIDTH) ||
position_.y + radius < 0.0f || position_.y - radius > static_cast<float>(HEIGHT))
{
damage();
}
sprite_.setPosition(position_);
}