-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.cpp
More file actions
65 lines (55 loc) · 1.43 KB
/
Particle.cpp
File metadata and controls
65 lines (55 loc) · 1.43 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
62
63
64
65
#include "Particle.h"
#include "TextureData.h"
#include <SFML/Graphics/RenderTarget.hpp>
Particle::Particle(
const sf::Vector2f &position,
const sf::Vector2f &velocity,
const sf::Vector2f &acceleration,
float velRotation,
float durationSecs,
TextureId textureId)
:
sf::Drawable(),
durationClock_(durationSecs),
sprite_(TextureData::getInstance().getTexture(textureId)),
position_(position),
velocity_(velocity),
acceleration_(acceleration),
rotation_(0.0f),
velRotation_(velRotation),
scale_(1.0f)
{
sprite_.setPosition(position_);
sprite_.setRotation(rotation_);
const sf::FloatRect spriteLocalBounds = sprite_.getLocalBounds();
sprite_.setOrigin(
spriteLocalBounds.width * 0.5f,
spriteLocalBounds.height * 0.5f);
}
void Particle::update(float deltaTime)
{
durationClock_.update(deltaTime);
scale_ = (durationClock_.getTargetSecs() - durationClock_.getSecs()) / durationClock_.getTargetSecs();
position_ += velocity_ * deltaTime;
velocity_ += acceleration_ * deltaTime;
rotation_ += velRotation_ * deltaTime;
if (rotation_ >= 360.0f)
{
rotation_ -= 360.0f;
}
else if (rotation_ <= -360.0f)
{
rotation_ += 360.0f;
}
sprite_.setPosition(position_);
sprite_.setRotation(rotation_);
sprite_.setScale(sf::Vector2f(scale_, scale_));
}
void Particle::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(sprite_, states);
}
bool Particle::isLive() const
{
return !durationClock_.isDone();
}