command_controller.cpp 8.8 KB

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