2021-03-22 23:56:29 +00:00
|
|
|
//
|
|
|
|
// Created by SeanOMik on 3/21/2021.
|
|
|
|
// Github: https://github.com/SeanOMik
|
|
|
|
// Email: seanomik@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef SIMPLEENGINE_PARTICLE_H
|
|
|
|
#define SIMPLEENGINE_PARTICLE_H
|
|
|
|
|
|
|
|
#include "../event.h"
|
|
|
|
#include "particle_property.h"
|
|
|
|
|
|
|
|
#include <SFML/Graphics/Rect.hpp>
|
|
|
|
#include <SFML/Graphics/Sprite.hpp>
|
|
|
|
#include <SFML/System/Time.hpp>
|
|
|
|
#include <SFML/System/Vector2.hpp>
|
|
|
|
|
2021-03-30 00:52:27 +00:00
|
|
|
#include <chrono>
|
2021-03-22 23:56:29 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace simpleengine {
|
|
|
|
namespace particle {
|
|
|
|
class Particle : public simpleengine::Event {
|
|
|
|
private:
|
2021-03-23 00:54:32 +00:00
|
|
|
using Super = simpleengine::Event;
|
2021-03-22 23:56:29 +00:00
|
|
|
protected:
|
2021-03-30 00:52:27 +00:00
|
|
|
using ParticlePropertyPtr = std::unique_ptr<simpleengine::particle::ParticleProperty>;
|
2021-03-22 23:56:29 +00:00
|
|
|
using ParticlePropertyVector = std::vector<ParticlePropertyPtr>;
|
|
|
|
public:
|
2021-03-30 00:52:27 +00:00
|
|
|
friend class ParticleEmitter;
|
|
|
|
|
|
|
|
template<class DurRep, class DurPeriod>
|
|
|
|
Particle(sf::Texture& texture, sf::Vector2f velocity, std::chrono::duration<DurRep, DurPeriod> lifetime, float rotation_velocity,
|
|
|
|
ParticlePropertyVector properties)
|
|
|
|
: Super(nullptr), velocity(velocity), rotation_velocity(rotation_velocity), properties(std::move(properties)) {
|
|
|
|
sprite.setTexture(texture);
|
|
|
|
|
|
|
|
birth_point = std::chrono::high_resolution_clock::now();
|
|
|
|
death_point = birth_point + lifetime;
|
|
|
|
|
|
|
|
// Trigger OnParticleSpawn for particle properities.
|
|
|
|
for (ParticlePropertyPtr& property : this->properties) {
|
|
|
|
property->OnParticleSpawn(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Particle(const Particle& other);
|
|
|
|
Particle& operator=(Particle other);
|
2021-03-22 23:56:29 +00:00
|
|
|
|
2021-03-23 00:54:32 +00:00
|
|
|
virtual void Update(const float& delta_time) override;
|
|
|
|
virtual void Render(sf::RenderTarget* target) override;
|
2021-03-22 23:56:29 +00:00
|
|
|
|
2021-03-30 00:52:27 +00:00
|
|
|
template<class DurRep, class DurPeriod>
|
|
|
|
void SetLifetime(std::chrono::duration<DurRep, DurPeriod> lifetime) {
|
|
|
|
death_point = std::chrono::high_resolution_clock::now() + lifetime;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::microseconds GetAge() const;
|
|
|
|
std::chrono::duration<long long, std::ratio<1, 1000000000>> GetLifetime() const;
|
2021-03-22 23:56:29 +00:00
|
|
|
|
|
|
|
sf::Sprite sprite;
|
|
|
|
sf::Vector2f velocity;
|
|
|
|
float rotation_velocity;
|
|
|
|
ParticlePropertyVector properties;
|
|
|
|
private:
|
2021-03-30 00:52:27 +00:00
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> birth_point;
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> death_point;
|
2021-03-22 23:56:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //SIMPLEENGINE_PARTICLE_H
|