// IWYU pragma: no_include #include "path_sprite.h" #include // Para function #include // Para move // Constructor para paths por puntos (compatibilidad) Path::Path(const std::vector &spots_init, int waiting_counter_init) : spots(spots_init), is_point_path(true) { constexpr float FRAME_TIME_MS = 1000.0f / 60.0f; waiting_time_ms = static_cast(waiting_counter_init) * FRAME_TIME_MS; } // Devuelve un vector con los puntos que conforman la ruta auto createPath(float start, float end, PathType type, float fixed_pos, int steps, const std::function &easing_function) -> std::vector { std::vector v; v.reserve(steps); for (int i = 0; i < steps; ++i) { double t = static_cast(i) / (steps - 1); double value = start + ((end - start) * easing_function(t)); if ((start > 0 && end < 0) || (start < 0 && end > 0)) { value = start + (end > 0 ? 1 : -1) * std::abs(end - start) * easing_function(t); } switch (type) { case PathType::HORIZONTAL: v.emplace_back(SDL_FPoint{static_cast(value), fixed_pos}); break; case PathType::VERTICAL: v.emplace_back(SDL_FPoint{fixed_pos, static_cast(value)}); break; default: break; } } return v; } // Actualiza la posición y comprueba si ha llegado a su destino (compatibilidad) void PathSprite::update() { constexpr float FRAME_TIME_MS = 1000.0f / 60.0f; // 16.67ms por frame a 60 FPS update(FRAME_TIME_MS); } // Actualiza la posición y comprueba si ha llegado a su destino void PathSprite::update(float delta_time) { if (enabled_ && !has_finished_) { moveThroughCurrentPath(delta_time); goToNextPathOrDie(); } } // Muestra el sprite por pantalla void PathSprite::render() { if (enabled_) { Sprite::render(); } } // Añade un recorrido void PathSprite::addPath(Path path, bool centered) { PathCentered path_centered = PathCentered::NONE; if (centered) { path_centered = (path.spots.back().x == path.spots.front().x) ? PathCentered::ON_X : PathCentered::ON_Y; } switch (path_centered) { case PathCentered::ON_X: { const int X = path.spots.back().x - (pos_.w / 2); for (auto &spot : path.spots) { spot.x = X; } paths_.emplace_back(path); break; } case PathCentered::ON_Y: { const int Y = path.spots.back().y - (pos_.h / 2); for (auto &spot : path.spots) { spot.y = Y; } paths_.emplace_back(path); break; } default: paths_.emplace_back(path); break; } } // Añade un recorrido void PathSprite::addPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function &easing_function, int waiting_counter) { // Convertir frames a milisegundos constexpr float FRAME_TIME_MS = 1000.0f / 60.0f; float duration_ms = static_cast(steps) * FRAME_TIME_MS; float waiting_ms = static_cast(waiting_counter) * FRAME_TIME_MS; paths_.emplace_back(static_cast(start), static_cast(end), type, static_cast(fixed_pos), duration_ms, waiting_ms, easing_function); } // Añade un recorrido void PathSprite::addPath(const std::vector &spots, int waiting_counter) { paths_.emplace_back(spots, waiting_counter); } // Habilita el objeto void PathSprite::enable() { if (paths_.empty() || enabled_) { return; } enabled_ = true; // Establece la posición inicial auto &path = paths_.at(current_path_); if (path.is_point_path) { const auto &p = path.spots.at(path.counter); setPosition(p); } else { // Para paths generados, establecer posición inicial SDL_FPoint initial_pos; if (path.type == PathType::HORIZONTAL) { initial_pos = {path.start_pos, path.fixed_pos}; } else { initial_pos = {path.fixed_pos, path.start_pos}; } setPosition(initial_pos); } } // Coloca el sprite en los diferentes puntos del recorrido void PathSprite::moveThroughCurrentPath(float delta_time) { auto &path = paths_.at(current_path_); if (path.is_point_path) { // Lógica para paths por puntos (compatibilidad) const auto &p = path.spots.at(path.counter); setPosition(p); if (!path.on_destination) { ++path.counter; if (path.counter >= static_cast(path.spots.size())) { path.on_destination = true; path.counter = static_cast(path.spots.size()) - 1; } } if (path.on_destination) { path.waiting_elapsed += delta_time; if (path.waiting_elapsed >= path.waiting_time_ms) { path.finished = true; } } } else { // Lógica para paths generados en tiempo real if (!path.on_destination) { path.elapsed_time += delta_time; // Calcular progreso (0.0 a 1.0) float progress = path.elapsed_time / path.duration_ms; if (progress >= 1.0f) { progress = 1.0f; path.on_destination = true; } // Aplicar función de easing double eased_progress = path.easing_function(progress); // Calcular posición actual float current_pos = path.start_pos + (path.end_pos - path.start_pos) * static_cast(eased_progress); // Establecer posición según el tipo SDL_FPoint position; if (path.type == PathType::HORIZONTAL) { position = {current_pos, path.fixed_pos}; } else { position = {path.fixed_pos, current_pos}; } setPosition(position); } else { // Esperar en destino path.waiting_elapsed += delta_time; if (path.waiting_elapsed >= path.waiting_time_ms) { path.finished = true; } } } } // Cambia de recorrido o finaliza void PathSprite::goToNextPathOrDie() { // Comprueba si ha terminado el recorrdo actual if (paths_.at(current_path_).finished) { ++current_path_; } // Comprueba si quedan mas recorridos if (current_path_ >= static_cast(paths_.size())) { has_finished_ = true; current_path_ = 0; } } // Indica si ha terminado todos los recorridos auto PathSprite::hasFinished() const -> bool { return has_finished_; }