input_command_handler.cpp 14 KB

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