feat: make Decorator listen to block interactions

This commit is contained in:
MihailRis 2024-11-05 03:46:41 +03:00
parent f5bc6fc136
commit 77ae35e364
5 changed files with 36 additions and 21 deletions

View File

@ -25,7 +25,7 @@ LevelFrontend::LevelFrontend(
"block-previews" "block-previews"
); );
controller->getBlocksController()->listenBlockInteraction( controller->getBlocksController()->listenBlockInteraction(
[=](Player* player, glm::ivec3 pos, const Block& def, BlockInteraction type) { [=](auto player, const auto& pos, const auto& def, BlockInteraction type) {
auto material = level->content->findBlockMaterial(def.material); auto material = level->content->findBlockMaterial(def.material);
if (material == nullptr) { if (material == nullptr) {
return; return;

View File

@ -46,8 +46,9 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get(), controller->getPlayer()); worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get(), controller->getPlayer());
hud = std::make_unique<Hud>(engine, frontend.get(), controller->getPlayer()); hud = std::make_unique<Hud>(engine, frontend.get(), controller->getPlayer());
decorator = decorator = std::make_unique<Decorator>(
std::make_unique<Decorator>(*level, *worldRenderer->particles, *assets); *controller, *worldRenderer->particles, *assets
);
keepAlive(settings.graphics.backlight.observe([=](bool) { keepAlive(settings.graphics.backlight.observe([=](bool) {
controller->getLevel()->chunks->saveAndClear(); controller->getLevel()->chunks->saveAndClear();

View File

@ -7,6 +7,7 @@
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
#include "world/Level.hpp" #include "world/Level.hpp"
#include "window/Camera.hpp" #include "window/Camera.hpp"
#include "logic/LevelController.hpp"
/// @brief Not greather than 64 for this BIG_PRIME value /// @brief Not greather than 64 for this BIG_PRIME value
inline constexpr int UPDATE_AREA_DIAMETER = 32; inline constexpr int UPDATE_AREA_DIAMETER = 32;
@ -19,9 +20,32 @@ inline constexpr int ITERATIONS = 512;
inline constexpr int BIG_PRIME = 666667; inline constexpr int BIG_PRIME = 666667;
Decorator::Decorator( Decorator::Decorator(
const Level& level, ParticlesRenderer& particles, const Assets& assets LevelController& controller, ParticlesRenderer& particles, const Assets& assets
) )
: level(level), particles(particles), assets(assets) { : level(*controller.getLevel()), particles(particles), assets(assets) {
controller.getBlocksController()->listenBlockInteraction(
[this](auto player, const auto& pos, const auto& def, BlockInteraction type) {
if (type == BlockInteraction::placing && def.particles) {
addParticles(def, pos);
}
});
}
void Decorator::addParticles(const Block& def, const glm::ivec3& pos) {
const auto& found = blockEmitters.find(pos);
if (found == blockEmitters.end()) {
auto treg = util::get_texture_region(
assets, def.particles->texture, ""
);
blockEmitters[pos] = particles.add(std::make_unique<Emitter>(
level,
glm::vec3{pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
*def.particles,
treg.texture,
treg.region,
-1
));
}
} }
void Decorator::update( void Decorator::update(
@ -42,20 +66,7 @@ void Decorator::update(
if (auto vox = chunks.get(pos)) { if (auto vox = chunks.get(pos)) {
const auto& def = indices.blocks.require(vox->id); const auto& def = indices.blocks.require(vox->id);
if (def.particles) { if (def.particles) {
const auto& found = blockEmitters.find(pos); addParticles(def, pos);
if (found == blockEmitters.end()) {
auto treg = util::get_texture_region(
assets, def.particles->texture, ""
);
blockEmitters[pos] = particles.add(std::make_unique<Emitter>(
level,
glm::vec3{pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
*def.particles,
treg.texture,
treg.region,
-1
));
}
} }
} }
} }

View File

@ -10,6 +10,8 @@ class Level;
class Chunks; class Chunks;
class Camera; class Camera;
class Assets; class Assets;
struct Block;
class LevelController;
class ParticlesRenderer; class ParticlesRenderer;
class Decorator { class Decorator {
@ -22,9 +24,10 @@ class Decorator {
void update( void update(
float delta, const glm::ivec3& areaStart, const glm::ivec3& areaCenter float delta, const glm::ivec3& areaStart, const glm::ivec3& areaCenter
); );
void addParticles(const Block& def, const glm::ivec3& pos);
public: public:
Decorator( Decorator(
const Level& level, ParticlesRenderer& particles, const Assets& assets LevelController& level, ParticlesRenderer& particles, const Assets& assets
); );
void update(float delta, const Camera& camera); void update(float delta, const Camera& camera);

View File

@ -20,7 +20,7 @@ enum class BlockInteraction { step, destruction, placing };
/// @brief Player argument is nullable /// @brief Player argument is nullable
using on_block_interaction = std::function< using on_block_interaction = std::function<
void(Player*, glm::ivec3, const Block&, BlockInteraction type)>; void(Player*, const glm::ivec3&, const Block&, BlockInteraction type)>;
/// BlocksController manages block updates and data (inventories, metadata) /// BlocksController manages block updates and data (inventories, metadata)
class BlocksController { class BlocksController {