selection_query_service.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "selection_query_service.h"
  2. #include "game/core/component.h"
  3. #include "game/core/world.h"
  4. #include "game/systems/selection_system.h"
  5. #include "game/units/spawn_type.h"
  6. #include "game/units/troop_config.h"
  7. SelectionQueryService::SelectionQueryService(Engine::Core::World *world,
  8. QObject *parent)
  9. : QObject(parent), m_world(world) {}
  10. auto SelectionQueryService::get_selected_units_command_mode() const -> QString {
  11. if (!m_world) {
  12. return "normal";
  13. }
  14. auto *selection_system =
  15. m_world->get_system<Game::Systems::SelectionSystem>();
  16. if (!selection_system) {
  17. return "normal";
  18. }
  19. const auto &sel = selection_system->get_selected_units();
  20. if (sel.empty()) {
  21. return "normal";
  22. }
  23. int attacking_count = 0;
  24. int patrolling_count = 0;
  25. int guarding_count = 0;
  26. int total_units = 0;
  27. for (auto id : sel) {
  28. auto *e = m_world->get_entity(id);
  29. if (!e) {
  30. continue;
  31. }
  32. auto *u = e->get_component<Engine::Core::UnitComponent>();
  33. if (!u || u->spawn_type == Game::Units::SpawnType::Barracks) {
  34. continue;
  35. }
  36. total_units++;
  37. if (e->get_component<Engine::Core::AttackTargetComponent>()) {
  38. attacking_count++;
  39. }
  40. auto *patrol = e->get_component<Engine::Core::PatrolComponent>();
  41. if (patrol && patrol->patrolling) {
  42. patrolling_count++;
  43. }
  44. auto *guard = e->get_component<Engine::Core::GuardModeComponent>();
  45. if (guard && guard->active) {
  46. guarding_count++;
  47. }
  48. }
  49. if (total_units == 0) {
  50. return "normal";
  51. }
  52. if (patrolling_count == total_units) {
  53. return "patrol";
  54. }
  55. if (attacking_count == total_units) {
  56. return "attack";
  57. }
  58. if (guarding_count == total_units) {
  59. return "guard";
  60. }
  61. return "normal";
  62. }
  63. auto SelectionQueryService::get_selected_units_mode_availability() const
  64. -> QVariantMap {
  65. QVariantMap result;
  66. result["canAttack"] = false;
  67. result["canGuard"] = false;
  68. result["canHold"] = false;
  69. result["canPatrol"] = false;
  70. result["canHeal"] = false;
  71. result["canBuild"] = false;
  72. if (!m_world) {
  73. return result;
  74. }
  75. auto *selection_system =
  76. m_world->get_system<Game::Systems::SelectionSystem>();
  77. if (!selection_system) {
  78. return result;
  79. }
  80. const auto &sel = selection_system->get_selected_units();
  81. if (sel.empty()) {
  82. return result;
  83. }
  84. bool can_attack = false;
  85. bool can_guard = false;
  86. bool can_hold = false;
  87. bool can_patrol = false;
  88. bool can_heal = false;
  89. bool can_build = false;
  90. for (auto id : sel) {
  91. if (can_attack && can_guard && can_hold && can_patrol && can_heal &&
  92. can_build) {
  93. break;
  94. }
  95. auto *e = m_world->get_entity(id);
  96. if (!e) {
  97. continue;
  98. }
  99. auto *u = e->get_component<Engine::Core::UnitComponent>();
  100. if (!u || u->spawn_type == Game::Units::SpawnType::Barracks) {
  101. continue;
  102. }
  103. if (!can_attack && Game::Units::can_use_attack_mode(u->spawn_type)) {
  104. can_attack = true;
  105. }
  106. if (!can_guard && Game::Units::can_use_guard_mode(u->spawn_type)) {
  107. can_guard = true;
  108. }
  109. if (!can_hold && Game::Units::can_use_hold_mode(u->spawn_type)) {
  110. can_hold = true;
  111. }
  112. if (!can_patrol && Game::Units::can_use_patrol_mode(u->spawn_type)) {
  113. can_patrol = true;
  114. }
  115. if (!can_heal && u->spawn_type == Game::Units::SpawnType::Healer) {
  116. can_heal = true;
  117. }
  118. if (!can_build && u->spawn_type == Game::Units::SpawnType::Builder) {
  119. can_build = true;
  120. }
  121. }
  122. result["canAttack"] = can_attack;
  123. result["canGuard"] = can_guard;
  124. result["canHold"] = can_hold;
  125. result["canPatrol"] = can_patrol;
  126. result["canHeal"] = can_heal;
  127. result["canBuild"] = can_build;
  128. return result;
  129. }