From 9bb1882d9244242110dbe1b235696db67990f945 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 2 Aug 2026 16:14:56 +0800 Subject: [PATCH] fix: correct velocity clamping and hitbox loading Preserve sign when clamping velocity; negative velocities now clamp to zero instead of flipping direction. Support loading hitbox definitions from JSON arrays and objects. Tune pig entity movement, spawn height, and wander probabilities. --- src/gameplay/hitbox_manager.cpp | 20 ++++++++++++-------- src/gameplay/server_entity_manager.cpp | 3 +++ src/gameplay/server_world.cpp | 2 +- src/gameplay/systems/speed_system.cpp | 13 ++++++++----- src/gameplay/systems/wander_ai_system.cpp | 4 ++-- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/gameplay/hitbox_manager.cpp b/src/gameplay/hitbox_manager.cpp index f12a10d..ba29357 100644 --- a/src/gameplay/hitbox_manager.cpp +++ b/src/gameplay/hitbox_manager.cpp @@ -86,15 +86,19 @@ HitboxManager::Handle HitboxManager::load(std::string_view name) { std::ifstream s{p}; json j = json::parse(s); if (j.contains("boxes")) { - if (j["boxes"].contains("center")) { - center.x = j["boxes"]["center"].at(0).get(); - center.y = j["boxes"]["center"].at(1).get(); - center.z = j["boxes"]["center"].at(2).get(); + const json* box = &j["boxes"]; + if (box->is_array() && !box->empty()) { + box = &(*box)[0]; } - if (j["boxes"].contains("half")) { - half.x = j["boxes"]["half"].at(0).get(); - half.y = j["boxes"]["half"].at(1).get(); - half.z = j["boxes"]["half"].at(2).get(); + if (box->contains("center")) { + center.x = (*box)["center"].at(0).get(); + center.y = (*box)["center"].at(1).get(); + center.z = (*box)["center"].at(2).get(); + } + if (box->contains("half")) { + half.x = (*box)["half"].at(0).get(); + half.y = (*box)["half"].at(1).get(); + half.z = (*box)["half"].at(2).get(); } } { diff --git a/src/gameplay/server_entity_manager.cpp b/src/gameplay/server_entity_manager.cpp index 209ae30..6f6930d 100644 --- a/src/gameplay/server_entity_manager.cpp +++ b/src/gameplay/server_entity_manager.cpp @@ -20,6 +20,9 @@ void ServerEntityManager::init() { m_factories.try_emplace("cubed:pig", [this]() { BaseServerCreature c; c.hitbox = HitboxManager::instance().get_hitbox_id("cubed:pig"); + c.gravity.value = 1.0f; + c.movement.acceleration = 1.0f; + c.movement.deceleration = 1.0f; return create_entity_in_factory( Entity{m_next}, EntityInfo{"cubed:pig", ""}, std::move(c), PigTag{}, AIBase{}, WanderAITag{}, MoveBoost{}); diff --git a/src/gameplay/server_world.cpp b/src/gameplay/server_world.cpp index 13e02ba..1e18407 100644 --- a/src/gameplay/server_world.cpp +++ b/src/gameplay/server_world.cpp @@ -179,7 +179,7 @@ void ServerWorld::send_chunk(int task_id, const std::string& uuid, void ServerWorld::init_world() { m_entity_manager.init(); - m_entity_manager.add_entity("cubed:pig", {0, 90, 0}); + m_entity_manager.add_entity("cubed:pig", {0, 225, 0}); register_timer("player disconnect", 5, [this]() { std::vector disconnect; { diff --git a/src/gameplay/systems/speed_system.cpp b/src/gameplay/systems/speed_system.cpp index ae7c202..119c485 100644 --- a/src/gameplay/systems/speed_system.cpp +++ b/src/gameplay/systems/speed_system.cpp @@ -23,11 +23,14 @@ void SpeedSystem::update(float dt, entt::registry& registry) { } creature.velocity.value.y += -creature.gravity.value * dt; auto v_clamp = [](float& v, float max) { - auto temp = std::abs(v); - max = std::abs(max); - temp = std::clamp(temp, 0.0f, max); - - v = temp; + if (max < 0.0f) { + return; + } + auto sign = v < 0.0f ? -1.0f : 1.0f; + v = sign * std::clamp(std::abs(v), 0.0f, max); + if (v < 0.0f) { + v = 0.0f; + } }; v_clamp(creature.velocity.value.x, creature.velocity.max.x); v_clamp(creature.velocity.value.y, creature.velocity.max.y); diff --git a/src/gameplay/systems/wander_ai_system.cpp b/src/gameplay/systems/wander_ai_system.cpp index 31e75d0..8ff646d 100644 --- a/src/gameplay/systems/wander_ai_system.cpp +++ b/src/gameplay/systems/wander_ai_system.cpp @@ -5,8 +5,8 @@ #include "Cubed/tools/cubed_random.hpp" namespace { -constexpr double DIRECTION_PROBABILITY = 0.1; -constexpr double MOVE_PROBABILITY = 0.1; +constexpr double DIRECTION_PROBABILITY = 0.01; +constexpr double MOVE_PROBABILITY = 0.01; } // namespace namespace Cubed {