Reduida la dependencia de PathSprite a Sprite

Treballant en els missatges de text que ixen durant la partida
This commit is contained in:
2024-10-29 20:05:05 +01:00
parent d83c05bad4
commit ba05eab79e
7 changed files with 134 additions and 113 deletions

View File

@@ -201,14 +201,6 @@ void Game::setResources()
item_animations_.emplace_back(Resource::get()->getAnimation("item_coffee.ani")); item_animations_.emplace_back(Resource::get()->getAnimation("item_coffee.ani"));
item_animations_.emplace_back(Resource::get()->getAnimation("item_coffee_machine.ani")); item_animations_.emplace_back(Resource::get()->getAnimation("item_coffee_machine.ani"));
} }
// Texto
{
text_ = std::make_unique<Text>(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"));
text_nokia2_ = std::make_unique<Text>(Resource::get()->getTexture("nokia.png"), Resource::get()->getTextFile("nokia.txt"));
text_nokia2_big_ = std::make_unique<Text>(Resource::get()->getTexture("nokia.png"), Resource::get()->getTextFile("nokia.txt"));
text_04b_25_ = std::make_unique<Text>(Resource::get()->getTexture("04b_25.png"), Resource::get()->getTextFile("04b_25.txt"));
}
} }
// Crea una formación de enemigos // Crea una formación de enemigos
@@ -652,28 +644,28 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
{ {
player->addScore(1000); player->addScore(1000);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[0]->getWidth()) / 2; const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[0]->getWidth()) / 2;
createPathSprite(x, game_text_textures_[0]); createItemText(x, game_text_textures_[0]);
break; break;
} }
case ItemType::GAVINA: case ItemType::GAVINA:
{ {
player->addScore(2500); player->addScore(2500);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[1]->getWidth()) / 2; const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[1]->getWidth()) / 2;
createPathSprite(x, game_text_textures_[1]); createItemText(x, game_text_textures_[1]);
break; break;
} }
case ItemType::PACMAR: case ItemType::PACMAR:
{ {
player->addScore(5000); player->addScore(5000);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[2]->getWidth()) / 2; const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[2]->getWidth()) / 2;
createPathSprite(x, game_text_textures_[2]); createItemText(x, game_text_textures_[2]);
break; break;
} }
case ItemType::CLOCK: case ItemType::CLOCK:
{ {
enableTimeStopItem(); enableTimeStopItem();
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[5]->getWidth()) / 2; const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[5]->getWidth()) / 2;
createPathSprite(x, game_text_textures_[5]); createItemText(x, game_text_textures_[5]);
break; break;
} }
case ItemType::COFFEE: case ItemType::COFFEE:
@@ -682,13 +674,13 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
{ {
player->addScore(5000); player->addScore(5000);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[2]->getWidth()) / 2; const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[2]->getWidth()) / 2;
createPathSprite(x, game_text_textures_[2]); createItemText(x, game_text_textures_[2]);
} }
else else
{ {
player->giveExtraHit(); player->giveExtraHit();
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[4]->getWidth()) / 2; const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[4]->getWidth()) / 2;
createPathSprite(x, game_text_textures_[4]); createItemText(x, game_text_textures_[4]);
} }
break; break;
} }
@@ -697,7 +689,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
player->setPowerUp(); player->setPowerUp();
coffee_machine_enabled_ = false; coffee_machine_enabled_ = false;
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[3]->getWidth()) / 2; const auto x = item->getPosX() + (item->getWidth() - game_text_textures_[3]->getWidth()) / 2;
createPathSprite(x, game_text_textures_[3]); createItemText(x, game_text_textures_[3]);
break; break;
} }
default: default:
@@ -897,7 +889,7 @@ void Game::freeItems()
} }
// Crea un objeto PathSprite // Crea un objeto PathSprite
void Game::createPathSprite(int x, std::shared_ptr<Texture> texture) void Game::createItemText(int x, std::shared_ptr<Texture> texture)
{ {
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture)); path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
@@ -905,7 +897,7 @@ void Game::createPathSprite(int x, std::shared_ptr<Texture> texture)
const auto h = texture->getHeight(); const auto h = texture->getHeight();
const int y0 = param.game.play_area.rect.h - h; const int y0 = param.game.play_area.rect.h - h;
const int y1 = 160; const int y1 = 155;
const int y2 = -h; const int y2 = -h;
// Ajusta para que no se dibuje fuera de pantalla // Ajusta para que no se dibuje fuera de pantalla
@@ -920,6 +912,17 @@ void Game::createPathSprite(int x, std::shared_ptr<Texture> texture)
path_sprites_.back()->enable(); path_sprites_.back()->enable();
} }
// Crea un objeto PathSprite
void Game::createMessage(std::vector<Path> paths, std::shared_ptr<Texture> texture)
{
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
// Inicializa
for (const auto &path : paths)
path_sprites_.back()->addPath(path);
path_sprites_.back()->enable();
}
// Vacia el vector de smartsprites // Vacia el vector de smartsprites
void Game::freeSmartSprites() void Game::freeSmartSprites()
{ {
@@ -1286,8 +1289,11 @@ void Game::updateMenace()
void Game::renderMessages() void Game::renderMessages()
{ {
// GetReady // GetReady
if (counter_ < STAGE_COUNTER_ && !demo_.enabled) if (counter_ == 10 && !demo_.enabled)
text_nokia2_big_->write((int)get_ready_bitmap_path_[counter_], param.game.play_area.center_y - 8, lang::getText(75), -2); { // text_04b_25_->write2X((int)get_ready_bitmap_path_[counter_], param.game.play_area.center_y - 8, lang::getText(75), -2);
std::vector<Path> paths = {paths_.at(0), paths_.at(1)};
createMessage(paths, Resource::get()->getTexture("get_ready"));
}
// STAGE NUMBER // STAGE NUMBER
if (stage_bitmap_counter_ < STAGE_COUNTER_) if (stage_bitmap_counter_ < STAGE_COUNTER_)
@@ -1309,14 +1315,14 @@ void Game::renderMessages()
if (!game_completed_) if (!game_completed_)
{ {
// Escribe el número de fases restantes // Escribe el número de fases restantes
text_nokia2_big_->writeDX(TEXT_CENTER, param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_], text, -2, no_color, 2, shdw_txt_color); //text_04b_25_->write2X(param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_], text, -2);
} }
else else
{ {
// Escribe el texto de juego completado // Escribe el texto de juego completado
text = lang::getText(50); text = lang::getText(50);
text_nokia2_big_->writeDX(TEXT_CENTER, param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_], text, -2, no_color, 1, shdw_txt_color); //text_nokia2_big_->writeDX(TEXT_CENTER, param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_], text, -2, no_color, 1, shdw_txt_color);
text_nokia2_->writeDX(TEXT_CENTER, param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_] + text_nokia2_big_->getCharacterSize() + 2, lang::getText(76), -1, no_color, 1, shdw_txt_color); //text_nokia2_->writeDX(TEXT_CENTER, param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_] + text_nokia2_big_->getCharacterSize() + 2, lang::getText(76), -1, no_color, 1, shdw_txt_color);
} }
} }
} }
@@ -1379,6 +1385,18 @@ int Game::calculateScreenPower()
// Inicializa las variables que contienen puntos de ruta para mover objetos // Inicializa las variables que contienen puntos de ruta para mover objetos
void Game::initPaths() void Game::initPaths()
{ {
// Recorrido para el texto de "Get Ready!"
const auto &texture = Resource::get()->getTexture("get_ready");
const auto w = texture->getWidth();
const auto h = texture->getHeight();
const int x0 = -w;
const int x1 = param.game.play_area.center_x - w / 2;
const int x2 = param.game.play_area.rect.w;
const int y = param.game.play_area.center_y - h / 2;
paths_.emplace_back(Path(createPath(x0, x1, PathType::HORIZONTAL, y, 80, easeOutQuint), 10));
paths_.emplace_back(Path(createPath(x1, x2, PathType::HORIZONTAL, y, 80, easeInQuint), 0));
// Vector con los valores del seno para 360 grados // Vector con los valores del seno para 360 grados
float sin[360]; float sin[360];
for (int i = 0; i < 360; ++i) for (int i = 0; i < 360; ++i)
@@ -1404,33 +1422,6 @@ void Game::initPaths()
int index = static_cast<int>(((i - 149) * 1.8f) + 90) % 360; int index = static_cast<int>(((i - 149) * 1.8f) + 90) % 360;
stage_bitmap_path_[i] = sin[index] * (center_point + 17) - 17; stage_bitmap_path_[i] = sin[index] * (center_point + 17) - 17;
} }
// Letrero de GetReady
const auto size = text_nokia2_big_->lenght(lang::getText(75), -2);
const float start1 = param.game.play_area.rect.x - size;
const float finish1 = param.game.play_area.center_x - (size / 2);
const float finish2 = param.game.play_area.rect.w;
const float distance1 = finish1 - start1;
const float distance2 = finish2 - finish1;
for (int i = 0; i < first_part; ++i)
{
get_ready_bitmap_path_[i] = sin[(int)(i * 1.8f)];
get_ready_bitmap_path_[i] *= distance1;
get_ready_bitmap_path_[i] -= size;
}
for (int i = first_part; i < second_part; ++i)
get_ready_bitmap_path_[i] = (int)finish1;
for (int i = second_part; i < STAGE_COUNTER_; ++i)
{
get_ready_bitmap_path_[i] = sin[(int)((i - second_part) * 1.8f)];
get_ready_bitmap_path_[i] *= distance2;
get_ready_bitmap_path_[i] += finish1;
}
} }
// Actualiza el tramo final de juego, una vez completado // Actualiza el tramo final de juego, una vez completado
@@ -1565,7 +1556,7 @@ void Game::checkEvents()
case SDLK_5: // Crea un PathSprite case SDLK_5: // Crea un PathSprite
{ {
const int x = players_.at(0)->getPosX() + (players_.at(0)->getWidth() - game_text_textures_[3]->getWidth()) / 2; const int x = players_.at(0)->getPosX() + (players_.at(0)->getWidth() - game_text_textures_[3]->getWidth()) / 2;
createPathSprite(x, game_text_textures_.at(3)); createItemText(x, game_text_textures_.at(3));
break; break;
} }
default: default:

