| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- #include "input_command_handler.h"
- #include "../controllers/action_vfx.h"
- #include "../controllers/command_controller.h"
- #include "../models/cursor_manager.h"
- #include "../models/cursor_mode.h"
- #include "../models/hover_tracker.h"
- #include "../utils/movement_utils.h"
- #include "game/core/component.h"
- #include "game/core/world.h"
- #include "game/game_config.h"
- #include "game/systems/command_service.h"
- #include "game/systems/formation_planner.h"
- #include "game/systems/picking_service.h"
- #include "game/systems/selection_system.h"
- #include "render/gl/camera.h"
- InputCommandHandler::InputCommandHandler(
- Engine::Core::World *world,
- Game::Systems::SelectionController *selection_controller,
- App::Controllers::CommandController *command_controller,
- CursorManager *cursor_manager, HoverTracker *hover_tracker,
- Game::Systems::PickingService *picking_service, Render::GL::Camera *camera)
- : m_world(world), m_selection_controller(selection_controller),
- m_command_controller(command_controller),
- m_cursor_manager(cursor_manager), m_hover_tracker(hover_tracker),
- m_picking_service(picking_service), m_camera(camera) {}
- void InputCommandHandler::on_map_clicked(qreal sx, qreal sy, int local_owner_id,
- const ViewportState &viewport) {
- if (m_is_spectator_mode) {
- return;
- }
- if (m_selection_controller && m_camera) {
- m_selection_controller->on_click_select(sx, sy, false, viewport.width,
- viewport.height, m_camera,
- local_owner_id);
- }
- }
- namespace {
- void handle_move_command(Engine::Core::World *world,
- const std::vector<Engine::Core::EntityID> &selected,
- Game::Systems::PickingService *picking_service,
- Render::GL::Camera *camera, qreal sx, qreal sy,
- int local_owner_id, const ViewportState &viewport) {
- if (!picking_service || !camera || !world) {
- return;
- }
- Engine::Core::EntityID const target_id =
- picking_service->pick_unit_first(float(sx), float(sy), *world, *camera,
- viewport.width, viewport.height, 0);
- if (target_id != 0U) {
- auto *target_entity = world->get_entity(target_id);
- if (target_entity != nullptr) {
- auto *target_unit =
- target_entity->get_component<Engine::Core::UnitComponent>();
- if (target_unit != nullptr) {
- bool const is_enemy = (target_unit->owner_id != local_owner_id);
- if (is_enemy) {
- Game::Systems::CommandService::attack_target(*world, selected,
- target_id, true);
- return;
- }
- }
- }
- }
- QVector3D hit;
- if (picking_service->screen_to_ground(QPointF(sx, sy), *camera,
- viewport.width, viewport.height, hit)) {
- auto formation_result =
- Game::Systems::FormationPlanner::get_formation_with_facing(
- *world, selected, hit,
- Game::GameConfig::instance().gameplay().formation_spacing_default);
- for (size_t i = 0; i < selected.size(); ++i) {
- auto *entity = world->get_entity(selected[i]);
- if (entity == nullptr) {
- continue;
- }
- auto *transform =
- entity->get_component<Engine::Core::TransformComponent>();
- if (transform != nullptr && i < formation_result.facing_angles.size()) {
- transform->desired_yaw = formation_result.facing_angles[i];
- transform->has_desired_yaw = true;
- }
- }
- Game::Systems::CommandService::MoveOptions opts;
- opts.group_move = selected.size() > 1;
- Game::Systems::CommandService::moveUnits(*world, selected,
- formation_result.positions, opts);
- }
- }
- } // namespace
- void InputCommandHandler::on_right_click(qreal sx, qreal sy, int local_owner_id,
- const ViewportState &viewport) {
- if (m_is_spectator_mode || !m_world) {
- return;
- }
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if (selection_system == nullptr) {
- return;
- }
- if (m_cursor_manager->mode() == CursorMode::Patrol ||
- m_cursor_manager->mode() == CursorMode::Attack ||
- m_cursor_manager->mode() == CursorMode::Guard ||
- m_cursor_manager->mode() == CursorMode::PlaceBuilding) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- return;
- }
- const auto &sel = selection_system->get_selected_units();
- if (sel.empty()) {
- return;
- }
- if (m_command_controller) {
- m_command_controller->disable_run_mode_for_selected();
- }
- handle_move_command(m_world, sel, m_picking_service, m_camera, sx, sy,
- local_owner_id, viewport);
- }
- void InputCommandHandler::on_right_double_click(qreal sx, qreal sy,
- int local_owner_id,
- const ViewportState &viewport) {
- if (m_is_spectator_mode || !m_world) {
- return;
- }
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if (selection_system == nullptr) {
- return;
- }
- if (m_cursor_manager->mode() == CursorMode::Patrol ||
- m_cursor_manager->mode() == CursorMode::Attack ||
- m_cursor_manager->mode() == CursorMode::Guard ||
- m_cursor_manager->mode() == CursorMode::PlaceBuilding) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- return;
- }
- const auto &sel = selection_system->get_selected_units();
- if (sel.empty()) {
- return;
- }
- if (m_command_controller) {
- m_command_controller->enable_run_mode_for_selected();
- }
- handle_move_command(m_world, sel, m_picking_service, m_camera, sx, sy,
- local_owner_id, viewport);
- }
- void InputCommandHandler::on_attack_click(qreal sx, qreal sy,
- const ViewportState &viewport) {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller || !m_camera) {
- return;
- }
- auto result = m_command_controller->on_attack_click(
- sx, sy, viewport.width, viewport.height, m_camera);
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if ((selection_system == nullptr) || !m_picking_service || !m_camera ||
- !m_world) {
- return;
- }
- const auto &selected = selection_system->get_selected_units();
- if (!selected.empty()) {
- Engine::Core::EntityID const target_id = m_picking_service->pick_unit_first(
- float(sx), float(sy), *m_world, *m_camera, viewport.width,
- viewport.height, 0);
- if (target_id != 0) {
- auto *target_entity = m_world->get_entity(target_id);
- if (target_entity != nullptr) {
- auto *target_unit =
- target_entity->get_component<Engine::Core::UnitComponent>();
- if ((target_unit != nullptr)) {
- App::Controllers::ActionVFX::spawnAttackArrow(m_world, target_id);
- }
- }
- }
- }
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::reset_movement(Engine::Core::Entity *entity) {
- App::Utils::reset_movement(entity);
- }
- void InputCommandHandler::on_stop_command() {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller) {
- return;
- }
- auto result = m_command_controller->on_stop_command();
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_hold_command() {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller) {
- return;
- }
- auto result = m_command_controller->on_hold_command();
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_guard_command() {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller) {
- return;
- }
- auto result = m_command_controller->on_guard_command();
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_formation_command() {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller) {
- return;
- }
- auto result = m_command_controller->on_formation_command();
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_run_command() {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller) {
- return;
- }
- auto result = m_command_controller->on_run_command();
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_guard_click(qreal sx, qreal sy,
- const ViewportState &viewport) {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller || !m_camera) {
- return;
- }
- auto result = m_command_controller->on_guard_click(sx, sy, viewport.width,
- viewport.height, m_camera);
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- auto InputCommandHandler::any_selected_in_hold_mode() const -> bool {
- if (!m_command_controller) {
- return false;
- }
- return m_command_controller->any_selected_in_hold_mode();
- }
- auto InputCommandHandler::any_selected_in_guard_mode() const -> bool {
- if (!m_command_controller) {
- return false;
- }
- return m_command_controller->any_selected_in_guard_mode();
- }
- auto InputCommandHandler::any_selected_in_formation_mode() const -> bool {
- if (!m_command_controller) {
- return false;
- }
- return m_command_controller->any_selected_in_formation_mode();
- }
- auto InputCommandHandler::any_selected_in_run_mode() const -> bool {
- if (!m_command_controller) {
- return false;
- }
- return m_command_controller->any_selected_in_run_mode();
- }
- auto InputCommandHandler::is_placing_formation() const -> bool {
- if (!m_command_controller) {
- return false;
- }
- return m_command_controller->is_placing_formation();
- }
- void InputCommandHandler::on_formation_mouse_move(
- qreal sx, qreal sy, const ViewportState &viewport) {
- if (!m_command_controller || !m_camera || !m_picking_service) {
- return;
- }
- if (!m_command_controller->is_placing_formation()) {
- return;
- }
- QVector3D hit;
- if (m_picking_service->screen_to_ground(
- QPointF(sx, sy), *m_camera, viewport.width, viewport.height, hit)) {
- m_command_controller->update_formation_placement(hit);
- }
- }
- void InputCommandHandler::on_formation_scroll(float delta) {
- if (!m_command_controller) {
- return;
- }
- if (!m_command_controller->is_placing_formation()) {
- return;
- }
- float current_angle = m_command_controller->get_formation_placement_angle();
- float new_angle = current_angle + delta * 5.0F;
- while (new_angle < 0.0F) {
- new_angle += 360.0F;
- }
- while (new_angle >= 360.0F) {
- new_angle -= 360.0F;
- }
- m_command_controller->update_formation_rotation(new_angle);
- }
- void InputCommandHandler::on_formation_confirm() {
- if (!m_command_controller) {
- return;
- }
- m_command_controller->confirm_formation_placement();
- }
- void InputCommandHandler::on_formation_cancel() {
- if (!m_command_controller) {
- return;
- }
- m_command_controller->cancel_formation_placement();
- }
- void InputCommandHandler::on_patrol_click(qreal sx, qreal sy,
- const ViewportState &viewport) {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_command_controller || !m_camera) {
- return;
- }
- auto result = m_command_controller->on_patrol_click(
- sx, sy, viewport.width, viewport.height, m_camera);
- if (result.reset_cursor_to_normal) {
- m_cursor_manager->set_mode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_click_select(qreal sx, qreal sy, bool additive,
- int local_owner_id,
- const ViewportState &viewport) {
- if (m_is_spectator_mode) {
- return;
- }
- if (m_selection_controller && m_camera) {
- m_selection_controller->on_click_select(sx, sy, additive, viewport.width,
- viewport.height, m_camera,
- local_owner_id);
- }
- }
- void InputCommandHandler::on_area_selected(qreal x1, qreal y1, qreal x2,
- qreal y2, bool additive,
- int local_owner_id,
- const ViewportState &viewport) {
- if (m_is_spectator_mode) {
- return;
- }
- if (m_selection_controller && m_camera) {
- m_selection_controller->on_area_selected(x1, y1, x2, y2, additive,
- viewport.width, viewport.height,
- m_camera, local_owner_id);
- }
- }
- void InputCommandHandler::select_all_troops(int local_owner_id) {
- if (m_is_spectator_mode) {
- return;
- }
- if (m_selection_controller) {
- m_selection_controller->select_all_player_troops(local_owner_id);
- }
- }
- void InputCommandHandler::select_unit_by_id(int unit_id, int local_owner_id) {
- if (m_is_spectator_mode) {
- return;
- }
- if (!m_selection_controller || (unit_id <= 0)) {
- return;
- }
- m_selection_controller->select_single_unit(
- static_cast<Engine::Core::EntityID>(unit_id), local_owner_id);
- }
- void InputCommandHandler::set_hover_at_screen(qreal sx, qreal sy,
- const ViewportState &viewport) {
- if (!m_hover_tracker || !m_camera || !m_world) {
- return;
- }
- m_hover_tracker->update_hover(float(sx), float(sy), *m_world, *m_camera,
- viewport.width, viewport.height);
- }
|