I want to enable particles on a fireball, but the fireball's position is dynamically changing via a Vector2f position, to which the Sprite position is adjusted. Basically I've got a Vector2f fireball.position and then I'm using sprFireball.setPosition(fireball.position). It's for convenience only.
But to the point. I've got a ParticleSystem and most basic Emitter properties declared outside the game loop, but as the fireball's position is changing in the game loop, I need to somehow move the Emitter's particle position to that of the fireball. I've looked up Bromeon's tutorial on Particles and Connections, but that didn't help in my case. I was toying around with it for the past 2 hours or so and can't find a solution.
I'll try to minimize the code and put it below
thor::ParticleSystem fireballParticleSystem;
//adding textures
thor::UniversalEmitter fireballEmitter;
//setting properties
fireballParticleSystem.addEmitter(fireballEmitter);
// adding the emitter for the first time
sf::Clock fireballFrameTime;
// for emitter updates
// window and isOpen,
{ // game loop
// setting the position of the fireball, its rotation, moving it and using the fireball.position to store the
position
}
thor::UniversalEmitter has several attributes, each of which can be set to a Distribution. You can read the docs to understand exactly what this is in Thor's context.
In your case, thor::UniversalEmitter::setParticlePosition() (http://www.bromeon.ch/libraries/thor/documentation/v2.1/classthor_1_1_universal_emitter.html#a3550a5b17adedfbb320dec10b3d05226) should do the job. It can be assigned either a constant, or a function that dynamically fetches the position.
thor::Distribution<sf::Vector2f> positionQuery = [...] () -> sf::Vector2f
{
// compute position of your fireball here
return fireball.GetPosition();
};
thor::UniversalEmitter emitter;
emitter.setParticlePosition(positionQuery);
It may be necessary to construct an intermediate std::function<sf::Vector2f()>, I'm not 100% sure the type inference of the thor::Distribution constructor catches this. As far as I remember, the SFINAE black magic is quite advanced there ;)