feat: add switch between fly and walk

This commit is contained in:
2026-04-05 21:13:55 +08:00
parent ae914a944c
commit 061b72b740
2 changed files with 46 additions and 16 deletions

View File

@@ -15,7 +15,9 @@ class Player {
private: private:
constexpr static float ACCELERATION = 10.0f; constexpr static float ACCELERATION = 10.0f;
constexpr static float DECELERATION = 15.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_yaw = 0.0f;
float m_pitch = 0.0f; float m_pitch = 0.0f;
@@ -25,6 +27,11 @@ private:
float max_speed = 7.5f; float max_speed = 7.5f;
float y_speed = 0.0f; float y_speed = 0.0f;
bool can_up = true; bool can_up = true;
float space_on_time = 0.0f;
bool space_on = false;
bool is_fly = false;
float speed = 0; float speed = 0;
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f); glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f);

View File

@@ -172,6 +172,14 @@ void Player::update_player_move_state(int key, int action) {
case GLFW_KEY_SPACE: case GLFW_KEY_SPACE:
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
m_move_state.up = true; 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) { if (action == GLFW_RELEASE) {
m_move_state.up = false; m_move_state.up = false;
@@ -185,6 +193,7 @@ void Player::update_player_move_state(int key, int action) {
m_move_state.down = false; m_move_state.down = false;
} }
break; break;
} }
} }
@@ -273,6 +282,14 @@ void Player::update_lookup_block() {
} }
void Player::update_move(float delta_time) { 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 // calculate speed
if (m_move_state.forward || m_move_state.back || m_move_state.left || m_move_state.right || m_move_state.up) { 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); 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}; move_distance = {direction.x * speed * delta_time, 0.0f, direction.z * speed * delta_time};
/* if (is_fly) {
if (m_move_state.up && can_up) { 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; y_speed = 7.5;
can_up = false; can_up = false;
} }
y_speed += -G * delta_time; 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;
} }
move_distance.y = y_speed * delta_time; move_distance.y = y_speed * delta_time;
// y // y
@@ -379,6 +401,7 @@ void Player::update_y_move() {
y_speed = 0.0f; y_speed = 0.0f;
if (move_distance.y < 0) { if (move_distance.y < 0) {
can_up = true; can_up = true;
is_fly = false;
} }
return; return;
} }