| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #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_selection_controller && m_camera) {
- m_selection_controller->on_click_select(sx, sy, false, viewport.width,
- viewport.height, m_camera,
- local_owner_id);
- }
- }
- void InputCommandHandler::on_right_click(qreal sx, qreal sy, int local_owner_id,
- const ViewportState &viewport) {
- if (!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->setMode(CursorMode::Normal);
- return;
- }
- const auto &sel = selection_system->get_selected_units();
- if (sel.empty()) {
- return;
- }
- if (m_picking_service && m_camera) {
- 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 != 0U) {
- 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) {
- bool const is_enemy = (target_unit->owner_id != local_owner_id);
- if (is_enemy) {
- Game::Systems::CommandService::attack_target(*m_world, sel,
- target_id, true);
- return;
- }
- }
- }
- }
- }
- if (m_picking_service && m_camera) {
- QVector3D hit;
- if (m_picking_service->screen_to_ground(
- QPointF(sx, sy), *m_camera, viewport.width, viewport.height, hit)) {
- auto targets = Game::Systems::FormationPlanner::spreadFormation(
- int(sel.size()), hit,
- Game::GameConfig::instance().gameplay().formation_spacing_default);
- Game::Systems::CommandService::MoveOptions opts;
- opts.group_move = sel.size() > 1;
- Game::Systems::CommandService::moveUnits(*m_world, sel, targets, opts);
- }
- }
- }
- void InputCommandHandler::on_attack_click(qreal sx, qreal sy,
- const ViewportState &viewport) {
- if (!m_command_controller || !m_camera) {
- return;
- }
- auto result = m_command_controller->onAttackClick(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.resetCursorToNormal) {
- m_cursor_manager->setMode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::reset_movement(Engine::Core::Entity *entity) {
- App::Utils::reset_movement(entity);
- }
- void InputCommandHandler::on_stop_command() {
- if (!m_command_controller) {
- return;
- }
- auto result = m_command_controller->onStopCommand();
- if (result.resetCursorToNormal) {
- m_cursor_manager->setMode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_hold_command() {
- if (!m_command_controller) {
- return;
- }
- auto result = m_command_controller->onHoldCommand();
- if (result.resetCursorToNormal) {
- m_cursor_manager->setMode(CursorMode::Normal);
- }
- }
- auto InputCommandHandler::any_selected_in_hold_mode() const -> bool {
- if (!m_command_controller) {
- return false;
- }
- return m_command_controller->anySelectedInHoldMode();
- }
- void InputCommandHandler::on_patrol_click(qreal sx, qreal sy,
- const ViewportState &viewport) {
- if (!m_command_controller || !m_camera) {
- return;
- }
- auto result = m_command_controller->onPatrolClick(sx, sy, viewport.width,
- viewport.height, m_camera);
- if (result.resetCursorToNormal) {
- m_cursor_manager->setMode(CursorMode::Normal);
- }
- }
- void InputCommandHandler::on_click_select(qreal sx, qreal sy, bool additive,
- int local_owner_id,
- const ViewportState &viewport) {
- 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_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_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_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_cursor_manager->updateCursorShape(nullptr);
- m_hover_tracker->update_hover(float(sx), float(sy), *m_world, *m_camera,
- viewport.width, viewport.height);
- }
|