refactor(gameplay): extract movement, gravity, orientation, and walk pose into structs

This commit is contained in:
2026-07-28 08:53:51 +08:00
parent ab605f7590
commit 802d2ecb5a
3 changed files with 40 additions and 32 deletions

View File

@@ -3,6 +3,7 @@
#include "Cubed/constants.hpp" #include "Cubed/constants.hpp"
#include "Cubed/gameplay/block.hpp" #include "Cubed/gameplay/block.hpp"
#include "Cubed/gameplay/chunk_pos.hpp" #include "Cubed/gameplay/chunk_pos.hpp"
#include "Cubed/gameplay/entity.hpp"
#include "Cubed/gameplay/game_mode.hpp" #include "Cubed/gameplay/game_mode.hpp"
#include "Cubed/gameplay/game_time.hpp" #include "Cubed/gameplay/game_time.hpp"
#include "Cubed/gameplay/item_stack.hpp" #include "Cubed/gameplay/item_stack.hpp"
@@ -91,15 +92,14 @@ private:
using enum GameMode; using enum GameMode;
float m_max_walk_speed = DEFAULT_MAX_WALK_SPEED; float m_max_walk_speed = DEFAULT_MAX_WALK_SPEED;
float m_max_run_speed = DEFAULT_MAX_RUN_SPEED; float m_max_run_speed = DEFAULT_MAX_RUN_SPEED;
float m_acceleration = DEFAULT_ACCELERATION;
float m_deceleration = DEFAULT_DECELERATION;
float m_g = DEFAULT_G;
static constexpr float MAX_SPACE_ON_TIME = 0.3f; static constexpr float MAX_SPACE_ON_TIME = 0.3f;
static constexpr float PLACE_BLOCK_INTERVAL = 0.2f; static constexpr float PLACE_BLOCK_INTERVAL = 0.2f;
Movement m_movement{};
Gravity m_gravity{};
float m_place_time = PLACE_BLOCK_INTERVAL; float m_place_time = PLACE_BLOCK_INTERVAL;
std::atomic<float> m_yaw = 0.0f; Orientation m_angle;
std::atomic<float> m_pitch = 0.0f; WalkPose m_walk_pos;
std::array<ItemStack, HOTBAR_SUM> m_hotbar; std::array<ItemStack, HOTBAR_SUM> m_hotbar;
float m_sensitivity = 0.15f; float m_sensitivity = 0.15f;
@@ -131,7 +131,6 @@ private:
glm::vec3 m_right{0, 0, 0}; glm::vec3 m_right{0, 0, 0};
static constexpr glm::vec3 M_SIZE{0.6f, 1.8f, 0.6f}; static constexpr glm::vec3 M_SIZE{0.6f, 1.8f, 0.6f};
std::atomic<Gait> m_gait = Gait::STOP;
MoveState m_move_state{}; MoveState m_move_state{};
MouseState m_mouse_state{}; MouseState m_mouse_state{};
GameMode m_game_mode = CREATIVE; GameMode m_game_mode = CREATIVE;
@@ -141,9 +140,6 @@ private:
std::string m_uuid; std::string m_uuid;
ClientWorld& m_world; ClientWorld& m_world;
float m_angle{0.0f};
float m_walk_time{0.0f};
std::unordered_map<std::string, Timer> m_timers; std::unordered_map<std::string, Timer> m_timers;
mutable std::shared_mutex m_player_pos_mutex; mutable std::shared_mutex m_player_pos_mutex;

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Cubed/AABB.hpp" #include "Cubed/AABB.hpp"
#include "Cubed/constants.hpp"
#include "Cubed/gameplay/player.hpp" #include "Cubed/gameplay/player.hpp"
#include <glm/glm.hpp> #include <glm/glm.hpp>
@@ -42,4 +43,14 @@ struct HitBoxes {
std::vector<AABB> boxex; std::vector<AABB> boxex;
}; };
struct Movement {
float acceleration = DEFAULT_ACCELERATION;
float deceleration = DEFAULT_DECELERATION;
float jump_power = 0;
};
struct Gravity {
float value = DEFAULT_G;
};
} // namespace Cubed } // namespace Cubed

View File

