commit de me ane cap a casa
This commit is contained in:
@@ -919,7 +919,7 @@ void Game::createItemScoreSprite(int x, int y, std::shared_ptr<Texture> texture)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Crea un objeto PathSprite
|
// Crea un objeto PathSprite
|
||||||
void Game::createPathSprite(SDL_Point start, SDL_Point end, std::shared_ptr<Texture> texture)
|
void Game::createPathSprite(std::shared_ptr<Texture> texture)
|
||||||
{
|
{
|
||||||
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
|
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
|
||||||
|
|
||||||
@@ -930,7 +930,6 @@ void Game::createPathSprite(SDL_Point start, SDL_Point end, std::shared_ptr<Text
|
|||||||
path_sprites_.back()->setWidth(w);
|
path_sprites_.back()->setWidth(w);
|
||||||
path_sprites_.back()->setHeight(h);
|
path_sprites_.back()->setHeight(h);
|
||||||
path_sprites_.back()->setSpriteClip({0, 0, w, h});
|
path_sprites_.back()->setSpriteClip({0, 0, w, h});
|
||||||
path_sprites_.back()->addVerticalPath(start, end, 50, 10);
|
|
||||||
path_sprites_.back()->enable();
|
path_sprites_.back()->enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1579,10 +1578,9 @@ void Game::checkEvents()
|
|||||||
case SDLK_5: // Crea un PathSprite
|
case SDLK_5: // Crea un PathSprite
|
||||||
{
|
{
|
||||||
const auto x = players_.at(0)->getPosX();
|
const auto x = players_.at(0)->getPosX();
|
||||||
const SDL_Point start = {x, 200};
|
createPathSprite(game_text_textures_.at(3));
|
||||||
const SDL_Point end = {x, 170};
|
path_sprites_.back()->addVerticalPath({x,220}, {x,170}, 100, 5);
|
||||||
createPathSprite(start, end, game_text_textures_.at(3));
|
path_sprites_.back()->addVerticalPath({x,170}, {x,-20}, 120, 5);
|
||||||
path_sprites_.back()->addVerticalPath({x,170}, {x,0}, 100, 1);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -308,7 +308,7 @@ private:
|
|||||||
void createItemScoreSprite(int x, int y, std::shared_ptr<Texture> texture);
|
void createItemScoreSprite(int x, int y, std::shared_ptr<Texture> texture);
|
||||||
|
|
||||||
// Crea un objeto PathSprite
|
// Crea un objeto PathSprite
|
||||||
void createPathSprite(SDL_Point x, SDL_Point y, std::shared_ptr<Texture> texture);
|
void createPathSprite(std::shared_ptr<Texture> texture);
|
||||||
|
|
||||||
// Vacia el vector de smartsprites
|
// Vacia el vector de smartsprites
|
||||||
void freeSmartSprites();
|
void freeSmartSprites();
|
||||||
|
|||||||
@@ -16,34 +16,46 @@ void PathSprite::update()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Añade un path vertical
|
// Añade un recorrido vertical
|
||||||
void PathSprite::addVerticalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
void PathSprite::addVerticalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
||||||
{
|
{
|
||||||
paths_.emplace_back(createVerticalPath(start, end, steps), waiting_counter);
|
paths_.emplace_back(createVerticalPath(start, end, steps), waiting_counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Añade un path horizontal
|
// Añade un recorrido horizontal
|
||||||
void PathSprite::addHorizontalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
void PathSprite::addHorizontalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
||||||
{
|
{
|
||||||
paths_.emplace_back(createHorizontalPath(start, end, steps), waiting_counter);
|
paths_.emplace_back(createHorizontalPath(start, end, steps), waiting_counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Devuelve un vector con los puntos que conforman la ruta
|
// Añade un recorrido desde un vector
|
||||||
std::vector<SDL_Point> PathSprite::createVerticalPath(SDL_Point start, SDL_Point end, int steps)
|
void PathSprite::addPath(std::vector<SDL_Point> spots, int waiting_counter)
|
||||||
{
|
{
|
||||||
std::vector<SDL_Point> v;
|
paths_.emplace_back(spots, waiting_counter);
|
||||||
v.reserve(steps);
|
|
||||||
|
|
||||||
for (int i = 0; i < steps; ++i)
|
|
||||||
{
|
|
||||||
double t = static_cast<double>(i) / steps;
|
|
||||||
double value = static_cast<double>(start.y) + (end.y - start.y) * easeOutQuint(t);
|
|
||||||
v.emplace_back(SDL_Point{start.x, static_cast<int>(value)});
|
|
||||||
}
|
|
||||||
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// 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::createHorizontalPath(SDL_Point start, SDL_Point end, int steps)
|
||||||
{
|
{
|
||||||
@@ -66,6 +78,7 @@ void PathSprite::enable()
|
|||||||
enabled_ = true;
|
enabled_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Coloca el sprite en los diferentes puntos del recorrido
|
||||||
void PathSprite::moveThroughCurrentPath()
|
void PathSprite::moveThroughCurrentPath()
|
||||||
{
|
{
|
||||||
auto &path = paths_.at(current_path_);
|
auto &path = paths_.at(current_path_);
|
||||||
@@ -92,6 +105,7 @@ void PathSprite::moveThroughCurrentPath()
|
|||||||
--path.waiting_counter;
|
--path.waiting_counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cambia de recorrido o finaliza
|
||||||
void PathSprite::goToNextPathOrDie()
|
void PathSprite::goToNextPathOrDie()
|
||||||
{
|
{
|
||||||
// Comprueba si ha terminado el recorrdo actual
|
// Comprueba si ha terminado el recorrdo actual
|
||||||
|
|||||||
@@ -4,23 +4,31 @@
|
|||||||
#include "animated_sprite.h" // para SpriteAnimated
|
#include "animated_sprite.h" // para SpriteAnimated
|
||||||
class Texture;
|
class Texture;
|
||||||
|
|
||||||
|
enum class PathType
|
||||||
|
{
|
||||||
|
VERTICAL,
|
||||||
|
HORIZONTAL,
|
||||||
|
};
|
||||||
|
|
||||||
// Clase PathSprite
|
// Clase PathSprite
|
||||||
class PathSprite : public AnimatedSprite
|
class PathSprite : public AnimatedSprite
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
// Estructuras
|
// Estructuras
|
||||||
struct Path
|
struct Path {
|
||||||
{
|
std::vector<SDL_Point> spots; // Puntos por los que se desplazará el sprite
|
||||||
std::vector<SDL_Point> spots; // Puntos por los que se desplazará el sprite
|
int fixed_pos; // Coordenada fija
|
||||||
int waiting_counter; // Tiempo de espera una vez en el destino
|
int waiting_counter; // Tiempo de espera una vez en el destino
|
||||||
bool on_destination = false; // Indica si ha llegado al destino
|
bool on_destination = false; // Indica si ha llegado al destino
|
||||||
bool finished = false; // Indica si ha terminado de esperarse
|
bool finished = false; // Indica si ha terminado de esperarse
|
||||||
int counter = 0; // Contador interno
|
int counter = 0; // Contador interno
|
||||||
|
PathType type; // Tipo de recorrido
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Path(const std::vector<SDL_Point>& spots_init, int waiting_counter_init, int fixed_pos_init, PathType type_init)
|
||||||
|
: spots(spots_init), waiting_counter(waiting_counter_init), fixed_pos(fixed_pos_init), type(type_init) {}
|
||||||
|
};
|
||||||
|
|
||||||
// Constructor
|
|
||||||
Path(const std::vector<SDL_Point> &spots_init, int waiting_counter_init)
|
|
||||||
: spots(spots_init), waiting_counter(waiting_counter_init) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
bool finished_ = false; // Indica si ya ha terminado
|
bool finished_ = false; // Indica si ya ha terminado
|
||||||
@@ -28,13 +36,10 @@ private:
|
|||||||
int current_path_ = 0; // Path que se está recorriendo actualmente
|
int current_path_ = 0; // Path que se está recorriendo actualmente
|
||||||
std::vector<Path> paths_; // Caminos a recorrer por el sprite
|
std::vector<Path> paths_; // Caminos a recorrer por el sprite
|
||||||
|
|
||||||
// Devuelve un vector con los puntos que conforman la ruta
|
// Coloca el sprite en los diferentes puntos del recorrido
|
||||||
std::vector<SDL_Point> createVerticalPath(SDL_Point start, SDL_Point end, int steps);
|
|
||||||
|
|
||||||
// Devuelve un vector con los puntos que conforman la ruta
|
|
||||||
std::vector<SDL_Point> createHorizontalPath(SDL_Point start, SDL_Point end, int steps);
|
|
||||||
|
|
||||||
void moveThroughCurrentPath();
|
void moveThroughCurrentPath();
|
||||||
|
|
||||||
|
// Cambia de recorrido o finaliza
|
||||||
void goToNextPathOrDie();
|
void goToNextPathOrDie();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -47,12 +52,21 @@ public:
|
|||||||
// Actualiza la posición del sprite
|
// Actualiza la posición del sprite
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|
||||||
// Añade un path vertical
|
// Añade un recorrido vertical
|
||||||
void addVerticalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter);
|
void addVerticalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter);
|
||||||
|
|
||||||
// Añade un path horizontal
|
// Añade un recorrido horizontal
|
||||||
void addHorizontalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter);
|
void addHorizontalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter);
|
||||||
|
|
||||||
|
// Añade un recorrido desde un vector
|
||||||
|
void addPath(std::vector<SDL_Point> spots, int waiting_counter);
|
||||||
|
|
||||||
|
// Devuelve un vector con los puntos que conforman la ruta
|
||||||
|
std::vector<SDL_Point> createVerticalPath(const SDL_Point &start, const SDL_Point &end, int steps);
|
||||||
|
|
||||||
|
// Devuelve un vector con los puntos que conforman la ruta
|
||||||
|
std::vector<SDL_Point> createHorizontalPath(SDL_Point start, SDL_Point end, int steps);
|
||||||
|
|
||||||
// Habilita el objeto
|
// Habilita el objeto
|
||||||
void enable();
|
void enable();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user