Move some code into source files, create alternative constructors
The constructors were for the particle properties
This commit is contained in:
parent
d5eeeb4553
commit
c949d44d98
|
@ -29,27 +29,27 @@ namespace se = simpleengine;
|
||||||
|
|
||||||
class ParticleEmitter : public se::particle::ParticleEmitter {
|
class ParticleEmitter : public se::particle::ParticleEmitter {
|
||||||
private:
|
private:
|
||||||
using super = se::particle::ParticleEmitter;
|
using Super = se::particle::ParticleEmitter;
|
||||||
|
|
||||||
sf::Sprite sprite;
|
sf::Sprite sprite;
|
||||||
sf::Texture texture;
|
sf::Texture texture;
|
||||||
|
|
||||||
super::ParticlePropertyVector properties = {
|
Super::ParticlePropertyVector properties = {
|
||||||
std::make_shared<se::particle::RandomVelocityParticleProperty>(se::Range2f(-1.5f, 1.5f, -1.5f, 1.5f)),
|
std::make_shared<se::particle::RandomVelocityParticleProperty>(se::Range2f(-1.5f, 1.5f, -1.5f, 1.5f)),
|
||||||
std::make_shared<se::particle::RandomLifetimeParticleProperty>(se::Rangef(1'500, 9'500))
|
std::make_shared<se::particle::RandomLifetimeParticleProperty>(se::Rangef(1'500, 9'500))
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
ParticleEmitter() : super(texture, sf::Vector2f(350, 350), 0.4, 5, 1500, se::Range2f(-15, 15, -15, -15), se::particle::ParticleAttributes{0, sf::Vector2f(), 5, sf::Vector2f(2, 2)}, properties) {
|
ParticleEmitter() : Super(texture, sf::Vector2f(350, 350), 0.7, 5, 1500, se::Range2f(-50, 50, -50, 50), se::particle::ParticleAttributes{0, sf::Vector2f(), 5, sf::Vector2f(2, 2)}, properties) {
|
||||||
texture.loadFromFile("particle.png"); // The particle I tested with was 5x5 pixels
|
texture.loadFromFile("particle.png"); // The particle I tested with was 5x5 pixels
|
||||||
texture.setSmooth(true);
|
texture.setSmooth(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update(const float& delta_time) override {
|
void Update(const float& delta_time) override {
|
||||||
super::Update(delta_time);
|
Super::Update(delta_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Render(sf::RenderTarget* target) override {
|
void Render(sf::RenderTarget* target) override {
|
||||||
super::Render(target);
|
Super::Render(target);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -28,10 +28,6 @@ namespace simpleengine {
|
||||||
sf::Vector2f start_velocity;
|
sf::Vector2f start_velocity;
|
||||||
float rotation_velocity = 0;
|
float rotation_velocity = 0;
|
||||||
sf::Vector2f scale = sf::Vector2f(1, 1);
|
sf::Vector2f scale = sf::Vector2f(1, 1);
|
||||||
|
|
||||||
/* ParticleAttributes(uint32_t lifetime_ms, sf::Vector2f start_velocity, float rotation_velocity) : lifetime_ms(lifetime_ms), start_velocity(start_velocity), rotation_velocity(rotation_velocity) {
|
|
||||||
|
|
||||||
} */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ParticleEmitter : public simpleengine::Event {
|
class ParticleEmitter : public simpleengine::Event {
|
||||||
|
@ -39,51 +35,12 @@ namespace simpleengine {
|
||||||
using ParticlePropertyPtr = std::shared_ptr<simpleengine::particle::ParticleProperty>;
|
using ParticlePropertyPtr = std::shared_ptr<simpleengine::particle::ParticleProperty>;
|
||||||
using ParticlePropertyVector = std::vector<ParticlePropertyPtr>;
|
using ParticlePropertyVector = std::vector<ParticlePropertyPtr>;
|
||||||
public:
|
public:
|
||||||
explicit ParticleEmitter(sf::Texture& texture, sf::Vector2f position, double emit_variance, uint32_t emit_count, uint32_t particle_count, Range2f particle_range, ParticleAttributes attributes, ParticlePropertyVector& properties)
|
ParticleEmitter(sf::Texture& texture, sf::Vector2f position, double emit_variance, uint32_t emit_count, uint32_t particle_count, Range2f particle_range, ParticleAttributes attributes, ParticlePropertyVector& properties);
|
||||||
: texture(texture), position(position), emit_variance(emit_variance), emit_count(emit_count), particle_count(particle_count), particle_range(particle_range), attributes(attributes), properties(properties) {
|
|
||||||
time.restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetProperties(ParticlePropertyVector& properties) {
|
void SetProperties(ParticlePropertyVector& properties);
|
||||||
this->properties = properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void Update(const float& delta_time) override {
|
virtual void Update(const float& delta_time) override;
|
||||||
for (std::vector<simpleengine::particle::Particle>::iterator it = particles.begin(); it != particles.end(); ) {
|
virtual void Render(sf::RenderTarget* target) override;
|
||||||
it->Update(delta_time);
|
|
||||||
|
|
||||||
if (it->IsDestroying()) {
|
|
||||||
it = particles.erase(it);
|
|
||||||
} else {
|
|
||||||
it++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (particles.size() < particle_count) {
|
|
||||||
if (random.NextInRange(0, 1) < emit_variance) {
|
|
||||||
for (uint32_t i = 0; i < emit_count; i++) {
|
|
||||||
Particle particle(texture, attributes.start_velocity, attributes.lifetime_ms, attributes.rotation_velocity, properties);
|
|
||||||
|
|
||||||
// Offset the position of the particle randomly
|
|
||||||
sf::Vector2f new_pos;
|
|
||||||
new_pos.x = random.NextInRange(particle_range.min_x, particle_range.max_x);
|
|
||||||
new_pos.y = random.NextInRange(particle_range.min_y, particle_range.max_y);
|
|
||||||
new_pos += position;
|
|
||||||
particle.GetSprite().setPosition(new_pos);
|
|
||||||
|
|
||||||
particle.GetSprite().setScale(attributes.scale.x, attributes.scale.y);
|
|
||||||
|
|
||||||
particles.emplace_back(std::move(particle));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void Render(sf::RenderTarget* target) override {
|
|
||||||
for (simpleengine::particle::Particle particle : particles) {
|
|
||||||
particle.Render(target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
protected:
|
protected:
|
||||||
sf::Texture& texture;
|
sf::Texture& texture;
|
||||||
sf::Vector2f position;
|
sf::Vector2f position;
|
||||||
|
|
|
@ -21,63 +21,18 @@ namespace simpleengine {
|
||||||
namespace particle {
|
namespace particle {
|
||||||
class Particle : public simpleengine::Event {
|
class Particle : public simpleengine::Event {
|
||||||
private:
|
private:
|
||||||
using super = simpleengine::Event;
|
using Super = simpleengine::Event;
|
||||||
protected:
|
protected:
|
||||||
using ParticlePropertyPtr = std::shared_ptr<simpleengine::particle::ParticleProperty>;
|
using ParticlePropertyPtr = std::shared_ptr<simpleengine::particle::ParticleProperty>;
|
||||||
using ParticlePropertyVector = std::vector<ParticlePropertyPtr>;
|
using ParticlePropertyVector = std::vector<ParticlePropertyPtr>;
|
||||||
public:
|
public:
|
||||||
Particle(sf::Texture& texture, sf::Vector2f velocity, uint32_t lifetime_ms, float rotation_velocity,
|
Particle(sf::Texture& texture, sf::Vector2f velocity, uint32_t lifetime_ms, float rotation_velocity, ParticlePropertyVector properties);
|
||||||
ParticlePropertyVector properties) : simpleengine::Event(nullptr), velocity(velocity), lifetime_ms(lifetime_ms),
|
|
||||||
rotation_velocity(rotation_velocity), properties(properties) {
|
|
||||||
age_clock.restart();
|
|
||||||
sprite.setTexture(texture);
|
|
||||||
|
|
||||||
for (ParticlePropertyPtr property : properties) {
|
virtual void Update(const float& delta_time) override;
|
||||||
property->OnParticleSpawn(*this);
|
virtual void Render(sf::RenderTarget* target) override;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void Update(const float& delta_time) override {
|
sf::Sprite& GetSprite();
|
||||||
if (age_clock.getElapsedTime().asMilliseconds() >= lifetime_ms) {
|
const sf::Time GetAge() const;
|
||||||
super::Destroy();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ParticlePropertyPtr property : properties) {
|
|
||||||
property->Update(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
sprite.move(velocity.x, velocity.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void Render(sf::RenderTarget* target) override {
|
|
||||||
target->draw(sprite);
|
|
||||||
}
|
|
||||||
|
|
||||||
sf::Sprite& GetSprite() {
|
|
||||||
return sprite;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* void SetVelocity(const sf::Vector2f& velocity) {
|
|
||||||
this->velocity = velocity;
|
|
||||||
}
|
|
||||||
|
|
||||||
const sf::Vector2f& GetVelocity() {
|
|
||||||
return velocity;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetRotationVelocity(const float& rotation_velocity) {
|
|
||||||
this->rotation_velocity = rotation_velocity;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float& GetRotationVelocity() {
|
|
||||||
return rotation_velocity;
|
|
||||||
}
|
|
||||||
private: */
|
|
||||||
|
|
||||||
const sf::Time GetAge() const {
|
|
||||||
return age_clock.getElapsedTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
sf::Sprite sprite;
|
sf::Sprite sprite;
|
||||||
sf::Vector2f velocity;
|
sf::Vector2f velocity;
|
||||||
|
|
|
@ -20,6 +20,10 @@ namespace simpleengine {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RandomLifetimeParticleProperty(float min, float max) : RandomLifetimeParticleProperty(Rangef(min, max)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void OnParticleSpawn(simpleengine::particle::Particle& particle) override {
|
void OnParticleSpawn(simpleengine::particle::Particle& particle) override {
|
||||||
simpleengine::Random<float> rand;
|
simpleengine::Random<float> rand;
|
||||||
particle.lifetime_ms = rand.NextInRange(range.min, range.max);
|
particle.lifetime_ms = rand.NextInRange(range.min, range.max);
|
||||||
|
|
|
@ -20,6 +20,11 @@ namespace simpleengine {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RandomVelocityParticleProperty(float min_x, float max_x, float min_y, float max_y)
|
||||||
|
: RandomVelocityParticleProperty(Range2f(min_x, max_x, min_y, max_y)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void OnParticleSpawn(simpleengine::particle::Particle& particle) override {
|
void OnParticleSpawn(simpleengine::particle::Particle& particle) override {
|
||||||
simpleengine::Random<float> rand;
|
simpleengine::Random<float> rand;
|
||||||
sf::Vector2f velocity;
|
sf::Vector2f velocity;
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
#include "particle/emitter.h"
|
||||||
|
|
||||||
|
simpleengine::particle::ParticleEmitter::ParticleEmitter(sf::Texture& texture, sf::Vector2f position, double emit_variance, uint32_t emit_count, uint32_t particle_count, Range2f particle_range, ParticleAttributes attributes, ParticlePropertyVector& properties) : texture(texture), position(position), emit_variance(emit_variance), emit_count(emit_count), particle_count(particle_count), particle_range(particle_range), attributes(attributes), properties(properties) {
|
||||||
|
time.restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::particle::ParticleEmitter::SetProperties(ParticlePropertyVector& properties) {
|
||||||
|
this->properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::particle::ParticleEmitter::Update(const float &delta_time) {
|
||||||
|
// Update particles and check if they are being destroyed. If they are, destroy them.
|
||||||
|
for (std::vector<simpleengine::particle::Particle>::iterator it = particles.begin(); it != particles.end(); ) {
|
||||||
|
it->Update(delta_time);
|
||||||
|
|
||||||
|
if (it->IsDestroying()) {
|
||||||
|
it = particles.erase(it);
|
||||||
|
} else {
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we can spawn particles with max particle count, also use chance to check if we can.
|
||||||
|
if (particles.size() < particle_count && random.NextInRange(0, 1) < emit_variance) {
|
||||||
|
// Make sure that we don't emit too many particles to go over the max particle count
|
||||||
|
uint32_t emitting_ct = (particles.size() + emit_count > particle_count) ? particle_count - particles.size() : emit_count;
|
||||||
|
for (uint32_t i = 0; i < emitting_ct; i++) {
|
||||||
|
Particle particle(texture, attributes.start_velocity, attributes.lifetime_ms, attributes.rotation_velocity, properties);
|
||||||
|
|
||||||
|
// Offset the position of the particle randomly
|
||||||
|
sf::Vector2f new_pos;
|
||||||
|
new_pos.x = random.NextInRange(particle_range.min_x, particle_range.max_x);
|
||||||
|
new_pos.y = random.NextInRange(particle_range.min_y, particle_range.max_y);
|
||||||
|
new_pos += position;
|
||||||
|
particle.GetSprite().setPosition(new_pos);
|
||||||
|
|
||||||
|
particle.GetSprite().setScale(attributes.scale.x, attributes.scale.y);
|
||||||
|
|
||||||
|
particles.emplace_back(std::move(particle));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::particle::ParticleEmitter::Render(sf::RenderTarget *target) {
|
||||||
|
for (simpleengine::particle::Particle particle : particles) {
|
||||||
|
particle.Render(target);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
#include "particle/particle.h"
|
||||||
|
|
||||||
|
simpleengine::particle::Particle::Particle(sf::Texture& texture, sf::Vector2f velocity, uint32_t lifetime_ms, float rotation_velocity,
|
||||||
|
ParticlePropertyVector properties) : Super(nullptr), velocity(velocity), lifetime_ms(lifetime_ms),
|
||||||
|
rotation_velocity(rotation_velocity), properties(properties) {
|
||||||
|
age_clock.restart(); // Start age clock
|
||||||
|
sprite.setTexture(texture);
|
||||||
|
|
||||||
|
// Trigger OnParticleSpawn for particle properities.
|
||||||
|
for (ParticlePropertyPtr property : properties) {
|
||||||
|
property->OnParticleSpawn(*this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::particle::Particle::Update(const float& delta_time) {
|
||||||
|
// If the particle is older than its lifetime, destroy it.
|
||||||
|
if (age_clock.getElapsedTime().asMilliseconds() >= lifetime_ms) {
|
||||||
|
Super::Destroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update all properties.
|
||||||
|
for (ParticlePropertyPtr property : properties) {
|
||||||
|
property->Update(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
sprite.move(velocity.x, velocity.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::particle::Particle::Render(sf::RenderTarget* target) {
|
||||||
|
target->draw(sprite);
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Sprite& simpleengine::particle::Particle::GetSprite() {
|
||||||
|
return sprite;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sf::Time simpleengine::particle::Particle::GetAge() const {
|
||||||
|
return age_clock.getElapsedTime();
|
||||||
|
}
|
Loading…
Reference in New Issue