Pasaeta de include-what-you-use
Acabada de perfilar la classe PathSprite Menjeades declaracions de utils.h als fitxers que toca
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
#include "path_sprite.h"
|
||||
#include "utils.h"
|
||||
class Texture;
|
||||
#include <bits/std_abs.h> // Para abs
|
||||
#include <stdlib.h> // Para abs
|
||||
#include <utility> // Para move
|
||||
#include "moving_sprite.h" // Para MovingSprite
|
||||
#include "utils.h" // Para easeOutQuint
|
||||
#include <functional>
|
||||
class Texture; // lines 3-3
|
||||
|
||||
// Constructor
|
||||
PathSprite::PathSprite(std::shared_ptr<Texture> texture)
|
||||
@@ -16,57 +21,49 @@ void PathSprite::update()
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un recorrido vertical
|
||||
void PathSprite::addVerticalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
||||
// Añade un recorrido
|
||||
void PathSprite::addPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction, int waiting_counter)
|
||||
{
|
||||
paths_.emplace_back(createVerticalPath(start, end, steps), waiting_counter);
|
||||
paths_.emplace_back(createPath(start, end, type, fixed_pos, steps, easingFunction), waiting_counter);
|
||||
}
|
||||
|
||||
// Añade un recorrido horizontal
|
||||
void PathSprite::addHorizontalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
||||
{
|
||||
paths_.emplace_back(createHorizontalPath(start, end, steps), waiting_counter);
|
||||
}
|
||||
|
||||
// Añade un recorrido desde un vector
|
||||
// Añade un recorrido
|
||||
void PathSprite::addPath(std::vector<SDL_Point> spots, int waiting_counter)
|
||||
{
|
||||
paths_.emplace_back(spots, waiting_counter);
|
||||
paths_.emplace_back(std::move(spots), waiting_counter);
|
||||
}
|
||||
|
||||
std::vector<SDL_Point> PathSprite::createVerticalPath(const SDL_Point& start, const SDL_Point& end, int steps) {
|
||||
std::vector<SDL_Point> v;
|
||||
v.reserve(steps);
|
||||
|
||||
for (int i = 0; i < steps; ++i) {
|
||||
double t = static_cast<double>(i) / steps;
|
||||
double value = start.y + (end.y - start.y) * easeOutQuint(t);
|
||||
|
||||
// Ajusta el valor si los puntos tienen signos diferentes
|
||||
if (start.y > 0 && end.y < 0) {
|
||||
value = start.y - std::abs(end.y - start.y) * easeOutQuint(t);
|
||||
} else if (start.y < 0 && end.y > 0) {
|
||||
value = start.y + std::abs(end.y - start.y) * easeOutQuint(t);
|
||||
}
|
||||
|
||||
v.emplace_back(SDL_Point{start.x, static_cast<int>(value)});
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
// Devuelve un vector con los puntos que conforman la ruta
|
||||
std::vector<SDL_Point> PathSprite::createHorizontalPath(SDL_Point start, SDL_Point end, int steps)
|
||||
std::vector<SDL_Point> PathSprite::createPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction)
|
||||
{
|
||||
std::vector<SDL_Point> v;
|
||||
v.reserve(steps);
|
||||
|
||||
for (int i = 0; i < steps; ++i)
|
||||
{
|
||||
double t = static_cast<double>(i) / steps;
|
||||
double value = static_cast<double>(start.x) + (end.x - start.x) * easeOutQuint(t);
|
||||
v.emplace_back(SDL_Point{static_cast<int>(value), start.y});
|
||||
double t = static_cast<double>(i) / (steps - 1);
|
||||
double value = start + (end - start) * easingFunction(t);
|
||||
|
||||
if (start > 0 && end < 0)
|
||||
{
|
||||
value = start - std::abs(end - start) * easingFunction(t);
|
||||
}
|
||||
else if (start < 0 && end > 0)
|
||||
{
|
||||
value = start + std::abs(end - start) * easingFunction(t);
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case PathType::HORIZONTAL:
|
||||
v.emplace_back(SDL_Point{static_cast<int>(value), fixed_pos});
|
||||
break;
|
||||
case PathType::VERTICAL:
|
||||
v.emplace_back(SDL_Point{fixed_pos, static_cast<int>(value)});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
@@ -84,7 +81,7 @@ void PathSprite::moveThroughCurrentPath()
|
||||
auto &path = paths_.at(current_path_);
|
||||
|
||||
// Establece la posición
|
||||
const SDL_Point p = path.spots.at(path.counter);
|
||||
const auto &p = path.spots.at(path.counter);
|
||||
MovingSprite::setPos(p.x, p.y);
|
||||
|
||||
// Comprobar si ha terminado el recorrido
|
||||
@@ -99,10 +96,13 @@ void PathSprite::moveThroughCurrentPath()
|
||||
}
|
||||
|
||||
// Comprobar si ha terminado la espera
|
||||
if (path.waiting_counter == 0)
|
||||
path.finished = true;
|
||||
else if (path.on_destination)
|
||||
if (path.on_destination)
|
||||
{
|
||||
if (path.waiting_counter == 0)
|
||||
path.finished = true;
|
||||
else
|
||||
--path.waiting_counter;
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia de recorrido o finaliza
|
||||
|
||||
Reference in New Issue
Block a user