-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.cpp
71 lines (58 loc) · 1.4 KB
/
Bullet.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
#include "Bullet.h"
#include "UtilsForVector.h"
#include "Assets.h"
#include "Settings.h"
const float Bullet::kMaxDistance = 800.0f;
const float Bullet::kAccelerationBullet = 10.0f;
Bullet::Bullet()
{
m_texture.loadFromImage(Assets::Instance().getBullet());
setTexture(m_texture);
sf::IntRect textureRect = getTextureRect();
setOrigin(textureRect.width / 2.f, textureRect.height / 2.f);
m_isAlive = false;
m_distance = 0.0f;
m_vectorSpeed = { 0.0f, 0.0f };
}
void Bullet::update(float deltaTime)
{
if (m_isAlive)
{
move(m_vectorSpeed);
m_distance += lengthVector(m_vectorSpeed);
if (m_distance > kMaxDistance)
die();
}
}
void Bullet::move(sf::Vector2f vectorSpeed)
{
sf::Vector2f bulletPosition = getPosition();
sf::Vector2f mapSize = Settings::Instance().getMapSize();
bulletPosition += vectorSpeed;
if (bulletPosition.x > mapSize.x) bulletPosition.x = 0;
if (bulletPosition.x < 0) bulletPosition.x = mapSize.x;
if (bulletPosition.y > mapSize.y) bulletPosition.y = 0;
if (bulletPosition.y < 0) bulletPosition.y = mapSize.y;
setPosition(bulletPosition);
}
bool Bullet::isAlive() const
{
return m_isAlive;
}
void Bullet::wakeUp()
{
m_isAlive = true;
m_distance = 0.0f;
m_vectorSpeed = vectorDirection(getRotation()) * kAccelerationBullet;
}
void Bullet::die()
{
m_isAlive = false;
}
void Bullet::render(sf::RenderWindow* renderWindow)
{
if (m_isAlive)
{
renderWindow->draw(*this);
}
}