mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(sound): add block place and break sounds
This commit is contained in:
BIN
assets/sound/block/sand/place.ogg
Normal file
BIN
assets/sound/block/sand/place.ogg
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -76,6 +76,7 @@ public:
|
|||||||
void need_upload();
|
void need_upload();
|
||||||
|
|
||||||
void set_chunk_block(int index, unsigned id);
|
void set_chunk_block(int index, unsigned id);
|
||||||
|
BlockType get_chunk_block(int index);
|
||||||
bool is_temp_chunk() const;
|
bool is_temp_chunk() const;
|
||||||
ChunkPos chunk_pos() const;
|
ChunkPos chunk_pos() const;
|
||||||
BiomeType biome() const;
|
BiomeType biome() const;
|
||||||
|
|||||||
@@ -108,6 +108,12 @@ private:
|
|||||||
using OtherPlayerHashMap = std::unordered_map<std::string, PlayerInfo>;
|
using OtherPlayerHashMap = std::unordered_map<std::string, PlayerInfo>;
|
||||||
using chunk_acc = ChunkHashMap::accessor;
|
using chunk_acc = ChunkHashMap::accessor;
|
||||||
using chunk_cacc = ChunkHashMap::const_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 WORLD_EXIT_TIMEOUT = 200;
|
||||||
static constexpr int MAX_UPLOAD_CHUNK_SUM = 16;
|
static constexpr int MAX_UPLOAD_CHUNK_SUM = 16;
|
||||||
ClientPlayer m_player;
|
ClientPlayer m_player;
|
||||||
@@ -123,7 +129,7 @@ private:
|
|||||||
|
|
||||||
tbb::concurrent_queue<std::unique_ptr<ClientChunk>> m_pending_upload_queue;
|
tbb::concurrent_queue<std::unique_ptr<ClientChunk>> m_pending_upload_queue;
|
||||||
tbb::concurrent_queue<ChunkPos> m_dirty_chunk_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_vbo;
|
||||||
std::vector<GLuint> m_pending_delete_vao;
|
std::vector<GLuint> m_pending_delete_vao;
|
||||||
|
|
||||||
|
|||||||
@@ -282,7 +282,7 @@ void ClientChunk::need_upload() { m_need_upload = true; }
|
|||||||
void ClientChunk::set_chunk_block(int index, unsigned id) {
|
void ClientChunk::set_chunk_block(int index, unsigned id) {
|
||||||
m_blocks[index] = 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; }
|
ChunkPos ClientChunk::chunk_pos() const { return m_chunk_pos; }
|
||||||
|
|
||||||
BiomeType ClientChunk::biome() const { return m_biome; }
|
BiomeType ClientChunk::biome() const { return m_biome; }
|
||||||
|
|||||||
@@ -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);
|
auto [chunk_x, chunk_z] = get_chunk_pos(world_x, world_z);
|
||||||
ChunkPos pos{chunk_x, chunk_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;
|
chunk_acc acc;
|
||||||
|
|
||||||
@@ -159,16 +162,26 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
|
|||||||
return;
|
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 ||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUNK_SIZE) {
|
z >= CHUNK_SIZE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
int idx = ClientChunk::index(x, y, z);
|
||||||
acc->second->set_chunk_block(ClientChunk::index(x, y, z), id);
|
origin_id = acc->second->get_chunk_block(idx);
|
||||||
|
acc->second->set_chunk_block(idx, id);
|
||||||
acc->second->mark_dirty();
|
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();
|
auto pool = m_thread_pool.load();
|
||||||
|
|
||||||
@@ -759,6 +772,12 @@ void ClientWorld::update(float delta_time) {
|
|||||||
m_player.get_gait(), m_player.angle());
|
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 {
|
glm::vec3 ClientWorld::sunlight_dir() const {
|
||||||
|
|||||||
Reference in New Issue
Block a user