refactor: move movement logic into SpeedSystem and Entity components

This commit is contained in:
2026-07-28 10:54:13 +08:00
parent 831569cec6
commit bd1a9557a5
9 changed files with 145 additions and 100 deletions

View File

@@ -65,7 +65,7 @@ public:
void set_uuid(std::string_view uuid);
std::string get_uuid() const;
const std::string& get_name() const;
void reset_key_status();
void reset_input_status();
void init(std::string_view name);
bool ray_cast(const glm::vec3& start, const glm::vec3& dir,
@@ -83,7 +83,7 @@ private:
using enum GameMode;
float m_max_walk_speed = DEFAULT_MAX_WALK_SPEED;
float m_max_run_speed = DEFAULT_MAX_RUN_SPEED;
float m_max_y_speed = 7.5f;
static constexpr float MAX_SPACE_ON_TIME = 0.3f;
static constexpr float PLACE_BLOCK_INTERVAL = 0.2f;
Movement m_movement{};
@@ -93,12 +93,8 @@ private:
std::array<ItemStack, HOTBAR_SUM> m_hotbar;
float m_sensitivity = 0.15f;
float m_fly_y_speed = 7.5f;
bool can_up = true;
float space_on_time = 0.0f;
bool space_on = false;
bool is_fly = false;
int m_selected_hotbar = 0;
@@ -106,7 +102,6 @@ private:
bool m_sprinting = false;
bool m_underwater = false;
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 move_distance{0.0f, 0.0f, 0.0f};
// player is tow block tall, the pos is the lower pos
ChunkPos m_last_chunk_pos{0, 0};
@@ -115,7 +110,6 @@ private:
glm::vec3 m_right{0, 0, 0};
static constexpr glm::vec3 M_SIZE{0.6f, 1.8f, 0.6f};
MoveState m_move_state{};
MouseState m_mouse_state{};
GameMode m_game_mode = CREATIVE;
std::optional<LookBlock> m_look_block = std::nullopt;

View File

@@ -37,7 +37,7 @@ struct Orientation {
struct Velocity {
glm::vec3 value{0.0f};
glm::vec3 max{4.5f};
glm::vec3 max{4.5f, 7.5f, 7.5f};
};
struct HitBoxes {
@@ -54,6 +54,22 @@ struct Gravity {
float value = DEFAULT_G;
};
struct MoveState {
bool forward = false;
bool back = false;
bool left = false;
bool right = false;
bool down = false;
bool up = false;
bool is_fly = false;
bool can_up = true;
};
struct Direction {
glm::vec3 value{0.0f};
};
class Entity {
public:
glm::vec3& max_speed();
@@ -67,13 +83,24 @@ public:
float& walk_time();
Gait get_gait() const;
Velocity& velocity();
Position& pos();
WalkPose& walk_pose();
Orientation& angle();
Movement& movement();
Gravity& gravity();
MoveState& move_state();
Direction& direction();
protected:
Position m_pos;
WalkPose m_walk_pos;
WalkPose m_walk_pose;
Velocity m_velocity;
Orientation m_angle;
Movement m_movement;
Gravity m_gravity;
MoveState m_move_state;
Direction m_direction;
};
} // namespace Cubed

View File

@@ -0,0 +1,63 @@
#pragma once
#include "Cubed/gameplay/entity.hpp"
namespace Cubed {
class SpeedSystem {
public:
static void update(float dt, Entity& e) {
auto& m_velocity = e.velocity();
auto& m_move_state = e.move_state();
auto& m_movement = e.movement();
auto& direction = e.direction();
auto& m_gravity = e.gravity();
// calculate speed
if (m_move_state.forward || m_move_state.back || m_move_state.left ||
m_move_state.right) {
direction.value = glm::vec3(0.0f, 0.0f, 0.0f);
m_velocity.value.x += m_movement.acceleration * dt;
m_velocity.value.z += m_movement.acceleration * dt;
if (m_velocity.value.x > m_velocity.max.x) {
m_velocity.value.x = m_velocity.max.x;
}
if (m_velocity.value.z > m_velocity.max.z) {
m_velocity.value.z = m_velocity.max.z;
}
} else {
m_velocity.value.x += -m_movement.deceleration * dt;
m_velocity.value.z += -m_movement.deceleration * dt;
if (m_velocity.value.z < 0.0f) {
m_velocity.value.z = 0.0f;
}
if (m_velocity.value.x < 0.0f) {
m_velocity.value.x = 0.0f;
}
if (m_velocity.value.z < 0.0f && m_velocity.value.x < 0.0f) {
direction.value = glm::vec3(0.0f, 0.0f, 0.0f);
}
}
if (m_move_state.is_fly) {
if (m_move_state.up) {
m_velocity.value.y = m_velocity.max.y;
}
if (m_move_state.down) {
m_velocity.value.y = -m_velocity.max.y;
}
if (!m_move_state.down && !m_move_state.up) {
m_velocity.value.y = 0.0f;
}
} else {
if (m_move_state.up && m_move_state.can_up) {
m_velocity.value.y = m_movement.jump_power;
m_move_state.can_up = false;
}
m_velocity.value.y += -m_gravity.value * dt;
}
}
private:
};
} // namespace Cubed

View File

@@ -2,15 +2,6 @@
namespace Cubed {
struct MoveState {
bool forward = false;
bool back = false;
bool left = false;
bool right = false;
bool down = false;
bool up = false;
};
struct MouseState {
bool left = false;
bool right = false;