command_controller.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "units/spawn_type.h"
  12. #include <QPointF>
  13. #include <qglobal.h>
  14. #include <qobject.h>
  15. #include <qtmetamacros.h>
  16. #include <qvectornd.h>
  17. namespace App::Controllers {
  18. CommandController::CommandController(
  19. Engine::Core::World *world,
  20. Game::Systems::SelectionSystem *selection_system,
  21. Game::Systems::PickingService *pickingService, QObject *parent)
  22. : QObject(parent), m_world(world), m_selection_system(selection_system),
  23. m_pickingService(pickingService) {}
  24. auto CommandController::onAttackClick(qreal sx, qreal sy, int viewportWidth,
  25. int viewportHeight,
  26. void *camera) -> CommandResult {
  27. CommandResult result;
  28. if ((m_selection_system == nullptr) || (m_pickingService == nullptr) ||
  29. (camera == nullptr) || (m_world == nullptr)) {
  30. result.resetCursorToNormal = true;
  31. return result;
  32. }
  33. const auto &selected = m_selection_system->getSelectedUnits();
  34. if (selected.empty()) {
  35. result.resetCursorToNormal = true;
  36. return result;
  37. }
  38. auto *cam = static_cast<Render::GL::Camera *>(camera);
  39. Engine::Core::EntityID const target_id = m_pickingService->pickUnitFirst(
  40. float(sx), float(sy), *m_world, *cam, viewportWidth, viewportHeight, 0);
  41. if (target_id == 0) {
  42. result.resetCursorToNormal = true;
  43. return result;
  44. }
  45. auto *target_entity = m_world->getEntity(target_id);
  46. if (target_entity == nullptr) {
  47. return result;
  48. }
  49. auto *target_unit =
  50. target_entity->getComponent<Engine::Core::UnitComponent>();
  51. if (target_unit == nullptr) {
  52. return result;
  53. }
  54. Game::Systems::CommandService::attack_target(*m_world, selected, target_id,
  55. true);
  56. emit attack_targetSelected();
  57. result.inputConsumed = true;
  58. result.resetCursorToNormal = true;
  59. return result;
  60. }
  61. auto CommandController::onStopCommand() -> CommandResult {
  62. CommandResult result;
  63. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  64. return result;
  65. }
  66. const auto &selected = m_selection_system->getSelectedUnits();
  67. if (selected.empty()) {
  68. return result;
  69. }
  70. for (auto id : selected) {
  71. auto *entity = m_world->getEntity(id);
  72. if (entity == nullptr) {
  73. continue;
  74. }
  75. resetMovement(entity);
  76. entity->removeComponent<Engine::Core::AttackTargetComponent>();
  77. if (auto *patrol = entity->getComponent<Engine::Core::PatrolComponent>()) {
  78. patrol->patrolling = false;
  79. patrol->waypoints.clear();
  80. }
  81. auto *hold_mode = entity->getComponent<Engine::Core::HoldModeComponent>();
  82. if ((hold_mode != nullptr) && hold_mode->active) {
  83. hold_mode->active = false;
  84. hold_mode->exitCooldown = hold_mode->standUpDuration;
  85. emit hold_modeChanged(false);
  86. }
  87. }
  88. result.inputConsumed = true;
  89. result.resetCursorToNormal = true;
  90. return result;
  91. }
  92. auto CommandController::onHoldCommand() -> CommandResult {
  93. CommandResult result;
  94. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  95. return result;
  96. }
  97. const auto &selected = m_selection_system->getSelectedUnits();
  98. if (selected.empty()) {
  99. return result;
  100. }
  101. for (auto id : selected) {
  102. auto *entity = m_world->getEntity(id);
  103. if (entity == nullptr) {
  104. continue;
  105. }
  106. auto *unit = entity->getComponent<Engine::Core::UnitComponent>();
  107. if ((unit == nullptr) ||
  108. (unit->spawn_type != Game::Units::SpawnType::Archer &&
  109. unit->spawn_type != Game::Units::SpawnType::Spearman)) {
  110. continue;
  111. }
  112. auto *hold_mode = entity->getComponent<Engine::Core::HoldModeComponent>();
  113. if ((hold_mode != nullptr) && hold_mode->active) {
  114. hold_mode->active = false;
  115. hold_mode->exitCooldown = hold_mode->standUpDuration;
  116. emit hold_modeChanged(false);
  117. continue;
  118. }
  119. resetMovement(entity);
  120. entity->removeComponent<Engine::Core::AttackTargetComponent>();
  121. if (auto *patrol = entity->getComponent<Engine::Core::PatrolComponent>()) {
  122. patrol->patrolling = false;
  123. patrol->waypoints.clear();
  124. }
  125. if (hold_mode == nullptr) {
  126. hold_mode = entity->addComponent<Engine::Core::HoldModeComponent>();
  127. }
  128. hold_mode->active = true;
  129. hold_mode->exitCooldown = 0.0F;
  130. emit hold_modeChanged(true);
  131. auto *movement = entity->getComponent<Engine::Core::MovementComponent>();
  132. if (movement != nullptr) {
  133. movement->hasTarget = false;
  134. movement->path.clear();
  135. movement->pathPending = false;
  136. movement->vx = 0.0F;
  137. movement->vz = 0.0F;
  138. }
  139. }
  140. result.inputConsumed = true;
  141. result.resetCursorToNormal = true;
  142. return result;
  143. }
  144. auto CommandController::onPatrolClick(qreal sx, qreal sy, int viewportWidth,
  145. int viewportHeight,
  146. void *camera) -> CommandResult {
  147. CommandResult result;
  148. if ((m_selection_system == nullptr) || (m_world == nullptr) ||
  149. (m_pickingService == nullptr) || (camera == nullptr)) {
  150. if (m_hasPatrolFirstWaypoint) {
  151. clearPatrolFirstWaypoint();
  152. result.resetCursorToNormal = true;
  153. }
  154. return result;
  155. }
  156. const auto &selected = m_selection_system->getSelectedUnits();
  157. if (selected.empty()) {
  158. if (m_hasPatrolFirstWaypoint) {
  159. clearPatrolFirstWaypoint();
  160. result.resetCursorToNormal = true;
  161. }
  162. return result;
  163. }
  164. auto *cam = static_cast<Render::GL::Camera *>(camera);
  165. QVector3D hit;
  166. if (!Game::Systems::PickingService::screenToGround(
  167. QPointF(sx, sy), *cam, viewportWidth, viewportHeight, hit)) {
  168. if (m_hasPatrolFirstWaypoint) {
  169. clearPatrolFirstWaypoint();
  170. result.resetCursorToNormal = true;
  171. }
  172. return result;
  173. }
  174. if (!m_hasPatrolFirstWaypoint) {
  175. m_hasPatrolFirstWaypoint = true;
  176. m_patrolFirstWaypoint = hit;
  177. result.inputConsumed = true;
  178. return result;
  179. }
  180. QVector3D const second_waypoint = hit;
  181. for (auto id : selected) {
  182. auto *entity = m_world->getEntity(id);
  183. if (entity == nullptr) {
  184. continue;
  185. }
  186. auto *building = entity->getComponent<Engine::Core::BuildingComponent>();
  187. if (building != nullptr) {
  188. continue;
  189. }
  190. auto *patrol = entity->getComponent<Engine::Core::PatrolComponent>();
  191. if (patrol == nullptr) {
  192. patrol = entity->addComponent<Engine::Core::PatrolComponent>();
  193. }
  194. if (patrol != nullptr) {
  195. patrol->waypoints.clear();
  196. patrol->waypoints.emplace_back(m_patrolFirstWaypoint.x(),
  197. m_patrolFirstWaypoint.z());
  198. patrol->waypoints.emplace_back(second_waypoint.x(), second_waypoint.z());
  199. patrol->currentWaypoint = 0;
  200. patrol->patrolling = true;
  201. }
  202. resetMovement(entity);
  203. entity->removeComponent<Engine::Core::AttackTargetComponent>();
  204. }
  205. clearPatrolFirstWaypoint();
  206. result.inputConsumed = true;
  207. result.resetCursorToNormal = true;
  208. return result;
  209. }
  210. auto CommandController::setRallyAtScreen(qreal sx, qreal sy, int viewportWidth,
  211. int viewportHeight, void *camera,
  212. int localOwnerId) -> CommandResult {
  213. CommandResult result;
  214. if ((m_world == nullptr) || (m_selection_system == nullptr) ||
  215. (m_pickingService == nullptr) || (camera == nullptr)) {
  216. return result;
  217. }
  218. auto *cam = static_cast<Render::GL::Camera *>(camera);
  219. QVector3D hit;
  220. if (!Game::Systems::PickingService::screenToGround(
  221. QPointF(sx, sy), *cam, viewportWidth, viewportHeight, hit)) {
  222. return result;
  223. }
  224. Game::Systems::ProductionService::setRallyForFirstSelectedBarracks(
  225. *m_world, m_selection_system->getSelectedUnits(), localOwnerId, hit.x(),
  226. hit.z());
  227. result.inputConsumed = true;
  228. return result;
  229. }
  230. void CommandController::recruitNearSelected(const QString &unit_type,
  231. int localOwnerId) {
  232. if ((m_world == nullptr) || (m_selection_system == nullptr)) {
  233. return;
  234. }
  235. const auto &sel = m_selection_system->getSelectedUnits();
  236. if (sel.empty()) {
  237. return;
  238. }
  239. auto result =
  240. Game::Systems::ProductionService::startProductionForFirstSelectedBarracks(
  241. *m_world, sel, localOwnerId, unit_type.toStdString());
  242. if (result == Game::Systems::ProductionResult::GlobalTroopLimitReached) {
  243. emit troopLimitReached();
  244. }
  245. }
  246. void CommandController::resetMovement(Engine::Core::Entity *entity) {
  247. App::Utils::resetMovement(entity);
  248. }
  249. auto CommandController::anySelectedInHoldMode() const -> bool {
  250. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  251. return false;
  252. }
  253. const auto &selected = m_selection_system->getSelectedUnits();
  254. for (Engine::Core::EntityID const entity_id : selected) {
  255. Engine::Core::Entity *entity = m_world->getEntity(entity_id);
  256. if (entity == nullptr) {
  257. continue;
  258. }
  259. auto *hold_mode = entity->getComponent<Engine::Core::HoldModeComponent>();
  260. if ((hold_mode != nullptr) && hold_mode->active) {
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. } // namespace App::Controllers