View File

@@ -10,6 +10,7 @@
#include "options.h" // Para Options, OptionsGame, options #include "options.h" // Para Options, OptionsGame, options
#include "player.h" // Para Player #include "player.h" // Para Player
#include "utils.h" // Para Demo #include "utils.h" // Para Demo
#include "path_sprite.h"
class Asset; // lines 12-12 class Asset; // lines 12-12
class Background; // lines 13-13 class Background; // lines 13-13
class BalloonFormations; // lines 14-14 class BalloonFormations; // lines 14-14
@@ -144,12 +145,8 @@ private:
std::vector<std::vector<std::string>> balloon_animations_; // Vector con las animaciones de los globos std::vector<std::vector<std::string>> balloon_animations_; // Vector con las animaciones de los globos
std::vector<std::vector<std::string>> explosions_animations_; // Vector con las animaciones de las explosiones std::vector<std::vector<std::string>> explosions_animations_; // Vector con las animaciones de las explosiones
std::unique_ptr<Text> text_; // Fuente para los textos del juego
std::unique_ptr<Text> text_04b_25_; // Fuente de texto grande
std::unique_ptr<Text> text_nokia2_; // Otra fuente de texto para mensajes
std::unique_ptr<Text> text_nokia2_big_; // Y la versión en grande
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades std::unique_ptr<Fade> fade_; // Objeto para renderizar fades
std::vector<Path> paths_; // Vector con los recorridos precalculados almacenados
// Variables // Variables
HiScoreEntry hi_score_ = HiScoreEntry( HiScoreEntry hi_score_ = HiScoreEntry(
@@ -304,7 +301,10 @@ private:
void freeItems(); void freeItems();
// Crea un objeto PathSprite // Crea un objeto PathSprite
void createPathSprite(int x, std::shared_ptr<Texture> texture); void createItemText(int x, std::shared_ptr<Texture> texture);
// Crea un objeto PathSprite
void createMessage(std::vector<Path> paths, std::shared_ptr<Texture> texture);
// Vacia el vector de smartsprites // Vacia el vector de smartsprites
void freeSmartSprites(); void freeSmartSprites();

View File

@@ -7,34 +7,8 @@
#include <functional> #include <functional>
class Texture; // lines 3-3 class Texture; // lines 3-3
// 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 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(createPath(start, end, type, fixed_pos, steps, easingFunction), waiting_counter);
}
// Añade un recorrido
void PathSprite::addPath(std::vector<SDL_Point> spots, int waiting_counter)
{
paths_.emplace_back(std::move(spots), waiting_counter);
}
// 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::createPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction) std::vector<SDL_Point> createPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction)
{ {
std::vector<SDL_Point> v; std::vector<SDL_Point> v;
v.reserve(steps); v.reserve(steps);
@@ -69,6 +43,34 @@ std::vector<SDL_Point> PathSprite::createPath(int start, int end, PathType type,
return v; return v;
} }
// Actualiza la posición y comprueba si ha llegado a su destino
void PathSprite::update()
{
if (enabled_)
{
moveThroughCurrentPath();
goToNextPathOrDie();
}
}
// Añade un recorrido
void PathSprite::addPath(Path path)
{
paths_.emplace_back(path);
}
// 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(createPath(start, end, type, fixed_pos, steps, easingFunction), waiting_counter);
}
// Añade un recorrido
void PathSprite::addPath(std::vector<SDL_Point> spots, int waiting_counter)
{
paths_.emplace_back(std::move(spots), waiting_counter);
}
// Habilita el objeto // Habilita el objeto
void PathSprite::enable() void PathSprite::enable()
{ {
@@ -82,7 +84,7 @@ void PathSprite::moveThroughCurrentPath()
// Establece la posición // Establece la posición
const auto &p = path.spots.at(path.counter); const auto &p = path.spots.at(path.counter);
MovingSprite::setPos(p.x, p.y); setPosition(p);
// Comprobar si ha terminado el recorrido // Comprobar si ha terminado el recorrido
if (!path.on_destination) if (!path.on_destination)

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <SDL2/SDL_rect.h> // Para SDL_Point #include <SDL2/SDL_rect.h> // Para SDL_Point
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include <functional> #include <functional>
#include <vector> // Para vector #include <vector> // Para vector
#include "animated_sprite.h" // Para AnimatedSprite #include "animated_sprite.h" // Para AnimatedSprite
@@ -13,24 +13,27 @@ enum class PathType
HORIZONTAL, HORIZONTAL,
}; };
// 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) {}
};
// Devuelve un vector con los puntos que conforman la ruta
std::vector<SDL_Point> createPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction);
// Clase PathSprite // Clase PathSprite
class PathSprite : public AnimatedSprite class PathSprite : public Sprite
{ {
private: 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 // Variables
bool finished_ = false; // Indica si ya ha terminado bool finished_ = false; // Indica si ya ha terminado
bool enabled_ = false; // Indica si el objeto está habilitado bool enabled_ = false; // Indica si el objeto está habilitado
@@ -45,21 +48,20 @@ private:
public: public:
// Constructor // Constructor
explicit PathSprite(std::shared_ptr<Texture> texture); explicit PathSprite(std::shared_ptr<Texture> texture)
: Sprite(texture) {}
// Destructor // Destructor
~PathSprite() = default; ~PathSprite() = default;
// Actualiza la posición del sprite // Actualiza la posición del sprite
void update() override; void update();
// Añade un recorrido // Añade un recorrido
void addPath(Path path);
void addPath(std::vector<SDL_Point> spots, int waiting_counter); void addPath(std::vector<SDL_Point> spots, int waiting_counter);
void addPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction, int waiting_counter); void addPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction, int waiting_counter);
// Devuelve un vector con los puntos que conforman la ruta
std::vector<SDL_Point> createPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction);
// Habilita el objeto // Habilita el objeto
void enable(); void enable();

