add Emitter maxDistance
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
#include "Emitter.hpp"
|
#include "Emitter.hpp"
|
||||||
|
|
||||||
#include <glm/gtc/random.hpp>
|
#include <glm/gtc/random.hpp>
|
||||||
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
|
#include <glm/gtx/norm.hpp>
|
||||||
|
|
||||||
|
#include "window/Camera.hpp"
|
||||||
#include "graphics/core/Texture.hpp"
|
#include "graphics/core/Texture.hpp"
|
||||||
|
|
||||||
Emitter::Emitter(
|
Emitter::Emitter(
|
||||||
@@ -24,8 +27,12 @@ const Texture* Emitter::getTexture() const {
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Emitter::update(float delta, std::vector<Particle>& particles) {
|
void Emitter::update(
|
||||||
if (count == 0) {
|
float delta,
|
||||||
|
const glm::vec3& cameraPosition,
|
||||||
|
std::vector<Particle>& particles
|
||||||
|
) {
|
||||||
|
if (count == 0 || (count == -1 && spawnInterval < FLT_EPSILON)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
glm::vec3 position {};
|
glm::vec3 position {};
|
||||||
@@ -34,6 +41,18 @@ void Emitter::update(float delta, std::vector<Particle>& particles) {
|
|||||||
} else {
|
} else {
|
||||||
// TODO: implement for entity origin
|
// 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;
|
timer += delta;
|
||||||
while (count && timer > spawnInterval) {
|
while (count && timer > spawnInterval) {
|
||||||
// spawn particle
|
// spawn particle
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ struct Particle {
|
|||||||
/// @brief Pointer used to access common behaviour.
|
/// @brief Pointer used to access common behaviour.
|
||||||
/// Emitter must be utilized after all related particles despawn.
|
/// Emitter must be utilized after all related particles despawn.
|
||||||
Emitter* emitter;
|
Emitter* emitter;
|
||||||
|
/// @brief Some random integer for visuals configuration.
|
||||||
int random;
|
int random;
|
||||||
/// @brief Global position
|
/// @brief Global position
|
||||||
glm::vec3 position;
|
glm::vec3 position;
|
||||||
@@ -44,13 +45,15 @@ class Emitter {
|
|||||||
/// @brief Particles spawn interval
|
/// @brief Particles spawn interval
|
||||||
float spawnInterval;
|
float spawnInterval;
|
||||||
/// @brief Number of particles should be spawned before emitter deactivation.
|
/// @brief Number of particles should be spawned before emitter deactivation.
|
||||||
/// -1 is infinite
|
/// -1 is infinite.
|
||||||
int count;
|
int count;
|
||||||
/// @brief Spawn timer used to determine number of particles
|
/// @brief Spawn timer used to determine number of particles
|
||||||
/// to spawn on update. May be innacurate.
|
/// to spawn on update. May be innacurate.
|
||||||
float timer = 0.0f;
|
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};
|
glm::vec3 explosion {8.0f};
|
||||||
|
/// @brief Max distance of actually spawning particles.
|
||||||
|
float maxDistance = 32.0f;
|
||||||
|
|
||||||
util::PseudoRandom random;
|
util::PseudoRandom random;
|
||||||
public:
|
public:
|
||||||
@@ -71,8 +74,13 @@ public:
|
|||||||
|
|
||||||
/// @brief Update emitter and spawn particles
|
/// @brief Update emitter and spawn particles
|
||||||
/// @param delta delta time
|
/// @param delta delta time
|
||||||
|
/// @param cameraPosition current camera global position
|
||||||
/// @param particles destination particles vector
|
/// @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
|
/// @brief Set initial random velocity magitude
|
||||||
void setExplosion(const glm::vec3& magnitude);
|
void setExplosion(const glm::vec3& magnitude);
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ void ParticlesRenderer::render(const Camera& camera, float delta) {
|
|||||||
} else {
|
} else {
|
||||||
vec = &found->second;
|
vec = &found->second;
|
||||||
}
|
}
|
||||||
emitter.update(delta, *vec);
|
emitter.update(delta, camera.position, *vec);
|
||||||
iter++;
|
iter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user