Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PLIK PARTICLES.H:
- #ifndef PARTICLES_H
- #define PARTICLES_H
- #include <SDL2/SDL.h>
- #include <vector>
- #include <memory>
- #include "vector2d.h"
- #include "random.h"
- class Particle
- {
- public:
- Particle(Vector2D _position, Vector2D _velocity, int _size, float _lifeTime, SDL_Color _color);
- virtual ~Particle();
- void draw(SDL_Renderer* renderer);
- void animate(float dt);
- void setPosition(Vector2D _position) { position = _position; }
- Vector2D getPosition() const { return position; }
- void setVelocity(Vector2D _velocity) { velocity = _velocity; }
- void changeVelocity(Vector2D _dv) { velocity += _dv; }
- void setSize(int _size) { size = _size; }
- void changeSize(int ds) { size += ds; }
- int getSize() const { return size; }
- float getLifeTime() const { return lifeTime; }
- void setColor(SDL_Color _color) { color = _color; }
- float getCurrentTime() const { return time; }
- bool isAlive() { return live; }
- void dead() { live = false; }
- void setState(int i) { state = i; }
- int getState() { return state; }
- protected:
- SDL_Rect rect;
- Vector2D position;
- Vector2D velocity;
- int size;
- float lifeTime;
- SDL_Color color;
- bool live = true;
- int state = 0;
- float time = 0.0f;
- private:
- };
- class Emiter
- {
- public:
- Emiter(Vector2D _startPoint, float _maxTime, int _width, int _height, int _minParticles, int _numParticles,
- int _maxParticles, int _particleMinSize = 1, int _particleMaxSize = 2, float _particleTime = 1.0f,
- int r = 255, int g = 255, int b = 255, int a = 255, float _velocityDispersion = 0.0f, int _repeat = 0,
- float _frequency = 0.0f, Vector2D _drift = Vector2D(), bool _is_dir = false, float _dir = 0.0f,
- float _speed = 0.0f);
- virtual ~Emiter();
- void init();
- void draw(SDL_Renderer *renderer);
- void animate(float dt);
- void setDrift(Vector2D _drift) { drift = _drift; }
- void setDirection(float _dir) { dir = _dir; }
- void changeDirection(float _dr) { dir += _dr; }
- void setSpeed(float _speed) { speed = _speed; }
- bool isVisible() { return visible; }
- protected:
- virtual void modifyParticles(float dt) = 0;
- Vector2D startPoint;
- float maxTime;
- int width;
- int height;
- int minParticles;
- int numParticles;
- int maxParticles;
- int particleMinSize;
- int particleMaxSize;
- float particleTime;
- SDL_Color particleColor;
- float velocityDispersion;
- int repeat;
- float frequency;
- Vector2D drift;
- bool is_dir;
- float dir;
- float speed;
- std::vector<std::unique_ptr<Particle> > particle;
- bool visible = true;
- float time = 0.0f;
- Random random;
- private:
- void createParticles(float dt);
- void createParticle();
- float w, h, t = 0;
- int r = 0;
- Vector2D v;
- };
- class Fire : public Emiter
- {
- public:
- Fire(Vector2D _startPoint, float _direction);
- ~Fire();
- protected:
- void modifyParticles(float dt);
- private:
- };
- class Explosion : public Emiter
- {
- public:
- Explosion(Vector2D _startPoint, float _direction);
- ~Explosion();
- protected:
- void modifyParticles(float dt);
- private:
- };
- #endif // PARTICLES_H
- PLIK PARTICLES.CPP:
- #include "particles.h"
- #include <iostream>
- #include <cmath>
- using namespace std;
- Particle::Particle(Vector2D _position, Vector2D _velocity, int _size, float _lifeTime, SDL_Color _color)
- : position(_position), velocity(_velocity), size(_size), lifeTime(_lifeTime), color(_color)
- {
- }
- Particle::~Particle()
- {
- }
- void Particle::draw(SDL_Renderer* renderer)
- {
- if (!live) return;
- SDL_Rect rect;
- rect.x = position.getX();
- rect.y = position.getY();
- rect.w = size;
- rect.h = size;
- SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
- SDL_RenderFillRect(renderer, &rect);
- }
- void Particle::animate(float dt)
- {
- if (time >= lifeTime)
- {
- live = false;
- return;
- }
- position += velocity*dt;
- time += dt;
- }
- Emiter::Emiter(Vector2D _startPoint, float _maxTime, int _width, int _height, int _minParticles, int _numParticles,
- int _maxParticles, int _particleMinSize, int _particleMaxSize, float _particleTime, int r, int g, int b,
- int a, float _velocityDispersion, int _repeat, float _frequency, Vector2D _drift, bool _is_dir,
- float _dir, float _speed)
- : startPoint(_startPoint), maxTime(_maxTime), width(_width), height(_height), minParticles(_minParticles),
- numParticles(_numParticles), maxParticles(_maxParticles), particleMinSize(_particleMinSize),
- particleMaxSize(_particleMaxSize), particleTime(_particleTime), velocityDispersion(_velocityDispersion),
- repeat(_repeat), frequency(_frequency), drift(_drift), is_dir(_is_dir), dir(_dir), speed(_speed)
- {
- particleColor.r = r; particleColor.g = g; particleColor.b = b; particleColor.a = a;
- init();
- }
- Emiter::~Emiter()
- {
- }
- void Emiter::init()
- {
- if (is_dir)
- {
- dir = dir*M_PI/180.0f;
- float len = sqrt(width*width + height*height);
- w = len*cos(dir);
- h = len*sin(dir);
- }
- else
- {
- w = width;
- h = height;
- }
- for (int i = 0; i < minParticles; i++) createParticle();
- }
- void Emiter::draw(SDL_Renderer *renderer)
- {
- for (const auto &p: particle)
- {
- p->draw(renderer);
- }
- }
- void Emiter::animate(float dt)
- {
- if ((particle.empty()) || (maxTime != -1 && time > maxTime))
- {
- visible = false;
- return;
- }
- for (unsigned int i = 0; i < particle.size(); i++)
- {
- particle[i]->animate(dt);
- if (!particle[i]->isAlive())
- particle.erase(particle.begin() + i);
- }
- createParticles(dt);
- modifyParticles(dt);
- while (particle.size() >= static_cast<unsigned int>(maxParticles)) particle.pop_back();
- time += dt;
- }
- void Emiter::createParticles(float dt)
- {
- if (repeat != -1)
- {
- if (r >= repeat)
- return;
- if (t >= frequency)
- {
- for (int i = 0; i < numParticles; i++) createParticle();
- t = 0.0f;
- r += 1;
- }
- t += dt;
- }
- else createParticle();
- }
- void Emiter::createParticle()
- {
- if (is_dir)
- v = Vector2D(speed*cos(dir) + random.number(-velocityDispersion, velocityDispersion)*sin(dir),
- speed*sin(dir) + random.number(-velocityDispersion, velocityDispersion)*cos(dir))
- + drift;
- else
- v = Vector2D(random.number(-velocityDispersion, velocityDispersion),
- random.number(-velocityDispersion, velocityDispersion))
- + drift;
- particle.push_back(make_unique<Particle>(Vector2D(startPoint.getX() + random.number(-w, w),
- startPoint.getY() + random.number(-h, h)),
- v, random.number(particleMinSize, particleMaxSize),
- particleTime, particleColor));
- }
- Fire::Fire(Vector2D _startPoint, float _direction)
- : Emiter(_startPoint, -1, 0, 2, 5, 5, 400, 1, 3, 0.4f, 255, 0, 0, 200, 50, -1, 0.01f, Vector2D(0.0f, 0.0f), true,
- 270.0f, 600.0f)
- {
- }
- Fire::~Fire()
- {
- }
- void Fire::modifyParticles(float dt)
- {
- SDL_Color color; color.r = 25; color.g = 25; color.b = 25; color.a = 100;
- for (unsigned int i = 0; i < particle.size(); i++)
- {
- if ((particle[i]->getPosition() - startPoint).length() > 100.0f)
- {
- particle[i]->setState(1);
- particle[i]->setColor(color);
- }
- }
- }
- Explosion::Explosion(Vector2D _startPoint, float _direction)
- : Emiter(_startPoint, 100, 10, 10, 100, 5, 400, 1, 3, 0.5f, 255, 0, 0, 155, 200, 10, 0.01f, Vector2D(500.0f, 0.0f),
- false, 0.0f, 0.0f)
- {
- }
- Explosion::~Explosion()
- {
- }
- void Explosion::modifyParticles(float dt)
- {
- SDL_Color color; color.r = 25; color.g = 25; color.b = 25; color.a = 100;
- for (unsigned int i = 0; i < particle.size(); i++)
- {
- if ((particle[i]->getCurrentTime() >= (particle[i]->getLifeTime()*0.5f)) && (particle[i]->getState() == 0))
- {
- particle[i]->setState(1);
- particle[i]->setColor(color);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement