command_controller.cpp 8.9 KB

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