-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.cpp
65 lines (60 loc) · 1.38 KB
/
Particle.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
#include "Particle.h"
#include <iostream>
#include <cmath>
Particle::Particle(const sf::Sprite &source, float x, float y, float dir, float speed, float acc, float size, float life) : sf::Sprite(source)
{
this->dir = dir;
this->speed = speed;
this->acc = acc;
setPosition(x, y);
this->speed_x = Maths::lengthdir_x(speed, dir);
this->speed_y = Maths::lengthdir_y(speed, dir);
this->acc_x = Maths::lengthdir_x(acc, dir);
this->acc_y = Maths::lengthdir_y(acc, dir);
this->life = life;
if (life > 0)
{
this->alpha_dec = ((getColor().a) / (life));
}
else
{
this->alpha_dec = 0;
}
this->setScale(size, size);
this->alpha = getColor().a;
}
Particle::Particle(const sf::Sprite &source) : sf::Sprite(source)
{}
Particle::~Particle()
{}
bool Particle::step()
{
move(speed_x, speed_y);
if (alpha_dec)
{//if has a lifespan
if (getColor().a >= alpha_dec)
{
alpha -= alpha_dec;
setColor(sf::Color(getColor().r, getColor().g, getColor().b, alpha));
}
}
if (acc)
{
this->speed_x -= acc_x;
this->speed_y -= acc_y;
}
if (life)
{
life--;
return(false);
}
return(true);
}
void Particle::push(float pdir, float pspeed)
{
this->speed_x += Maths::lengthdir_x(pspeed, pdir);
this->speed_y += Maths::lengthdir_y(pspeed, pdir);
this->dir = std::atan(speed_x / speed_y);
this->acc_x = Maths::lengthdir_x(acc, dir);
this->acc_y = Maths::lengthdir_y(acc, dir);
}