add ParticlesPreset

This commit is contained in:
MihailRis 2024-11-03 14:25:29 +03:00
parent 83ddbda90b
commit 9728c674fc
5 changed files with 47 additions and 14 deletions

View File

@ -23,7 +23,7 @@ Emitter::Emitter(
texture(texture),
spawnInterval(spawnInterval),
count(count),
behaviour() {
preset() {
this->prototype.emitter = this;
}
@ -47,6 +47,7 @@ void Emitter::update(
position = entity->getTransform().pos;
}
}
const float maxDistance = preset.maxDistance;
if (glm::distance2(position, cameraPosition) > maxDistance * maxDistance) {
if (count > 0) {
if (spawnInterval < FLT_EPSILON) {

View File

@ -8,6 +8,7 @@
#include "maths/UVRegion.hpp"
#include "maths/util.hpp"
#include "presets/ParticlesPreset.hpp"
class Level;
class Emitter;
@ -30,12 +31,6 @@ struct Particle {
class Texture;
struct ParticleBehaviour {
bool collision = true;
bool lighting = true;
glm::vec3 gravity {0.0f, -16.0f, 0.0f};
};
class Emitter {
const Level& level;
/// @brief Static position or entity
@ -54,12 +49,10 @@ class Emitter {
float timer = 0.0f;
/// @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;
public:
ParticleBehaviour behaviour;
ParticlesPreset preset;
Emitter(
const Level& level,

View File

@ -35,12 +35,12 @@ ParticlesRenderer::~ParticlesRenderer() = default;
static inline void update_particle(
Particle& particle, float delta, const Chunks& chunks
) {
const auto& behave = particle.emitter->behaviour;
const auto& preset = particle.emitter->preset;
auto& pos = particle.position;
auto& vel = particle.velocity;
vel += delta * behave.gravity;
if (behave.collision && chunks.isObstacleAt(pos + vel * delta)) {
vel += delta * preset.acceleration;
if (preset.collision && chunks.isObstacleAt(pos + vel * delta)) {
vel *= 0.0f;
}
pos += vel * delta;
@ -72,7 +72,7 @@ void ParticlesRenderer::renderParticles(const Camera& camera, float delta) {
update_particle(particle, delta, chunks);
glm::vec4 light(1, 1, 1, 0);
if (particle.emitter->behaviour.lighting) {
if (particle.emitter->preset.lighting) {
light = MainBatch::sampleLight(
particle.position, chunks, backlight
);

View File

@ -0,0 +1,20 @@
#include "ParticlesPreset.hpp"
#include "data/dv_util.hpp"
dv::value ParticlesPreset::serialize() const {
auto root = dv::object();
root["collision"] = collision;
root["lighting"] = lighting;
root["max_distance"] = maxDistance;
root["acceleration"] = dv::to_value(acceleration);
return root;
}
void ParticlesPreset::deserialize(const dv::value& src) {
src.at("collision").get(collision);
src.at("lighting").get(lighting);
if (src.has("acceleration")) {
dv::get_vec(src["acceleration"], acceleration);
}
}

View File

@ -0,0 +1,19 @@
#pragma once
#include <glm/vec3.hpp>
#include "interfaces/Serializable.hpp"
struct ParticlesPreset : public Serializable {
/// @brief Collision detection
bool collision = true;
/// @brief Apply lighting
bool lighting = true;
/// @brief Max distance of actually spawning particles.
float maxDistance = 32.0f;
/// @brief Velocity acceleration
glm::vec3 acceleration {0.0f, -16.0f, 0.0f};
dv::value serialize() const override;
void deserialize(const dv::value& src) override;
};