minor refactor

This commit is contained in:
MihailRis 2025-02-11 02:39:31 +03:00
parent 7635fdf807
commit 830e05733f
4 changed files with 64 additions and 50 deletions

View File

@ -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<Emitter>(
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<audio::Sound>(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<Emitter>(
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<audio::Sound>(weather.fall.noise);
audio::play(
sound,
pos,
false,
1.0f,
1.0f,
false,
audio::PRIORITY_LOW,
audio::get_channel_index("ambient")
);
}
}
void Decorator::update(

View File

@ -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,

View File

@ -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;

View File

@ -1,7 +1,11 @@
#pragma once
#include <optional>
#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<ParticlesPreset> splash;
} fall {};
dv::value serialize() const override;