Change the particle example to move the particle emitter to the mouse

This commit is contained in:
SeanOMik 2021-03-29 22:10:54 -05:00
parent 2f1922952c
commit 32f83fa67b
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
1 changed files with 21 additions and 3 deletions

View File

@ -13,6 +13,7 @@
#include <SFML/System/Vector3.hpp>
#include <memory>
#include <ratio>
#include <simpleengine/game.h>
#include <simpleengine/event.h>
#include <simpleengine/entity.h>
@ -46,14 +47,16 @@ private:
Super::ParticlePropertyVector particle_properties;
Super::EmitterPropertyVector emitter_properties;
public:
ParticleEmitter() : Super(texture, sf::Vector2f(350, 350), .5f, 10, 900, se::Range2f(-50, 50, -50, 50), se::particle::ParticleAttributes{2000, sf::Vector2f(.1f, .1f), 5, sf::Vector2f(2, 2)}, particle_properties, emitter_properties) {
ParticleEmitter() : Super(texture, sf::Vector2f(350, 350), .5f, 5, 900, se::Range2f(-25, 25, -25, 25),
se::particle::ParticleAttributes{std::chrono::milliseconds(500), sf::Vector2f(.1f, .1f), 5,
sf::Vector2f(2, 2)}, particle_properties, emitter_properties) {
texture.loadFromFile("particle.png"); // The particle I tested with was 5x5 pixels
texture.setSmooth(true);
particle_properties.emplace_back(std::make_unique<se::particle::RandomVelocityParticleProperty>(se::Range2f(-1.5f, 1.5f, -1.5f, 1.5f)));
using namespace std::chrono_literals;
particle_properties.emplace_back(std::make_unique<se::particle::RandomLifetimeParticleProperty>(2'500ms, 5'500ms));
particle_properties.emplace_back(std::make_unique<se::particle::RandomLifetimeParticleProperty>(1'500ms, 2'500ms));
}
void Update(const float& delta_time) override {
@ -63,11 +66,26 @@ public:
void Render(sf::RenderTarget* target) override {
Super::Render(target);
}
void SetPosition(int x, int y) {
position.x = x;
position.y = y;
}
};
void OnMouseMove(ParticleEmitter* emitter, sf::Event::MouseMoveEvent event) {
emitter->SetPosition(event.x, event.y);
}
int main(int argc, char *argv[]) {
simpleengine::Game game(700, 700, "SimpleEngine - Particles Example");
game.AddEvent(new ParticleEmitter());
ParticleEmitter* emitter = new ParticleEmitter();
game.AddEventCallback<sf::Event::EventType::MouseMoved>(std::function([&](sf::Event::MouseMoveEvent event) {
OnMouseMove(emitter, event);
}));
game.AddEvent(emitter);
return game.Run();
}