feat(sound): add block place and break sounds

This commit is contained in:
2026-07-07 11:45:38 +08:00
parent 68ddd78a6f
commit a5bc4e238d
7 changed files with 32 additions and 6 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -76,6 +76,7 @@ public:
void need_upload();
void set_chunk_block(int index, unsigned id);
BlockType get_chunk_block(int index);
bool is_temp_chunk() const;
ChunkPos chunk_pos() const;
BiomeType biome() const;

View File

@@ -108,6 +108,12 @@ private:
using OtherPlayerHashMap = std::unordered_map<std::string, PlayerInfo>;
using chunk_acc = ChunkHashMap::accessor;
using chunk_cacc = ChunkHashMap::const_accessor;
struct PendingSound {
std::string sound;
glm::vec3 sound_pos;
};
static constexpr int WORLD_EXIT_TIMEOUT = 200;
static constexpr int MAX_UPLOAD_CHUNK_SUM = 16;
ClientPlayer m_player;
@@ -123,7 +129,7 @@ private:
tbb::concurrent_queue<std::unique_ptr<ClientChunk>> m_pending_upload_queue;
tbb::concurrent_queue<ChunkPos> m_dirty_chunk_queue;
tbb::concurrent_queue<PendingSound> m_pending_sound;
std::vector<GLuint> m_pending_delete_vbo;
std::vector<GLuint> m_pending_delete_vao;

View File

@@ -282,7 +282,7 @@ void ClientChunk::need_upload() { m_need_upload = true; }
void ClientChunk::set_chunk_block(int index, unsigned id) {
m_blocks[index] = id;
}
BlockType ClientChunk::get_chunk_block(int index) { return m_blocks[index]; }
ChunkPos ClientChunk::chunk_pos() const { return m_chunk_pos; }
BiomeType ClientChunk::biome() const { return m_biome; }

View File

@@ -152,6 +152,9 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
auto [chunk_x, chunk_z] = get_chunk_pos(world_x, world_z);
ChunkPos pos{chunk_x, chunk_z};
auto [x, y, z] = ClientChunk::world_to_block(world_x, world_y, world_z,
chunk_x, chunk_z);
BlockType origin_id = 0;
{
chunk_acc acc;
@@ -159,16 +162,26 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
return;
}
auto [x, y, z] = ClientChunk::world_to_block(world_x, world_y, world_z,
chunk_x, chunk_z);
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
z >= CHUNK_SIZE) {
return;
}
acc->second->set_chunk_block(ClientChunk::index(x, y, z), id);
int idx = ClientChunk::index(x, y, z);
origin_id = acc->second->get_chunk_block(idx);
acc->second->set_chunk_block(idx, id);
acc->second->mark_dirty();
}
glm::vec3 sound_pos{x + 0.5, y, z + 0.5};
if (id == 0) {
std::string name = BlockManager::name_form_id(origin_id);
std::string sound = "block/" + name + "/break.ogg";
m_pending_sound.emplace(sound, sound_pos);
} else {
std::string name = BlockManager::name_form_id(id);
std::string sound = "block/" + name + "/place.ogg";
m_pending_sound.emplace(sound, sound_pos);
}
auto pool = m_thread_pool.load();
@@ -759,6 +772,12 @@ void ClientWorld::update(float delta_time) {
m_player.get_gait(), m_player.angle());
}
}
// sound
PendingSound pending_sound;
while (m_pending_sound.try_pop(pending_sound)) {
m_audio.play_3d(pending_sound.sound, pending_sound.sound_pos);
}
}
glm::vec3 ClientWorld::sunlight_dir() const {