Просмотр исходного кода

Address code review feedback: fix spear_thrust_variant, remove unused function, improve attacker_type handling, limit knockback

Co-authored-by: djeada <[email protected]>
copilot-swe-agent[bot] 1 неделя назад
Родитель
Сommit
289f0fb3a2

+ 0 - 5
game/audio/AudioEventHandler.cpp

@@ -15,11 +15,6 @@ namespace {
 
 thread_local std::mt19937 g_audio_rng(std::random_device{}());
 
-auto get_pitch_variation() -> float {
-  std::uniform_real_distribution<float> dist(0.9F, 1.1F);
-  return dist(g_audio_rng);
-}
-
 auto get_volume_variation() -> float {
   std::uniform_real_distribution<float> dist(0.85F, 1.0F);
   return dist(g_audio_rng);

+ 15 - 4
game/systems/combat_system.cpp

@@ -675,7 +675,7 @@ void CombatSystem::dealDamage(Engine::Core::World *world,
     unit->health = std::max(0, unit->health - damage);
 
     int attacker_owner_id = 0;
-    Game::Units::SpawnType attacker_type = Game::Units::SpawnType::Archer;
+    std::optional<Game::Units::SpawnType> attacker_type_opt;
     if (attackerId != 0 && (world != nullptr)) {
       auto *attacker = world->get_entity(attackerId);
       if (attacker != nullptr) {
@@ -683,11 +683,14 @@ void CombatSystem::dealDamage(Engine::Core::World *world,
             attacker->get_component<Engine::Core::UnitComponent>();
         if (attacker_unit != nullptr) {
           attacker_owner_id = attacker_unit->owner_id;
-          attacker_type = attacker_unit->spawn_type;
+          attacker_type_opt = attacker_unit->spawn_type;
         }
       }
     }
 
+    Game::Units::SpawnType const attacker_type =
+        attacker_type_opt.value_or(Game::Units::SpawnType::Knight);
+
     Engine::Core::EventManager::instance().publish(
         Engine::Core::CombatHitEvent(attackerId, target->get_id(), damage,
                                      attacker_type, is_killing_blow));
@@ -1042,8 +1045,16 @@ void CombatSystem::process_hit_feedback(Engine::Core::World *world,
       auto *transform =
           unit->get_component<Engine::Core::TransformComponent>();
       if (transform != nullptr) {
-        transform->position.x += feedback->knockback_x * fade * delta_time;
-        transform->position.z += feedback->knockback_z * fade * delta_time;
+        float const max_displacement_per_frame = 0.02F;
+        float const dx = feedback->knockback_x * fade * delta_time;
+        float const dz = feedback->knockback_z * fade * delta_time;
+        float const displacement = std::sqrt(dx * dx + dz * dz);
+        float const scale = (displacement > max_displacement_per_frame &&
+                             displacement > 0.0001F)
+                                ? max_displacement_per_frame / displacement
+                                : 1.0F;
+        transform->position.x += dx * scale;
+        transform->position.z += dz * scale;
       }
     }
   }

+ 46 - 1
render/humanoid/pose_controller.cpp

@@ -591,7 +591,52 @@ void HumanoidPoseController::spear_thrust_variant(float attack_phase,
     break;
   }
 
-  spearThrust(attack_phase);
+  QVector3D hand_r_target;
+  QVector3D hand_l_target;
+
+  auto easeInOutCubic = [](float t) {
+    return t < 0.5F ? 4.0F * t * t * t
+                    : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
+  };
+
+  auto smoothstep = [](float edge0, float edge1, float x) {
+    float t = std::clamp((x - edge0) / (edge1 - edge0), 0.0F, 1.0F);
+    return t * t * (3.0F - 2.0F * t);
+  };
+
+  auto lerp = [](float a, float b, float t) { return a * (1.0F - t) + b * t; };
+
+  if (attack_phase < 0.20F) {
+    float const t = easeInOutCubic(attack_phase / 0.20F);
+    hand_r_target = guard_pos * (1.0F - t) + prepare_pos * t;
+    hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F,
+                              0.20F * (1.0F - t) + 0.08F * t);
+  } else if (attack_phase < 0.30F) {
+    hand_r_target = prepare_pos;
+    hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F, 0.08F);
+  } else if (attack_phase < 0.50F) {
+    float t = (attack_phase - 0.30F) / 0.20F;
+    t = t * t * t;
+    hand_r_target = prepare_pos * (1.0F - t) + thrust_pos * t;
+    hand_l_target =
+        QVector3D(-0.10F + 0.05F * t, HP::SHOULDER_Y - 0.05F + 0.03F * t,
+                  0.08F + 0.45F * t);
+  } else if (attack_phase < 0.70F) {
+    float const t = easeInOutCubic((attack_phase - 0.50F) / 0.20F);
+    hand_r_target = thrust_pos * (1.0F - t) + recover_pos * t;
+    hand_l_target = QVector3D(-0.05F * (1.0F - t) - 0.10F * t,
+                              HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.06F * t,
+                              lerp(0.53F, 0.35F, t));
+  } else {
+    float const t = smoothstep(0.70F, 1.0F, attack_phase);
+    hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
+    hand_l_target =
+        QVector3D(-0.10F - 0.02F * (1.0F - t),
+                  HP::SHOULDER_Y - 0.06F + 0.01F * t, lerp(0.35F, 0.25F, t));
+  }
+
+  placeHandAt(false, hand_r_target);
+  placeHandAt(true, hand_l_target);
 }
 
 } // namespace Render::GL