command_controller.cpp 8.7 KB

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