mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
refactor(ecs): move entity physics to tick-based systems
Update PhysicalSystem and SpeedSystem to operate on entt::registry instead of individual components. Add TickVelocity for server creatures so movement is calculated per tick without frame delta time. LocalPlayer now implements its own client-side physics with collision detection.
This commit is contained in:
@@ -1,54 +1,24 @@
|
||||
#include "Cubed/gameplay/systems/speed_system.hpp"
|
||||
|
||||
#include "Cubed/gameplay/ecs/ai_struct.hpp"
|
||||
#include "Cubed/gameplay/ecs/server_entity.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
void SpeedSystem::update(float dt, Velocity& v, MoveState& move_state,
|
||||
Movement& movement, Direction& direction,
|
||||
const Gravity& g) {
|
||||
// calculate speed
|
||||
if (move_state.forward || move_state.back || move_state.left ||
|
||||
move_state.right) {
|
||||
direction.value = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
v.value.x += movement.acceleration * dt;
|
||||
v.value.z += movement.acceleration * dt;
|
||||
if (v.value.x > v.max.x) {
|
||||
v.value.x = v.max.x;
|
||||
}
|
||||
if (v.value.z > v.max.z) {
|
||||
v.value.z = v.max.z;
|
||||
}
|
||||
} else {
|
||||
v.value.x += -movement.deceleration * dt;
|
||||
v.value.z += -movement.deceleration * dt;
|
||||
if (v.value.z < 0.0f) {
|
||||
v.value.z = 0.0f;
|
||||
}
|
||||
if (v.value.x < 0.0f) {
|
||||
v.value.x = 0.0f;
|
||||
}
|
||||
if (v.value.z < 0.0f && v.value.x < 0.0f) {
|
||||
direction.value = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
}
|
||||
if (move_state.is_fly) {
|
||||
if (move_state.up) {
|
||||
v.value.y = v.max.y;
|
||||
}
|
||||
void SpeedSystem::update(float dt, entt::registry& registry) {
|
||||
auto view = registry.view<BaseServerCreature, MoveBoost>();
|
||||
|
||||
if (move_state.down) {
|
||||
v.value.y = -v.max.y;
|
||||
for (auto e : view) {
|
||||
auto [creature, moveboost] = view.get<BaseServerCreature, MoveBoost>(e);
|
||||
if (moveboost.count <= moveboost.duration) {
|
||||
++moveboost.count;
|
||||
creature.velocity.value +=
|
||||
creature.direction.value * creature.movement.acceleration;
|
||||
} else {
|
||||
creature.velocity.value -=
|
||||
creature.direction.value * creature.movement.deceleration;
|
||||
}
|
||||
|
||||
if (!move_state.down && !move_state.up) {
|
||||
v.value.y = 0.0f;
|
||||
}
|
||||
} else {
|
||||
if (move_state.up && move_state.can_up) {
|
||||
v.value.y = movement.jump_power;
|
||||
move_state.can_up = false;
|
||||
}
|
||||
|
||||
v.value.y += -g.value * dt;
|
||||
creature.velocity.value.y = creature.gravity.value * dt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user