implement entity-bound emitters

This commit is contained in:
MihailRis 2024-11-03 13:05:42 +03:00
parent 6d63c71221
commit 83ddbda90b
3 changed files with 21 additions and 7 deletions

View File

@ -6,15 +6,19 @@
#include "window/Camera.hpp"
#include "graphics/core/Texture.hpp"
#include "objects/Entities.hpp"
#include "world/Level.hpp"
Emitter::Emitter(
const Level& level,
std::variant<glm::vec3, entityid_t> origin,
Particle prototype,
const Texture* texture,
float spawnInterval,
int count
)
: origin(std::move(origin)),
: level(level),
origin(std::move(origin)),
prototype(std::move(prototype)),
texture(texture),
spawnInterval(spawnInterval),
@ -38,8 +42,10 @@ void Emitter::update(
glm::vec3 position {};
if (auto staticPos = std::get_if<glm::vec3>(&origin)) {
position = *staticPos;
} else {
// TODO: implement for entity origin
} else if (auto entityId = std::get_if<entityid_t>(&origin)) {
if (auto entity = level.entities->get(*entityId)) {
position = entity->getTransform().pos;
}
}
if (glm::distance2(position, cameraPosition) > maxDistance * maxDistance) {
if (count > 0) {

View File

@ -9,6 +9,7 @@
#include "maths/UVRegion.hpp"
#include "maths/util.hpp"
class Level;
class Emitter;
struct Particle {
@ -36,6 +37,7 @@ struct ParticleBehaviour {
};
class Emitter {
const Level& level;
/// @brief Static position or entity
std::variant<glm::vec3, entityid_t> origin;
/// @brief Particle prototype
@ -60,6 +62,7 @@ public:
ParticleBehaviour behaviour;
Emitter(
const Level& level,
std::variant<glm::vec3, entityid_t> origin,
Particle prototype,
const Texture* texture,

View File

@ -19,10 +19,15 @@ ParticlesRenderer::ParticlesRenderer(
: batch(std::make_unique<MainBatch>(1024)),
level(level),
settings(settings) {
auto region = util::get_texture_region(assets, "blocks:grass_top", "");
emitters.push_back(std::make_unique<Emitter>(glm::vec3(0, 80, 0), Particle {
nullptr, 0, glm::vec3(), glm::vec3(), 5.0f, region.region
}, region.texture, 0.002f, -1));
auto region = util::get_texture_region(assets, "blocks:grass_side", "");
emitters.push_back(std::make_unique<Emitter>(
level,
glm::vec3(0, 80, 0),
Particle {nullptr, 0, glm::vec3(), glm::vec3(), 5.0f, region.region},
region.texture,
0.002f,
-1
));
}
ParticlesRenderer::~ParticlesRenderer() = default;