add Emitter maxDistance

This commit is contained in:
MihailRis 2024-11-03 12:45:46 +03:00
parent 20a305fb02
commit 6d63c71221
3 changed files with 34 additions and 7 deletions

View File

@ -1,7 +1,10 @@
#include "Emitter.hpp"
#include <glm/gtc/random.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/norm.hpp>
#include "window/Camera.hpp"
#include "graphics/core/Texture.hpp"
Emitter::Emitter(
@ -24,8 +27,12 @@ const Texture* Emitter::getTexture() const {
return texture;
}
void Emitter::update(float delta, std::vector<Particle>& particles) {
if (count == 0) {
void Emitter::update(
float delta,
const glm::vec3& cameraPosition,
std::vector<Particle>& particles
) {
if (count == 0 || (count == -1 && spawnInterval < FLT_EPSILON)) {
return;
}
glm::vec3 position {};
@ -34,6 +41,18 @@ void Emitter::update(float delta, std::vector<Particle>& particles) {
} else {
// TODO: implement for entity origin
}
if (glm::distance2(position, cameraPosition) > maxDistance * maxDistance) {
if (count > 0) {
if (spawnInterval < FLT_EPSILON) {
count = 0;
return;
}
int skipped = timer / spawnInterval;
count = std::max(0, count - skipped);
timer -= skipped * spawnInterval;
}
return;
}
timer += delta;
while (count && timer > spawnInterval) {
// spawn particle

View File

@ -15,6 +15,7 @@ struct Particle {
/// @brief Pointer used to access common behaviour.
/// Emitter must be utilized after all related particles despawn.
Emitter* emitter;
/// @brief Some random integer for visuals configuration.
int random;
/// @brief Global position
glm::vec3 position;
@ -44,15 +45,17 @@ class Emitter {
/// @brief Particles spawn interval
float spawnInterval;
/// @brief Number of particles should be spawned before emitter deactivation.
/// -1 is infinite
/// -1 is infinite.
int count;
/// @brief Spawn timer used to determine number of particles
/// to spawn on update. May be innacurate.
float timer = 0.0f;
/// @brief Random velocity magnitude applying to spawned particles
/// @brief Random velocity magnitude applying to spawned particles.
glm::vec3 explosion {8.0f};
/// @brief Max distance of actually spawning particles.
float maxDistance = 32.0f;
util::PseudoRandom random;
util::PseudoRandom random;
public:
ParticleBehaviour behaviour;
@ -71,8 +74,13 @@ public:
/// @brief Update emitter and spawn particles
/// @param delta delta time
/// @param cameraPosition current camera global position
/// @param particles destination particles vector
void update(float delta, std::vector<Particle>& particles);
void update(
float delta,
const glm::vec3& cameraPosition,
std::vector<Particle>& particles
);
/// @brief Set initial random velocity magitude
void setExplosion(const glm::vec3& magnitude);

View File

@ -119,7 +119,7 @@ void ParticlesRenderer::render(const Camera& camera, float delta) {
} else {
vec = &found->second;
}
emitter.update(delta, *vec);
emitter.update(delta, camera.position, *vec);
iter++;
}
}