@@ -20,7 +20,7 @@ AABB ClientPlayer::get_aabb(const glm::vec3& pos) {
} }
const glm::vec3& ClientPlayer::get_front() const { return m_front; } const glm::vec3& ClientPlayer::get_front() const { return m_front; }
Gait ClientPlayer::get_gait() const { return m_gait.load(); } Gait ClientPlayer::get_gait() const { return m_walk_pos.gait; }
const std::optional<LookBlock>& ClientPlayer::get_look_block_pos() const { const std::optional<LookBlock>& ClientPlayer::get_look_block_pos() const {
return m_look_block; return m_look_block;
@@ -124,7 +124,9 @@ void ClientPlayer::reload_config() {
void ClientPlayer::set_player_pos(const glm::vec3& pos) { m_player_pos = pos; } void ClientPlayer::set_player_pos(const glm::vec3& pos) { m_player_pos = pos; }
void ClientPlayer::update(float delta_time) { void ClientPlayer::update(float delta_time) {
m_gait = compute_gait(); WalkPose pos = m_walk_pos;
pos.gait = compute_gait();
m_walk_pos = pos;
update_move(delta_time); update_move(delta_time);
update_lookup_block(); update_lookup_block();
place_block(delta_time); place_block(delta_time);
@@ -233,18 +235,17 @@ bool ClientPlayer::update_player_move_state(Key key, KeyAction action) {
} }
void ClientPlayer::update_front_vec(float offset_x, float offset_y) { void ClientPlayer::update_front_vec(float offset_x, float offset_y) {
m_yaw += offset_x * m_sensitivity; Orientation& angle = m_angle;
m_pitch += offset_y * m_sensitivity; angle.yaw += offset_x * m_sensitivity;
angle.pitch += offset_y * m_sensitivity;
// m_yaw = std::fmod(m_yaw.load(), 360.0); // m_yaw = std::fmod(m_yaw.load(), 360.0);
m_pitch = std::clamp(m_pitch.load(), -89.0f, 89.0f); angle.pitch = std::clamp(angle.pitch, -89.0f, 89.0f);
m_front.x = m_front.x = sin(glm::radians(angle.yaw)) * cos(glm::radians(angle.pitch));
sin(glm::radians(m_yaw.load())) * cos(glm::radians(m_pitch.load())); m_front.y = sin(glm::radians(angle.pitch));
m_front.y = sin(glm::radians(m_pitch.load())); m_front.z = -cos(glm::radians(angle.yaw)) * cos(glm::radians(angle.pitch));
m_front.z =
-cos(glm::radians(m_yaw.load())) * cos(glm::radians(m_pitch.load()));
m_front = glm::normalize(m_front); m_front = glm::normalize(m_front);
} }
@@ -345,7 +346,7 @@ void ClientPlayer::update_move(float delta_time) {
if (m_game_mode != SPECTATOR) { if (m_game_mode != SPECTATOR) {
m_max_speed = m_max_speed =
(m_gait == Gait::RUN) ? m_max_run_speed : m_max_walk_speed; (m_walk_pos.gait == Gait::RUN) ? m_max_run_speed : m_max_walk_speed;
} else { } else {
m_max_speed = m_max_run_speed; m_max_speed = m_max_run_speed;
} }
@@ -362,12 +363,12 @@ void ClientPlayer::update_move(float delta_time) {
if (m_move_state.forward || m_move_state.back || m_move_state.left || if (m_move_state.forward || m_move_state.back || m_move_state.left ||
m_move_state.right) { m_move_state.right) {
direction = glm::vec3(0.0f, 0.0f, 0.0f); direction = glm::vec3(0.0f, 0.0f, 0.0f);
m_xz_speed += m_acceleration * delta_time; m_xz_speed += m_movement.acceleration * delta_time;
if (m_xz_speed > m_max_speed) { if (m_xz_speed > m_max_speed) {
m_xz_speed = m_max_speed; m_xz_speed = m_max_speed;
} }
} else { } else {
m_xz_speed += -m_deceleration * delta_time; m_xz_speed += -m_movement.deceleration * delta_time;
if (m_xz_speed < 0) { if (m_xz_speed < 0) {
m_xz_speed = 0; m_xz_speed = 0;
direction = glm::vec3(0.0f, 0.0f, 0.0f); direction = glm::vec3(0.0f, 0.0f, 0.0f);
@@ -397,7 +398,7 @@ void ClientPlayer::update_move(float delta_time) {
can_up = false; can_up = false;
} }
m_y_speed += -m_g * delta_time; m_y_speed += -m_gravity.value * delta_time;
} }
move_distance.y = m_y_speed * delta_time; move_distance.y = m_y_speed * delta_time;
@@ -650,14 +651,14 @@ ClientPlayer::ChunkPosSet ClientPlayer::get_chunk_pos_set() {
float& ClientPlayer::max_walk_speed() { return m_max_walk_speed; } float& ClientPlayer::max_walk_speed() { return m_max_walk_speed; }
float& ClientPlayer::max_run_speed() { return m_max_run_speed; } float& ClientPlayer::max_run_speed() { return m_max_run_speed; }
float& ClientPlayer::max_speed() { return m_max_speed; } float& ClientPlayer::max_speed() { return m_max_speed; }
float& ClientPlayer::acceleration() { return m_acceleration; } float& ClientPlayer::acceleration() { return m_movement.acceleration; }
float& ClientPlayer::deceleration() { return m_deceleration; } float& ClientPlayer::deceleration() { return m_movement.deceleration; }
float& ClientPlayer::g() { return m_g; } float& ClientPlayer::g() { return m_gravity.value; }
float& ClientPlayer::fly_y_speed() { return m_fly_y_speed; } float& ClientPlayer::fly_y_speed() { return m_fly_y_speed; }
const ItemStack& ClientPlayer::get_current_itemstack() const { const ItemStack& ClientPlayer::get_current_itemstack() const {
return m_hotbar[m_selected_hotbar]; return m_hotbar[m_selected_hotbar];
}; };
void ClientPlayer::set_gait(Gait gait) { m_gait = gait; } void ClientPlayer::set_gait(Gait gait) { m_walk_pos.gait = gait; }
GameMode& ClientPlayer::game_mode() { return m_game_mode; } GameMode& ClientPlayer::game_mode() { return m_game_mode; }
ClientWorld& ClientPlayer::get_world() { return m_world; } ClientWorld& ClientPlayer::get_world() { return m_world; }
@@ -712,8 +713,8 @@ void ClientPlayer::init(std::string_view name) {
bool ClientPlayer::is_underwater() const { return m_underwater; } bool ClientPlayer::is_underwater() const { return m_underwater; }
void ClientPlayer::set_underwater(bool u) { m_underwater = u; } void ClientPlayer::set_underwater(bool u) { m_underwater = u; }
float ClientPlayer::yaw() const { return m_yaw; } float ClientPlayer::yaw() const { return m_angle.yaw; }
float ClientPlayer::pitch() const { return m_pitch; } float ClientPlayer::pitch() const { return m_angle.pitch; }
float& ClientPlayer::angle() { return m_angle; } float& ClientPlayer::angle() { return m_angle.roll; }
float& ClientPlayer::walk_time() { return m_walk_time; } float& ClientPlayer::walk_time() { return m_walk_pos.walk_time; }
} // namespace Cubed } // namespace Cubed