Files
twang-mp/TWANG32/Particle.h

65 lines
1.2 KiB
C
Raw Normal View History

#include "Arduino.h"
#define FRICTION 1
class Particle
{
public:
void Spawn(int pos, int life, int aging_speed, const int color[3]);
void Tick(int USE_GRAVITY);
void Kill();
bool Alive();
int _pos;
int _power;
int _color[3];
private:
int _life;
int _alive;
int _sp;
int _aging;
};
void Particle::Spawn(int pos, int life, int aging_speed, const int color[3]){
_pos = pos;
_sp = random(-life, life);
_power = 255;
_alive = 1;
_life = life - abs(_sp);
_aging = aging_speed;
for (int i = 0; i< 3; i++) _color[i] = color[i];
}
void Particle::Tick(int USE_GRAVITY){
if(_alive){
_life ++;
if(_sp > 0){
_sp -= _life/10;
}else{
_sp += _life/10;
}
if(USE_GRAVITY && _pos > 500) _sp -= 10;
_power = 100 - _aging*_life;
if(_power <= 0){
Kill();
}else{
_pos += _sp/7.0;
if(_pos > 1000){
_pos = 1000;
_sp = 0-(_sp/2);
}
else if(_pos < 0){
_pos = 0;
_sp = 0-(_sp/2);
}
}
}
}
bool Particle::Alive(){
return _alive;
}
void Particle::Kill(){
_alive = 0;
}