command_controller.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "command_controller.h"
  2. #include "../../game/core/component.h"
  3. #include "../../game/core/entity.h"
  4. #include "../../game/core/world.h"
  5. #include "../../game/systems/command_service.h"
  6. #include "../../game/systems/picking_service.h"
  7. #include "../../game/systems/production_service.h"
  8. #include "../../game/systems/selection_system.h"
  9. #include "../../render/gl/camera.h"
  10. #include "../utils/movement_utils.h"
  11. #include <QPointF>
  12. namespace App::Controllers {
  13. CommandController::CommandController(
  14. Engine::Core::World *world, Game::Systems::SelectionSystem *selectionSystem,
  15. Game::Systems::PickingService *pickingService, QObject *parent)
  16. : QObject(parent), m_world(world), m_selectionSystem(selectionSystem),
  17. m_pickingService(pickingService) {}
  18. CommandResult CommandController::onAttackClick(qreal sx, qreal sy,
  19. int viewportWidth,
  20. int viewportHeight,
  21. void *camera) {
  22. CommandResult result;
  23. if (!m_selectionSystem || !m_pickingService || !camera || !m_world) {
  24. result.resetCursorToNormal = true;
  25. return result;
  26. }
  27. const auto &selected = m_selectionSystem->getSelectedUnits();
  28. if (selected.empty()) {
  29. result.resetCursorToNormal = true;
  30. return result;
  31. }
  32. auto *cam = static_cast<Render::GL::Camera *>(camera);
  33. Engine::Core::EntityID targetId = m_pickingService->pickUnitFirst(
  34. float(sx), float(sy), *m_world, *cam, viewportWidth, viewportHeight, 0);
  35. if (targetId == 0) {
  36. result.resetCursorToNormal = true;
  37. return result;
  38. }
  39. auto *targetEntity = m_world->getEntity(targetId);
  40. if (!targetEntity) {
  41. return result;
  42. }
  43. auto *targetUnit = targetEntity->getComponent<Engine::Core::UnitComponent>();
  44. if (!targetUnit) {
  45. return result;
  46. }
  47. Game::Systems::CommandService::attackTarget(*m_world, selected, targetId,
  48. true);
  49. emit attackTargetSelected();
  50. result.inputConsumed = true;
  51. result.resetCursorToNormal = true;
  52. return result;
  53. }
  54. CommandResult CommandController::onStopCommand() {
  55. CommandResult result;
  56. if (!m_selectionSystem || !m_world) {
  57. return result;
  58. }
  59. const auto &selected = m_selectionSystem->getSelectedUnits();
  60. if (selected.empty())
  61. return result;
  62. for (auto id : selected) {
  63. auto *entity = m_world->getEntity(id);
  64. if (!entity)
  65. continue;
  66. resetMovement(entity);
  67. entity->removeComponent<Engine::Core::AttackTargetComponent>();
  68. if (auto *patrol = entity->getComponent<Engine::Core::PatrolComponent>()) {
  69. patrol->patrolling = false;
  70. patrol->waypoints.clear();
  71. }
  72. }
  73. result.inputConsumed = true;
  74. result.resetCursorToNormal = true;
  75. return result;
  76. }
  77. CommandResult CommandController::onPatrolClick(qreal sx, qreal sy,
  78. int viewportWidth,
  79. int viewportHeight,
  80. void *camera) {
  81. CommandResult result;
  82. if (!m_selectionSystem || !m_world || !m_pickingService || !camera) {
  83. if (m_hasPatrolFirstWaypoint) {
  84. clearPatrolFirstWaypoint();
  85. result.resetCursorToNormal = true;
  86. }
  87. return result;
  88. }
  89. const auto &selected = m_selectionSystem->getSelectedUnits();
  90. if (selected.empty()) {
  91. if (m_hasPatrolFirstWaypoint) {
  92. clearPatrolFirstWaypoint();
  93. result.resetCursorToNormal = true;
  94. }
  95. return result;
  96. }
  97. auto *cam = static_cast<Render::GL::Camera *>(camera);
  98. QVector3D hit;
  99. if (!m_pickingService->screenToGround(QPointF(sx, sy), *cam, viewportWidth,
  100. viewportHeight, hit)) {
  101. if (m_hasPatrolFirstWaypoint) {
  102. clearPatrolFirstWaypoint();
  103. result.resetCursorToNormal = true;
  104. }
  105. return result;
  106. }
  107. if (!m_hasPatrolFirstWaypoint) {
  108. m_hasPatrolFirstWaypoint = true;
  109. m_patrolFirstWaypoint = hit;
  110. result.inputConsumed = true;
  111. return result;
  112. }
  113. QVector3D secondWaypoint = hit;
  114. for (auto id : selected) {
  115. auto *entity = m_world->getEntity(id);
  116. if (!entity)
  117. continue;
  118. auto *building = entity->getComponent<Engine::Core::BuildingComponent>();
  119. if (building)
  120. continue;
  121. auto *patrol = entity->getComponent<Engine::Core::PatrolComponent>();
  122. if (!patrol) {
  123. patrol = entity->addComponent<Engine::Core::PatrolComponent>();
  124. }
  125. if (patrol) {
  126. patrol->waypoints.clear();
  127. patrol->waypoints.push_back(
  128. {m_patrolFirstWaypoint.x(), m_patrolFirstWaypoint.z()});
  129. patrol->waypoints.push_back({secondWaypoint.x(), secondWaypoint.z()});
  130. patrol->currentWaypoint = 0;
  131. patrol->patrolling = true;
  132. }
  133. resetMovement(entity);
  134. entity->removeComponent<Engine::Core::AttackTargetComponent>();
  135. }
  136. clearPatrolFirstWaypoint();
  137. result.inputConsumed = true;
  138. result.resetCursorToNormal = true;
  139. return result;
  140. }
  141. CommandResult CommandController::setRallyAtScreen(qreal sx, qreal sy,
  142. int viewportWidth,
  143. int viewportHeight,
  144. void *camera,
  145. int localOwnerId) {
  146. CommandResult result;
  147. if (!m_world || !m_selectionSystem || !m_pickingService || !camera)
  148. return result;
  149. auto *cam = static_cast<Render::GL::Camera *>(camera);
  150. QVector3D hit;
  151. if (!m_pickingService->screenToGround(QPointF(sx, sy), *cam, viewportWidth,
  152. viewportHeight, hit))
  153. return result;
  154. Game::Systems::ProductionService::setRallyForFirstSelectedBarracks(
  155. *m_world, m_selectionSystem->getSelectedUnits(), localOwnerId, hit.x(),
  156. hit.z());
  157. result.inputConsumed = true;
  158. return result;
  159. }
  160. void CommandController::recruitNearSelected(const QString &unitType,
  161. int localOwnerId) {
  162. if (!m_world || !m_selectionSystem)
  163. return;
  164. const auto &sel = m_selectionSystem->getSelectedUnits();
  165. if (sel.empty())
  166. return;
  167. auto result =
  168. Game::Systems::ProductionService::startProductionForFirstSelectedBarracks(
  169. *m_world, sel, localOwnerId, unitType.toStdString());
  170. if (result == Game::Systems::ProductionResult::GlobalTroopLimitReached) {
  171. emit troopLimitReached();
  172. }
  173. }
  174. void CommandController::resetMovement(Engine::Core::Entity *entity) {
  175. App::Utils::resetMovement(entity);
  176. }
  177. } // namespace App::Controllers