selection_system.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 deltaTime) {}
  19. void SelectionSystem::selectUnit(Engine::Core::EntityID unit_id) {
  20. auto it = std::find(m_selectedUnits.begin(), m_selectedUnits.end(), unit_id);
  21. if (it == m_selectedUnits.end()) {
  22. m_selectedUnits.push_back(unit_id);
  23. Engine::Core::EventManager::instance().publish(
  24. Engine::Core::UnitSelectedEvent(unit_id));
  25. }
  26. }
  27. void SelectionSystem::deselectUnit(Engine::Core::EntityID unit_id) {
  28. auto it = std::find(m_selectedUnits.begin(), m_selectedUnits.end(), unit_id);
  29. if (it != m_selectedUnits.end()) {
  30. m_selectedUnits.erase(it);
  31. }
  32. }
  33. void SelectionSystem::clearSelection() { m_selectedUnits.clear(); }
  34. void SelectionSystem::selectUnitsInArea(float x1, float y1, float x2,
  35. float y2) {}
  36. auto SelectionSystem::isUnitInArea(Engine::Core::Entity *entity, float x1,
  37. float y1, float x2, float y2) -> bool {
  38. auto *transform = entity->getComponent<Engine::Core::TransformComponent>();
  39. if (transform == nullptr) {
  40. return false;
  41. }
  42. float const x = transform->position.x;
  43. float const z = transform->position.z;
  44. return x >= std::min(x1, x2) && x <= std::max(x1, x2) &&
  45. z >= std::min(y1, y2) && z <= std::max(y1, y2);
  46. }
  47. SelectionController::SelectionController(Engine::Core::World *world,
  48. SelectionSystem *selection_system,
  49. PickingService *pickingService,
  50. QObject *parent)
  51. : QObject(parent), m_world(world), m_selection_system(selection_system),
  52. m_pickingService(pickingService) {}
  53. void SelectionController::onClickSelect(qreal sx, qreal sy, bool additive,
  54. int viewportWidth, int viewportHeight,
  55. void *camera, int localOwnerId) {
  56. if ((m_selection_system == nullptr) || (m_pickingService == nullptr) ||
  57. (camera == nullptr) || (m_world == nullptr)) {
  58. return;
  59. }
  60. auto *cam = static_cast<Render::GL::Camera *>(camera);
  61. Engine::Core::EntityID const picked =
  62. Game::Systems::PickingService::pickSingle(
  63. float(sx), float(sy), *m_world, *cam, viewportWidth, viewportHeight,
  64. localOwnerId, true);
  65. if (picked != 0U) {
  66. if (!additive) {
  67. m_selection_system->clearSelection();
  68. }
  69. m_selection_system->selectUnit(picked);
  70. syncSelectionFlags();
  71. emit selectionChanged();
  72. return;
  73. }
  74. if (!additive && !m_selection_system->getSelectedUnits().empty()) {
  75. m_selection_system->clearSelection();
  76. syncSelectionFlags();
  77. emit selectionChanged();
  78. }
  79. }
  80. void SelectionController::onAreaSelected(qreal x1, qreal y1, qreal x2, qreal y2,
  81. bool additive, int viewportWidth,
  82. int viewportHeight, void *camera,
  83. int localOwnerId) {
  84. if ((m_selection_system == nullptr) || (m_pickingService == nullptr) ||
  85. (camera == nullptr) || (m_world == nullptr)) {
  86. return;
  87. }
  88. if (!additive) {
  89. m_selection_system->clearSelection();
  90. }
  91. auto *cam = static_cast<Render::GL::Camera *>(camera);
  92. auto picked = Game::Systems::PickingService::pickInRect(
  93. float(x1), float(y1), float(x2), float(y2), *m_world, *cam, viewportWidth,
  94. viewportHeight, localOwnerId);
  95. for (auto id : picked) {
  96. m_selection_system->selectUnit(id);
  97. }
  98. syncSelectionFlags();
  99. emit selectionChanged();
  100. }
  101. void SelectionController::onRightClickClearSelection() {
  102. if (m_selection_system == nullptr) {
  103. return;
  104. }
  105. m_selection_system->clearSelection();
  106. syncSelectionFlags();
  107. emit selectionChanged();
  108. }
  109. void SelectionController::selectAllPlayerTroops(int localOwnerId) {
  110. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  111. return;
  112. }
  113. m_selection_system->clearSelection();
  114. auto entities = m_world->getEntitiesWith<Engine::Core::UnitComponent>();
  115. for (auto *e : entities) {
  116. auto *unit = e->getComponent<Engine::Core::UnitComponent>();
  117. if ((unit == nullptr) || unit->owner_id != localOwnerId) {
  118. continue;
  119. }
  120. if (e->hasComponent<Engine::Core::BuildingComponent>()) {
  121. continue;
  122. }
  123. if (unit->health <= 0) {
  124. continue;
  125. }
  126. m_selection_system->selectUnit(e->getId());
  127. }
  128. syncSelectionFlags();
  129. emit selectionChanged();
  130. }
  131. auto SelectionController::hasUnitsSelected() const -> bool {
  132. if (m_selection_system == nullptr) {
  133. return false;
  134. }
  135. const auto &sel = m_selection_system->getSelectedUnits();
  136. return !sel.empty();
  137. }
  138. void SelectionController::getSelectedUnitIds(
  139. std::vector<Engine::Core::EntityID> &out) const {
  140. out.clear();
  141. if (m_selection_system == nullptr) {
  142. return;
  143. }
  144. const auto &ids = m_selection_system->getSelectedUnits();
  145. out.assign(ids.begin(), ids.end());
  146. }
  147. auto SelectionController::hasSelectedType(const QString &type) const -> bool {
  148. if ((m_world == nullptr) || (m_selection_system == nullptr)) {
  149. return false;
  150. }
  151. const auto &sel = m_selection_system->getSelectedUnits();
  152. for (auto id : sel) {
  153. if (auto *e = m_world->getEntity(id)) {
  154. if (auto *u = e->getComponent<Engine::Core::UnitComponent>()) {
  155. if (QString::fromStdString(
  156. Game::Units::spawn_typeToString(u->spawn_type)) == type) {
  157. return true;
  158. }
  159. }
  160. }
  161. }
  162. return false;
  163. }
  164. void SelectionController::syncSelectionFlags() {
  165. if ((m_world == nullptr) || (m_selection_system == nullptr)) {
  166. return;
  167. }
  168. App::Utils::sanitizeSelection(m_world, m_selection_system);
  169. }
  170. } // namespace Game::Systems