feat(gameplay): add time sync and network improvements

This commit is contained in:
2026-06-25 20:13:51 +08:00
parent d7d47e9e7f
commit 47552a1887
12 changed files with 123 additions and 10 deletions

View File

@@ -32,6 +32,8 @@ public:
// void rebuild_world();
void report_block_change(const glm::ivec3& pos, unsigned id) const;
void receive_block_change(const BlockChangeRsp& rsp);
void receive_time(const UpdateTime& rsp);
int rendering_distance() const;
void rendering_distance(int rendering_distance);
void start_client_thread(std::string_view uuid);
@@ -72,6 +74,8 @@ private:
tbb::concurrent_unordered_map<std::string, Timer> m_timers;
std::atomic<bool> m_game_running{false};
std::atomic<int> m_rendering_distance{24};
std::atomic<TickType> m_game_ticks{0};
std::atomic<TickType> m_day_tick{6000};
std::shared_ptr<NetworkClient> m_client;
void client_run(std::stop_token token);

View File

@@ -8,14 +8,15 @@
namespace Cubed {
using asio::ip::tcp;
class ClientWorld;
class NetworkClient : std::enable_shared_from_this<NetworkClient> {
class NetworkClient : public std::enable_shared_from_this<NetworkClient> {
public:
NetworkClient(ClientWorld& world);
~NetworkClient();
void close();
void stop();
void send(Packet packet);
void start(std::string ip, int port = 25530);
bool is_connected() const;
private:
asio::io_context m_io;
@@ -30,7 +31,7 @@ private:
asio::awaitable<void> read_loop();
std::atomic<bool> m_closed{false};
std::atomic<bool> m_connected{false};
// ClientWorld is managed by App
ClientWorld& m_world;

View File

@@ -17,6 +17,7 @@ enum class PacketEnum {
CHUNK_DATA_RSP = 3002,
BLOCK_CHANGE_REQ = 3003,
BLOCK_CHANGE_RSP = 3004,
UPDATE_TIME = 3005,
PING = 9001,
PONG = 9002
@@ -45,6 +46,8 @@ template <typename T> constexpr uint16_t get_packet_id() {
return to_num(BLOCK_CHANGE_REQ);
} else if constexpr (is_same_v<U, BlockChangeRsp>) {
return to_num(BLOCK_CHANGE_RSP);
} else if constexpr (is_same_v<U, UpdateTime>) {
return to_num(UPDATE_TIME);
} else if constexpr (is_same_v<U, Ping>) {
return to_num(PING);
} else if (is_same_v<U, Pong>) {

View File

@@ -73,6 +73,12 @@ public:
void handle_chunk_req(const std::string& uuid, ChunkPos pos);
void handle_block_change(const BlockChangeReq& req);
template <typename Fn>
void register_timer(std::string_view id, TickType threshold, Fn&& f) {
m_timers.emplace(std::piecewise_construct,
std::forward_as_tuple(std::string(id)),
std::forward_as_tuple(threshold, std::forward<Fn>(f)));
}
private:
enum class ChunkLoadStyle { RANDOM, CENTER };
@@ -128,6 +134,7 @@ private:
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::RANDOM};
PlayerUUIDMap m_uuid_to_name;
tbb::concurrent_unordered_map<std::string, Timer> m_timers;
void init_chunks();
void gen_chunks_internal(std::optional<std::string> uuid);
@@ -139,5 +146,7 @@ private:
void submit_new_chunks(const std::optional<std::string>& uuid);
void poll_finished_chunks();
void wait_all_chunk_tasks();
void send_time();
};
} // namespace Cubed