Completada la classe PathSprite
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include "manage_hiscore_table.h" // para ManageHiScoreTable
|
||||
#include "notifier.h" // para Notifier
|
||||
#include "param.h" // para param
|
||||
#include "path_sprite.h" // para PathSprite
|
||||
#include "player.h" // para Player, PlayerStatus
|
||||
#include "resource.h" // para Resource
|
||||
#include "scoreboard.h" // para Scoreboard, ScoreboardMode, SCOREB...
|
||||
@@ -917,8 +918,24 @@ void Game::createItemScoreSprite(int x, int y, std::shared_ptr<Texture> texture)
|
||||
smart_sprites_.back()->setFinishedCounter(100);
|
||||
}
|
||||
|
||||
// Crea un objeto PathSprite
|
||||
void Game::createPathSprite(SDL_Point start, SDL_Point end, std::shared_ptr<Texture> texture)
|
||||
{
|
||||
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
|
||||
|
||||
const auto w = texture->getWidth();
|
||||
const auto h = texture->getHeight();
|
||||
|
||||
// Inicializa
|
||||
path_sprites_.back()->setWidth(w);
|
||||
path_sprites_.back()->setHeight(h);
|
||||
path_sprites_.back()->setSpriteClip({0, 0, w, h});
|
||||
path_sprites_.back()->addVerticalPath(start, end, 50, 10);
|
||||
path_sprites_.back()->enable();
|
||||
}
|
||||
|
||||
// Vacia el vector de smartsprites
|
||||
void Game::freeSpriteSmarts()
|
||||
void Game::freeSmartSprites()
|
||||
{
|
||||
if (!smart_sprites_.empty())
|
||||
for (int i = smart_sprites_.size() - 1; i >= 0; --i)
|
||||
@@ -926,6 +943,15 @@ void Game::freeSpriteSmarts()
|
||||
smart_sprites_.erase(smart_sprites_.begin() + i);
|
||||
}
|
||||
|
||||
// Vacia el vector de pathsprites
|
||||
void Game::freePathSprites()
|
||||
{
|
||||
if (!path_sprites_.empty())
|
||||
for (int i = path_sprites_.size() - 1; i >= 0; --i)
|
||||
if (path_sprites_[i]->hasFinished())
|
||||
path_sprites_.erase(path_sprites_.begin() + i);
|
||||
}
|
||||
|
||||
// Crea un SpriteSmart para arrojar el item café al recibir un impacto
|
||||
void Game::throwCoffee(int x, int y)
|
||||
{
|
||||
@@ -949,18 +975,32 @@ void Game::throwCoffee(int x, int y)
|
||||
smart_sprites_.back()->setRotateAmount(90.0);
|
||||
}
|
||||
|
||||
// Actualiza los SpriteSmarts
|
||||
void Game::updateSpriteSmarts()
|
||||
// Actualiza los SmartSprites
|
||||
void Game::updateSmartSprites()
|
||||
{
|
||||
for (auto &ss : smart_sprites_)
|
||||
ss->update();
|
||||
for (auto &sprite : smart_sprites_)
|
||||
sprite->update();
|
||||
}
|
||||
|
||||
// Pinta los SpriteSmarts activos
|
||||
void Game::renderSpriteSmarts()
|
||||
// Pinta los SmartSprites activos
|
||||
void Game::renderSmartSprites()
|
||||
{
|
||||
for (auto &ss : smart_sprites_)
|
||||
ss->render();
|
||||
for (auto &sprite : smart_sprites_)
|
||||
sprite->render();
|
||||
}
|
||||
|
||||
// Actualiza los PathSprites
|
||||
void Game::updatePathSprites()
|
||||
{
|
||||
for (auto &sprite : path_sprites_)
|
||||
sprite->update();
|
||||
}
|
||||
|
||||
// Pinta los PathSprites activos
|
||||
void Game::renderPathSprites()
|
||||
{
|
||||
for (auto &sprite : path_sprites_)
|
||||
sprite->render();
|
||||
}
|
||||
|
||||
// Acciones a realizar cuando el jugador muere
|
||||
@@ -1122,7 +1162,10 @@ void Game::update()
|
||||
updateGameOver();
|
||||
|
||||
// Actualiza los SpriteSmarts
|
||||
updateSpriteSmarts();
|
||||
updateSmartSprites();
|
||||
|
||||
// Actualiza los PathSmarts
|
||||
updatePathSprites();
|
||||
|
||||
// Actualiza los contadores de estado y efectos
|
||||
updateTimeStopped();
|
||||
@@ -1150,7 +1193,8 @@ void Game::update()
|
||||
freeBullets();
|
||||
freeBalloons();
|
||||
freeItems();
|
||||
freeSpriteSmarts();
|
||||
freeSmartSprites();
|
||||
freePathSprites();
|
||||
}
|
||||
|
||||
// Comprueba si la música ha de estar sonando
|
||||
@@ -1197,7 +1241,8 @@ void Game::fillCanvas()
|
||||
// Dibuja los objetos
|
||||
background_->render();
|
||||
renderItems();
|
||||
renderSpriteSmarts();
|
||||
renderSmartSprites();
|
||||
renderPathSprites();
|
||||
explosions_->render();
|
||||
renderBalloons();
|
||||
renderBullets();
|
||||
@@ -1506,7 +1551,7 @@ void Game::checkEvents()
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
else if (event.type == SDL_KEYDOWN)
|
||||
else if (event.type == SDL_KEYDOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event.key.keysym.sym)
|
||||
{
|
||||
@@ -1531,6 +1576,15 @@ void Game::checkEvents()
|
||||
createItem(ItemType::CLOCK, players_.at(0)->getPosX(), players_.at(0)->getPosY() - 40);
|
||||
break;
|
||||
}
|
||||
case SDLK_5: // Crea un PathSprite
|
||||
{
|
||||
const auto x = players_.at(0)->getPosX();
|
||||
const SDL_Point start = {x, 200};
|
||||
const SDL_Point end = {x, 170};
|
||||
createPathSprite(start, end, game_text_textures_.at(3));
|
||||
path_sprites_.back()->addVerticalPath({x,170}, {x,0}, 100, 1);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ class Explosions;
|
||||
class Fade;
|
||||
class Input;
|
||||
class Item;
|
||||
class PathSprite;
|
||||
class Scoreboard;
|
||||
class Screen;
|
||||
class SmartSprite;
|
||||
@@ -129,6 +130,7 @@ private:
|
||||
std::vector<std::unique_ptr<Bullet>> bullets_; // Vector con las balas
|
||||
std::vector<std::unique_ptr<Item>> items_; // Vector con los items
|
||||
std::vector<std::unique_ptr<SmartSprite>> smart_sprites_; // Vector con los smartsprites
|
||||
std::vector<std::unique_ptr<PathSprite>> path_sprites_; // Vector con los smartsprites
|
||||
|
||||
std::shared_ptr<Texture> bullet_texture_; // Textura para las balas
|
||||
std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items
|
||||
@@ -305,17 +307,29 @@ private:
|
||||
// Crea un objeto SpriteSmart
|
||||
void createItemScoreSprite(int x, int y, std::shared_ptr<Texture> texture);
|
||||
|
||||
// Crea un objeto PathSprite
|
||||
void createPathSprite(SDL_Point x, SDL_Point y, std::shared_ptr<Texture> texture);
|
||||
|
||||
// Vacia el vector de smartsprites
|
||||
void freeSpriteSmarts();
|
||||
void freeSmartSprites();
|
||||
|
||||
// Vacia el vector de pathsprites
|
||||
void freePathSprites();
|
||||
|
||||
// Crea un SpriteSmart para arrojar el item café al recibir un impacto
|
||||
void throwCoffee(int x, int y);
|
||||
|
||||
// Actualiza los SpriteSmarts
|
||||
void updateSpriteSmarts();
|
||||
void updateSmartSprites();
|
||||
|
||||
// Pinta los SpriteSmarts activos
|
||||
void renderSpriteSmarts();
|
||||
void renderSmartSprites();
|
||||
|
||||
// Actualiza los PathSprites
|
||||
void updatePathSprites();
|
||||
|
||||
// Pinta los PathSprites activos
|
||||
void renderPathSprites();
|
||||
|
||||
// Acciones a realizar cuando el jugador muere
|
||||
void killPlayer(std::shared_ptr<Player> &player);
|
||||
|
||||
110
source/path_sprite.cpp
Normal file
110
source/path_sprite.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include "path_sprite.h"
|
||||
#include "utils.h"
|
||||
class Texture;
|
||||
|
||||
// Constructor
|
||||
PathSprite::PathSprite(std::shared_ptr<Texture> texture)
|
||||
: AnimatedSprite(texture) {}
|
||||
|
||||
// Actualiza la posición y comprueba si ha llegado a su destino
|
||||
void PathSprite::update()
|
||||
{
|
||||
if (enabled_)
|
||||
{
|
||||
moveThroughCurrentPath();
|
||||
goToNextPathOrDie();
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un path vertical
|
||||
void PathSprite::addVerticalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
||||
{
|
||||
paths_.emplace_back(createVerticalPath(start, end, steps), waiting_counter);
|
||||
}
|
||||
|
||||
// Añade un path horizontal
|
||||
void PathSprite::addHorizontalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter)
|
||||
{
|
||||
paths_.emplace_back(createHorizontalPath(start, end, steps), waiting_counter);
|
||||
}
|
||||
|
||||
// Devuelve un vector con los puntos que conforman la ruta
|
||||
std::vector<SDL_Point> PathSprite::createVerticalPath(SDL_Point start, 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 = static_cast<double>(start.y) + (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> 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});
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
// Habilita el objeto
|
||||
void PathSprite::enable()
|
||||
{
|
||||
enabled_ = true;
|
||||
}
|
||||
|
||||
void PathSprite::moveThroughCurrentPath()
|
||||
{
|
||||
auto &path = paths_.at(current_path_);
|
||||
|
||||
// Establece la posición
|
||||
const SDL_Point p = path.spots.at(path.counter);
|
||||
MovingSprite::setPos(p.x, p.y);
|
||||
|
||||
// Comprobar si ha terminado el recorrido
|
||||
if (!path.on_destination)
|
||||
{
|
||||
++path.counter;
|
||||
if (path.counter >= static_cast<int>(path.spots.size()))
|
||||
{
|
||||
path.on_destination = true;
|
||||
path.counter = static_cast<int>(path.spots.size()) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprobar si ha terminado la espera
|
||||
if (path.waiting_counter == 0)
|
||||
path.finished = true;
|
||||
else if (path.on_destination)
|
||||
--path.waiting_counter;
|
||||
}
|
||||
|
||||
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<int>(paths_.size()))
|
||||
enabled_ = false;
|
||||
}
|
||||
|
||||
// Indica si ha terminado todos los recorridos
|
||||
bool PathSprite::hasFinished()
|
||||
{
|
||||
return !enabled_;
|
||||
}
|
||||
61
source/path_sprite.h
Normal file
61
source/path_sprite.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory> // para shared_ptr
|
||||
#include "animated_sprite.h" // para SpriteAnimated
|
||||
class Texture;
|
||||
|
||||
// Clase PathSprite
|
||||
class PathSprite : public AnimatedSprite
|
||||
{
|
||||
private:
|
||||
// Estructuras
|
||||
struct Path
|
||||
{
|
||||
std::vector<SDL_Point> spots; // Puntos por los que se desplazará el sprite
|
||||
int waiting_counter; // Tiempo de espera una vez en el destino
|
||||
bool on_destination = false; // Indica si ha llegado al destino
|
||||
bool finished = false; // Indica si ha terminado de esperarse
|
||||
int counter = 0; // Contador interno
|
||||
|
||||
// Constructor
|
||||
Path(const std::vector<SDL_Point> &spots_init, int waiting_counter_init)
|
||||
: spots(spots_init), waiting_counter(waiting_counter_init) {}
|
||||
};
|
||||
|
||||
// Variables
|
||||
bool finished_ = false; // Indica si ya ha terminado
|
||||
bool enabled_ = false; // Indica si el objeto está habilitado
|
||||
int current_path_ = 0; // Path que se está recorriendo actualmente
|
||||
std::vector<Path> paths_; // Caminos a recorrer por el sprite
|
||||
|
||||
// Devuelve un vector con los puntos que conforman la ruta
|
||||
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 goToNextPathOrDie();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
explicit PathSprite(std::shared_ptr<Texture> texture);
|
||||
|
||||
// Destructor
|
||||
~PathSprite() = default;
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
void update() override;
|
||||
|
||||
// Añade un path vertical
|
||||
void addVerticalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter);
|
||||
|
||||
// Añade un path horizontal
|
||||
void addHorizontalPath(SDL_Point start, SDL_Point end, int steps, int waiting_counter);
|
||||
|
||||
// Habilita el objeto
|
||||
void enable();
|
||||
|
||||
// Indica si ha terminado todos los recorridos
|
||||
bool hasFinished();
|
||||
};
|
||||
@@ -127,6 +127,7 @@ bool SmartSprite::hasFinished() const
|
||||
return finished_;
|
||||
}
|
||||
|
||||
// Habilita el objeto
|
||||
void SmartSprite::setEnabled(bool value)
|
||||
{
|
||||
enabled_ = value;
|
||||
|
||||
@@ -53,5 +53,6 @@ public:
|
||||
// Obtiene el valor de la variable
|
||||
bool hasFinished() const;
|
||||
|
||||
// Habilita el objeto
|
||||
void setEnabled(bool value);
|
||||
};
|
||||
Reference in New Issue
Block a user