mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(gameplay): optimize chunk request with player chunk tracking
Add `update_player_chunk()` method to `ClientPlayer` that triggers a chunk request when the player moves more than 2 chunks away from the last recorded chunk. Introduce an atomic `m_requesting_chunk` flag in `ClientWorld` to prevent concurrent requests. Rename `m_player_chunk_pos` to `m_last_chunk_pos` for clarity.
This commit is contained in:
@@ -197,7 +197,6 @@ void ClientWorld::init(std::string_view player_name,
|
||||
m_client = client;
|
||||
// timer
|
||||
register_timer("player_pos", 2, [this]() { report_player_pos(); });
|
||||
// register_timer("chunk_request", 20, [this]() { request_chunk(); });
|
||||
LoginReq req;
|
||||
req.set_name(m_player.get_name());
|
||||
while (!client->is_connected()) {
|
||||
@@ -261,6 +260,10 @@ void ClientWorld::report_player_pos() {
|
||||
}
|
||||
|
||||
void ClientWorld::request_chunk() {
|
||||
if (m_requesting_chunk.exchange(true)) {
|
||||
Logger::warn("It is requesting new chunk!");
|
||||
return;
|
||||
}
|
||||
ChunkPosSet required_chunks;
|
||||
|
||||
glm::vec3 player_pos = m_player.get_player_pos();
|
||||
@@ -310,6 +313,7 @@ void ClientWorld::request_chunk() {
|
||||
p->set_z(pos.z);
|
||||
m_client->send(make_packet(req));
|
||||
}
|
||||
m_requesting_chunk = false;
|
||||
}
|
||||
|
||||
void ClientWorld::receive_chunk(const ChunkDataRsp& data) {
|
||||
@@ -318,7 +322,6 @@ void ClientWorld::receive_chunk(const ChunkDataRsp& data) {
|
||||
{
|
||||
std::lock_guard lock(m_pending_queue_mutex);
|
||||
m_pending_queue.emplace_back(std::move(chunk));
|
||||
Logger::info("ClientWorld Add a new pending chunk");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user