Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Feb 10, 2025
1 parent d5df2a0 commit 6c44c3f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
95 changes: 45 additions & 50 deletions src/graphics/render/Decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/render/Decorator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions src/graphics/render/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/presets/WeatherPreset.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#pragma once

#include <optional>

#include "interfaces/Serializable.hpp"

#include "ParticlesPreset.hpp"

struct WeatherPreset : Serializable {
struct {
/// @brief Precipitation texture
Expand All @@ -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;
Expand Down

0 comments on commit 6c44c3f

Please sign in to comment.