command_controller.cpp 9.3 KB

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