#pragma once #include "Cubed/AABB.hpp" #include "Cubed/gameplay/block.hpp" #include "Cubed/gameplay/chunk_pos.hpp" #include "Cubed/gameplay/client_chunk.hpp" #include "Cubed/gameplay/client_player.hpp" #include "Cubed/gameplay/game_time.hpp" #include "Cubed/gameplay/network_client.hpp" #include #include namespace Cubed { class ClientWorld { public: ClientWorld(std::string_view player_name, std::shared_ptr client); ~ClientWorld(); void init(); void update(float delta_time); bool can_move(const AABB& player_box) const; const std::optional& get_look_block_pos(const std::string& name) const; ClientPlayer& get_player(); int get_block(const glm::ivec3& block_pos) const; bool is_solid(const glm::ivec3& block_pos) const; bool can_pass_block(const glm::ivec3& block_pos) const; BlockType get_block_tpye(const glm::ivec3& block_pos) const; void set_block(const glm::ivec3& pos, unsigned id); void push_delete_vbo(GLuint vbo); void push_delete_vao(GLuint vao); void hot_reload(); void rebuild_world(); int rendering_distance() const; void rendering_distance(int rendering_distance); void start_client_thread(std::string_view uuid); void stop_client_thread(); std::vector& planes(); std::vector& render_snapshots(); glm::vec3 sunlight_dir() const; void receive_chunk(const ChunkDataRsp& data); template 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(f))); } private: using ChunkHashMap = tbb::concurrent_unordered_map; using ChunkPosSet = std::unordered_set; ClientPlayer m_player; ChunkHashMap m_chunks; std::vector m_planes; std::jthread m_client_thread; mutable std::shared_mutex m_chunks_mutex; std::mutex m_delete_vbo_mutex; std::mutex m_delete_vao_mutex; std::mutex m_pending_queue_mutex; std::deque m_pending_queue; std::vector m_pending_delete_vbo; std::vector m_pending_delete_vao; std::deque m_dirty_queue; std::vector m_render_snapshots; tbb::concurrent_unordered_map m_timers; std::atomic m_game_running{false}; std::atomic m_rendering_distance{24}; std::shared_ptr m_client; void client_run(std::stop_token token); void set_player_pos(); void report_player_pos(); void request_chunk(); }; } // namespace Cubed