From 830e05733f487514ff582a9cf7c8bebe18612326 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Feb 2025 02:39:31 +0300 Subject: [PATCH] minor refactor --- src/graphics/render/Decorator.cpp | 95 +++++++++++++-------------- src/graphics/render/Decorator.hpp | 2 + src/graphics/render/WorldRenderer.cpp | 11 ++++ src/presets/WeatherPreset.hpp | 6 ++ 4 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/graphics/render/Decorator.cpp b/src/graphics/render/Decorator.cpp index 4dde75d2..bd3d23d0 100644 --- a/src/graphics/render/Decorator.cpp +++ b/src/graphics/render/Decorator.cpp @@ -7,6 +7,7 @@ #include "assets/assets_util.hpp" #include "content/Content.hpp" #include "voxels/Chunks.hpp" +#include "voxels/Chunk.hpp" #include "voxels/Block.hpp" #include "world/Level.hpp" #include "window/Camera.hpp" @@ -91,64 +92,58 @@ void Decorator::updateRandom( const auto& chunks = *player.chunks; const auto& indices = *level.content.getIndices(); - - ParticlesPreset rainSplash; - rainSplash.frames = { - "particles:rain_splash_0", - "particles:rain_splash_1", - "particles:rain_splash_2" - }; - rainSplash.lifetime = 0.2f; - rainSplash.spawnInterval = 0.0f; - rainSplash.size = {0.2f, 0.2f, 0.2f}; + const auto& rainSplash = weather.fall.splash; auto pos = areaCenter + glm::ivec3( random.rand32() % 12, random.rand32() % 12, random.rand32() % 12 ); - if (auto vox = chunks.get(pos)) { - const auto& def = indices.blocks.require(vox->id); - auto dst2 = util::distance2(pos, areaCenter); - if (dst2 < 256 && def.obstacle && - !weather.fall.noise.empty()) { - bool is_covered = false; - for (int y = pos.y + 1; y < CHUNK_H; y++) { - if (indices.blocks.require(chunks.get(pos.x, y, pos.z)->id).obstacle) { - is_covered = true; - break; - } - } - if (!is_covered) { - if (dst2 < 128) { - auto treg = util::get_texture_region( - assets, "particles:rain_splash_0", "" - ); - renderer.particles->add(std::make_unique( - level, - glm::vec3{pos.x + random.randFloat(), pos.y + 1.1, pos.z + random.randFloat()}, - rainSplash, - treg.texture, - treg.region, - 2 - )); - } - if (random.rand() % 200 < 2 && pos.y < areaCenter.y + 1) { - auto sound = assets.get(weather.fall.noise); - audio::play( - sound, - pos, - false, - 1.0f, - 1.0f, - false, - audio::PRIORITY_LOW, - audio::get_channel_index("ambient") - ); - } - } + auto vox = chunks.get(pos); + auto chunk = chunks.getChunkByVoxel(pos); + if (vox == nullptr || chunk == nullptr) { + return; + } + + const auto& def = indices.blocks.require(vox->id); + auto dst2 = util::distance2(pos, areaCenter); + if (!def.obstacle || dst2 >= 256 || weather.fall.noise.empty()) { + return; + } + for (int y = pos.y + 1; y < chunk->top; y++) { + if (indices.blocks.require(chunks.get(pos.x, y, pos.z)->id).obstacle) { + return; } } + if (dst2 < 128 && rainSplash.has_value()) { + auto treg = util::get_texture_region( + assets, "particles:rain_splash_0", "" + ); + renderer.particles->add(std::make_unique( + level, + glm::vec3 { + pos.x + random.randFloat(), + pos.y + 1.1, + pos.z + random.randFloat()}, + *rainSplash, + treg.texture, + treg.region, + 2 + )); + } + if (random.rand() % 200 < 2 && pos.y < areaCenter.y + 1) { + auto sound = assets.get(weather.fall.noise); + audio::play( + sound, + pos, + false, + 1.0f, + 1.0f, + false, + audio::PRIORITY_LOW, + audio::get_channel_index("ambient") + ); + } } void Decorator::update( diff --git a/src/graphics/render/Decorator.hpp b/src/graphics/render/Decorator.hpp index fb74d11a..acc87d1c 100644 --- a/src/graphics/render/Decorator.hpp +++ b/src/graphics/render/Decorator.hpp @@ -36,6 +36,8 @@ class Decorator { const glm::ivec3& areaStart, const glm::ivec3& areaCenter ); + + /// @brief Updates weather effects, blocks ambient sounds, etc.. void updateRandom( float delta, const glm::ivec3& areaCenter, diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 8deb8527..2dbc1f16 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -107,6 +107,17 @@ WorldRenderer::WorldRenderer( fall.vspeed = 2.0f; fall.texture = "misc/rain"; fall.noise = "ambient/rain"; + + ParticlesPreset rainSplash; + rainSplash.frames = { + "particles:rain_splash_0", + "particles:rain_splash_1", + "particles:rain_splash_2" + }; + rainSplash.lifetime = 0.2f; + rainSplash.spawnInterval = 0.0f; + rainSplash.size = {0.2f, 0.2f, 0.2f}; + fall.splash = std::move(rainSplash); } WorldRenderer::~WorldRenderer() = default; diff --git a/src/presets/WeatherPreset.hpp b/src/presets/WeatherPreset.hpp index 404980b6..68f68c7e 100644 --- a/src/presets/WeatherPreset.hpp +++ b/src/presets/WeatherPreset.hpp @@ -1,7 +1,11 @@ #pragma once +#include + #include "interfaces/Serializable.hpp" +#include "ParticlesPreset.hpp" + struct WeatherPreset : Serializable { struct { /// @brief Precipitation texture @@ -14,6 +18,8 @@ struct WeatherPreset : Serializable { float hspeed = 0.1f; /// @brief UV scaling float scale = 0.1f; + /// @brief Fall splash + std::optional splash; } fall {}; dv::value serialize() const override;