mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
refactor(gameplay): extract physics and collision from ClientPlayer into PhysicalSystem
Move per-axis collision detection and move distance calculation to new PhysicalSystem. Move SpeedSystem implementation from inline header to separate .cpp file. Add const accessors to Entity. Replace inline AABB helper with HitboxManager registration of player hitbox using new PLAYER_SIZE constant. Remove obsolete members and functions from ClientPlayer.
This commit is contained in:
56
src/gameplay/systems/speed_system.cpp
Normal file
56
src/gameplay/systems/speed_system.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "Cubed/gameplay/systems/speed_system.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
void SpeedSystem::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;
|
||||
}
|
||||
}
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user