View File

@@ -3,6 +3,7 @@
#include <iostream> // Para basic_ostream, operator<<, endl, cout, cerr #include <iostream> // Para basic_ostream, operator<<, endl, cout, cerr
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
#include "asset.h" // Para Asset, AssetType #include "asset.h" // Para Asset, AssetType
#include "lang.h" // Para lang
#include "jail_audio.h" // Para JA_LoadMusic, JA_LoadSound #include "jail_audio.h" // Para JA_LoadMusic, JA_LoadSound
#include "screen.h" // Para Screen #include "screen.h" // Para Screen
#include "text.h" // Para Text #include "text.h" // Para Text
@@ -250,7 +251,17 @@ void Resource::createTextures()
std::cout << "\n>> CREATING TEXTURES" << std::endl; std::cout << "\n>> CREATING TEXTURES" << std::endl;
for (const auto &s : strings) for (const auto &s : strings)
{ {
textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, -2))); textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2)));
printWithDots("Texture : ", s.name, "[ DONE ]");
}
std::vector<NameAndText> strings2X = {
NameAndText("get_ready", lang::getText(75)),
NameAndText("stages_remaining", "9 " + lang::getText(38))};
for (const auto &s : strings2X)
{
textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, 2, -2)));
printWithDots("Texture : ", s.name, "[ DONE ]"); printWithDots("Texture : ", s.name, "[ DONE ]");
} }
} }

