command_controller.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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::onHoldCommand() {
  78. CommandResult result;
  79. if (!m_selectionSystem || !m_world) {
  80. return result;
  81. }
  82. const auto &selected = m_selectionSystem->getSelectedUnits();
  83. if (selected.empty())
  84. return result;
  85. for (auto id : selected) {
  86. auto *entity = m_world->getEntity(id);
  87. if (!entity)
  88. continue;
  89. resetMovement(entity);
  90. entity->removeComponent<Engine::Core::AttackTargetComponent>();
  91. if (auto *patrol = entity->getComponent<Engine::Core::PatrolComponent>()) {
  92. patrol->patrolling = false;
  93. patrol->waypoints.clear();
  94. }
  95. auto *holdMode = entity->getComponent<Engine::Core::HoldModeComponent>();
  96. if (!holdMode) {
  97. holdMode = entity->addComponent<Engine::Core::HoldModeComponent>();
  98. }
  99. holdMode->active = true;
  100. auto *movement = entity->getComponent<Engine::Core::MovementComponent>();
  101. if (movement) {
  102. movement->hasTarget = false;
  103. movement->path.clear();
  104. movement->pathPending = false;
  105. movement->vx = 0.0f;
  106. movement->vz = 0.0f;
  107. }
  108. }
  109. result.inputConsumed = true;
  110. result.resetCursorToNormal = true;
  111. return result;
  112. }
  113. CommandResult CommandController::onPatrolClick(qreal sx, qreal sy,
  114. int viewportWidth,
  115. int viewportHeight,
  116. void *camera) {
  117. CommandResult result;
  118. if (!m_selectionSystem || !m_world || !m_pickingService || !camera) {
  119. if (m_hasPatrolFirstWaypoint) {
  120. clearPatrolFirstWaypoint();
  121. result.resetCursorToNormal = true;
  122. }
  123. return result;
  124. }
  125. const auto &selected = m_selectionSystem->getSelectedUnits();
  126. if (selected.empty()) {
  127. if (m_hasPatrolFirstWaypoint) {
  128. clearPatrolFirstWaypoint();
  129. result.resetCursorToNormal = true;
  130. }
  131. return result;
  132. }
  133. auto *cam = static_cast<Render::GL::Camera *>(camera);
  134. QVector3D hit;
  135. if (!m_pickingService->screenToGround(QPointF(sx, sy), *cam, viewportWidth,
  136. viewportHeight, hit)) {
  137. if (m_hasPatrolFirstWaypoint) {
  138. clearPatrolFirstWaypoint();
  139. result.resetCursorToNormal = true;
  140. }
  141. return result;
  142. }
  143. if (!m_hasPatrolFirstWaypoint) {
  144. m_hasPatrolFirstWaypoint = true;
  145. m_patrolFirstWaypoint = hit;
  146. result.inputConsumed = true;
  147. return result;
  148. }
  149. QVector3D secondWaypoint = hit;
  150. for (auto id : selected) {
  151. auto *entity = m_world->getEntity(id);
  152. if (!entity)
  153. continue;
  154. auto *building = entity->getComponent<Engine::Core::BuildingComponent>();
  155. if (building)
  156. continue;
  157. auto *patrol = entity->getComponent<Engine::Core::PatrolComponent>();
  158. if (!patrol) {
  159. patrol = entity->addComponent<Engine::Core::PatrolComponent>();
  160. }
  161. if (patrol) {
  162. patrol->waypoints.clear();
  163. patrol->waypoints.push_back(
  164. {m_patrolFirstWaypoint.x(), m_patrolFirstWaypoint.z()});
  165. patrol->waypoints.push_back({secondWaypoint.x(), secondWaypoint.z()});
  166. patrol->currentWaypoint = 0;
  167. patrol->patrolling = true;
  168. }
  169. resetMovement(entity);
  170. entity->removeComponent<Engine::Core::AttackTargetComponent>();
  171. }
  172. clearPatrolFirstWaypoint();
  173. result.inputConsumed = true;
  174. result.resetCursorToNormal = true;
  175. return result;
  176. }
  177. CommandResult CommandController::setRallyAtScreen(qreal sx, qreal sy,
  178. int viewportWidth,
  179. int viewportHeight,
  180. void *camera,
  181. int localOwnerId) {
  182. CommandResult result;
  183. if (!m_world || !m_selectionSystem || !m_pickingService || !camera)
  184. return result;
  185. auto *cam = static_cast<Render::GL::Camera *>(camera);
  186. QVector3D hit;
  187. if (!m_pickingService->screenToGround(QPointF(sx, sy), *cam, viewportWidth,
  188. viewportHeight, hit))
  189. return result;
  190. Game::Systems::ProductionService::setRallyForFirstSelectedBarracks(
  191. *m_world, m_selectionSystem->getSelectedUnits(), localOwnerId, hit.x(),
  192. hit.z());
  193. result.inputConsumed = true;
  194. return result;
  195. }
  196. void CommandController::recruitNearSelected(const QString &unitType,
  197. int localOwnerId) {
  198. if (!m_world || !m_selectionSystem)
  199. return;
  200. const auto &sel = m_selectionSystem->getSelectedUnits();
  201. if (sel.empty())
  202. return;
  203. auto result =
  204. Game::Systems::ProductionService::startProductionForFirstSelectedBarracks(
  205. *m_world, sel, localOwnerId, unitType.toStdString());
  206. if (result == Game::Systems::ProductionResult::GlobalTroopLimitReached) {
  207. emit troopLimitReached();
  208. }
  209. }
  210. void CommandController::resetMovement(Engine::Core::Entity *entity) {
  211. App::Utils::resetMovement(entity);
  212. }
  213. } // namespace App::Controllers