From 0ff5820e6ea4393b0dae9f986c54650309a1c14f Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Tue, 28 Jul 2026 18:09:43 +0800 Subject: [PATCH] refactor(gameplay): rename ClientPlayer to LocalPlayer --- include/Cubed/camera.hpp | 8 +- include/Cubed/dev_panel.hpp | 4 +- include/Cubed/gameplay/client_world.hpp | 8 +- .../{client_player.hpp => local_player.hpp} | 6 +- src/CMakeLists.txt | 2 +- src/camera.cpp | 6 +- src/dev_panel.cpp | 2 +- src/gameplay/client_world.cpp | 8 +- .../{client_player.cpp => local_player.cpp} | 92 +++++++++---------- src/ui/inventory_ui.cpp | 2 +- src/ui/world_ui_manager.cpp | 4 +- src/window.cpp | 2 +- 12 files changed, 72 insertions(+), 72 deletions(-) rename include/Cubed/gameplay/{client_player.hpp => local_player.hpp} (97%) rename src/gameplay/{client_player.cpp => local_player.cpp} (85%) diff --git a/include/Cubed/camera.hpp b/include/Cubed/camera.hpp index ff40f72..5fe9769 100644 --- a/include/Cubed/camera.hpp +++ b/include/Cubed/camera.hpp @@ -8,7 +8,7 @@ namespace Cubed { -class ClientPlayer; +class LocalPlayer; class Camera { private: @@ -18,7 +18,7 @@ private: THIRD_PERSON_FRONT, }; - ClientPlayer* m_player; + LocalPlayer* m_player; float m_last_mouse_x, m_last_mouse_y; glm::vec3 m_camera_pos; bool m_under_water = false; @@ -35,7 +35,7 @@ public: void update_move_camera(); - void camera_init(ClientPlayer* player); + void camera_init(LocalPlayer* player); void hot_reload(); void update_cursor_position_camera(float offset_x, float offset_y); @@ -47,7 +47,7 @@ public: void change_perspective(); bool is_first_person() const; bool handle_event(const Event& e); - ClientPlayer* player(); + LocalPlayer* player(); }; } // namespace Cubed diff --git a/include/Cubed/dev_panel.hpp b/include/Cubed/dev_panel.hpp index 6b4303c..28a6935 100644 --- a/include/Cubed/dev_panel.hpp +++ b/include/Cubed/dev_panel.hpp @@ -7,7 +7,7 @@ namespace Cubed { class WorldScene; -class ClientPlayer; +class LocalPlayer; class App; class DevPanel { struct ConfigView { @@ -42,7 +42,7 @@ private: WorldScene& m_world_scene; Config& m_config; ConfigView m_config_view; - ClientPlayer* m_player; + LocalPlayer* m_player; PlayerProfile m_player_profile; bool m_need_save_config = false; bool m_gen_thread_running = true; diff --git a/include/Cubed/gameplay/client_world.hpp b/include/Cubed/gameplay/client_world.hpp index a9a27a0..77ffe67 100644 --- a/include/Cubed/gameplay/client_world.hpp +++ b/include/Cubed/gameplay/client_world.hpp @@ -5,8 +5,8 @@ #include "Cubed/gameplay/chat_message.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/local_player.hpp" #include "Cubed/gameplay/network_client.hpp" #include "Cubed/gameplay/player_data.hpp" #include "Cubed/gameplay/world.hpp" @@ -37,8 +37,8 @@ public: void update_players(float dt); bool handle_event(const Event& e); const std::optional& get_look_block_pos() const; - ClientPlayer& get_player(); - const ClientPlayer& get_player() const; + LocalPlayer& get_player(); + const LocalPlayer& get_player() const; int get_block(const glm::ivec3& block_pos) const override; bool is_solid(const glm::ivec3& block_pos) const override; bool can_pass_block(const glm::ivec3& block_pos) const override; @@ -131,7 +131,7 @@ private: static constexpr int WORLD_EXIT_TIMEOUT = 200; static constexpr int MAX_UPLOAD_CHUNK_SUM = 16; - ClientPlayer m_player; + LocalPlayer m_player; ChunkHashMap m_chunks; AudioEngine& m_audio; Config& m_config; diff --git a/include/Cubed/gameplay/client_player.hpp b/include/Cubed/gameplay/local_player.hpp similarity index 97% rename from include/Cubed/gameplay/client_player.hpp rename to include/Cubed/gameplay/local_player.hpp index 5e755b5..0329eeb 100644 --- a/include/Cubed/gameplay/client_player.hpp +++ b/include/Cubed/gameplay/local_player.hpp @@ -17,14 +17,14 @@ namespace Cubed { class ClientWorld; -class ClientPlayer : public Entity { +class LocalPlayer : public Entity { public: static constexpr size_t HOTBAR_SUM = 10; static constexpr float WALK_SOUND_INTERVAL = 0.45f; static constexpr float RUN_SOUND_INTERVAL = 0.3f; using ChunkPosSet = absl::flat_hash_set; - ClientPlayer(ClientWorld& world); - ~ClientPlayer(); + LocalPlayer(ClientWorld& world); + ~LocalPlayer(); bool handle_mouse_button_event(const MouseButtonEvent& e); bool handle_key_event(const KeyEvent& e); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e64ed11..a43a62a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,7 +37,7 @@ target_sources(${PROJECT_NAME} gameplay/server_chunk.cpp gameplay/client_chunk.cpp gameplay/server_player.cpp - gameplay/client_player.cpp + gameplay/local_player.cpp gameplay/session.cpp gameplay/network_client.cpp render/player_renderer.cpp diff --git a/src/camera.cpp b/src/camera.cpp index 5272dc1..59d0208 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -1,7 +1,7 @@ #include "Cubed/camera.hpp" -#include "Cubed/gameplay/client_player.hpp" #include "Cubed/gameplay/client_world.hpp" +#include "Cubed/gameplay/local_player.hpp" #include "Cubed/tools/cubed_assert.hpp" namespace { @@ -58,7 +58,7 @@ void Camera::update_move_camera() { } } -void Camera::camera_init(ClientPlayer* player) { +void Camera::camera_init(LocalPlayer* player) { m_player = player; update_move_camera(); hot_reload(); @@ -100,7 +100,7 @@ void Camera::change_perspective() { bool Camera::is_first_person() const { return m_perspective == Perspective::FIRST_PERSON; } -ClientPlayer* Camera::player() { return m_player; } +LocalPlayer* Camera::player() { return m_player; } bool Camera::handle_event(const Event& e) { return std::visit( Overloaded{[this](const MouseMoveEvent& e) { diff --git a/src/dev_panel.cpp b/src/dev_panel.cpp index 8c0d54d..ef633c2 100644 --- a/src/dev_panel.cpp +++ b/src/dev_panel.cpp @@ -3,7 +3,7 @@ #include "Cubed/app.hpp" #include "Cubed/config.hpp" #include "Cubed/gameplay/cave_path.hpp" -#include "Cubed/gameplay/client_player.hpp" +#include "Cubed/gameplay/local_player.hpp" #include "Cubed/gameplay/river.path.hpp" #include "Cubed/scene/world_scene.hpp" #include "Cubed/tools/log.hpp" diff --git a/src/gameplay/client_world.cpp b/src/gameplay/client_world.cpp index e119c7b..abff8d3 100644 --- a/src/gameplay/client_world.cpp +++ b/src/gameplay/client_world.cpp @@ -61,8 +61,8 @@ const std::optional& ClientWorld::get_look_block_pos() const { return m_player.get_look_block_pos(); } -ClientPlayer& ClientWorld::get_player() { return m_player; } -const ClientPlayer& ClientWorld::get_player() const { return m_player; } +LocalPlayer& ClientWorld::get_player() { return m_player; } +const LocalPlayer& ClientWorld::get_player() const { return m_player; } int ClientWorld::get_block(const glm::ivec3& block_pos) const { auto [chunk_x, chunk_z] = get_chunk_pos(block_pos.x, block_pos.z); chunk_cacc cacc; @@ -930,13 +930,13 @@ void ClientWorld::update_players(float dt) { m_audio.play_3d(sound, player.render_pos.value); }; if (player.walk.gait == Gait::WALK) { - if (player.walk.moving_time >= ClientPlayer::WALK_SOUND_INTERVAL) { + if (player.walk.moving_time >= LocalPlayer::WALK_SOUND_INTERVAL) { player.walk.moving_time = 0.0f; play_walk_sound(); } } if (player.walk.gait == Gait::RUN) { - if (player.walk.moving_time >= ClientPlayer::RUN_SOUND_INTERVAL) { + if (player.walk.moving_time >= LocalPlayer::RUN_SOUND_INTERVAL) { player.walk.moving_time = 0.0f; play_walk_sound(); } diff --git a/src/gameplay/client_player.cpp b/src/gameplay/local_player.cpp similarity index 85% rename from src/gameplay/client_player.cpp rename to src/gameplay/local_player.cpp index fe073b0..9955304 100644 --- a/src/gameplay/client_player.cpp +++ b/src/gameplay/local_player.cpp @@ -1,4 +1,4 @@ -#include "Cubed/gameplay/client_player.hpp" +#include "Cubed/gameplay/local_player.hpp" #include "Cubed/audio/audio_engine.hpp" #include "Cubed/config.hpp" @@ -10,26 +10,26 @@ namespace {} // namespace namespace Cubed { -ClientPlayer::ClientPlayer(ClientWorld& world) +LocalPlayer::LocalPlayer(ClientWorld& world) : Entity(0, "cubed:player"), m_world(world) {} -ClientPlayer::~ClientPlayer() {} +LocalPlayer::~LocalPlayer() {} -const glm::vec3& ClientPlayer::get_front() const { return m_front; } +const glm::vec3& LocalPlayer::get_front() const { return m_front; } -const std::optional& ClientPlayer::get_look_block_pos() const { +const std::optional& LocalPlayer::get_look_block_pos() const { return m_look_block; } -glm::vec3 ClientPlayer::get_player_pos() const { +glm::vec3 LocalPlayer::get_player_pos() const { std::shared_lock lock(m_player_pos_mutex); return m_pos.value; } -const MoveState& ClientPlayer::get_move_state() const { return m_move_state; } +const MoveState& LocalPlayer::get_move_state() const { return m_move_state; } -bool ClientPlayer::ray_cast(const glm::vec3& start, const glm::vec3& front, - glm::ivec3& block_pos, glm::vec3& normal, - float distance) { +bool LocalPlayer::ray_cast(const glm::vec3& start, const glm::vec3& front, + glm::ivec3& block_pos, glm::vec3& normal, + float distance) { glm::vec3 dir = glm::normalize(front); // float step = 0.1f; glm::ivec3 cur = glm::floor(start); @@ -100,7 +100,7 @@ bool ClientPlayer::ray_cast(const glm::vec3& start, const glm::vec3& front, return false; } -void ClientPlayer::change_mode(GameMode mode) { +void LocalPlayer::change_mode(GameMode mode) { m_game_mode = mode; Logger::info("Change GameMode to {}", to_str(mode)); if (mode == CREATIVE) { @@ -115,17 +115,17 @@ void ClientPlayer::change_mode(GameMode mode) { glm::vec3{m_max_run_speed, m_max_y_speed, m_max_run_speed}; } } -void ClientPlayer::reload_config() { +void LocalPlayer::reload_config() { auto& config = m_world.get_config(); m_sensitivity = config.get("player.mouse_sensitivity", 0.15f); } -void ClientPlayer::set_player_pos(const glm::vec3& pos) { +void LocalPlayer::set_player_pos(const glm::vec3& pos) { std::lock_guard lock(m_player_pos_mutex); m_pos.value = pos; } -void ClientPlayer::update(float delta_time) { +void LocalPlayer::update(float delta_time) { WalkPose pos = m_walk_pose; pos.gait = compute_gait(); m_walk_pose = pos; @@ -138,7 +138,7 @@ void ClientPlayer::update(float delta_time) { d_rep("speed", "Speed(x, y, z): {:.2} m/s {:.2} m/s {:.2} m/s", m_velocity.value.x, m_velocity.value.y, m_velocity.value.z); } -bool ClientPlayer::update_player_move_state(Key key, KeyAction action) { +bool LocalPlayer::update_player_move_state(Key key, KeyAction action) { if (key == Key::W) { if (action == KeyAction::PRESS) { m_move_state.forward = true; @@ -238,7 +238,7 @@ bool ClientPlayer::update_player_move_state(Key key, KeyAction action) { return true; } -void ClientPlayer::update_front_vec(float offset_x, float offset_y) { +void LocalPlayer::update_front_vec(float offset_x, float offset_y) { Orientation& angle = m_angle; angle.yaw += offset_x * m_sensitivity; angle.pitch += offset_y * m_sensitivity; @@ -254,7 +254,7 @@ void ClientPlayer::update_front_vec(float offset_x, float offset_y) { m_front = glm::normalize(m_front); } -void ClientPlayer::update_direction() { +void LocalPlayer::update_direction() { m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f))); glm::vec3 move_dir_front = glm::vec3(0.0f); @@ -279,7 +279,7 @@ void ClientPlayer::update_direction() { } } -void ClientPlayer::update_lookup_block() { +void LocalPlayer::update_lookup_block() { // calculate the block that is looked glm::ivec3 block_pos; glm::vec3 block_normal; @@ -292,7 +292,7 @@ void ClientPlayer::update_lookup_block() { m_look_block = std::nullopt; } } -void ClientPlayer::place_block(float dt) { +void LocalPlayer::place_block(float dt) { if (m_look_block == std::nullopt) { return; @@ -324,17 +324,17 @@ void ClientPlayer::place_block(float dt) { } } -int ClientPlayer::selected_hotbar() const { return m_selected_hotbar; } -void ClientPlayer::set_hotbar(int pos, const ItemStack& item) { +int LocalPlayer::selected_hotbar() const { return m_selected_hotbar; } +void LocalPlayer::set_hotbar(int pos, const ItemStack& item) { ASSERT(pos >= 0 && static_cast(pos) < HOTBAR_SUM); m_hotbar[pos] = item; } -std::span -ClientPlayer::get_hotbar() const { +std::span +LocalPlayer::get_hotbar() const { return m_hotbar; } -void ClientPlayer::update_move(float delta_time) { +void LocalPlayer::update_move(float delta_time) { // if frame rate less than 1 frame per second, don't update if (delta_time > 1.0f) { return; @@ -408,7 +408,7 @@ void ClientPlayer::update_move(float delta_time) { } } -void ClientPlayer::update_player_chunk() { +void LocalPlayer::update_player_chunk() { float x, z; { std::shared_lock lock(m_player_pos_mutex); @@ -424,7 +424,7 @@ void ClientPlayer::update_player_chunk() { } } -Gait ClientPlayer::compute_gait() const { +Gait LocalPlayer::compute_gait() const { if (m_velocity.value.x < 0.01f && m_velocity.value.z < 0.01f) return Gait::STOP; @@ -434,7 +434,7 @@ Gait ClientPlayer::compute_gait() const { return Gait::WALK; } -bool ClientPlayer::update_scroll(float yoffset) { +bool LocalPlayer::update_scroll(float yoffset) { if (m_game_mode == SPECTATOR) { if (yoffset > 0) { if (m_max_run_speed < 500.0f) { @@ -462,7 +462,7 @@ bool ClientPlayer::update_scroll(float yoffset) { return true; } -bool ClientPlayer::handle_mouse_button_event(const MouseButtonEvent& e) { +bool LocalPlayer::handle_mouse_button_event(const MouseButtonEvent& e) { if (e.action == KeyAction::PRESS) { if (e.key == MouseKey::LEFT_BUTTON) { m_mouse_state.left = true; @@ -488,7 +488,7 @@ bool ClientPlayer::handle_mouse_button_event(const MouseButtonEvent& e) { return false; } -bool ClientPlayer::handle_key_event(const KeyEvent& e) { +bool LocalPlayer::handle_key_event(const KeyEvent& e) { if (update_player_move_state(e.key, e.action)) { return true; @@ -496,51 +496,51 @@ bool ClientPlayer::handle_key_event(const KeyEvent& e) { return false; } -bool ClientPlayer::handle_mouse_wheel_event(const MouseWheelEvent& e) { +bool LocalPlayer::handle_mouse_wheel_event(const MouseWheelEvent& e) { if (update_scroll(e.offset)) { return true; } return false; } -void ClientPlayer::update_chunk_set(const ChunkPosSet& set) { +void LocalPlayer::update_chunk_set(const ChunkPosSet& set) { std::lock_guard lock(m_chunk_pos_mutex); m_player_chunk_pos_set.clear(); m_player_chunk_pos_set.insert(set.begin(), set.end()); } -const ClientPlayer::ChunkPosSet& ClientPlayer::get_chunk_pos_set() const { +const LocalPlayer::ChunkPosSet& LocalPlayer::get_chunk_pos_set() const { std::shared_lock lock(m_chunk_pos_mutex); return m_player_chunk_pos_set; } -ClientPlayer::ChunkPosSet ClientPlayer::get_chunk_pos_set() { +LocalPlayer::ChunkPosSet LocalPlayer::get_chunk_pos_set() { std::lock_guard lock(m_chunk_pos_mutex); return m_player_chunk_pos_set; } -float& ClientPlayer::max_walk_speed() { return m_max_walk_speed; } -float& ClientPlayer::max_run_speed() { return m_max_run_speed; } -float& ClientPlayer::fly_y_speed() { return m_max_y_speed; } -const ItemStack& ClientPlayer::get_current_itemstack() const { +float& LocalPlayer::max_walk_speed() { return m_max_walk_speed; } +float& LocalPlayer::max_run_speed() { return m_max_run_speed; } +float& LocalPlayer::fly_y_speed() { return m_max_y_speed; } +const ItemStack& LocalPlayer::get_current_itemstack() const { return m_hotbar[m_selected_hotbar]; }; -GameMode& ClientPlayer::game_mode() { return m_game_mode; } -ClientWorld& ClientPlayer::get_world() { return m_world; } +GameMode& LocalPlayer::game_mode() { return m_game_mode; } +ClientWorld& LocalPlayer::get_world() { return m_world; } -void ClientPlayer::set_uuid(std::string_view uuid) { +void LocalPlayer::set_uuid(std::string_view uuid) { std::lock_guard lock(m_uuid_mutex); m_uuid = uuid; } -std::string ClientPlayer::get_uuid() const { +std::string LocalPlayer::get_uuid() const { std::shared_lock lock(m_uuid_mutex); return m_uuid; } -const std::string& ClientPlayer::get_name() const { return m_name; } +const std::string& LocalPlayer::get_name() const { return m_name; } -void ClientPlayer::reset_input_status() { +void LocalPlayer::reset_input_status() { m_mouse_state.left = false; m_mouse_state.right = false; m_move_state.left = false; @@ -551,7 +551,7 @@ void ClientPlayer::reset_input_status() { m_move_state.up = false; } -void ClientPlayer::init(std::string_view name) { +void LocalPlayer::init(std::string_view name) { { std::lock_guard lock(m_player_pos_mutex); m_pos.value = {0.0f, 255.0f, 0.0f}; @@ -588,6 +588,6 @@ void ClientPlayer::init(std::string_view name) { } } -bool ClientPlayer::is_underwater() const { return m_underwater; } -void ClientPlayer::set_underwater(bool u) { m_underwater = u; } +bool LocalPlayer::is_underwater() const { return m_underwater; } +void LocalPlayer::set_underwater(bool u) { m_underwater = u; } } // namespace Cubed \ No newline at end of file diff --git a/src/ui/inventory_ui.cpp b/src/ui/inventory_ui.cpp index 04561b9..d59e864 100644 --- a/src/ui/inventory_ui.cpp +++ b/src/ui/inventory_ui.cpp @@ -175,7 +175,7 @@ ItemSlot* InventoryUI::get_hovered_slot() { return nullptr; } std::pair InventoryUI::get_hovered_hotbar_slot() { - for (size_t i = 0; i < ClientPlayer::HOTBAR_SUM; ++i) { + for (size_t i = 0; i < LocalPlayer::HOTBAR_SUM; ++i) { auto& slot = m_hotbar[i]; if (slot->hovered()) { diff --git a/src/ui/world_ui_manager.cpp b/src/ui/world_ui_manager.cpp index e0d8c06..dd1a5b6 100644 --- a/src/ui/world_ui_manager.cpp +++ b/src/ui/world_ui_manager.cpp @@ -30,7 +30,7 @@ void WorldUIManager::init() { auto& hotbar = m_root_widget->add_child(); hotbar.set_anchor(Anchor::BOTTOM_CENTER); m_hotbar = &hotbar; - for (size_t i = 0; i < ClientPlayer::HOTBAR_SUM; ++i) { + for (size_t i = 0; i < LocalPlayer::HOTBAR_SUM; ++i) { auto& slot = hotbar.add_child(); slot.set_default_background(texture_manager); slot.set_scale(5.0f); @@ -130,7 +130,7 @@ void WorldUIManager::update_hotbar() { auto& player = m_scene.client_world().get_player(); auto hotbar = player.get_hotbar(); size_t selected = player.selected_hotbar(); - for (size_t i = 0; i < ClientPlayer::HOTBAR_SUM; ++i) { + for (size_t i = 0; i < LocalPlayer::HOTBAR_SUM; ++i) { auto type = hotbar[i].type; if (selected == i) { m_hotbar_slot[i]->set_border_visale(true); diff --git a/src/window.cpp b/src/window.cpp index f5c7a83..39ee5ca 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,7 +1,7 @@ #include "Cubed/window.hpp" #include "Cubed/camera.hpp" -#include "Cubed/gameplay/client_player.hpp" +#include "Cubed/gameplay/local_player.hpp" #include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/env_tools.hpp" #include "Cubed/tools/font.hpp"