input_command_handler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "input_command_handler.h"
  2. #include "../controllers/action_vfx.h"
  3. #include "../controllers/command_controller.h"
  4. #include "../models/cursor_manager.h"
  5. #include "../models/cursor_mode.h"
  6. #include "../models/hover_tracker.h"
  7. #include "../utils/movement_utils.h"
  8. #include "game/core/component.h"
  9. #include "game/core/world.h"
  10. #include "game/game_config.h"
  11. #include "game/systems/command_service.h"
  12. #include "game/systems/formation_planner.h"
  13. #include "game/systems/picking_service.h"
  14. #include "game/systems/selection_system.h"
  15. #include "render/gl/camera.h"
  16. InputCommandHandler::InputCommandHandler(
  17. Engine::Core::World *world,
  18. Game::Systems::SelectionController *selection_controller,
  19. App::Controllers::CommandController *command_controller,
  20. CursorManager *cursor_manager, HoverTracker *hover_tracker,
  21. Game::Systems::PickingService *picking_service, Render::GL::Camera *camera)
  22. : m_world(world), m_selection_controller(selection_controller),
  23. m_command_controller(command_controller),
  24. m_cursor_manager(cursor_manager), m_hover_tracker(hover_tracker),
  25. m_picking_service(picking_service), m_camera(camera) {}
  26. void InputCommandHandler::on_map_clicked(qreal sx, qreal sy, int local_owner_id,
  27. const ViewportState &viewport) {
  28. if (m_is_spectator_mode) {
  29. return;
  30. }
  31. if (m_selection_controller && m_camera) {
  32. m_selection_controller->on_click_select(sx, sy, false, viewport.width,
  33. viewport.height, m_camera,
  34. local_owner_id);
  35. }
  36. }
  37. namespace {
  38. void handle_move_command(Engine::Core::World *world,
  39. const std::vector<Engine::Core::EntityID> &selected,
  40. Game::Systems::PickingService *picking_service,
  41. Render::GL::Camera *camera, qreal sx, qreal sy,
  42. int local_owner_id, const ViewportState &viewport) {
  43. if (!picking_service || !camera || !world) {
  44. return;
  45. }
  46. Engine::Core::EntityID const target_id =
  47. picking_service->pick_unit_first(float(sx), float(sy), *world, *camera,
  48. viewport.width, viewport.height, 0);
  49. if (target_id != 0U) {
  50. auto *target_entity = world->get_entity(target_id);
  51. if (target_entity != nullptr) {
  52. auto *target_unit =
  53. target_entity->get_component<Engine::Core::UnitComponent>();
  54. if (target_unit != nullptr) {
  55. bool const is_enemy = (target_unit->owner_id != local_owner_id);
  56. bool const is_building =
  57. target_entity->has_component<Engine::Core::BuildingComponent>();
  58. if (is_enemy && !is_building) {
  59. Game::Systems::CommandService::attack_target(*world, selected,
  60. target_id, true);
  61. return;
  62. }
  63. }
  64. }
  65. }
  66. QVector3D hit;
  67. if (picking_service->screen_to_ground(QPointF(sx, sy), *camera,
  68. viewport.width, viewport.height, hit)) {
  69. auto formation_result =
  70. Game::Systems::FormationPlanner::get_formation_with_facing(
  71. *world, selected, hit,
  72. Game::GameConfig::instance().gameplay().formation_spacing_default);
  73. for (size_t i = 0; i < selected.size(); ++i) {
  74. auto *entity = world->get_entity(selected[i]);
  75. if (entity == nullptr) {
  76. continue;
  77. }
  78. auto *formation_mode =
  79. entity->get_component<Engine::Core::FormationModeComponent>();
  80. if ((formation_mode == nullptr) || !formation_mode->active) {
  81. continue;
  82. }
  83. auto *transform =
  84. entity->get_component<Engine::Core::TransformComponent>();
  85. if (transform != nullptr && i < formation_result.facing_angles.size()) {
  86. transform->desired_yaw = formation_result.facing_angles[i];
  87. transform->has_desired_yaw = true;
  88. }
  89. }
  90. Game::Systems::CommandService::MoveOptions opts;
  91. opts.group_move = selected.size() > 1;
  92. Game::Systems::CommandService::move_units(*world, selected,
  93. formation_result.positions, opts);
  94. }
  95. }
  96. } // namespace
  97. void InputCommandHandler::on_right_click(qreal sx, qreal sy, int local_owner_id,
  98. const ViewportState &viewport) {
  99. if (m_is_spectator_mode || !m_world) {
  100. return;
  101. }
  102. auto *selection_system =
  103. m_world->get_system<Game::Systems::SelectionSystem>();
  104. if (selection_system == nullptr) {
  105. return;
  106. }
  107. if (m_cursor_manager->mode() == CursorMode::Patrol ||
  108. m_cursor_manager->mode() == CursorMode::Attack ||
  109. m_cursor_manager->mode() == CursorMode::Guard ||
  110. m_cursor_manager->mode() == CursorMode::PlaceBuilding ||
  111. m_cursor_manager->mode() == CursorMode::Heal ||
  112. m_cursor_manager->mode() == CursorMode::Build) {
  113. m_cursor_manager->set_mode(CursorMode::Normal);
  114. return;
  115. }
  116. const auto &sel = selection_system->get_selected_units();
  117. if (sel.empty()) {
  118. return;
  119. }
  120. if (m_command_controller) {
  121. m_command_controller->disable_run_mode_for_selected();
  122. }
  123. handle_move_command(m_world, sel, m_picking_service, m_camera, sx, sy,
  124. local_owner_id, viewport);
  125. }
  126. void InputCommandHandler::on_right_double_click(qreal sx, qreal sy,
  127. int local_owner_id,
  128. const ViewportState &viewport) {
  129. if (m_is_spectator_mode || !m_world) {
  130. return;
  131. }
  132. auto *selection_system =
  133. m_world->get_system<Game::Systems::SelectionSystem>();
  134. if (selection_system == nullptr) {
  135. return;
  136. }
  137. if (m_cursor_manager->mode() == CursorMode::Patrol ||
  138. m_cursor_manager->mode() == CursorMode::Attack ||
  139. m_cursor_manager->mode() == CursorMode::Guard ||
  140. m_cursor_manager->mode() == CursorMode::PlaceBuilding ||
  141. m_cursor_manager->mode() == CursorMode::Heal ||
  142. m_cursor_manager->mode() == CursorMode::Build) {
  143. m_cursor_manager->set_mode(CursorMode::Normal);
  144. return;
  145. }
  146. const auto &sel = selection_system->get_selected_units();
  147. if (sel.empty()) {
  148. return;
  149. }
  150. if (m_command_controller) {
  151. m_command_controller->enable_run_mode_for_selected();
  152. }
  153. handle_move_command(m_world, sel, m_picking_service, m_camera, sx, sy,
  154. local_owner_id, viewport);
  155. }
  156. void InputCommandHandler::on_attack_click(qreal sx, qreal sy,
  157. const ViewportState &viewport) {
  158. if (m_is_spectator_mode) {
  159. return;
  160. }
  161. if (!m_command_controller || !m_camera) {
  162. return;
  163. }
  164. auto result = m_command_controller->on_attack_click(
  165. sx, sy, viewport.width, viewport.height, m_camera);
  166. auto *selection_system =
  167. m_world->get_system<Game::Systems::SelectionSystem>();
  168. if ((selection_system == nullptr) || !m_picking_service || !m_camera ||
  169. !m_world) {
  170. return;
  171. }
  172. const auto &selected = selection_system->get_selected_units();
  173. if (!selected.empty()) {
  174. Engine::Core::EntityID const target_id = m_picking_service->pick_unit_first(
  175. float(sx), float(sy), *m_world, *m_camera, viewport.width,
  176. viewport.height, 0);
  177. if (target_id != 0) {
  178. auto *target_entity = m_world->get_entity(target_id);
  179. if (target_entity != nullptr) {
  180. auto *target_unit =
  181. target_entity->get_component<Engine::Core::UnitComponent>();
  182. if ((target_unit != nullptr)) {
  183. App::Controllers::ActionVFX::spawnAttackArrow(m_world, target_id);
  184. }
  185. }
  186. }
  187. }
  188. if (result.reset_cursor_to_normal) {
  189. m_cursor_manager->set_mode(CursorMode::Normal);
  190. }
  191. }
  192. void InputCommandHandler::reset_movement(Engine::Core::Entity *entity) {
  193. App::Utils::reset_movement(entity);
  194. }
  195. void InputCommandHandler::on_stop_command() {
  196. if (m_is_spectator_mode) {
  197. return;
  198. }
  199. if (!m_command_controller) {
  200. return;
  201. }
  202. auto result = m_command_controller->on_stop_command();
  203. if (result.reset_cursor_to_normal) {
  204. m_cursor_manager->set_mode(CursorMode::Normal);
  205. }
  206. }
  207. void InputCommandHandler::on_hold_command() {
  208. if (m_is_spectator_mode) {
  209. return;
  210. }
  211. if (!m_command_controller) {
  212. return;
  213. }
  214. auto result = m_command_controller->on_hold_command();
  215. if (result.reset_cursor_to_normal) {
  216. m_cursor_manager->set_mode(CursorMode::Normal);
  217. }
  218. }
  219. void InputCommandHandler::on_guard_command() {
  220. if (m_is_spectator_mode) {
  221. return;
  222. }
  223. if (!m_command_controller) {
  224. return;
  225. }
  226. auto result = m_command_controller->on_guard_command();
  227. if (result.reset_cursor_to_normal) {
  228. m_cursor_manager->set_mode(CursorMode::Normal);
  229. }
  230. }
  231. void InputCommandHandler::on_formation_command() {
  232. if (m_is_spectator_mode) {
  233. return;
  234. }
  235. if (!m_command_controller) {
  236. return;
  237. }
  238. auto result = m_command_controller->on_formation_command();
  239. if (result.reset_cursor_to_normal) {
  240. m_cursor_manager->set_mode(CursorMode::Normal);
  241. }
  242. }
  243. void InputCommandHandler::on_run_command() {
  244. if (m_is_spectator_mode) {
  245. return;
  246. }
  247. if (!m_command_controller) {
  248. return;
  249. }
  250. auto result = m_command_controller->on_run_command();
  251. if (result.reset_cursor_to_normal) {
  252. m_cursor_manager->set_mode(CursorMode::Normal);
  253. }
  254. }
  255. void InputCommandHandler::on_guard_click(qreal sx, qreal sy,
  256. const ViewportState &viewport) {
  257. if (m_is_spectator_mode) {
  258. return;
  259. }
  260. if (!m_command_controller || !m_camera) {
  261. return;
  262. }
  263. auto result = m_command_controller->on_guard_click(sx, sy, viewport.width,
  264. viewport.height, m_camera);
  265. if (result.reset_cursor_to_normal) {
  266. m_cursor_manager->set_mode(CursorMode::Normal);
  267. }
  268. }
  269. auto InputCommandHandler::any_selected_in_hold_mode() const -> bool {
  270. if (!m_command_controller) {
  271. return false;
  272. }
  273. return m_command_controller->any_selected_in_hold_mode();
  274. }
  275. auto InputCommandHandler::any_selected_in_guard_mode() const -> bool {
  276. if (!m_command_controller) {
  277. return false;
  278. }
  279. return m_command_controller->any_selected_in_guard_mode();
  280. }
  281. auto InputCommandHandler::any_selected_in_formation_mode() const -> bool {
  282. if (!m_command_controller) {
  283. return false;
  284. }
  285. return m_command_controller->any_selected_in_formation_mode();
  286. }
  287. auto InputCommandHandler::any_selected_in_run_mode() const -> bool {
  288. if (!m_command_controller) {
  289. return false;
  290. }
  291. return m_command_controller->any_selected_in_run_mode();
  292. }
  293. auto InputCommandHandler::is_placing_formation() const -> bool {
  294. if (!m_command_controller) {
  295. return false;
  296. }
  297. return m_command_controller->is_placing_formation();
  298. }
  299. void InputCommandHandler::on_formation_mouse_move(
  300. qreal sx, qreal sy, const ViewportState &viewport) {
  301. if (!m_command_controller || !m_camera || !m_picking_service) {
  302. return;
  303. }
  304. if (!m_command_controller->is_placing_formation()) {
  305. return;
  306. }
  307. QVector3D hit;
  308. if (m_picking_service->screen_to_ground(
  309. QPointF(sx, sy), *m_camera, viewport.width, viewport.height, hit)) {
  310. m_command_controller->update_formation_placement(hit);
  311. }
  312. }
  313. void InputCommandHandler::on_formation_scroll(float delta) {
  314. if (!m_command_controller) {
  315. return;
  316. }
  317. if (!m_command_controller->is_placing_formation()) {
  318. return;
  319. }
  320. float current_angle = m_command_controller->get_formation_placement_angle();
  321. float new_angle = current_angle + delta * 5.0F;
  322. while (new_angle < 0.0F) {
  323. new_angle += 360.0F;
  324. }
  325. while (new_angle >= 360.0F) {
  326. new_angle -= 360.0F;
  327. }
  328. m_command_controller->update_formation_rotation(new_angle);
  329. }
  330. void InputCommandHandler::on_formation_confirm() {
  331. if (!m_command_controller) {
  332. return;
  333. }
  334. m_command_controller->confirm_formation_placement();
  335. }
  336. void InputCommandHandler::on_formation_cancel() {
  337. if (!m_command_controller) {
  338. return;
  339. }
  340. m_command_controller->cancel_formation_placement();
  341. }
  342. void InputCommandHandler::on_patrol_click(qreal sx, qreal sy,
  343. const ViewportState &viewport) {
  344. if (m_is_spectator_mode) {
  345. return;
  346. }
  347. if (!m_command_controller || !m_camera) {
  348. return;
  349. }
  350. auto result = m_command_controller->on_patrol_click(
  351. sx, sy, viewport.width, viewport.height, m_camera);
  352. if (result.reset_cursor_to_normal) {
  353. m_cursor_manager->set_mode(CursorMode::Normal);
  354. }
  355. }
  356. void InputCommandHandler::on_click_select(qreal sx, qreal sy, bool additive,
  357. int local_owner_id,
  358. const ViewportState &viewport) {
  359. if (m_is_spectator_mode) {
  360. return;
  361. }
  362. if (m_selection_controller && m_camera) {
  363. m_selection_controller->on_click_select(sx, sy, additive, viewport.width,
  364. viewport.height, m_camera,
  365. local_owner_id);
  366. }
  367. }
  368. void InputCommandHandler::on_area_selected(qreal x1, qreal y1, qreal x2,
  369. qreal y2, bool additive,
  370. int local_owner_id,
  371. const ViewportState &viewport) {
  372. if (m_is_spectator_mode) {
  373. return;
  374. }
  375. if (m_selection_controller && m_camera) {
  376. m_selection_controller->on_area_selected(x1, y1, x2, y2, additive,
  377. viewport.width, viewport.height,
  378. m_camera, local_owner_id);
  379. }
  380. }
  381. void InputCommandHandler::select_all_troops(int local_owner_id) {
  382. if (m_is_spectator_mode) {
  383. return;
  384. }
  385. if (m_selection_controller) {
  386. m_selection_controller->select_all_player_troops(local_owner_id);
  387. }
  388. }
  389. void InputCommandHandler::select_unit_by_id(int unit_id, int local_owner_id) {
  390. if (m_is_spectator_mode) {
  391. return;
  392. }
  393. if (!m_selection_controller || (unit_id <= 0)) {
  394. return;
  395. }
  396. m_selection_controller->select_single_unit(
  397. static_cast<Engine::Core::EntityID>(unit_id), local_owner_id);
  398. }
  399. void InputCommandHandler::set_hover_at_screen(qreal sx, qreal sy,
  400. const ViewportState &viewport) {
  401. if (!m_hover_tracker || !m_camera || !m_world) {
  402. return;
  403. }
  404. m_hover_tracker->update_hover(float(sx), float(sy), *m_world, *m_camera,
  405. viewport.width, viewport.height);
  406. }