From 061b72b7404b3c211994c183a78b59da236121ad Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 5 Apr 2026 21:13:55 +0800 Subject: [PATCH] feat: add switch between fly and walk --- include/Cubed/gameplay/player.hpp | 9 +++++- src/gameplay/player.cpp | 53 ++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/include/Cubed/gameplay/player.hpp b/include/Cubed/gameplay/player.hpp index 7f8ef9b..b980d5d 100644 --- a/include/Cubed/gameplay/player.hpp +++ b/include/Cubed/gameplay/player.hpp @@ -15,7 +15,9 @@ class Player { private: constexpr static float ACCELERATION = 10.0f; constexpr static float DECELERATION = 15.0f; - constexpr static float G = 22.5f; + constexpr static float G = 22.5f; + + constexpr static float MAX_SPACE_ON_TIME = 0.3f; float m_yaw = 0.0f; float m_pitch = 0.0f; @@ -25,6 +27,11 @@ private: float max_speed = 7.5f; float y_speed = 0.0f; bool can_up = true; + + float space_on_time = 0.0f; + bool space_on = false; + bool is_fly = false; + float speed = 0; glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f); diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 00ac82e..7a7d2de 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -172,6 +172,14 @@ void Player::update_player_move_state(int key, int action) { case GLFW_KEY_SPACE: if (action == GLFW_PRESS) { m_move_state.up = true; + if (space_on) { + is_fly = !is_fly ? true : false; + y_speed = 0.0f; + space_on = false; + space_on_time = 0.0f; + } else { + space_on = true; + } } if (action == GLFW_RELEASE) { m_move_state.up = false; @@ -185,6 +193,7 @@ void Player::update_player_move_state(int key, int action) { m_move_state.down = false; } break; + } } @@ -273,6 +282,14 @@ void Player::update_lookup_block() { } void Player::update_move(float delta_time) { + if (space_on) { + space_on_time += delta_time; + if (space_on_time >= MAX_SPACE_ON_TIME) { + space_on = false; + space_on_time = 0.0f; + } + } + // calculate speed if (m_move_state.forward || m_move_state.back || m_move_state.left || m_move_state.right || m_move_state.up) { direction = glm::vec3(0.0f, 0.0f, 0.0f); @@ -292,26 +309,31 @@ void Player::update_move(float delta_time) { move_distance = {direction.x * speed * delta_time, 0.0f, direction.z * speed * delta_time}; - /* - if (m_move_state.up && can_up) { + if (is_fly) { + if (m_move_state.up) { + y_speed = 7.5f; + } + + if (m_move_state.down) { + y_speed = -7.5f; + } + + if (!m_move_state.down && !m_move_state.up) { + y_speed = 0.0f; + } + } else { + if (m_move_state.up && can_up) { y_speed = 7.5; can_up = false; - } + } - y_speed += -G * delta_time; - */ - if (m_move_state.up) { - y_speed = 7.5f; - } - - if (m_move_state.down) { - y_speed = -7.5f; - } - - if (!m_move_state.down && !m_move_state.up) { - y_speed = 0.0f; + y_speed += -G * delta_time; } + + + + move_distance.y = y_speed * delta_time; // y @@ -379,6 +401,7 @@ void Player::update_y_move() { y_speed = 0.0f; if (move_distance.y < 0) { can_up = true; + is_fly = false; } return; }