From 82f0e1deae29c1e888a5e4933fd22ca51da11e98 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 5 Apr 2026 21:47:32 +0800 Subject: [PATCH] feat: add run and walk gait --- include/Cubed/config.hpp | 2 +- include/Cubed/gameplay/player.hpp | 12 ++++++++++-- include/Cubed/gameplay/world.hpp | 1 - include/Cubed/renderer.hpp | 3 +++ src/app.cpp | 13 +++++++++++-- src/gameplay/player.cpp | 21 +++++++++++++++++++-- src/renderer.cpp | 8 +++++++- 7 files changed, 51 insertions(+), 9 deletions(-) diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp index 8ac1c29..95d28a3 100644 --- a/include/Cubed/config.hpp +++ b/include/Cubed/config.hpp @@ -7,7 +7,7 @@ constexpr int CHUCK_SIZE = 16; constexpr int DISTANCE = 16; constexpr int MAX_BLOCK_STATUS = 1; constexpr int MAX_CHARACTER = 128; -constexpr float FOV = 70.0f; +constexpr float NORMAL_FOV = 70.0f; constexpr int SEED = 999; diff --git a/include/Cubed/gameplay/player.hpp b/include/Cubed/gameplay/player.hpp index b980d5d..886c5b4 100644 --- a/include/Cubed/gameplay/player.hpp +++ b/include/Cubed/gameplay/player.hpp @@ -9,10 +9,17 @@ #include #include +enum class Gait{ + WALK, + RUN +}; + class World; class Player { private: + constexpr static float WALK_SPEED = 4.5f; + constexpr static float RUN_SPEED = 7.0f; constexpr static float ACCELERATION = 10.0f; constexpr static float DECELERATION = 15.0f; constexpr static float G = 22.5f; @@ -23,8 +30,7 @@ private: float m_sensitivity = 0.15f; - //float max_speed = 4.5f; - float max_speed = 7.5f; + float max_speed = WALK_SPEED; float y_speed = 0.0f; bool can_up = true; @@ -41,6 +47,7 @@ private: glm::vec3 m_front {0, 0, -1}; glm::vec3 m_right {0, 0, 0}; glm::vec3 m_size {0.6f, 1.8f, 0.6f}; + Gait m_gait = Gait::WALK; MoveState m_move_state {}; std::optional m_look_block = std::nullopt; @@ -61,6 +68,7 @@ public: ~Player(); AABB get_aabb() const; const glm::vec3& get_front() const; + const Gait& get_gait() const; const std::optional& get_look_block_pos() const; const glm::vec3& get_player_pos() const; const MoveState& get_move_state() const; diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index 6f1ef0b..7427fc7 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -19,7 +19,6 @@ private: std::pair chunk_pos(int world_x, int world_z); void gen_chunks(); - public: diff --git a/include/Cubed/renderer.hpp b/include/Cubed/renderer.hpp index 8baed46..4145e9b 100644 --- a/include/Cubed/renderer.hpp +++ b/include/Cubed/renderer.hpp @@ -19,6 +19,7 @@ public: void init(); const Shader& get_shader(const std::string& name) const; void render(); + void update_fov(float fov); void update_proj_matrix(float aspect, float width, float height); private: @@ -26,6 +27,8 @@ private: const TextureManager& m_texture_manager; World& m_world; + float m_aspect = 0.0f; + float m_fov = NORMAL_FOV; glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat; GLuint m_mv_loc; diff --git a/src/app.cpp b/src/app.cpp index 7b08cb9..0748b26 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -138,7 +138,7 @@ void App::run() { } } - +static Gait player_gait = Gait::WALK; void App::update() { glfwPollEvents(); current_time = glfwGetTime(); @@ -155,7 +155,16 @@ void App::update() { } m_world.update(delta_time); m_camera.update_move_camera(); - + const auto& player= m_world.get_player("TestPlayer"); + if (player_gait != player.get_gait()) { + player_gait = player.get_gait(); + if (player_gait == Gait::WALK) { + m_renderer.update_fov(NORMAL_FOV); + } + if (player_gait == Gait::RUN) { + m_renderer.update_fov(NORMAL_FOV + 3.0f); + } + } } diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 7a7d2de..4ede531 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -40,6 +40,10 @@ const glm::vec3& Player::get_front() const { return m_front; } +const Gait& Player::get_gait() const { + return m_gait; +} + const std::optional& Player::get_look_block_pos() const { return m_look_block; } @@ -143,6 +147,7 @@ void Player::update_player_move_state(int key, int action) { } if (action == GLFW_RELEASE) { m_move_state.forward = false; + m_gait = Gait::WALK; } break; case GLFW_KEY_S: @@ -193,7 +198,11 @@ void Player::update_player_move_state(int key, int action) { m_move_state.down = false; } break; - + case GLFW_KEY_LEFT_CONTROL: + if (action == GLFW_PRESS) { + m_gait = Gait::RUN; + } + break; } } @@ -282,6 +291,13 @@ void Player::update_lookup_block() { } void Player::update_move(float delta_time) { + if (m_gait == Gait::RUN) { + max_speed = RUN_SPEED; + } + if (m_gait == Gait::WALK) { + max_speed = WALK_SPEED; + } + if (space_on) { space_on_time += delta_time; if (space_on_time >= MAX_SPACE_ON_TIME) { @@ -368,7 +384,7 @@ void Player::update_x_move() { glm::vec3{static_cast(x + 1), static_cast(y + 1), static_cast(z + 1)} }; if (player_box.intersects(block_box)) { - + m_gait = Gait::WALK; m_player_pos.x -= move_distance.x; return; } @@ -430,6 +446,7 @@ void Player::update_z_move() { glm::vec3{static_cast(x + 1), static_cast(y + 1), static_cast(z + 1)} }; if (player_box.intersects(block_box)) { + m_gait = Gait::WALK; m_player_pos.z -= move_distance.z; return; } diff --git a/src/renderer.cpp b/src/renderer.cpp index b005eaa..7fd1029 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -237,8 +237,14 @@ void Renderer::render_ui() { } +void Renderer::update_fov(float fov) { + m_fov = fov; + m_p_mat = glm::perspective(glm::radians(fov), m_aspect, 0.1f, 1000.0f); +} + void Renderer::update_proj_matrix(float aspect, float width, float height) { - m_p_mat = glm::perspective(glm::radians(FOV), aspect, 0.1f, 1000.0f); + m_aspect = aspect; + m_p_mat = glm::perspective(glm::radians(m_fov), aspect, 0.1f, 1000.0f); m_ui_proj = glm::ortho(0.0f, width, height, 0.0f, -1.0f, 1.0f); // scale and then translate m_ui_m_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(width / 2.0f, height / 2.0f, 0.0)) *