View File

@@ -139,16 +139,30 @@ void Text::write(int x, int y, const std::string &text, int kerning, int lenght)
} }
} }
// Escribe texto en pantalla
void Text::write2X(int x, int y, const std::string &text, int kerning)
{
int shift = 0;
for (size_t i = 0; i < text.length(); ++i)
{
auto index = static_cast<size_t>(text[i]);
SDL_Rect rect = {offset_[index].x, offset_[index].y, box_width_, box_height_};
sprite_->getTexture()->render(x + shift, y, &rect, 2.0f, 2.0f);
shift += (offset_[index].w + kerning) * 2;
}
}
// Escribe el texto en una textura // Escribe el texto en una textura
std::shared_ptr<Texture> Text::writeToTexture(const std::string &text, int kerning) std::shared_ptr<Texture> Text::writeToTexture(const std::string &text, int zoom, int kerning)
{ {
auto renderer = Screen::get()->getRenderer(); auto renderer = Screen::get()->getRenderer();
auto texture = std::make_shared<Texture>(renderer); auto texture = std::make_shared<Texture>(renderer);
auto width = lenght(text, kerning); auto width = lenght(text, kerning) * zoom;
texture->createBlank(width, box_height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); auto height = box_height_ * zoom;
texture->setBlendMode(SDL_BLENDMODE_BLEND); texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET);
texture->setBlendMode(SDL_BLENDMODE_BLEND);
texture->setAsRenderTarget(renderer); texture->setAsRenderTarget(renderer);
write(0, 0, text, kerning); zoom == 1 ? write(0, 0, text, kerning) : write2X(0, 0, text, kerning);
return texture; return texture;
} }

View File

@@ -50,9 +50,10 @@ public:
// Escribe el texto en pantalla // Escribe el texto en pantalla
void write(int x, int y, const std::string &text, int kerning = 1, int lenght = -1); void write(int x, int y, const std::string &text, int kerning = 1, int lenght = -1);
void write2X(int x, int y, const std::string &text, int kerning = 1);
// Escribe el texto en una textura // Escribe el texto en una textura
std::shared_ptr<Texture> writeToTexture(const std::string &text, int kerning = 1); std::shared_ptr<Texture> writeToTexture(const std::string &text, int zoom = 1, int kerning = 1);
// Escribe el texto con colores // Escribe el texto con colores
void writeColored(int x, int y, const std::string &text, Color color, int kerning = 1, int lenght = -1); void writeColored(int x, int y, const std::string &text, Color color, int kerning = 1, int lenght = -1);