diff --git a/assets/sound/block/sand/place.ogg b/assets/sound/block/sand/place.ogg new file mode 100644 index 0000000..feb142f Binary files /dev/null and b/assets/sound/block/sand/place.ogg differ diff --git a/assets/sound/block/sand/place.wav b/assets/sound/block/sand/place.wav deleted file mode 100644 index 97f8433..0000000 Binary files a/assets/sound/block/sand/place.wav and /dev/null differ diff --git a/assets/sound/block/stone/place.ogg b/assets/sound/block/stone/place.ogg index ddf5ac6..a8d3e2a 100644 Binary files a/assets/sound/block/stone/place.ogg and b/assets/sound/block/stone/place.ogg differ diff --git a/include/Cubed/gameplay/client_chunk.hpp b/include/Cubed/gameplay/client_chunk.hpp index bc283cf..a4fe258 100644 --- a/include/Cubed/gameplay/client_chunk.hpp +++ b/include/Cubed/gameplay/client_chunk.hpp @@ -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; diff --git a/include/Cubed/gameplay/client_world.hpp b/include/Cubed/gameplay/client_world.hpp index 7ecb736..406ebb9 100644 --- a/include/Cubed/gameplay/client_world.hpp +++ b/include/Cubed/gameplay/client_world.hpp @@ -108,6 +108,12 @@ private: using OtherPlayerHashMap = std::unordered_map; 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> m_pending_upload_queue; tbb::concurrent_queue m_dirty_chunk_queue; - + tbb::concurrent_queue m_pending_sound; std::vector m_pending_delete_vbo; std::vector m_pending_delete_vao; diff --git a/src/gameplay/client_chunk.cpp b/src/gameplay/client_chunk.cpp index 79ad810..daa122d 100644 --- a/src/gameplay/client_chunk.cpp +++ b/src/gameplay/client_chunk.cpp @@ -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; } diff --git a/src/gameplay/client_world.cpp b/src/gameplay/client_world.cpp index 852010a..d4ab031 100644 --- a/src/gameplay/client_world.cpp +++ b/src/gameplay/client_world.cpp @@ -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 {