selection_system.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "selection_system.h"
  2. #include "../../app/utils/selection_utils.h"
  3. #include "../../render/gl/camera.h"
  4. #include "../core/component.h"
  5. #include "../core/event_manager.h"
  6. #include "../core/world.h"
  7. #include "command_service.h"
  8. #include "picking_service.h"
  9. #include "units/spawn_type.h"
  10. #include <QPointF>
  11. #include <QVector3D>
  12. #include <algorithm>
  13. #include <qglobal.h>
  14. #include <qobject.h>
  15. #include <qtmetamacros.h>
  16. #include <vector>
  17. namespace Game::Systems {
  18. void SelectionSystem::update(Engine::Core::World *world, float delta_time) {}
  19. void SelectionSystem::select_unit(Engine::Core::EntityID unit_id) {
  20. auto it =
  21. std::find(m_selected_units.begin(), m_selected_units.end(), unit_id);
  22. if (it == m_selected_units.end()) {
  23. m_selected_units.push_back(unit_id);
  24. Engine::Core::EventManager::instance().publish(
  25. Engine::Core::UnitSelectedEvent(unit_id));
  26. }
  27. }
  28. void SelectionSystem::deselect_unit(Engine::Core::EntityID unit_id) {
  29. auto it =
  30. std::find(m_selected_units.begin(), m_selected_units.end(), unit_id);
  31. if (it != m_selected_units.end()) {
  32. m_selected_units.erase(it);
  33. }
  34. }
  35. void SelectionSystem::clear_selection() { m_selected_units.clear(); }
  36. void SelectionSystem::select_units_in_area(float x1, float y1, float x2,
  37. float y2) {}
  38. auto SelectionSystem::is_unit_in_area(Engine::Core::Entity *entity, float x1,
  39. float y1, float x2, float y2) -> bool {
  40. auto *transform = entity->get_component<Engine::Core::TransformComponent>();
  41. if (transform == nullptr) {
  42. return false;
  43. }
  44. float const x = transform->position.x;
  45. float const z = transform->position.z;
  46. return x >= std::min(x1, x2) && x <= std::max(x1, x2) &&
  47. z >= std::min(y1, y2) && z <= std::max(y1, y2);
  48. }
  49. SelectionController::SelectionController(Engine::Core::World *world,
  50. SelectionSystem *selection_system,
  51. PickingService *pickingService,
  52. QObject *parent)
  53. : QObject(parent), m_world(world), m_selection_system(selection_system),
  54. m_picking_service(pickingService) {}
  55. void SelectionController::on_click_select(qreal sx, qreal sy, bool additive,
  56. int viewport_width,
  57. int viewport_height, void *camera,
  58. int local_owner_id) {
  59. if ((m_selection_system == nullptr) || (m_picking_service == nullptr) ||
  60. (camera == nullptr) || (m_world == nullptr)) {
  61. return;
  62. }
  63. auto *cam = static_cast<Render::GL::Camera *>(camera);
  64. Engine::Core::EntityID const picked =
  65. Game::Systems::PickingService::pick_single(
  66. float(sx), float(sy), *m_world, *cam, viewport_width, viewport_height,
  67. local_owner_id, true);
  68. if (picked != 0U) {
  69. if (!additive) {
  70. m_selection_system->clear_selection();
  71. }
  72. m_selection_system->select_unit(picked);
  73. sync_selection_flags();
  74. emit selection_changed();
  75. return;
  76. }
  77. if (!additive && !m_selection_system->get_selected_units().empty()) {
  78. m_selection_system->clear_selection();
  79. sync_selection_flags();
  80. emit selection_changed();
  81. }
  82. }
  83. void SelectionController::on_area_selected(qreal x1, qreal y1, qreal x2,
  84. qreal y2, bool additive,
  85. int viewport_width,
  86. int viewport_height, void *camera,
  87. int local_owner_id) {
  88. if ((m_selection_system == nullptr) || (m_picking_service == nullptr) ||
  89. (camera == nullptr) || (m_world == nullptr)) {
  90. return;
  91. }
  92. if (!additive) {
  93. m_selection_system->clear_selection();
  94. }
  95. auto *cam = static_cast<Render::GL::Camera *>(camera);
  96. auto picked = Game::Systems::PickingService::pick_in_rect(
  97. float(x1), float(y1), float(x2), float(y2), *m_world, *cam,
  98. viewport_width, viewport_height, local_owner_id);
  99. for (auto id : picked) {
  100. m_selection_system->select_unit(id);
  101. }
  102. sync_selection_flags();
  103. emit selection_changed();
  104. }
  105. void SelectionController::on_right_click_clear_selection() {
  106. if (m_selection_system == nullptr) {
  107. return;
  108. }
  109. m_selection_system->clear_selection();
  110. sync_selection_flags();
  111. emit selection_changed();
  112. }
  113. void SelectionController::select_all_player_troops(int local_owner_id) {
  114. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  115. return;
  116. }
  117. m_selection_system->clear_selection();
  118. auto entities = m_world->get_entities_with<Engine::Core::UnitComponent>();
  119. for (auto *e : entities) {
  120. auto *unit = e->get_component<Engine::Core::UnitComponent>();
  121. if ((unit == nullptr) || unit->owner_id != local_owner_id) {
  122. continue;
  123. }
  124. if (e->has_component<Engine::Core::BuildingComponent>()) {
  125. continue;
  126. }
  127. if (unit->health <= 0) {
  128. continue;
  129. }
  130. m_selection_system->select_unit(e->get_id());
  131. }
  132. sync_selection_flags();
  133. emit selection_changed();
  134. }
  135. void SelectionController::select_single_unit(Engine::Core::EntityID id,
  136. int local_owner_id) {
  137. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  138. return;
  139. }
  140. auto *entity = m_world->get_entity(id);
  141. if (entity == nullptr) {
  142. return;
  143. }
  144. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  145. if ((unit == nullptr) || (unit->health <= 0) ||
  146. (unit->owner_id != local_owner_id)) {
  147. return;
  148. }
  149. m_selection_system->clear_selection();
  150. m_selection_system->select_unit(id);
  151. sync_selection_flags();
  152. emit selection_changed();
  153. }
  154. auto SelectionController::has_units_selected() const -> bool {
  155. if (m_selection_system == nullptr) {
  156. return false;
  157. }
  158. const auto &sel = m_selection_system->get_selected_units();
  159. return !sel.empty();
  160. }
  161. void SelectionController::get_selected_unit_ids(
  162. std::vector<Engine::Core::EntityID> &out) const {
  163. out.clear();
  164. if (m_selection_system == nullptr) {
  165. return;
  166. }
  167. const auto &ids = m_selection_system->get_selected_units();
  168. out.assign(ids.begin(), ids.end());
  169. }
  170. auto SelectionController::has_selected_type(const QString &type) const -> bool {
  171. if ((m_world == nullptr) || (m_selection_system == nullptr)) {
  172. return false;
  173. }
  174. const auto &sel = m_selection_system->get_selected_units();
  175. for (auto id : sel) {
  176. if (auto *e = m_world->get_entity(id)) {
  177. if (auto *u = e->get_component<Engine::Core::UnitComponent>()) {
  178. if (QString::fromStdString(
  179. Game::Units::spawn_typeToString(u->spawn_type)) == type) {
  180. return true;
  181. }
  182. }
  183. }
  184. }
  185. return false;
  186. }
  187. void SelectionController::sync_selection_flags() {
  188. if ((m_world == nullptr) || (m_selection_system == nullptr)) {
  189. return;
  190. }
  191. App::Utils::sanitize_selection(m_world, m_selection_system);
  192. }
  193. } // namespace Game::Systems