command_controller.cpp 9.1 KB

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