mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
Add movement constants for pigs and switch deceleration to per-axis friction so creatures stop naturally. Extend wander boost duration for smoother behavior.
41 lines
1.4 KiB
C++
41 lines
1.4 KiB
C++
#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, entt::registry& registry) {
|
|
auto view = registry.view<BaseServerCreature, MoveBoost>();
|
|
|
|
for (auto e : view) {
|
|
auto [creature, moveboost] = view.get<BaseServerCreature, MoveBoost>(e);
|
|
auto& v = creature.velocity.value;
|
|
if (moveboost.count <= moveboost.duration) {
|
|
++moveboost.count;
|
|
v += creature.direction.value * creature.movement.acceleration;
|
|
} else {
|
|
// Decelerated by friction in all directions
|
|
auto decay = [](float& c, float d) {
|
|
if (c > 0.0f)
|
|
c = std::max(0.0f, c - d);
|
|
else if (c < 0.0f)
|
|
c = std::min(0.0f, c + d);
|
|
};
|
|
decay(v.x, creature.movement.deceleration);
|
|
decay(v.z, creature.movement.deceleration);
|
|
}
|
|
v.y += -creature.gravity.value * dt;
|
|
auto v_clamp = [](float& c, float max) {
|
|
if (max < 0.0f) {
|
|
return; //-1 = unlimited
|
|
}
|
|
c = std::clamp(c, -max, max);
|
|
};
|
|
v_clamp(v.x, creature.velocity.max.x);
|
|
v_clamp(v.y, creature.velocity.max.y);
|
|
v_clamp(v.z, creature.velocity.max.z);
|
|
}
|
|
}
|
|
|
|
} // namespace Cubed
|