combat_mode_processor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "combat_mode_processor.h"
  2. #include "../../core/component.h"
  3. #include "../../core/world.h"
  4. #include "../owner_registry.h"
  5. #include <cmath>
  6. #include <limits>
  7. namespace Game::Systems::Combat {
  8. void update_combat_mode(Engine::Core::Entity *attacker,
  9. Engine::Core::World *world,
  10. Engine::Core::AttackComponent *attack_comp) {
  11. if (attack_comp == nullptr) {
  12. return;
  13. }
  14. if (attack_comp->preferred_mode !=
  15. Engine::Core::AttackComponent::CombatMode::Auto) {
  16. attack_comp->current_mode = attack_comp->preferred_mode;
  17. return;
  18. }
  19. bool const in_melee_combat = attack_comp->in_melee_lock;
  20. bool const has_attack_target =
  21. attacker->has_component<Engine::Core::AttackTargetComponent>();
  22. if (!in_melee_combat && !has_attack_target) {
  23. if (attack_comp->can_ranged) {
  24. attack_comp->current_mode =
  25. Engine::Core::AttackComponent::CombatMode::Ranged;
  26. } else {
  27. attack_comp->current_mode =
  28. Engine::Core::AttackComponent::CombatMode::Melee;
  29. }
  30. return;
  31. }
  32. auto *attacker_transform =
  33. attacker->get_component<Engine::Core::TransformComponent>();
  34. if (attacker_transform == nullptr) {
  35. return;
  36. }
  37. auto *attacker_unit = attacker->get_component<Engine::Core::UnitComponent>();
  38. if (attacker_unit == nullptr) {
  39. return;
  40. }
  41. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  42. auto units = world->get_entities_with<Engine::Core::UnitComponent>();
  43. float closest_enemy_dist_sq = std::numeric_limits<float>::max();
  44. float closest_height_diff = 0.0F;
  45. for (auto *target : units) {
  46. if (target == attacker) {
  47. continue;
  48. }
  49. auto *target_unit = target->get_component<Engine::Core::UnitComponent>();
  50. if ((target_unit == nullptr) || target_unit->health <= 0) {
  51. continue;
  52. }
  53. if (owner_registry.are_allies(attacker_unit->owner_id,
  54. target_unit->owner_id)) {
  55. continue;
  56. }
  57. if (target->has_component<Engine::Core::BuildingComponent>()) {
  58. continue;
  59. }
  60. auto *target_transform =
  61. target->get_component<Engine::Core::TransformComponent>();
  62. if (target_transform == nullptr) {
  63. continue;
  64. }
  65. float const dx =
  66. target_transform->position.x - attacker_transform->position.x;
  67. float const dz =
  68. target_transform->position.z - attacker_transform->position.z;
  69. float const dy =
  70. target_transform->position.y - attacker_transform->position.y;
  71. float const dist_sq = dx * dx + dz * dz;
  72. if (dist_sq < closest_enemy_dist_sq) {
  73. closest_enemy_dist_sq = dist_sq;
  74. closest_height_diff = std::abs(dy);
  75. }
  76. }
  77. if (closest_enemy_dist_sq == std::numeric_limits<float>::max()) {
  78. if (attack_comp->can_ranged) {
  79. attack_comp->current_mode =
  80. Engine::Core::AttackComponent::CombatMode::Ranged;
  81. } else {
  82. attack_comp->current_mode =
  83. Engine::Core::AttackComponent::CombatMode::Melee;
  84. }
  85. return;
  86. }
  87. float const closest_dist = std::sqrt(closest_enemy_dist_sq);
  88. bool const in_melee_range =
  89. attack_comp->is_in_melee_range(closest_dist, closest_height_diff);
  90. bool const in_ranged_range = attack_comp->is_in_ranged_range(closest_dist);
  91. if (in_melee_range && attack_comp->can_melee) {
  92. attack_comp->current_mode =
  93. Engine::Core::AttackComponent::CombatMode::Melee;
  94. } else if (in_ranged_range && attack_comp->can_ranged) {
  95. attack_comp->current_mode =
  96. Engine::Core::AttackComponent::CombatMode::Ranged;
  97. } else if (attack_comp->can_ranged) {
  98. attack_comp->current_mode =
  99. Engine::Core::AttackComponent::CombatMode::Ranged;
  100. } else {
  101. attack_comp->current_mode =
  102. Engine::Core::AttackComponent::CombatMode::Melee;
  103. }
  104. }
  105. } // namespace Game::Systems::Combat