segon commit
This commit is contained in:
106
source/game/defaults.hpp
Normal file
106
source/game/defaults.hpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen::Filter
|
||||
#include "utils/defines.hpp" // Para GameCanvas
|
||||
#include "utils/utils.hpp" // Para PaletteColor
|
||||
|
||||
// Forward declarations from Options namespace
|
||||
namespace Options {
|
||||
// enum class ControlScheme;
|
||||
enum class NotificationPosition;
|
||||
} // namespace Options
|
||||
|
||||
namespace Defaults::Canvas {
|
||||
constexpr int WIDTH = GameCanvas::WIDTH; // Ancho del canvas del juego (256)
|
||||
constexpr int HEIGHT = GameCanvas::HEIGHT; // Alto del canvas del juego (192)
|
||||
} // namespace Defaults::Canvas
|
||||
|
||||
namespace Defaults::Window {
|
||||
constexpr int ZOOM = 2; // Zoom de la ventana por defecto
|
||||
} // namespace Defaults::Window
|
||||
|
||||
namespace Defaults::Video {
|
||||
constexpr bool FULLSCREEN = false; // Modo de pantalla completa por defecto (false = ventana)
|
||||
constexpr Screen::Filter FILTER = Screen::Filter::NEAREST; // Filtro por defecto
|
||||
constexpr bool VERTICAL_SYNC = true; // Vsync activado por defecto
|
||||
constexpr bool SHADER_ENABLED = false; // Shaders de post-procesado desactivados por defecto
|
||||
constexpr bool SUPERSAMPLING = false; // Supersampling desactivado por defecto
|
||||
constexpr bool INTEGER_SCALE = true; // Escalado entero activado por defecto
|
||||
constexpr bool KEEP_ASPECT = true; // Mantener aspecto activado por defecto
|
||||
constexpr const char* PALETTE_NAME = "cpc"; // Paleta por defecto
|
||||
constexpr const char* PALETTE_SORT = "original"; // Modo de ordenación de paleta por defecto
|
||||
constexpr bool LINEAR_UPSCALE = false; // Upscale NEAREST por defecto
|
||||
constexpr int DOWNSCALE_ALGO = 1; // Downscale Lanczos2 por defecto
|
||||
constexpr bool GPU_ACCELERATION = true; // Aceleración GPU activada por defecto
|
||||
} // namespace Defaults::Video
|
||||
|
||||
namespace Defaults::Border {
|
||||
constexpr bool ENABLED = true; // Borde activado por defecto
|
||||
constexpr int WIDTH = 32; // Ancho del borde por defecto
|
||||
constexpr int HEIGHT = 24; // Alto del borde por defectoF
|
||||
} // namespace Defaults::Border
|
||||
|
||||
namespace Defaults::Audio {
|
||||
constexpr float VOLUME = 1.0F; // Volumen por defecto
|
||||
constexpr bool ENABLED = true; // Audio por defecto
|
||||
} // namespace Defaults::Audio
|
||||
|
||||
namespace Defaults::Music {
|
||||
constexpr float VOLUME = 0.8F; // Volumen por defecto de la musica
|
||||
constexpr bool ENABLED = true; // Musica habilitada por defecto
|
||||
} // namespace Defaults::Music
|
||||
|
||||
namespace Defaults::Sound {
|
||||
constexpr float VOLUME = 1.0F; // Volumen por defecto de los efectos de sonido
|
||||
constexpr bool ENABLED = true; // Sonido habilitado por defecto
|
||||
} // namespace Defaults::Sound
|
||||
|
||||
namespace Defaults::Cheat {
|
||||
constexpr bool INFINITE_LIVES = false; // Vidas infinitas desactivadas por defecto
|
||||
constexpr bool INVINCIBLE = false; // Invencibilidad desactivada por defecto
|
||||
constexpr bool JAIL_IS_OPEN = false; // Jail abierta desactivada por defecto
|
||||
} // namespace Defaults::Cheat
|
||||
|
||||
namespace Defaults::Stats {
|
||||
constexpr int ROOMS = 0; // Habitaciones visitadas por defecto
|
||||
constexpr int ITEMS = 0; // Items obtenidos por defecto
|
||||
} // namespace Defaults::Stats
|
||||
|
||||
namespace Defaults::Controls {
|
||||
constexpr SDL_Scancode KEY_LEFT = SDL_SCANCODE_LEFT; // Tecla izquierda por defecto
|
||||
constexpr SDL_Scancode KEY_RIGHT = SDL_SCANCODE_RIGHT; // Tecla derecha por defecto
|
||||
constexpr SDL_Scancode KEY_JUMP = SDL_SCANCODE_UP; // Tecla salto por defecto
|
||||
|
||||
constexpr int GAMEPAD_BUTTON_LEFT = SDL_GAMEPAD_BUTTON_DPAD_LEFT; // Botón izquierda por defecto
|
||||
constexpr int GAMEPAD_BUTTON_RIGHT = SDL_GAMEPAD_BUTTON_DPAD_RIGHT; // Botón derecha por defecto
|
||||
constexpr int GAMEPAD_BUTTON_JUMP = SDL_GAMEPAD_BUTTON_WEST; // Botón salto por defecto
|
||||
} // namespace Defaults::Controls
|
||||
|
||||
namespace Defaults::Kiosk {
|
||||
constexpr bool ENABLED = false; // Modo kiosko desactivado por defecto
|
||||
constexpr const char* TEXT = "KIOSK MODE"; // Texto del modo kiosko por defecto
|
||||
constexpr bool INFINITE_LIVES = true; // Vidas infinitas en modo kiosko desactivadas por defecto
|
||||
} // namespace Defaults::Kiosk
|
||||
|
||||
namespace Defaults::Localization {
|
||||
constexpr const char* LANGUAGE = "ca"; // Idioma por defecto (en = inglés, ca = catalán)
|
||||
} // namespace Defaults::Localization
|
||||
|
||||
namespace Defaults::Game::Items {
|
||||
constexpr const float PERCENT_TO_OPEN_THE_JAIL = 0.9F; // Porcentaje de items necesarios para abrir la jail
|
||||
} // namespace Defaults::Game::Items
|
||||
|
||||
namespace Defaults::Game::Room {
|
||||
constexpr const char* INITIAL = "03.yaml"; // Habitación de inicio
|
||||
constexpr const char* END_ROOM = "01"; // Habitación final (jail)
|
||||
} // namespace Defaults::Game::Room
|
||||
|
||||
namespace Defaults::Game::Player {
|
||||
constexpr int SPAWN_X = 25 * Tile::SIZE; // Posición X inicial
|
||||
constexpr int SPAWN_Y = 13 * Tile::SIZE; // Posición Y inicial
|
||||
constexpr SDL_FlipMode SPAWN_FLIP = Flip::LEFT; // Orientación inicial
|
||||
constexpr const char* SKIN = "default"; // Skin del jugador por defecto
|
||||
constexpr int COLOR = -1; // Color del jugador (-1 = automático según cheats)
|
||||
} // namespace Defaults::Game::Player
|
||||
89
source/game/editor/editor_statusbar.cpp
Normal file
89
source/game/editor/editor_statusbar.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include "game/editor/editor_statusbar.hpp"
|
||||
|
||||
#include <string> // Para to_string
|
||||
#include <utility>
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource::Cache
|
||||
#include "game/options.hpp" // Para Options::game
|
||||
#include "utils/defines.hpp" // Para Tile::SIZE
|
||||
#include "utils/utils.hpp" // Para stringToColor, toLower
|
||||
|
||||
// Constructor
|
||||
EditorStatusBar::EditorStatusBar(std::string room_number, std::string room_name)
|
||||
: room_number_(std::move(room_number)),
|
||||
room_name_(std::move(room_name)) {
|
||||
const float SURFACE_WIDTH = Options::game.width;
|
||||
constexpr float SURFACE_HEIGHT = 6.0F * Tile::SIZE; // 48 pixels, igual que el scoreboard
|
||||
|
||||
surface_ = std::make_shared<Surface>(SURFACE_WIDTH, SURFACE_HEIGHT);
|
||||
surface_dest_ = {.x = 0, .y = Options::game.height - SURFACE_HEIGHT, .w = SURFACE_WIDTH, .h = SURFACE_HEIGHT};
|
||||
}
|
||||
|
||||
// Pinta la barra de estado en pantalla
|
||||
void EditorStatusBar::render() {
|
||||
surface_->render(nullptr, &surface_dest_);
|
||||
}
|
||||
|
||||
// Actualiza la barra de estado
|
||||
void EditorStatusBar::update([[maybe_unused]] float delta_time) {
|
||||
fillTexture();
|
||||
}
|
||||
|
||||
void EditorStatusBar::setMouseTile(int tile_x, int tile_y) {
|
||||
mouse_tile_x_ = tile_x;
|
||||
mouse_tile_y_ = tile_y;
|
||||
}
|
||||
|
||||
void EditorStatusBar::setLine2(const std::string& text) { line2_ = text; }
|
||||
void EditorStatusBar::setLine3(const std::string& text) { line3_ = text; }
|
||||
void EditorStatusBar::setLine4(const std::string& text) { line4_ = text; }
|
||||
void EditorStatusBar::setLine5(const std::string& text) { line5_ = text; }
|
||||
|
||||
// Dibuja los elementos en la surface
|
||||
void EditorStatusBar::fillTexture() {
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface_);
|
||||
|
||||
surface_->clear(stringToColor("black"));
|
||||
|
||||
auto text = Resource::Cache::get()->getText("8bithud");
|
||||
const Uint8 LABEL_COLOR = stringToColor("bright_cyan");
|
||||
const Uint8 VALUE_COLOR = stringToColor("white");
|
||||
const Uint8 DETAIL_COLOR = stringToColor("bright_yellow");
|
||||
|
||||
// Línea 1: Nombre de la habitación
|
||||
text->writeColored(LEFT_X, LINE1_Y, toLower(room_number_ + " " + room_name_), LABEL_COLOR);
|
||||
|
||||
// Línea 2: Propiedades de room o info de enemigo
|
||||
if (!line2_.empty()) {
|
||||
text->writeColored(LEFT_X, LINE2_Y, toLower(line2_), DETAIL_COLOR);
|
||||
}
|
||||
|
||||
// Línea 3: Conexiones+items o propiedades del enemigo
|
||||
if (!line3_.empty()) {
|
||||
text->writeColored(LEFT_X, LINE3_Y, toLower(line3_), VALUE_COLOR);
|
||||
}
|
||||
|
||||
// Línea 4: Extra
|
||||
if (!line4_.empty()) {
|
||||
text->writeColored(LEFT_X, LINE4_Y, toLower(line4_), DETAIL_COLOR);
|
||||
}
|
||||
|
||||
// Línea 5: Tile coords + drag info
|
||||
const std::string TILE_X_STR = (mouse_tile_x_ < 10 ? "0" : "") + std::to_string(mouse_tile_x_);
|
||||
const std::string TILE_Y_STR = (mouse_tile_y_ < 10 ? "0" : "") + std::to_string(mouse_tile_y_);
|
||||
std::string line5 = "tile:" + TILE_X_STR + "," + TILE_Y_STR;
|
||||
if (!line5_.empty()) {
|
||||
line5 += " " + line5_;
|
||||
}
|
||||
text->writeColored(LEFT_X, LINE5_Y, toLower(line5), stringToColor("bright_green"));
|
||||
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
}
|
||||
|
||||
#endif // _DEBUG
|
||||
52
source/game/editor/editor_statusbar.hpp
Normal file
52
source/game/editor/editor_statusbar.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
|
||||
class Surface;
|
||||
|
||||
class EditorStatusBar {
|
||||
public:
|
||||
EditorStatusBar(std::string room_number, std::string room_name);
|
||||
~EditorStatusBar() = default;
|
||||
|
||||
void render();
|
||||
void update(float delta_time);
|
||||
void setMouseTile(int tile_x, int tile_y);
|
||||
void setLine2(const std::string& text);
|
||||
void setLine3(const std::string& text);
|
||||
void setLine4(const std::string& text);
|
||||
void setLine5(const std::string& text);
|
||||
|
||||
private:
|
||||
void fillTexture(); // Dibuja los elementos en la surface
|
||||
|
||||
// Constantes de posición (en pixels dentro de la surface de 256x48)
|
||||
// Font 8bithud lowercase = 6px alto → 5 líneas con 8px de separación
|
||||
static constexpr int LINE1_Y = 2; // Nombre de la habitación
|
||||
static constexpr int LINE2_Y = 10; // Propiedades de room / enemy info
|
||||
static constexpr int LINE3_Y = 18; // Conexiones+items / enemy detail
|
||||
static constexpr int LINE4_Y = 26; // Extra
|
||||
static constexpr int LINE5_Y = 34; // Tile coords + drag info
|
||||
static constexpr int LEFT_X = 4; // Margen izquierdo
|
||||
|
||||
// Objetos
|
||||
std::shared_ptr<Surface> surface_; // Surface donde dibujar la barra
|
||||
SDL_FRect surface_dest_{}; // Rectángulo destino en pantalla
|
||||
|
||||
// Variables
|
||||
std::string room_number_; // Número de la habitación
|
||||
std::string room_name_; // Nombre de la habitación
|
||||
int mouse_tile_x_{0}; // Coordenada X del ratón en tiles
|
||||
int mouse_tile_y_{0}; // Coordenada Y del ratón en tiles
|
||||
std::string line2_; // Contenido de la línea 2
|
||||
std::string line3_; // Contenido de la línea 3
|
||||
std::string line4_; // Contenido de la línea 4
|
||||
std::string line5_; // Contenido de la línea 5
|
||||
};
|
||||
|
||||
#endif // _DEBUG
|
||||
1633
source/game/editor/map_editor.cpp
Normal file
1633
source/game/editor/map_editor.cpp
Normal file
File diff suppressed because it is too large
Load Diff
160
source/game/editor/map_editor.hpp
Normal file
160
source/game/editor/map_editor.hpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr, unique_ptr
|
||||
#include <string> // Para string
|
||||
|
||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||
#include "game/editor/mini_map.hpp" // Para MiniMap
|
||||
#include "game/editor/tile_picker.hpp" // Para TilePicker
|
||||
#include "game/entities/enemy.hpp" // Para Enemy::Data
|
||||
#include "game/entities/item.hpp" // Para Item::Data
|
||||
#include "game/entities/player.hpp" // Para Player::SpawnData
|
||||
#include "game/gameplay/room.hpp" // Para Room::Data
|
||||
#include "game/gameplay/scoreboard.hpp" // Para Scoreboard::Data
|
||||
#include "game/options.hpp" // Para Options::Cheat
|
||||
|
||||
class EditorStatusBar;
|
||||
|
||||
class MapEditor {
|
||||
public:
|
||||
static void init(); // [SINGLETON] Crea el objeto
|
||||
static void destroy(); // [SINGLETON] Destruye el objeto
|
||||
static auto get() -> MapEditor*; // [SINGLETON] Obtiene el objeto
|
||||
|
||||
void enter(std::shared_ptr<Room> room, std::shared_ptr<Player> player, const std::string& room_path, std::shared_ptr<Scoreboard::Data> scoreboard_data);
|
||||
void exit();
|
||||
[[nodiscard]] auto isActive() const -> bool { return active_; }
|
||||
|
||||
void update(float delta_time);
|
||||
void render();
|
||||
void handleEvent(const SDL_Event& event);
|
||||
auto revert() -> std::string;
|
||||
|
||||
// Comandos para enemigos (llamados desde console_commands)
|
||||
auto setEnemyProperty(const std::string& property, const std::string& value) -> std::string;
|
||||
auto addEnemy() -> std::string;
|
||||
auto deleteEnemy() -> std::string;
|
||||
auto duplicateEnemy() -> std::string;
|
||||
[[nodiscard]] auto hasSelectedEnemy() const -> bool;
|
||||
[[nodiscard]] auto getSetCompletions() const -> std::vector<std::string>;
|
||||
|
||||
// Comandos para propiedades de la habitación
|
||||
auto setRoomProperty(const std::string& property, const std::string& value) -> std::string;
|
||||
auto createNewRoom(const std::string& direction = "") -> std::string;
|
||||
auto deleteRoom() -> std::string;
|
||||
|
||||
// Opciones del editor (llamados desde console_commands / teclas)
|
||||
auto showInfo(bool show) -> std::string;
|
||||
auto showGrid(bool show) -> std::string;
|
||||
[[nodiscard]] auto isGridEnabled() const -> bool { return settings_.grid; }
|
||||
void toggleMiniMap();
|
||||
void setReenter(bool value) { reenter_ = value; }
|
||||
auto setMiniMapBg(const std::string& color) -> std::string;
|
||||
auto setMiniMapConn(const std::string& color) -> std::string;
|
||||
|
||||
// Comandos para items
|
||||
auto setItemProperty(const std::string& property, const std::string& value) -> std::string;
|
||||
auto addItem() -> std::string;
|
||||
auto deleteItem() -> std::string;
|
||||
auto duplicateItem() -> std::string;
|
||||
[[nodiscard]] auto hasSelectedItem() const -> bool;
|
||||
void openTilePicker(const std::string& tileset_name, int current_tile);
|
||||
|
||||
private:
|
||||
static MapEditor* instance_; // NOLINT(readability-identifier-naming) [SINGLETON] Objeto privado
|
||||
|
||||
MapEditor(); // Constructor
|
||||
~MapEditor(); // Destructor
|
||||
|
||||
// Opciones persistentes del editor
|
||||
struct Settings {
|
||||
bool grid{false};
|
||||
bool show_render_info{false};
|
||||
std::string minimap_bg{"blue"};
|
||||
std::string minimap_conn{"white"};
|
||||
};
|
||||
Settings settings_;
|
||||
void loadSettings();
|
||||
void saveSettings() const;
|
||||
|
||||
// Tipos para drag & drop y selección
|
||||
enum class DragTarget { NONE,
|
||||
PLAYER,
|
||||
ENEMY_INITIAL,
|
||||
ENEMY_BOUND1,
|
||||
ENEMY_BOUND2,
|
||||
ITEM };
|
||||
|
||||
struct DragState {
|
||||
DragTarget target{DragTarget::NONE};
|
||||
int index{-1};
|
||||
float offset_x{0.0F};
|
||||
float offset_y{0.0F};
|
||||
float snap_x{0.0F};
|
||||
float snap_y{0.0F};
|
||||
bool moved{false}; // true si el ratón se movió durante el drag
|
||||
};
|
||||
|
||||
// Métodos internos
|
||||
void updateMousePosition();
|
||||
void renderEnemyBoundaries();
|
||||
static void renderBoundaryMarker(float x, float y, Uint8 color);
|
||||
void renderSelectionHighlight();
|
||||
void renderGrid() const;
|
||||
void handleMouseDown(float game_x, float game_y);
|
||||
void handleMouseUp();
|
||||
void updateDrag();
|
||||
void autosave();
|
||||
void updateStatusBarInfo();
|
||||
static auto snapToGrid(float value) -> float;
|
||||
static auto pointInRect(float px, float py, const SDL_FRect& rect) -> bool;
|
||||
|
||||
// Estado del editor
|
||||
bool active_{false};
|
||||
DragState drag_;
|
||||
int selected_enemy_{-1}; // Índice del enemigo seleccionado (-1 = ninguno)
|
||||
int selected_item_{-1}; // Índice del item seleccionado (-1 = ninguno)
|
||||
static constexpr int NO_BRUSH = -2; // Sin brush activo
|
||||
static constexpr int ERASER_BRUSH = -1; // Brush borrador (pinta tile vacío = -1)
|
||||
int brush_tile_{NO_BRUSH}; // Tile activo para pintar
|
||||
bool painting_{false}; // true mientras se está pintando con click izquierdo mantenido
|
||||
|
||||
// Datos de la habitación
|
||||
Room::Data room_data_;
|
||||
std::string room_path_;
|
||||
std::string file_path_;
|
||||
|
||||
// YAML: nodo original (para campos que no se editan: name_ca, etc.)
|
||||
fkyaml::node yaml_;
|
||||
fkyaml::node yaml_backup_;
|
||||
|
||||
// Referencias a objetos vivos
|
||||
std::shared_ptr<Room> room_;
|
||||
std::shared_ptr<Player> player_;
|
||||
std::shared_ptr<Scoreboard::Data> scoreboard_data_;
|
||||
|
||||
// Barra de estado del editor
|
||||
std::unique_ptr<EditorStatusBar> statusbar_;
|
||||
|
||||
// Tile picker y mini mapa
|
||||
TilePicker tile_picker_;
|
||||
std::unique_ptr<MiniMap> mini_map_;
|
||||
bool mini_map_visible_{false};
|
||||
|
||||
// Estado del ratón
|
||||
float mouse_game_x_{0.0F};
|
||||
float mouse_game_y_{0.0F};
|
||||
int mouse_tile_x_{0};
|
||||
int mouse_tile_y_{0};
|
||||
|
||||
// Estado previo (para restaurar al salir)
|
||||
Options::Cheat::State invincible_before_editor_{Options::Cheat::State::DISABLED};
|
||||
bool render_info_before_editor_{false};
|
||||
bool reenter_{false}; // true cuando es un re-enter tras cambio de room (no tocar render_info)
|
||||
};
|
||||
|
||||
#endif // _DEBUG
|
||||
391
source/game/editor/mini_map.cpp
Normal file
391
source/game/editor/mini_map.cpp
Normal file
@@ -0,0 +1,391 @@
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include "game/editor/mini_map.hpp"
|
||||
|
||||
#include <algorithm> // Para std::max, std::min
|
||||
#include <array> // Para std::array
|
||||
#include <cmath> // Para std::floor
|
||||
#include <iostream> // Para cout
|
||||
#include <map> // Para std::map
|
||||
#include <queue> // Para queue (BFS)
|
||||
#include <set> // Para set
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource::Cache
|
||||
#include "game/gameplay/room.hpp" // Para Room::Data
|
||||
#include "utils/defines.hpp" // Para Tile::SIZE, PlayArea
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
// Constructor: construye todo el minimapa
|
||||
MiniMap::MiniMap(Uint8 bg_color, Uint8 conn_color)
|
||||
: bg_color_(bg_color),
|
||||
conn_color_(conn_color) {
|
||||
buildTileColorTable("standard.gif");
|
||||
layoutRooms();
|
||||
buildRoomSurfaces();
|
||||
composeFinalSurface();
|
||||
}
|
||||
|
||||
// Regenera la surface final con nuevo color de fondo
|
||||
void MiniMap::rebuild(Uint8 bg_color, Uint8 conn_color) {
|
||||
bg_color_ = bg_color;
|
||||
conn_color_ = conn_color;
|
||||
composeFinalSurface();
|
||||
}
|
||||
|
||||
// Analiza el tileset y crea tabla: tile_index → color predominante
|
||||
void MiniMap::buildTileColorTable(const std::string& tileset_name) {
|
||||
auto tileset = Resource::Cache::get()->getSurface(tileset_name);
|
||||
if (!tileset) { return; }
|
||||
|
||||
tileset_width_ = static_cast<int>(tileset->getWidth()) / Tile::SIZE;
|
||||
tileset_transparent_ = tileset->getTransparentColor();
|
||||
int tileset_height = static_cast<int>(tileset->getHeight()) / Tile::SIZE;
|
||||
int total_tiles = tileset_width_ * tileset_height;
|
||||
|
||||
tile_colors_.resize(total_tiles, 0);
|
||||
|
||||
for (int tile = 0; tile < total_tiles; ++tile) {
|
||||
int tile_x = (tile % tileset_width_) * Tile::SIZE;
|
||||
int tile_y = (tile / tileset_width_) * Tile::SIZE;
|
||||
|
||||
// Contar frecuencia de cada color en el tile (ignorar el color transparente del tileset)
|
||||
Uint8 transparent = tileset->getTransparentColor();
|
||||
std::array<int, 256> freq{};
|
||||
for (int y = 0; y < Tile::SIZE; ++y) {
|
||||
for (int x = 0; x < Tile::SIZE; ++x) {
|
||||
Uint8 pixel = tileset->getPixel(tile_x + x, tile_y + y);
|
||||
if (pixel != transparent) {
|
||||
freq[pixel]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Encontrar el color más frecuente (transparent = tile vacío, no se pinta)
|
||||
Uint8 best_color = transparent;
|
||||
int best_count = 0;
|
||||
for (int c = 0; c < 256; ++c) {
|
||||
if (c == transparent) { continue; }
|
||||
if (freq[c] > best_count) {
|
||||
best_count = freq[c];
|
||||
best_color = static_cast<Uint8>(c);
|
||||
}
|
||||
}
|
||||
|
||||
tile_colors_[tile] = best_color;
|
||||
}
|
||||
}
|
||||
|
||||
// Posiciona las rooms en un grid usando BFS desde las conexiones
|
||||
void MiniMap::layoutRooms() {
|
||||
auto& rooms = Resource::Cache::get()->getRooms();
|
||||
if (rooms.empty()) { return; }
|
||||
|
||||
// Mapa de nombre → Room::Data
|
||||
std::unordered_map<std::string, std::shared_ptr<Room::Data>> room_map;
|
||||
for (const auto& r : rooms) {
|
||||
room_map[r.name] = r.room;
|
||||
}
|
||||
|
||||
// BFS para posicionar rooms
|
||||
std::set<std::string> visited;
|
||||
std::queue<std::pair<std::string, GridPos>> bfs;
|
||||
|
||||
// Empezar por la primera room
|
||||
const std::string& start = rooms[0].name;
|
||||
bfs.push({start, {.x = 0, .y = 0}});
|
||||
visited.insert(start);
|
||||
|
||||
// Grid ocupado: posición → nombre de room
|
||||
std::map<std::pair<int, int>, std::string> grid_occupied;
|
||||
|
||||
while (!bfs.empty()) {
|
||||
auto [name, pos] = bfs.front();
|
||||
bfs.pop();
|
||||
|
||||
auto key = std::make_pair(pos.x, pos.y);
|
||||
if (grid_occupied.contains(key)) { continue; }
|
||||
|
||||
grid_occupied[key] = name;
|
||||
room_positions_[name] = RoomMini{.surface = nullptr, .pos = pos};
|
||||
|
||||
auto it = room_map.find(name);
|
||||
if (it == room_map.end()) { continue; }
|
||||
const auto& data = it->second;
|
||||
|
||||
// Vecinos: up, down, left, right
|
||||
struct Neighbor {
|
||||
std::string room;
|
||||
int dx, dy;
|
||||
};
|
||||
std::array<Neighbor, 4> neighbors = {{
|
||||
{.room = data->upper_room, .dx = 0, .dy = -1},
|
||||
{.room = data->lower_room, .dx = 0, .dy = 1},
|
||||
{.room = data->left_room, .dx = -1, .dy = 0},
|
||||
{.room = data->right_room, .dx = 1, .dy = 0},
|
||||
}};
|
||||
|
||||
for (const auto& [neighbor_name, dx, dy] : neighbors) {
|
||||
if (neighbor_name == "0" || neighbor_name.empty()) { continue; }
|
||||
if (visited.contains(neighbor_name)) { continue; }
|
||||
|
||||
GridPos neighbor_pos = {.x = pos.x + dx, .y = pos.y + dy};
|
||||
auto nkey = std::make_pair(neighbor_pos.x, neighbor_pos.y);
|
||||
if (!grid_occupied.contains(nkey)) {
|
||||
visited.insert(neighbor_name);
|
||||
bfs.emplace(neighbor_name, neighbor_pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calcular bounds del grid
|
||||
min_grid_x_ = 0;
|
||||
min_grid_y_ = 0;
|
||||
int max_grid_x = 0;
|
||||
int max_grid_y = 0;
|
||||
for (const auto& [name, mini] : room_positions_) {
|
||||
min_grid_x_ = std::min(min_grid_x_, mini.pos.x);
|
||||
min_grid_y_ = std::min(min_grid_y_, mini.pos.y);
|
||||
max_grid_x = std::max(max_grid_x, mini.pos.x);
|
||||
max_grid_y = std::max(max_grid_y, mini.pos.y);
|
||||
}
|
||||
|
||||
int cols = max_grid_x - min_grid_x_ + 1;
|
||||
int rows = max_grid_y - min_grid_y_ + 1;
|
||||
map_width_ = cols * (CELL_W + GAP) - GAP + PADDING * 2;
|
||||
map_height_ = rows * (CELL_H + GAP) - GAP + PADDING * 2;
|
||||
|
||||
std::cout << "MiniMap: " << room_positions_.size() << " rooms, grid " << cols << "x" << rows
|
||||
<< " → " << map_width_ << "x" << map_height_ << " px\n";
|
||||
}
|
||||
|
||||
// Genera una mini-surface de 32x16 por room
|
||||
void MiniMap::buildRoomSurfaces() {
|
||||
for (auto& [name, mini] : room_positions_) {
|
||||
mini.surface = getRoomMiniSurface(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Genera la mini-surface de una room: 1 pixel por tile, color predominante
|
||||
auto MiniMap::getRoomMiniSurface(const std::string& room_name) -> std::shared_ptr<Surface> {
|
||||
auto room_data = Resource::Cache::get()->getRoom(room_name);
|
||||
if (!room_data) { return nullptr; }
|
||||
|
||||
auto surface = std::make_shared<Surface>(ROOM_W, ROOM_H);
|
||||
|
||||
auto prev = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface);
|
||||
surface->clear(stringToColor(room_data->bg_color));
|
||||
|
||||
const auto& tile_map = room_data->tile_map;
|
||||
for (int y = 0; y < ROOM_H; ++y) {
|
||||
for (int x = 0; x < ROOM_W; ++x) {
|
||||
int index = (y * ROOM_W) + x;
|
||||
if (index >= static_cast<int>(tile_map.size())) { continue; }
|
||||
|
||||
int tile = tile_map[index];
|
||||
if (tile < 0 || tile >= static_cast<int>(tile_colors_.size())) { continue; }
|
||||
|
||||
Uint8 color = tile_colors_[tile];
|
||||
if (color != tileset_transparent_) {
|
||||
surface->putPixel(x, y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Screen::get()->setRendererSurface(prev);
|
||||
return surface;
|
||||
}
|
||||
|
||||
// Compone la surface final con todas las rooms posicionadas
|
||||
void MiniMap::composeFinalSurface() {
|
||||
if (map_width_ <= 0 || map_height_ <= 0) { return; }
|
||||
|
||||
// Surface un poco más grande para la sombra del borde inferior/derecho
|
||||
map_surface_ = std::make_shared<Surface>(map_width_ + SHADOW_OFFSET, map_height_ + SHADOW_OFFSET);
|
||||
|
||||
auto prev = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(map_surface_);
|
||||
|
||||
// 1. Fondo general
|
||||
map_surface_->clear(bg_color_);
|
||||
|
||||
// 2. Líneas de conexión entre rooms (debajo de todo)
|
||||
drawConnections();
|
||||
|
||||
// 3. Sombras de las rooms (desplazadas 1px abajo-derecha)
|
||||
for (const auto& [name, mini] : room_positions_) {
|
||||
if (!mini.surface) { continue; }
|
||||
int px = cellPixelX(mini.pos.x) + SHADOW_OFFSET;
|
||||
int py = cellPixelY(mini.pos.y) + SHADOW_OFFSET;
|
||||
SDL_FRect shadow = {.x = static_cast<float>(px), .y = static_cast<float>(py), .w = static_cast<float>(CELL_W), .h = static_cast<float>(CELL_H)};
|
||||
map_surface_->fillRect(&shadow, COLOR_SHADOW);
|
||||
}
|
||||
|
||||
// 4. Borde negro de cada room + contenido de la room
|
||||
for (const auto& [name, mini] : room_positions_) {
|
||||
if (!mini.surface) { continue; }
|
||||
int px = cellPixelX(mini.pos.x);
|
||||
int py = cellPixelY(mini.pos.y);
|
||||
|
||||
// Borde negro (la celda entera)
|
||||
SDL_FRect cell = {.x = static_cast<float>(px), .y = static_cast<float>(py), .w = static_cast<float>(CELL_W), .h = static_cast<float>(CELL_H)};
|
||||
map_surface_->fillRect(&cell, COLOR_ROOM_BORDER);
|
||||
|
||||
// Miniroom dentro del borde
|
||||
SDL_FRect dst = {.x = static_cast<float>(px + BORDER), .y = static_cast<float>(py + BORDER), .w = static_cast<float>(ROOM_W), .h = static_cast<float>(ROOM_H)};
|
||||
mini.surface->render(nullptr, &dst);
|
||||
}
|
||||
|
||||
Screen::get()->setRendererSurface(prev);
|
||||
}
|
||||
|
||||
// Dibuja las líneas de conexión entre rooms vecinas
|
||||
void MiniMap::drawConnections() {
|
||||
for (const auto& [name, mini] : room_positions_) {
|
||||
auto room_data = Resource::Cache::get()->getRoom(name);
|
||||
if (!room_data) { continue; }
|
||||
|
||||
int px = cellPixelX(mini.pos.x);
|
||||
int py = cellPixelY(mini.pos.y);
|
||||
|
||||
// Conexión derecha
|
||||
if (room_data->right_room != "0" && !room_data->right_room.empty() && room_positions_.contains(room_data->right_room)) {
|
||||
int x1 = px + CELL_W;
|
||||
int y_mid = py + (CELL_H / 2) - 1;
|
||||
SDL_FRect line = {.x = static_cast<float>(x1), .y = static_cast<float>(y_mid), .w = static_cast<float>(GAP), .h = 3.0F};
|
||||
map_surface_->fillRect(&line, conn_color_);
|
||||
}
|
||||
|
||||
// Conexión abajo
|
||||
if (room_data->lower_room != "0" && !room_data->lower_room.empty() && room_positions_.contains(room_data->lower_room)) {
|
||||
int x_mid = px + (CELL_W / 2) - 1;
|
||||
int y1 = py + CELL_H;
|
||||
SDL_FRect line = {.x = static_cast<float>(x_mid), .y = static_cast<float>(y1), .w = 3.0F, .h = static_cast<float>(GAP)};
|
||||
map_surface_->fillRect(&line, conn_color_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Centra el viewport en una room
|
||||
void MiniMap::centerOnRoom(const std::string& room_name) {
|
||||
auto it = room_positions_.find(room_name);
|
||||
if (it == room_positions_.end()) { return; }
|
||||
const auto& pos = it->second.pos;
|
||||
|
||||
auto room_cx = static_cast<float>(cellPixelX(pos.x) + (CELL_W / 2));
|
||||
auto room_cy = static_cast<float>(cellPixelY(pos.y) + (CELL_H / 2));
|
||||
view_x_ = static_cast<float>(PlayArea::WIDTH) / 2.0F - room_cx;
|
||||
view_y_ = static_cast<float>(PlayArea::HEIGHT) / 2.0F - room_cy;
|
||||
}
|
||||
|
||||
// Devuelve el nombre de la room en una posición de pantalla, o vacío si no hay ninguna
|
||||
auto MiniMap::roomAtScreen(float screen_x, float screen_y) -> std::string {
|
||||
// Convertir coordenada de pantalla a coordenada dentro del minimapa
|
||||
float map_x = screen_x - view_x_;
|
||||
float map_y = screen_y - view_y_;
|
||||
|
||||
for (const auto& [name, mini] : room_positions_) {
|
||||
auto rx = static_cast<float>(cellPixelX(mini.pos.x));
|
||||
auto ry = static_cast<float>(cellPixelY(mini.pos.y));
|
||||
if (map_x >= rx && map_x < rx + CELL_W && map_y >= ry && map_y < ry + CELL_H) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Renderiza el minimapa
|
||||
void MiniMap::render(const std::string& current_room) {
|
||||
if (!map_surface_) { return; }
|
||||
|
||||
auto game_surface = Screen::get()->getRendererSurface();
|
||||
if (!game_surface) { return; }
|
||||
|
||||
// Renderizar la surface del minimapa con el viewport actual (alineado a pixel)
|
||||
float vx = std::floor(view_x_);
|
||||
float vy = std::floor(view_y_);
|
||||
SDL_FRect dst = {.x = vx, .y = vy, .w = static_cast<float>(map_width_ + SHADOW_OFFSET), .h = static_cast<float>(map_height_ + SHADOW_OFFSET)};
|
||||
map_surface_->render(nullptr, &dst);
|
||||
|
||||
// Highlight de la room actual (solo si está completamente visible en el play area)
|
||||
auto it = room_positions_.find(current_room);
|
||||
if (it != room_positions_.end()) {
|
||||
float cur_x = vx + static_cast<float>(cellPixelX(it->second.pos.x)) - 1;
|
||||
float cur_y = vy + static_cast<float>(cellPixelY(it->second.pos.y)) - 1;
|
||||
auto cur_w = static_cast<float>(CELL_W + 2);
|
||||
auto cur_h = static_cast<float>(CELL_H + 2);
|
||||
if (cur_x >= 0 && cur_y >= 0 && cur_x + cur_w <= PlayArea::WIDTH && cur_y + cur_h <= PlayArea::HEIGHT) {
|
||||
SDL_FRect highlight = {.x = cur_x, .y = cur_y, .w = cur_w, .h = cur_h};
|
||||
game_surface->drawRectBorder(&highlight, stringToColor("bright_white"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Maneja eventos del minimapa (drag para explorar, click para navegar)
|
||||
void MiniMap::handleEvent(const SDL_Event& event, const std::string& current_room) {
|
||||
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN && event.button.button == SDL_BUTTON_LEFT) {
|
||||
// Guardar posición inicial para detectar si es click o drag
|
||||
float mouse_x = 0.0F;
|
||||
float mouse_y = 0.0F;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
float render_x = 0.0F;
|
||||
float render_y = 0.0F;
|
||||
SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);
|
||||
SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
|
||||
|
||||
dragging_ = true;
|
||||
drag_start_x_ = render_x - dst_rect.x;
|
||||
drag_start_y_ = render_y - dst_rect.y;
|
||||
view_start_x_ = view_x_;
|
||||
view_start_y_ = view_y_;
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_MOUSE_MOTION && dragging_) {
|
||||
float mouse_x = 0.0F;
|
||||
float mouse_y = 0.0F;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
float render_x = 0.0F;
|
||||
float render_y = 0.0F;
|
||||
SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);
|
||||
SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
|
||||
|
||||
float game_x = render_x - dst_rect.x;
|
||||
float game_y = render_y - dst_rect.y;
|
||||
view_x_ = view_start_x_ + (game_x - drag_start_x_);
|
||||
view_y_ = view_start_y_ + (game_y - drag_start_y_);
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_LEFT) {
|
||||
if (dragging_) {
|
||||
// Comprobar si fue click (sin mover) o drag
|
||||
float mouse_x = 0.0F;
|
||||
float mouse_y = 0.0F;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
float render_x = 0.0F;
|
||||
float render_y = 0.0F;
|
||||
SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);
|
||||
SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
|
||||
|
||||
float game_x = render_x - dst_rect.x;
|
||||
float game_y = render_y - dst_rect.y;
|
||||
|
||||
float dx = game_x - drag_start_x_;
|
||||
float dy = game_y - drag_start_y_;
|
||||
bool was_click = (dx * dx + dy * dy) < 4.0F; // Menos de 2px de movimiento = click
|
||||
|
||||
if (was_click) {
|
||||
// Click: navegar a la room bajo el cursor
|
||||
std::string room = roomAtScreen(game_x, game_y);
|
||||
if (!room.empty() && room != current_room && on_navigate) {
|
||||
on_navigate(room);
|
||||
}
|
||||
}
|
||||
|
||||
dragging_ = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // _DEBUG
|
||||
103
source/game/editor/mini_map.hpp
Normal file
103
source/game/editor/mini_map.hpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <functional> // Para function
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <unordered_map> // Para unordered_map
|
||||
#include <vector> // Para vector
|
||||
|
||||
class Surface;
|
||||
|
||||
/**
|
||||
* @brief Minimapa global del juego para el editor
|
||||
*
|
||||
* Genera una vista en miniatura de todas las habitaciones del juego,
|
||||
* posicionadas según sus conexiones.
|
||||
* Cada tile del mapa se representa como 1 pixel del color predominante de ese tile.
|
||||
* Resultado: cada room = 32x16 pixels.
|
||||
*/
|
||||
class MiniMap {
|
||||
public:
|
||||
explicit MiniMap(Uint8 bg_color = 2, Uint8 conn_color = 14);
|
||||
~MiniMap() = default;
|
||||
|
||||
void render(const std::string& current_room);
|
||||
void handleEvent(const SDL_Event& event, const std::string& current_room);
|
||||
void rebuild(Uint8 bg_color, Uint8 conn_color);
|
||||
void centerOnRoom(const std::string& room_name);
|
||||
[[nodiscard]] auto isReady() const -> bool { return !room_positions_.empty(); }
|
||||
|
||||
// Callback al hacer click en una minihabitación (nombre del room)
|
||||
std::function<void(const std::string&)> on_navigate;
|
||||
|
||||
private:
|
||||
// Posición de una room en el grid del minimapa
|
||||
struct GridPos {
|
||||
int x{0};
|
||||
int y{0};
|
||||
};
|
||||
|
||||
// Una room renderizada
|
||||
struct RoomMini {
|
||||
std::shared_ptr<Surface> surface; // 32x16 pixels
|
||||
GridPos pos; // Posición en el grid
|
||||
};
|
||||
|
||||
void buildTileColorTable(const std::string& tileset_name);
|
||||
void buildRoomSurfaces();
|
||||
void layoutRooms();
|
||||
void composeFinalSurface();
|
||||
auto getRoomMiniSurface(const std::string& room_name) -> std::shared_ptr<Surface>;
|
||||
void drawConnections();
|
||||
auto roomAtScreen(float screen_x, float screen_y) -> std::string;
|
||||
auto cellPixelX(int grid_x) const -> int { return PADDING + ((grid_x - min_grid_x_) * (CELL_W + GAP)); }
|
||||
auto cellPixelY(int grid_y) const -> int { return PADDING + ((grid_y - min_grid_y_) * (CELL_H + GAP)); }
|
||||
|
||||
// Tabla de color predominante por tile index
|
||||
std::vector<Uint8> tile_colors_; // tile_index → palette color index
|
||||
int tileset_width_{0}; // Ancho del tileset en tiles
|
||||
Uint8 tileset_transparent_{16}; // Color transparente del tileset
|
||||
|
||||
// Rooms renderizadas y posicionadas
|
||||
std::unordered_map<std::string, RoomMini> room_positions_;
|
||||
|
||||
// Surface final compuesta
|
||||
std::shared_ptr<Surface> map_surface_;
|
||||
int map_width_{0}; // Ancho en pixels
|
||||
int map_height_{0}; // Alto en pixels
|
||||
|
||||
// Offset para normalizar coordenadas
|
||||
int min_grid_x_{0};
|
||||
int min_grid_y_{0};
|
||||
|
||||
// Viewport: offset de la surface del minimapa respecto al play area
|
||||
float view_x_{0.0F}; // Offset X actual
|
||||
float view_y_{0.0F}; // Offset Y actual
|
||||
bool dragging_{false};
|
||||
float drag_start_x_{0.0F}; // Posición del ratón al inicio del drag
|
||||
float drag_start_y_{0.0F};
|
||||
float view_start_x_{0.0F}; // Viewport al inicio del drag
|
||||
float view_start_y_{0.0F};
|
||||
|
||||
// Constantes
|
||||
static constexpr int ROOM_W = 32; // Ancho de una room en pixels del minimapa
|
||||
static constexpr int ROOM_H = 16; // Alto de una room en pixels del minimapa
|
||||
static constexpr int BORDER = 1; // Borde alrededor de cada room
|
||||
static constexpr int CELL_W = ROOM_W + (BORDER * 2); // Room + borde
|
||||
static constexpr int CELL_H = ROOM_H + (BORDER * 2);
|
||||
static constexpr int GAP = 4; // Separación entre celdas
|
||||
static constexpr int SHADOW_OFFSET = 1; // Desplazamiento de la sombra
|
||||
static constexpr int PADDING = 4; // Padding alrededor del minimapa
|
||||
|
||||
// Colores del minimapa (índices de paleta)
|
||||
Uint8 bg_color_{2}; // Fondo general (configurable)
|
||||
Uint8 conn_color_{14}; // Líneas de conexión (configurable)
|
||||
static constexpr Uint8 COLOR_ROOM_BORDER = 0; // Borde de cada miniroom
|
||||
static constexpr Uint8 COLOR_SHADOW = 1; // Sombra de cada miniroom
|
||||
};
|
||||
|
||||
#endif // _DEBUG
|
||||
178
source/game/editor/room_saver.cpp
Normal file
178
source/game/editor/room_saver.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include "game/editor/room_saver.hpp"
|
||||
|
||||
#include <cmath> // Para std::round
|
||||
#include <fstream> // Para ifstream, ofstream, istreambuf_iterator
|
||||
#include <iostream> // Para cout, cerr
|
||||
#include <sstream> // Para ostringstream
|
||||
|
||||
#include "utils/defines.hpp" // Para Tile::SIZE
|
||||
|
||||
// Carga el YAML original directamente del filesystem (no del resource pack)
|
||||
auto RoomSaver::loadYAML(const std::string& file_path) -> fkyaml::node {
|
||||
std::ifstream file(file_path);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "RoomSaver: Cannot open " << file_path << "\n";
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
return fkyaml::node::deserialize(content);
|
||||
}
|
||||
|
||||
// Convierte una room connection al formato YAML
|
||||
auto RoomSaver::roomConnectionToYAML(const std::string& connection) -> std::string {
|
||||
if (connection == "0" || connection.empty()) { return "null"; }
|
||||
return connection;
|
||||
}
|
||||
|
||||
// Convierte la dirección del conveyor belt a string
|
||||
auto RoomSaver::conveyorBeltToString(int direction) -> std::string {
|
||||
if (direction < 0) { return "left"; }
|
||||
if (direction > 0) { return "right"; }
|
||||
return "none";
|
||||
}
|
||||
|
||||
// Genera el YAML completo como texto con formato compacto
|
||||
auto RoomSaver::buildYAML(const fkyaml::node& original_yaml, const Room::Data& room_data) -> std::string { // NOLINT(readability-function-cognitive-complexity)
|
||||
std::ostringstream out;
|
||||
|
||||
// --- Cabecera: nombre como comentario ---
|
||||
out << "# " << room_data.name << "\n";
|
||||
|
||||
// --- Sección room ---
|
||||
out << "room:\n";
|
||||
|
||||
// Escribir todos los campos name_* del YAML original (preserva name_ca, name_en, etc.)
|
||||
if (original_yaml.contains("room")) {
|
||||
const auto& room_node = original_yaml["room"];
|
||||
for (auto it = room_node.begin(); it != room_node.end(); ++it) {
|
||||
const auto KEY = it.key().get_value<std::string>();
|
||||
if (KEY.substr(0, 5) == "name_") {
|
||||
out << " " << KEY << ": \"" << it.value().get_value<std::string>() << "\"\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out << " bgColor: " << room_data.bg_color << "\n";
|
||||
out << " border: " << room_data.border_color << "\n";
|
||||
out << " tileSetFile: " << room_data.tile_set_file << "\n";
|
||||
|
||||
// Conexiones
|
||||
out << "\n";
|
||||
out << " # Conexiones de la habitación (null = sin conexión)\n";
|
||||
out << " connections:\n";
|
||||
out << " up: " << roomConnectionToYAML(room_data.upper_room) << "\n";
|
||||
out << " down: " << roomConnectionToYAML(room_data.lower_room) << "\n";
|
||||
out << " left: " << roomConnectionToYAML(room_data.left_room) << "\n";
|
||||
out << " right: " << roomConnectionToYAML(room_data.right_room) << "\n";
|
||||
|
||||
// Colores de items
|
||||
out << "\n";
|
||||
out << " # Colores de los objetos\n";
|
||||
out << " itemColor1: " << (room_data.item_color1.empty() ? "yellow" : room_data.item_color1) << "\n";
|
||||
out << " itemColor2: " << (room_data.item_color2.empty() ? "magenta" : room_data.item_color2) << "\n";
|
||||
|
||||
// Conveyor belt
|
||||
out << "\n";
|
||||
out << " # Dirección de la cinta transportadora: left, none, right\n";
|
||||
out << " conveyorBelt: " << conveyorBeltToString(room_data.conveyor_belt_direction) << "\n";
|
||||
|
||||
// --- Tilemap (16 filas × 32 columnas, formato flow) ---
|
||||
out << "\n";
|
||||
out << "# Tilemap: 16 filas × 32 columnas (256×192 píxeles @ 8px/tile)\n";
|
||||
out << "# Índices de tiles (-1 = vacío)\n";
|
||||
out << "tilemap:\n";
|
||||
constexpr int MAP_WIDTH = 32;
|
||||
constexpr int MAP_HEIGHT = 16;
|
||||
for (int row = 0; row < MAP_HEIGHT; ++row) {
|
||||
out << " - [";
|
||||
for (int col = 0; col < MAP_WIDTH; ++col) {
|
||||
int index = (row * MAP_WIDTH) + col;
|
||||
if (index < static_cast<int>(room_data.tile_map.size())) {
|
||||
out << room_data.tile_map[index];
|
||||
} else {
|
||||
out << -1;
|
||||
}
|
||||
if (col < MAP_WIDTH - 1) { out << ", "; }
|
||||
}
|
||||
out << "]\n";
|
||||
}
|
||||
|
||||
// --- Enemigos ---
|
||||
if (!room_data.enemies.empty()) {
|
||||
out << "\n";
|
||||
out << "# Enemigos en esta habitación\n";
|
||||
out << "enemies:\n";
|
||||
for (const auto& enemy : room_data.enemies) {
|
||||
out << " - animation: " << enemy.animation_path << "\n";
|
||||
|
||||
int pos_x = static_cast<int>(std::round(enemy.x / Tile::SIZE));
|
||||
int pos_y = static_cast<int>(std::round(enemy.y / Tile::SIZE));
|
||||
out << " position: {x: " << pos_x << ", y: " << pos_y << "}\n";
|
||||
|
||||
out << " velocity: {x: " << enemy.vx << ", y: " << enemy.vy << "}\n";
|
||||
|
||||
int b1_x = enemy.x1 / Tile::SIZE;
|
||||
int b1_y = enemy.y1 / Tile::SIZE;
|
||||
int b2_x = enemy.x2 / Tile::SIZE;
|
||||
int b2_y = enemy.y2 / Tile::SIZE;
|
||||
out << " boundaries:\n";
|
||||
out << " position1: {x: " << b1_x << ", y: " << b1_y << "}\n";
|
||||
out << " position2: {x: " << b2_x << ", y: " << b2_y << "}\n";
|
||||
|
||||
if (!enemy.color.empty() && enemy.color != "white") {
|
||||
out << " color: " << enemy.color << "\n";
|
||||
}
|
||||
if (enemy.flip) { out << " flip: true\n"; }
|
||||
if (enemy.mirror) { out << " mirror: true\n"; }
|
||||
if (enemy.frame != -1) { out << " frame: " << enemy.frame << "\n"; }
|
||||
|
||||
out << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// --- Items ---
|
||||
if (!room_data.items.empty()) {
|
||||
out << "# Objetos en esta habitación\n";
|
||||
out << "items:\n";
|
||||
for (const auto& item : room_data.items) {
|
||||
out << " - tileSetFile: " << item.tile_set_file << "\n";
|
||||
out << " tile: " << item.tile << "\n";
|
||||
|
||||
int item_x = static_cast<int>(std::round(item.x / Tile::SIZE));
|
||||
int item_y = static_cast<int>(std::round(item.y / Tile::SIZE));
|
||||
out << " position: {x: " << item_x << ", y: " << item_y << "}\n";
|
||||
|
||||
if (item.counter != 0) {
|
||||
out << " counter: " << item.counter << "\n";
|
||||
}
|
||||
|
||||
out << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
// Guarda el YAML a disco
|
||||
auto RoomSaver::saveYAML(const std::string& file_path, const fkyaml::node& original_yaml, const Room::Data& room_data) -> std::string {
|
||||
std::string content = buildYAML(original_yaml, room_data);
|
||||
|
||||
std::ofstream file(file_path);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "RoomSaver: Cannot write to " << file_path << "\n";
|
||||
return "Error: Cannot write to " + file_path;
|
||||
}
|
||||
|
||||
file << content;
|
||||
file.close();
|
||||
|
||||
const std::string FILE_NAME = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::cout << "RoomSaver: Saved " << FILE_NAME << "\n";
|
||||
return "Saved " + FILE_NAME;
|
||||
}
|
||||
|
||||
#endif // _DEBUG
|
||||
35
source/game/editor/room_saver.hpp
Normal file
35
source/game/editor/room_saver.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include <string> // Para string
|
||||
|
||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||
#include "game/gameplay/room.hpp" // Para Room::Data
|
||||
|
||||
/**
|
||||
* @brief Guardado de archivos YAML de habitaciones para el editor de mapas
|
||||
*
|
||||
* Lee el YAML original con fkyaml (para acceder a todos los campos: name_ca, name_en, etc.)
|
||||
* Genera el YAML como texto formateado compacto (idéntico al formato original de los ficheros).
|
||||
* Solo se usa en builds de debug.
|
||||
*/
|
||||
class RoomSaver {
|
||||
public:
|
||||
RoomSaver() = delete;
|
||||
|
||||
// Carga el YAML original desde disco como nodo fkyaml (lee del filesystem, no del pack)
|
||||
static auto loadYAML(const std::string& file_path) -> fkyaml::node;
|
||||
|
||||
// Genera y guarda el YAML completo a disco
|
||||
// original_yaml: nodo fkyaml con los datos originales (para campos que no se editan: name_ca, etc.)
|
||||
// room_data: datos editados (posiciones de enemigos, items, etc.)
|
||||
static auto saveYAML(const std::string& file_path, const fkyaml::node& original_yaml, const Room::Data& room_data) -> std::string;
|
||||
|
||||
private:
|
||||
static auto buildYAML(const fkyaml::node& original_yaml, const Room::Data& room_data) -> std::string;
|
||||
static auto roomConnectionToYAML(const std::string& connection) -> std::string;
|
||||
static auto conveyorBeltToString(int direction) -> std::string;
|
||||
};
|
||||
|
||||
#endif // _DEBUG
|
||||
240
source/game/editor/tile_picker.cpp
Normal file
240
source/game/editor/tile_picker.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include "game/editor/tile_picker.hpp"
|
||||
|
||||
#include <algorithm> // Para std::clamp, std::min
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource::Cache
|
||||
#include "utils/defines.hpp" // Para Tile::SIZE, PlayArea
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
// Margen del borde alrededor del tileset (en pixels)
|
||||
static constexpr int BORDER_PAD = 3;
|
||||
|
||||
// Abre el picker con un tileset
|
||||
void TilePicker::open(const std::string& tileset_name, int current_tile, int bg_color, int source_color, int target_color, int tile_spacing_in, int tile_spacing_out) {
|
||||
tileset_ = Resource::Cache::get()->getSurface(tileset_name);
|
||||
if (!tileset_) {
|
||||
open_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
spacing_in_ = tile_spacing_in;
|
||||
spacing_out_ = tile_spacing_out;
|
||||
|
||||
// Calcular dimensiones del tileset en tiles (teniendo en cuenta spacing de entrada)
|
||||
int src_cell = Tile::SIZE + spacing_in_;
|
||||
tileset_width_ = static_cast<int>(tileset_->getWidth()) / src_cell;
|
||||
tileset_height_ = static_cast<int>(tileset_->getHeight()) / src_cell;
|
||||
// Corregir si el último tile cabe sin spacing
|
||||
if (tileset_width_ == 0 && tileset_->getWidth() >= Tile::SIZE) { tileset_width_ = 1; }
|
||||
if (tileset_height_ == 0 && tileset_->getHeight() >= Tile::SIZE) { tileset_height_ = 1; }
|
||||
|
||||
current_tile_ = current_tile;
|
||||
hover_tile_ = -1;
|
||||
scroll_y_ = 0;
|
||||
|
||||
// Dimensiones de salida (con spacing visual entre tiles)
|
||||
int out_cell = Tile::SIZE + spacing_out_;
|
||||
int display_w = (tileset_width_ * out_cell) - spacing_out_; // Sin trailing
|
||||
int display_h = (tileset_height_ * out_cell) - spacing_out_;
|
||||
|
||||
// Frame: display + borde
|
||||
int frame_w = display_w + (BORDER_PAD * 2);
|
||||
int frame_h = display_h + (BORDER_PAD * 2);
|
||||
frame_surface_ = std::make_shared<Surface>(frame_w, frame_h);
|
||||
|
||||
// Componer: fondo + borde + tiles uno a uno
|
||||
{
|
||||
auto prev = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(frame_surface_);
|
||||
|
||||
Uint8 fill_color = (bg_color >= 0) ? static_cast<Uint8>(bg_color) : stringToColor("black");
|
||||
frame_surface_->clear(fill_color);
|
||||
|
||||
// Borde doble
|
||||
SDL_FRect outer = {.x = 0, .y = 0, .w = static_cast<float>(frame_w), .h = static_cast<float>(frame_h)};
|
||||
frame_surface_->drawRectBorder(&outer, stringToColor("bright_white"));
|
||||
SDL_FRect inner = {.x = 1, .y = 1, .w = static_cast<float>(frame_w - 2), .h = static_cast<float>(frame_h - 2)};
|
||||
frame_surface_->drawRectBorder(&inner, stringToColor("white"));
|
||||
|
||||
// Renderizar cada tile individualmente
|
||||
constexpr auto TS = static_cast<float>(Tile::SIZE);
|
||||
for (int row = 0; row < tileset_height_; ++row) {
|
||||
for (int col = 0; col < tileset_width_; ++col) {
|
||||
// Fuente: posición en el tileset original
|
||||
SDL_FRect src = {
|
||||
.x = static_cast<float>(col * src_cell),
|
||||
.y = static_cast<float>(row * src_cell),
|
||||
.w = TS,
|
||||
.h = TS};
|
||||
|
||||
// Destino: posición en el frame con spacing de salida
|
||||
int dst_x = BORDER_PAD + (col * out_cell);
|
||||
int dst_y = BORDER_PAD + (row * out_cell);
|
||||
|
||||
if (source_color >= 0 && target_color >= 0) {
|
||||
tileset_->renderWithColorReplace(dst_x, dst_y, static_cast<Uint8>(source_color), static_cast<Uint8>(target_color), &src);
|
||||
} else {
|
||||
SDL_FRect dst = {.x = static_cast<float>(dst_x), .y = static_cast<float>(dst_y), .w = TS, .h = TS};
|
||||
tileset_->render(&src, &dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Screen::get()->setRendererSurface(prev);
|
||||
}
|
||||
|
||||
// Centrar en el play area
|
||||
offset_x_ = (PlayArea::WIDTH - frame_w) / 2;
|
||||
int offset_y = (PlayArea::HEIGHT - frame_h) / 2;
|
||||
offset_y = std::max(offset_y, 0);
|
||||
|
||||
visible_height_ = PlayArea::HEIGHT;
|
||||
|
||||
frame_dst_ = {.x = static_cast<float>(offset_x_), .y = static_cast<float>(offset_y), .w = static_cast<float>(frame_w), .h = static_cast<float>(frame_h)};
|
||||
|
||||
// Si el frame es más alto que el play area, necesitará scroll
|
||||
if (frame_h > visible_height_) {
|
||||
frame_dst_.y = 0;
|
||||
if (current_tile_ >= 0) {
|
||||
int tile_row = current_tile_ / tileset_width_;
|
||||
int tile_y_px = tile_row * out_cell;
|
||||
if (tile_y_px > visible_height_ / 2) {
|
||||
scroll_y_ = tile_y_px - visible_height_ / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open_ = true;
|
||||
updateMousePosition();
|
||||
}
|
||||
|
||||
// Cierra el picker
|
||||
void TilePicker::close() {
|
||||
open_ = false;
|
||||
tileset_.reset();
|
||||
frame_surface_.reset();
|
||||
}
|
||||
|
||||
// Renderiza el picker
|
||||
void TilePicker::render() {
|
||||
if (!open_ || !frame_surface_) { return; }
|
||||
|
||||
auto game_surface = Screen::get()->getRendererSurface();
|
||||
if (!game_surface) { return; }
|
||||
|
||||
int frame_h = static_cast<int>(frame_dst_.h);
|
||||
|
||||
if (frame_h <= visible_height_) {
|
||||
frame_surface_->render(nullptr, &frame_dst_);
|
||||
} else {
|
||||
int max_scroll = frame_h - visible_height_;
|
||||
scroll_y_ = std::clamp(scroll_y_, 0, max_scroll);
|
||||
|
||||
SDL_FRect src = {.x = 0, .y = static_cast<float>(scroll_y_), .w = frame_dst_.w, .h = static_cast<float>(visible_height_)};
|
||||
SDL_FRect dst = {.x = frame_dst_.x, .y = 0, .w = frame_dst_.w, .h = static_cast<float>(visible_height_)};
|
||||
frame_surface_->render(&src, &dst);
|
||||
}
|
||||
|
||||
// Highlights (en game_surface, encima del frame)
|
||||
int out_cell = Tile::SIZE + spacing_out_;
|
||||
float tileset_screen_x = frame_dst_.x + BORDER_PAD;
|
||||
float tileset_screen_y = frame_dst_.y + BORDER_PAD - static_cast<float>(scroll_y_);
|
||||
constexpr auto TS = static_cast<float>(Tile::SIZE);
|
||||
|
||||
// Highlight del tile bajo el cursor (blanco)
|
||||
if (hover_tile_ >= 0) {
|
||||
int col = hover_tile_ % tileset_width_;
|
||||
int row = hover_tile_ / tileset_width_;
|
||||
float hx = tileset_screen_x + static_cast<float>(col * out_cell);
|
||||
float hy = tileset_screen_y + static_cast<float>(row * out_cell);
|
||||
if (hy >= 0 && hy + TS <= visible_height_) {
|
||||
SDL_FRect highlight = {.x = hx, .y = hy, .w = TS, .h = TS};
|
||||
game_surface->drawRectBorder(&highlight, stringToColor("bright_white"));
|
||||
}
|
||||
}
|
||||
|
||||
// Highlight del tile actual (verde)
|
||||
if (current_tile_ >= 0) {
|
||||
int col = current_tile_ % tileset_width_;
|
||||
int row = current_tile_ / tileset_width_;
|
||||
float cx = tileset_screen_x + static_cast<float>(col * out_cell);
|
||||
float cy = tileset_screen_y + static_cast<float>(row * out_cell);
|
||||
if (cy >= 0 && cy + TS <= visible_height_) {
|
||||
SDL_FRect cur_rect = {.x = cx, .y = cy, .w = TS, .h = TS};
|
||||
game_surface->drawRectBorder(&cur_rect, stringToColor("bright_green"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Maneja eventos del picker
|
||||
void TilePicker::handleEvent(const SDL_Event& event) {
|
||||
if (!open_) { return; }
|
||||
|
||||
if (event.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
updateMousePosition();
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
if (event.button.button == SDL_BUTTON_LEFT && hover_tile_ >= 0) {
|
||||
if (on_select) { on_select(hover_tile_); }
|
||||
close();
|
||||
} else if (event.button.button == SDL_BUTTON_RIGHT) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_MOUSE_WHEEL) {
|
||||
scroll_y_ -= static_cast<int>(event.wheel.y) * Tile::SIZE * 2;
|
||||
int max_scroll = static_cast<int>(frame_dst_.h) - visible_height_;
|
||||
max_scroll = std::max(max_scroll, 0);
|
||||
scroll_y_ = std::clamp(scroll_y_, 0, max_scroll);
|
||||
updateMousePosition();
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula qué tile está bajo el cursor
|
||||
void TilePicker::updateMousePosition() {
|
||||
float mouse_x = 0.0F;
|
||||
float mouse_y = 0.0F;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
|
||||
float render_x = 0.0F;
|
||||
float render_y = 0.0F;
|
||||
SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);
|
||||
|
||||
SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
|
||||
float game_x = render_x - dst_rect.x;
|
||||
float game_y = render_y - dst_rect.y;
|
||||
|
||||
// Coordenada relativa al contenido del frame (con scroll)
|
||||
float rel_x = game_x - frame_dst_.x - BORDER_PAD;
|
||||
float rel_y = game_y - frame_dst_.y - BORDER_PAD + static_cast<float>(scroll_y_);
|
||||
|
||||
// Convertir a tile teniendo en cuenta el spacing de salida
|
||||
int out_cell = Tile::SIZE + spacing_out_;
|
||||
int tile_x = static_cast<int>(rel_x) / out_cell;
|
||||
int tile_y = static_cast<int>(rel_y) / out_cell;
|
||||
|
||||
// Verificar que estamos sobre un tile y no sobre el spacing
|
||||
int local_x = static_cast<int>(rel_x) % out_cell;
|
||||
int local_y = static_cast<int>(rel_y) % out_cell;
|
||||
bool on_tile = (local_x < Tile::SIZE && local_y < Tile::SIZE);
|
||||
|
||||
if (on_tile && rel_x >= 0 && rel_y >= 0 &&
|
||||
tile_x >= 0 && tile_x < tileset_width_ &&
|
||||
tile_y >= 0 && tile_y < tileset_height_) {
|
||||
hover_tile_ = tile_y * tileset_width_ + tile_x;
|
||||
} else {
|
||||
hover_tile_ = -1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // _DEBUG
|
||||
64
source/game/editor/tile_picker.hpp
Normal file
64
source/game/editor/tile_picker.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <functional> // Para function
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
|
||||
class Surface;
|
||||
|
||||
/**
|
||||
* @brief Selector visual de tiles de un tileset
|
||||
*
|
||||
* Muestra el tileset centrado en el play area.
|
||||
* Hover ilumina el tile bajo el cursor.
|
||||
* Click selecciona el tile y cierra el picker.
|
||||
* Mouse wheel para scroll si el tileset es más alto que el play area.
|
||||
* ESC o click derecho para cancelar.
|
||||
*/
|
||||
class TilePicker {
|
||||
public:
|
||||
TilePicker() = default;
|
||||
~TilePicker() = default;
|
||||
|
||||
// Abre el picker con un tileset
|
||||
// bg_color: color de fondo del panel (-1 = negro)
|
||||
// source_color/target_color: sustitución de color (-1 = sin sustitución)
|
||||
// tile_spacing_in: pixels de separación entre tiles en el fichero fuente
|
||||
// tile_spacing_out: pixels de separación visual entre tiles al mostrar
|
||||
void open(const std::string& tileset_name, int current_tile = -1, int bg_color = -1, int source_color = -1, int target_color = -1, int tile_spacing_in = 0, int tile_spacing_out = 1);
|
||||
void close();
|
||||
[[nodiscard]] auto isOpen() const -> bool { return open_; }
|
||||
|
||||
void render();
|
||||
void handleEvent(const SDL_Event& event);
|
||||
|
||||
// Callback al seleccionar un tile (índice del tile)
|
||||
std::function<void(int)> on_select;
|
||||
|
||||
private:
|
||||
void updateMousePosition();
|
||||
|
||||
bool open_{false};
|
||||
std::shared_ptr<Surface> tileset_; // Surface del tileset original
|
||||
std::shared_ptr<Surface> frame_surface_; // Surface compuesta: borde + tileset
|
||||
SDL_FRect frame_dst_{}; // Posición del frame en pantalla
|
||||
int tileset_width_{0}; // Ancho del tileset en tiles
|
||||
int tileset_height_{0}; // Alto del tileset en tiles
|
||||
int current_tile_{-1}; // Tile actualmente seleccionado (highlight)
|
||||
int hover_tile_{-1}; // Tile bajo el cursor
|
||||
|
||||
// Spacing
|
||||
int spacing_in_{0}; // Spacing en el fichero fuente
|
||||
int spacing_out_{1}; // Spacing visual al mostrar
|
||||
|
||||
// Scroll y posicionamiento
|
||||
int scroll_y_{0}; // Scroll vertical en pixels
|
||||
int offset_x_{0}; // Offset X para centrar en pantalla
|
||||
int visible_height_{0}; // Altura visible en pixels
|
||||
};
|
||||
|
||||
#endif // _DEBUG
|
||||
117
source/game/entities/enemy.cpp
Normal file
117
source/game/entities/enemy.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "game/entities/enemy.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cstdlib> // Para rand
|
||||
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
// Constructor
|
||||
Enemy::Enemy(const Data& enemy)
|
||||
: sprite_(std::make_shared<AnimatedSprite>(Resource::Cache::get()->getAnimationData(enemy.animation_path))),
|
||||
color_string_(enemy.color),
|
||||
x1_(enemy.x1),
|
||||
x2_(enemy.x2),
|
||||
y1_(enemy.y1),
|
||||
y2_(enemy.y2),
|
||||
should_flip_(enemy.flip),
|
||||
should_mirror_(enemy.mirror) {
|
||||
// Obten el resto de valores
|
||||
sprite_->setPosX(enemy.x);
|
||||
sprite_->setPosY(enemy.y);
|
||||
sprite_->setVelX(enemy.vx);
|
||||
sprite_->setVelY(enemy.vy);
|
||||
|
||||
const int FLIP = (should_flip_ && enemy.vx < 0.0F) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const int MIRROR = should_mirror_ ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
|
||||
sprite_->setFlip(static_cast<SDL_FlipMode>(FLIP | MIRROR)); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange) SDL flags are designed for bitwise OR
|
||||
|
||||
collider_ = getRect();
|
||||
|
||||
color_ = stringToColor(color_string_);
|
||||
|
||||
// Coloca un frame al azar o el designado
|
||||
sprite_->setCurrentAnimationFrame((enemy.frame == -1) ? (rand() % sprite_->getCurrentAnimationSize()) : enemy.frame);
|
||||
}
|
||||
|
||||
// Pinta el enemigo en pantalla
|
||||
void Enemy::render() {
|
||||
sprite_->render(1, color_);
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Enemy::update(float delta_time) {
|
||||
sprite_->update(delta_time);
|
||||
checkPath();
|
||||
collider_ = getRect();
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Solo actualiza la animación sin mover al enemigo
|
||||
void Enemy::updateAnimation(float delta_time) {
|
||||
sprite_->animate(delta_time);
|
||||
}
|
||||
|
||||
// Resetea el enemigo a su posición inicial (para editor)
|
||||
void Enemy::resetToInitialPosition(const Data& data) {
|
||||
sprite_->setPosX(data.x);
|
||||
sprite_->setPosY(data.y);
|
||||
sprite_->setVelX(data.vx);
|
||||
sprite_->setVelY(data.vy);
|
||||
|
||||
const int FLIP = (should_flip_ && data.vx < 0.0F) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const int MIRROR = should_mirror_ ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
|
||||
sprite_->setFlip(static_cast<SDL_FlipMode>(FLIP | MIRROR)); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
|
||||
|
||||
collider_ = getRect();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
||||
void Enemy::checkPath() { // NOLINT(readability-make-member-function-const)
|
||||
if (sprite_->getPosX() > x2_ || sprite_->getPosX() < x1_) {
|
||||
// Recoloca
|
||||
if (sprite_->getPosX() > x2_) {
|
||||
sprite_->setPosX(x2_);
|
||||
} else {
|
||||
sprite_->setPosX(x1_);
|
||||
}
|
||||
|
||||
// Cambia el sentido
|
||||
sprite_->setVelX(sprite_->getVelX() * (-1));
|
||||
|
||||
// Invierte el sprite
|
||||
if (should_flip_) {
|
||||
sprite_->flip();
|
||||
}
|
||||
}
|
||||
|
||||
if (sprite_->getPosY() > y2_ || sprite_->getPosY() < y1_) {
|
||||
// Recoloca
|
||||
if (sprite_->getPosY() > y2_) {
|
||||
sprite_->setPosY(y2_);
|
||||
} else {
|
||||
sprite_->setPosY(y1_);
|
||||
}
|
||||
|
||||
// Cambia el sentido
|
||||
sprite_->setVelY(sprite_->getVelY() * (-1));
|
||||
|
||||
// Invierte el sprite
|
||||
if (should_flip_) {
|
||||
sprite_->flip();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo que contiene al enemigo
|
||||
auto Enemy::getRect() -> SDL_FRect {
|
||||
return sprite_->getRect();
|
||||
}
|
||||
|
||||
// Obtiene el rectangulo de colision del enemigo
|
||||
auto Enemy::getCollider() -> SDL_FRect& {
|
||||
return collider_;
|
||||
}
|
||||
55
source/game/entities/enemy.hpp
Normal file
55
source/game/entities/enemy.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
class AnimatedSprite; // lines 7-7
|
||||
|
||||
class Enemy {
|
||||
public:
|
||||
struct Data {
|
||||
std::string animation_path; // Ruta al fichero con la animación
|
||||
float x{0.0F}; // Posición inicial en el eje X
|
||||
float y{0.0F}; // Posición inicial en el eje Y
|
||||
float vx{0.0F}; // Velocidad en el eje X
|
||||
float vy{0.0F}; // Velocidad en el eje Y
|
||||
int x1{0}; // Límite izquierdo de la ruta en el eje X
|
||||
int x2{0}; // Límite derecho de la ruta en el eje X
|
||||
int y1{0}; // Límite superior de la ruta en el eje Y
|
||||
int y2{0}; // Límite inferior de la ruta en el eje Y
|
||||
bool flip{false}; // Indica si el enemigo hace flip al terminar su ruta
|
||||
bool mirror{false}; // Indica si el enemigo está volteado verticalmente
|
||||
int frame{0}; // Frame inicial para la animación del enemigo
|
||||
std::string color; // Color del enemigo
|
||||
};
|
||||
|
||||
explicit Enemy(const Data& enemy); // Constructor
|
||||
~Enemy() = default; // Destructor
|
||||
|
||||
void render(); // Pinta el enemigo en pantalla
|
||||
void update(float delta_time); // Actualiza las variables del objeto
|
||||
#ifdef _DEBUG
|
||||
void updateAnimation(float delta_time); // Solo actualiza la animación sin mover al enemigo
|
||||
void resetToInitialPosition(const Data& data); // Resetea el enemigo a su posición inicial (para editor)
|
||||
#endif
|
||||
|
||||
auto getRect() -> SDL_FRect; // Devuelve el rectangulo que contiene al enemigo
|
||||
auto getCollider() -> SDL_FRect&; // Obtiene el rectangulo de colision del enemigo
|
||||
|
||||
private:
|
||||
void checkPath(); // Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
||||
|
||||
std::shared_ptr<AnimatedSprite> sprite_; // Sprite del enemigo
|
||||
|
||||
// Variables
|
||||
Uint8 color_{0}; // Color del enemigo
|
||||
std::string color_string_; // Color del enemigo en formato texto
|
||||
int x1_{0}; // Limite izquierdo de la ruta en el eje X
|
||||
int x2_{0}; // Limite derecho de la ruta en el eje X
|
||||
int y1_{0}; // Limite superior de la ruta en el eje Y
|
||||
int y2_{0}; // Limite inferior de la ruta en el eje Y
|
||||
SDL_FRect collider_{}; // Caja de colisión
|
||||
bool should_flip_{false}; // Indica si el enemigo hace flip al terminar su ruta
|
||||
bool should_mirror_{false}; // Indica si el enemigo se dibuja volteado verticalmente
|
||||
};
|
||||
71
source/game/entities/item.cpp
Normal file
71
source/game/entities/item.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "game/entities/item.hpp"
|
||||
|
||||
#include "core/rendering/sprite/sprite.hpp" // Para SSprite
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
|
||||
// Constructor
|
||||
Item::Item(const Data& item)
|
||||
: sprite_(std::make_shared<Sprite>(Resource::Cache::get()->getSurface(item.tile_set_file), item.x, item.y, ITEM_SIZE, ITEM_SIZE)),
|
||||
time_accumulator_(static_cast<float>(item.counter) * COLOR_CHANGE_INTERVAL) {
|
||||
// Inicia variables
|
||||
sprite_->setClip((item.tile % 10) * ITEM_SIZE, (item.tile / 10) * ITEM_SIZE, ITEM_SIZE, ITEM_SIZE);
|
||||
collider_ = sprite_->getRect();
|
||||
|
||||
// Inicializa los colores
|
||||
color_.push_back(item.color1);
|
||||
color_.push_back(item.color1);
|
||||
|
||||
color_.push_back(item.color2);
|
||||
color_.push_back(item.color2);
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Item::update(float delta_time) {
|
||||
if (is_paused_) {
|
||||
return;
|
||||
}
|
||||
|
||||
time_accumulator_ += delta_time;
|
||||
}
|
||||
|
||||
// Pinta el objeto en pantalla
|
||||
void Item::render() const { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Calcula el índice de color basado en el tiempo acumulado
|
||||
const int INDEX = static_cast<int>(time_accumulator_ / COLOR_CHANGE_INTERVAL) % static_cast<int>(color_.size());
|
||||
sprite_->render(1, color_.at(INDEX));
|
||||
}
|
||||
|
||||
// Obtiene su ubicación
|
||||
auto Item::getPos() -> SDL_FPoint { // NOLINT(readability-convert-member-functions-to-static)
|
||||
const SDL_FPoint P = {.x = sprite_->getX(), .y = sprite_->getY()};
|
||||
return P;
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Establece la posición del item (para editor)
|
||||
void Item::setPosition(float x, float y) {
|
||||
sprite_->setPosition(x, y);
|
||||
collider_ = sprite_->getRect();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Cambia el tile del item (para editor)
|
||||
void Item::setTile(int tile) {
|
||||
sprite_->setClip((tile % 10) * ITEM_SIZE, (tile / 10) * ITEM_SIZE, ITEM_SIZE, ITEM_SIZE);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Asigna los colores del objeto
|
||||
void Item::setColors(Uint8 col1, Uint8 col2) {
|
||||
// Reinicializa el vector de colores
|
||||
color_.clear();
|
||||
|
||||
// Añade el primer color
|
||||
color_.push_back(col1);
|
||||
color_.push_back(col1);
|
||||
|
||||
// Añade el segundo color
|
||||
color_.push_back(col2);
|
||||
color_.push_back(col2);
|
||||
}
|
||||
48
source/game/entities/item.hpp
Normal file
48
source/game/entities/item.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
class Sprite;
|
||||
|
||||
class Item {
|
||||
public:
|
||||
struct Data {
|
||||
std::string tile_set_file; // Ruta al fichero con los gráficos del item
|
||||
float x{0.0F}; // Posición del item en pantalla
|
||||
float y{0.0F}; // Posición del item en pantalla
|
||||
int tile{0}; // Número de tile dentro de la textura
|
||||
int counter{0}; // Contador inicial. Es el que lo hace cambiar de color
|
||||
Uint8 color1{0}; // Uno de los dos colores que se utiliza para el item
|
||||
Uint8 color2{0}; // Uno de los dos colores que se utiliza para el item
|
||||
};
|
||||
|
||||
explicit Item(const Data& item); // Constructor
|
||||
~Item() = default; // Destructor
|
||||
|
||||
void render() const; // Pinta el objeto en pantalla
|
||||
void update(float delta_time); // Actualiza las variables del objeto
|
||||
|
||||
void setPaused(bool paused) { is_paused_ = paused; } // Pausa/despausa el item
|
||||
auto getCollider() -> SDL_FRect& { return collider_; } // Obtiene el rectangulo de colision del objeto
|
||||
auto getPos() -> SDL_FPoint; // Obtiene su ubicación
|
||||
void setColors(Uint8 col1, Uint8 col2); // Asigna los colores del objeto
|
||||
#ifdef _DEBUG
|
||||
void setPosition(float x, float y); // Establece la posición del item (para editor)
|
||||
void setTile(int tile); // Cambia el tile del item (para editor)
|
||||
#endif
|
||||
|
||||
private:
|
||||
static constexpr float ITEM_SIZE = 8.0F; // Tamaño del item en pixels
|
||||
static constexpr float COLOR_CHANGE_INTERVAL = 0.06F; // Intervalo de cambio de color en segundos (4 frames a 66.67fps)
|
||||
|
||||
std::shared_ptr<Sprite> sprite_; // SSprite del objeto
|
||||
|
||||
// Variables
|
||||
std::vector<Uint8> color_; // Vector con los colores del objeto
|
||||
float time_accumulator_{0.0F}; // Acumulador de tiempo para cambio de color
|
||||
SDL_FRect collider_{}; // Rectangulo de colisión
|
||||
bool is_paused_{false}; // Indica si el item está pausado
|
||||
};
|
||||
972
source/game/entities/player.cpp
Normal file
972
source/game/entities/player.cpp
Normal file
@@ -0,0 +1,972 @@
|
||||
// IWYU pragma: no_include <bits/std_abs.h>
|
||||
#include "game/entities/player.hpp"
|
||||
|
||||
#include <algorithm> // Para max, min
|
||||
#include <cmath> // Para ceil, abs
|
||||
#include <iostream>
|
||||
#include <ranges> // Para std::ranges::any_of
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/input.hpp" // Para Input, InputAction
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "game/gameplay/room.hpp" // Para Room, TileType
|
||||
#include "game/options.hpp" // Para Cheat, Options, options
|
||||
#include "utils/defines.hpp" // Para RoomBorder::BOTTOM, RoomBorder::LEFT, RoomBorder::RIGHT
|
||||
|
||||
#ifdef _DEBUG
|
||||
#include "core/system/debug.hpp" // Para Debug
|
||||
#endif
|
||||
|
||||
// Constructor
|
||||
Player::Player(const Data& player)
|
||||
: room_(player.room) {
|
||||
initSprite(player.animations_path);
|
||||
setColor();
|
||||
applySpawnValues(player.spawn_data);
|
||||
placeSprite();
|
||||
initSounds();
|
||||
|
||||
previous_state_ = state_;
|
||||
}
|
||||
|
||||
// Pinta el jugador en pantalla
|
||||
void Player::render() {
|
||||
sprite_->render(1, color_);
|
||||
#ifdef _DEBUG
|
||||
if (Debug::get()->isEnabled()) {
|
||||
Screen::get()->getRendererSurface()->putPixel(under_right_foot_.x, under_right_foot_.y, static_cast<Uint8>(PaletteColor::GREEN));
|
||||
Screen::get()->getRendererSurface()->putPixel(under_left_foot_.x, under_left_foot_.y, static_cast<Uint8>(PaletteColor::GREEN));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Player::update(float delta_time) {
|
||||
if (!is_paused_) {
|
||||
handleInput();
|
||||
updateState(delta_time);
|
||||
move(delta_time);
|
||||
handleKillingTiles(); // Los collider_points_ están actualizados por syncSpriteAndCollider() dentro de move()
|
||||
animate(delta_time);
|
||||
border_ = handleBorders();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas y modifica variables
|
||||
void Player::handleInput() {
|
||||
if (ignore_input_) { return; }
|
||||
if (Input::get()->checkAction(InputAction::LEFT)) {
|
||||
wanna_go_ = Direction::LEFT;
|
||||
} else if (Input::get()->checkAction(InputAction::RIGHT)) {
|
||||
wanna_go_ = Direction::RIGHT;
|
||||
} else {
|
||||
wanna_go_ = Direction::NONE;
|
||||
}
|
||||
|
||||
wanna_jump_ = Input::get()->checkAction(InputAction::JUMP);
|
||||
}
|
||||
|
||||
// La lógica de movimiento está distribuida en move
|
||||
void Player::move(float delta_time) {
|
||||
switch (state_) {
|
||||
case State::ON_GROUND:
|
||||
moveOnGround(delta_time);
|
||||
break;
|
||||
case State::ON_SLOPE:
|
||||
moveOnSlope(delta_time);
|
||||
break;
|
||||
case State::JUMPING:
|
||||
moveJumping(delta_time);
|
||||
break;
|
||||
case State::FALLING:
|
||||
moveFalling(delta_time);
|
||||
break;
|
||||
}
|
||||
syncSpriteAndCollider(); // Actualiza la posición del sprite y las colisiones
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("P.X", std::to_string(static_cast<int>(x_)));
|
||||
Debug::get()->set("P.Y", std::to_string(static_cast<int>(y_)));
|
||||
Debug::get()->set("P.LGP", std::to_string(last_grounded_position_));
|
||||
switch (state_) {
|
||||
case State::ON_GROUND:
|
||||
Debug::get()->set("P.STATE", "ON_GROUND");
|
||||
break;
|
||||
case State::ON_SLOPE:
|
||||
Debug::get()->set("P.STATE", "ON_SLOPE");
|
||||
break;
|
||||
case State::JUMPING:
|
||||
Debug::get()->set("P.STATE", "JUMPING");
|
||||
break;
|
||||
case State::FALLING:
|
||||
Debug::get()->set("P.STATE", "FALLING");
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Player::handleConveyorBelts() {
|
||||
if (!auto_movement_ and isOnConveyorBelt() and wanna_go_ == Direction::NONE) {
|
||||
auto_movement_ = true;
|
||||
}
|
||||
|
||||
if (auto_movement_ and !isOnConveyorBelt()) {
|
||||
auto_movement_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::handleShouldFall() {
|
||||
if (!isOnFloor() and (state_ == State::ON_GROUND || state_ == State::ON_SLOPE)) {
|
||||
transitionToState(State::FALLING);
|
||||
}
|
||||
}
|
||||
|
||||
void Player::transitionToState(State state) {
|
||||
previous_state_ = state_;
|
||||
state_ = state;
|
||||
|
||||
switch (state) {
|
||||
case State::ON_GROUND:
|
||||
vy_ = 0;
|
||||
handleDeathByFalling();
|
||||
resetSoundControllersOnLanding();
|
||||
current_slope_ = nullptr;
|
||||
break;
|
||||
case State::ON_SLOPE:
|
||||
vy_ = 0;
|
||||
handleDeathByFalling();
|
||||
resetSoundControllersOnLanding();
|
||||
updateCurrentSlope();
|
||||
if (current_slope_ == nullptr) {
|
||||
// Los pies no coinciden con ninguna rampa: tratar como suelo plano
|
||||
state_ = State::ON_GROUND;
|
||||
}
|
||||
break;
|
||||
case State::JUMPING:
|
||||
// Puede saltar desde ON_GROUND o ON_SLOPE
|
||||
if (previous_state_ == State::ON_GROUND || previous_state_ == State::ON_SLOPE) {
|
||||
vy_ = -MAX_VY;
|
||||
last_grounded_position_ = y_;
|
||||
updateVelocity();
|
||||
jump_sound_ctrl_.start();
|
||||
current_slope_ = nullptr;
|
||||
}
|
||||
break;
|
||||
case State::FALLING:
|
||||
fall_start_position_ = static_cast<int>(y_);
|
||||
last_grounded_position_ = static_cast<int>(y_);
|
||||
vy_ = MAX_VY;
|
||||
vx_ = 0.0F;
|
||||
jump_sound_ctrl_.reset();
|
||||
fall_sound_ctrl_.start(y_);
|
||||
current_slope_ = nullptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::updateState(float delta_time) {
|
||||
switch (state_) {
|
||||
case State::ON_GROUND:
|
||||
updateOnGround(delta_time);
|
||||
break;
|
||||
case State::ON_SLOPE:
|
||||
updateOnSlope(delta_time);
|
||||
break;
|
||||
case State::JUMPING:
|
||||
updateJumping(delta_time);
|
||||
break;
|
||||
case State::FALLING:
|
||||
updateFalling(delta_time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualización lógica del estado ON_GROUND
|
||||
void Player::updateOnGround(float delta_time) {
|
||||
(void)delta_time; // No usado en este método, pero se mantiene por consistencia
|
||||
handleConveyorBelts(); // Gestiona las cintas transportadoras
|
||||
handleShouldFall(); // Verifica si debe caer (no tiene suelo)
|
||||
|
||||
// Verifica si el jugador quiere saltar
|
||||
if (wanna_jump_) { transitionToState(State::JUMPING); }
|
||||
}
|
||||
|
||||
// Actualización lógica del estado ON_SLOPE
|
||||
void Player::updateOnSlope(float delta_time) {
|
||||
(void)delta_time; // No usado en este método, pero se mantiene por consistencia
|
||||
|
||||
handleShouldFall();
|
||||
// NOTA: No llamamos handleShouldFall() aquí porque moveOnSlope() ya maneja
|
||||
// todas las condiciones de salida de la rampa (out of bounds, transición a superficie plana)
|
||||
|
||||
// Verifica si el jugador quiere saltar
|
||||
if (wanna_jump_) { transitionToState(State::JUMPING); }
|
||||
}
|
||||
|
||||
// Actualización lógica del estado JUMPING
|
||||
void Player::updateJumping(float delta_time) {
|
||||
auto_movement_ = false; // Desactiva el movimiento automático durante el salto
|
||||
playJumpSound(delta_time); // Reproduce los sonidos de salto
|
||||
handleJumpEnd(); // Verifica si el salto ha terminado (alcanzó la altura inicial)
|
||||
}
|
||||
|
||||
// Actualización lógica del estado FALLING
|
||||
void Player::updateFalling(float delta_time) {
|
||||
auto_movement_ = false; // Desactiva el movimiento automático durante la caída
|
||||
playFallSound(delta_time); // Reproduce los sonidos de caída
|
||||
}
|
||||
|
||||
// Movimiento físico del estado ON_GROUND
|
||||
void Player::moveOnGround(float delta_time) {
|
||||
// Determinama cuál debe ser la velocidad a partir de automovement o de wanna_go_
|
||||
updateVelocity();
|
||||
|
||||
if (vx_ == 0.0F) { return; }
|
||||
|
||||
// Movimiento horizontal y colision con muros
|
||||
applyHorizontalMovement(delta_time);
|
||||
|
||||
// Comprueba colision con rampas, corrige y cambia estado
|
||||
const int SIDE_X = vx_ < 0.0F ? static_cast<int>(x_) : static_cast<int>(x_) + WIDTH - 1;
|
||||
const LineVertical SIDE = {
|
||||
.x = SIDE_X,
|
||||
.y1 = static_cast<int>(y_) + HEIGHT - 2,
|
||||
.y2 = static_cast<int>(y_) + HEIGHT - 1};
|
||||
|
||||
// Comprueba la rampa correspondiente según la dirección
|
||||
const int SLOPE_Y = vx_ < 0.0F ? room_->checkLeftSlopes(SIDE) : room_->checkRightSlopes(SIDE);
|
||||
if (SLOPE_Y != Collision::NONE) {
|
||||
// Hay rampa: sube al jugador para pegarlo a la rampa
|
||||
y_ = SLOPE_Y - HEIGHT;
|
||||
transitionToState(State::ON_SLOPE);
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("sl.detect_y", SLOPE_Y != Collision::NONE ? std::to_string(SLOPE_Y) : "-");
|
||||
#endif
|
||||
|
||||
// Comprueba si está sobre una rampa
|
||||
if (isOnSlope()) { transitionToState(State::ON_SLOPE); }
|
||||
}
|
||||
|
||||
// Movimiento físico del estado ON_SLOPE
|
||||
void Player::moveOnSlope(float delta_time) {
|
||||
// Determinama cuál debe ser la velocidad a partir de automovement o de wanna_go_
|
||||
updateVelocity();
|
||||
|
||||
// Verificar rampa válida antes de comprobar velocidad: si no hay rampa siempre caer,
|
||||
// independientemente de si hay o no input (evita bloqueo con vx_=0 y slope null)
|
||||
if (current_slope_ == nullptr) {
|
||||
transitionToState(State::FALLING);
|
||||
return;
|
||||
}
|
||||
|
||||
if (vx_ == 0.0F) { return; }
|
||||
|
||||
// Determinar el tipo de rampa
|
||||
const bool IS_LEFT_SLOPE = isLeftSlope();
|
||||
|
||||
// Movimiento horizontal con colisión lateral
|
||||
applyHorizontalMovement(delta_time);
|
||||
|
||||
// Seleccionar el pie apropiado según el tipo de rampa
|
||||
// Left slopes (forma \) colisionan con el pie izquierdo
|
||||
// Right slopes (forma /) colisionan con el pie derecho
|
||||
const int X = IS_LEFT_SLOPE ? x_ : x_ + WIDTH - 1;
|
||||
|
||||
// Calcular la Y basada en la ecuación de la rampa (45 grados)
|
||||
// Left slope (\): y aumenta con x -> y = y1 + (x - x1)
|
||||
// Right slope (/): y disminuye con x -> y = y1 - (x - x1)
|
||||
if (IS_LEFT_SLOPE) {
|
||||
y_ = current_slope_->y1 + (X - current_slope_->x1) - HEIGHT;
|
||||
} else {
|
||||
y_ = current_slope_->y1 - (X - current_slope_->x1) - HEIGHT;
|
||||
}
|
||||
|
||||
// Verificar si el pie ha salido de los límites horizontales de la rampa
|
||||
// Usar min/max porque LEFT slopes tienen x1<x2 pero RIGHT slopes tienen x1>x2
|
||||
const int MIN_X = std::min(current_slope_->x1, current_slope_->x2);
|
||||
const int MAX_X = std::max(current_slope_->x1, current_slope_->x2);
|
||||
const bool OUT_OF_BOUNDS = (X < MIN_X) || (X > MAX_X);
|
||||
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("sl.foot", std::to_string(X));
|
||||
Debug::get()->set("sl.y_c", std::to_string(static_cast<int>(y_)));
|
||||
Debug::get()->set("sl.oob", OUT_OF_BOUNDS ? "YES" : "ok");
|
||||
#endif
|
||||
|
||||
if (OUT_OF_BOUNDS) {
|
||||
// Determinar si estamos saliendo por arriba o por abajo de la rampa
|
||||
const bool EXITING_DOWNWARD = (X > current_slope_->x2 && IS_LEFT_SLOPE) ||
|
||||
(X < current_slope_->x1 && !IS_LEFT_SLOPE);
|
||||
const bool EXITING_UPWARD = (X < current_slope_->x1 && IS_LEFT_SLOPE) ||
|
||||
(X > current_slope_->x2 && !IS_LEFT_SLOPE);
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("sl.oob", EXITING_DOWNWARD ? "DOWN" : "UP");
|
||||
#endif
|
||||
|
||||
if (EXITING_DOWNWARD) {
|
||||
// Salida por abajo: no hacer nada
|
||||
// y_ += 1.0F;
|
||||
}
|
||||
|
||||
if (EXITING_UPWARD) {
|
||||
// Salida por arriba: bajar un pixel ya que ha subido 1 de mas al salirse de la recta
|
||||
y_ += 1.0F;
|
||||
}
|
||||
|
||||
// Verificar si hay soporte debajo (suelo plano o conveyor belt)
|
||||
if (isOnTopSurface() || isOnConveyorBelt()) {
|
||||
// Hay soporte: transición a ON_GROUND (podría ser superficie o conveyor belt)
|
||||
transitionToState(State::ON_GROUND);
|
||||
} else {
|
||||
// Sin soporte: empezar a caer
|
||||
transitionToState(State::FALLING);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Verificar transición a superficie plana
|
||||
/*if (isOnTopSurface()) {
|
||||
transitionToState(State::ON_GROUND);
|
||||
return;
|
||||
}*/
|
||||
}
|
||||
|
||||
// Movimiento físico del estado JUMPING
|
||||
void Player::moveJumping(float delta_time) {
|
||||
// Movimiento horizontal
|
||||
applyHorizontalMovement(delta_time);
|
||||
|
||||
// Movimiento vertical
|
||||
applyGravity(delta_time);
|
||||
|
||||
const float DISPLACEMENT_Y = vy_ * delta_time;
|
||||
// Movimiento vertical hacia arriba
|
||||
if (vy_ < 0.0F) {
|
||||
const SDL_FRect PROJECTION = getProjection(Direction::UP, DISPLACEMENT_Y);
|
||||
|
||||
// Comprueba la colisión
|
||||
const int POS = room_->checkBottomSurfaces(PROJECTION);
|
||||
|
||||
// Calcula la nueva posición
|
||||
if (POS == Collision::NONE) {
|
||||
// Si no hay colisión
|
||||
y_ += DISPLACEMENT_Y;
|
||||
} else {
|
||||
// Si hay colisión lo mueve hasta donde no colisiona -> FALLING
|
||||
y_ = POS + 1;
|
||||
transitionToState(State::FALLING);
|
||||
}
|
||||
}
|
||||
// Movimiento vertical hacia abajo
|
||||
else if (vy_ > 0.0F) {
|
||||
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
||||
const SDL_FRect PROJECTION = getProjection(Direction::DOWN, DISPLACEMENT_Y);
|
||||
|
||||
// JUMPING colisiona con rampas solo si vx_ == 0
|
||||
if (vx_ == 0.0F) {
|
||||
handleLandingFromAir(DISPLACEMENT_Y, PROJECTION);
|
||||
} else {
|
||||
// Comprueba la colisión con las superficies y las cintas transportadoras (sin rampas)
|
||||
// Extendemos 1px hacia arriba para detectar suelos traversados ligeramente al
|
||||
// entrar horizontalmente (consecuencia del margen h=HEIGHT-1 en la proyección horizontal)
|
||||
const SDL_FRect ADJ = {.x = PROJECTION.x, .y = PROJECTION.y - 1.0F, .w = PROJECTION.w, .h = PROJECTION.h + 1.0F};
|
||||
const float POS = std::max(room_->checkTopSurfaces(ADJ), room_->checkAutoSurfaces(ADJ));
|
||||
if (POS != Collision::NONE) {
|
||||
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
|
||||
y_ = POS - HEIGHT;
|
||||
transitionToState(State::ON_GROUND);
|
||||
} else {
|
||||
// Esta saltando con movimiento horizontal y no hay colisión con los muros
|
||||
// Calcula la nueva posición (atraviesa rampas)
|
||||
y_ += DISPLACEMENT_Y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Movimiento físico del estado FALLING
|
||||
void Player::moveFalling(float delta_time) {
|
||||
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
||||
const float DISPLACEMENT = vy_ * delta_time;
|
||||
const SDL_FRect PROJECTION = getProjection(Direction::DOWN, DISPLACEMENT);
|
||||
|
||||
// Comprueba aterrizaje en superficies y rampas
|
||||
handleLandingFromAir(DISPLACEMENT, PROJECTION);
|
||||
}
|
||||
|
||||
// Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||
auto Player::handleBorders() -> Room::Border {
|
||||
if (x_ < PlayArea::LEFT) {
|
||||
return Room::Border::LEFT;
|
||||
}
|
||||
|
||||
if (x_ + WIDTH > PlayArea::RIGHT) {
|
||||
return Room::Border::RIGHT;
|
||||
}
|
||||
|
||||
if (y_ < PlayArea::TOP) {
|
||||
return Room::Border::TOP;
|
||||
}
|
||||
|
||||
if (y_ + HEIGHT > PlayArea::BOTTOM) {
|
||||
// Si llega en estado terminal, muere y no cruza
|
||||
const bool SHOULD_DIE = static_cast<int>(y_) - last_grounded_position_ > MAX_FALLING_HEIGHT;
|
||||
if (SHOULD_DIE) { markAsDead(); }
|
||||
return is_alive_ ? Room::Border::BOTTOM : Room::Border::NONE;
|
||||
}
|
||||
|
||||
return Room::Border::NONE;
|
||||
}
|
||||
|
||||
// Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
|
||||
void Player::switchBorders() {
|
||||
switch (border_) {
|
||||
case Room::Border::TOP:
|
||||
y_ = PlayArea::BOTTOM - HEIGHT - Tile::SIZE;
|
||||
// CRÍTICO: Resetear last_grounded_position_ para evitar muerte falsa por diferencia de Y entre pantallas
|
||||
last_grounded_position_ = static_cast<int>(y_);
|
||||
transitionToState(State::ON_GROUND); // TODO: Detectar si debe ser ON_SLOPE
|
||||
break;
|
||||
|
||||
case Room::Border::BOTTOM:
|
||||
y_ = PlayArea::TOP;
|
||||
// CRÍTICO: Resetear last_grounded_position_ para evitar muerte falsa por diferencia de Y entre pantallas
|
||||
last_grounded_position_ = static_cast<int>(y_);
|
||||
transitionToState(State::ON_GROUND); // TODO: Detectar si debe ser ON_SLOPE
|
||||
break;
|
||||
|
||||
case Room::Border::RIGHT:
|
||||
x_ = PlayArea::LEFT;
|
||||
break;
|
||||
|
||||
case Room::Border::LEFT:
|
||||
x_ = PlayArea::RIGHT - WIDTH;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
border_ = Room::Border::NONE;
|
||||
syncSpriteAndCollider();
|
||||
}
|
||||
|
||||
// Aplica gravedad al jugador
|
||||
void Player::applyGravity(float delta_time) {
|
||||
// La gravedad solo se aplica cuando el jugador esta saltando
|
||||
// Nunca mientras cae o esta de pie
|
||||
if (state_ == State::JUMPING) {
|
||||
vy_ += GRAVITY_FORCE * delta_time;
|
||||
vy_ = std::min(vy_, MAX_VY);
|
||||
}
|
||||
}
|
||||
|
||||
// Establece la animación del jugador
|
||||
void Player::animate(float delta_time) { // NOLINT(readability-make-member-function-const)
|
||||
if (vx_ != 0) {
|
||||
sprite_->update(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||
void Player::handleJumpEnd() {
|
||||
// Si el jugador vuelve EXACTAMENTE a la altura inicial, debe CONTINUAR en JUMPING
|
||||
// Solo cuando la SUPERA (desciende más allá) cambia a FALLING
|
||||
if (state_ == State::JUMPING && vy_ > 0.0F && static_cast<int>(y_) > last_grounded_position_) {
|
||||
transitionToState(State::FALLING);
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula y reproduce el sonido de salto basado en tiempo transcurrido
|
||||
void Player::playJumpSound(float delta_time) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
size_t sound_index;
|
||||
if (jump_sound_ctrl_.shouldPlay(delta_time, sound_index)) {
|
||||
if (sound_index < jumping_sound_.size()) {
|
||||
Audio::get()->playSound(jumping_sound_[sound_index], Audio::Group::GAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula y reproduce el sonido de caída basado en distancia vertical recorrida
|
||||
void Player::playFallSound(float delta_time) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
size_t sound_index;
|
||||
if (fall_sound_ctrl_.shouldPlay(delta_time, y_, sound_index)) {
|
||||
if (sound_index < falling_sound_.size()) {
|
||||
Audio::get()->playSound(falling_sound_[sound_index], Audio::Group::GAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si el jugador tiene suelo debajo de los pies
|
||||
auto Player::isOnFloor() -> bool {
|
||||
bool on_top_surface = false;
|
||||
bool on_conveyor_belt = false;
|
||||
updateFeet();
|
||||
|
||||
// Comprueba las superficies
|
||||
on_top_surface |= room_->checkTopSurfaces(under_left_foot_);
|
||||
on_top_surface |= room_->checkTopSurfaces(under_right_foot_);
|
||||
|
||||
// Comprueba las cintas transportadoras
|
||||
on_conveyor_belt |= room_->checkConveyorBelts(under_left_foot_);
|
||||
on_conveyor_belt |= room_->checkConveyorBelts(under_right_foot_);
|
||||
|
||||
// Comprueba las rampas
|
||||
auto on_slope_l = room_->checkLeftSlopes(under_left_foot_);
|
||||
auto on_slope_r = room_->checkRightSlopes(under_right_foot_);
|
||||
|
||||
return on_top_surface || on_conveyor_belt || on_slope_l || on_slope_r;
|
||||
}
|
||||
|
||||
// Comprueba si el jugador está sobre una superficie
|
||||
auto Player::isOnTopSurface() -> bool {
|
||||
bool on_top_surface = false;
|
||||
updateFeet();
|
||||
|
||||
// Comprueba las superficies
|
||||
on_top_surface |= room_->checkTopSurfaces(under_left_foot_);
|
||||
on_top_surface |= room_->checkTopSurfaces(under_right_foot_);
|
||||
|
||||
return on_top_surface;
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta sobre una cinta transportadora
|
||||
auto Player::isOnConveyorBelt() -> bool {
|
||||
bool on_conveyor_belt = false;
|
||||
updateFeet();
|
||||
|
||||
// Comprueba las superficies
|
||||
on_conveyor_belt |= room_->checkConveyorBelts(under_left_foot_);
|
||||
on_conveyor_belt |= room_->checkConveyorBelts(under_right_foot_);
|
||||
|
||||
return on_conveyor_belt;
|
||||
}
|
||||
|
||||
// Comprueba si el jugador está sobre una rampa
|
||||
// Retorna true SOLO si un pie está en rampa Y el otro pie está volando (sin soporte)
|
||||
auto Player::isOnSlope() -> bool {
|
||||
updateFeet();
|
||||
|
||||
// Verificar qué pie está en qué tipo de rampa
|
||||
const bool LEFT_FOOT_ON_LEFT_SLOPE = room_->checkLeftSlopes(under_left_foot_);
|
||||
const bool RIGHT_FOOT_ON_RIGHT_SLOPE = room_->checkRightSlopes(under_right_foot_);
|
||||
|
||||
// Verificar si cada pie está "volando" (sin soporte: ni top surface ni conveyor belt)
|
||||
const bool LEFT_FOOT_FLYING = !(room_->checkTopSurfaces(under_left_foot_) ||
|
||||
room_->checkConveyorBelts(under_left_foot_));
|
||||
const bool RIGHT_FOOT_FLYING = !(room_->checkTopSurfaces(under_right_foot_) ||
|
||||
room_->checkConveyorBelts(under_right_foot_));
|
||||
|
||||
// Retornar true si UN pie en rampa Y el OTRO volando
|
||||
return (LEFT_FOOT_ON_LEFT_SLOPE && RIGHT_FOOT_FLYING) ||
|
||||
(RIGHT_FOOT_ON_RIGHT_SLOPE && LEFT_FOOT_FLYING);
|
||||
}
|
||||
|
||||
// Comprueba si current_slope_ es una rampa izquierda (ascendente a la izquierda)
|
||||
// Las rampas izquierdas tienen forma \ con x1 < x2 (x aumenta de izq a der)
|
||||
auto Player::isLeftSlope() -> bool {
|
||||
if (current_slope_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
// Left slopes (\): x1 < x2 (x aumenta de izquierda a derecha)
|
||||
// Right slopes (/): x1 > x2 (x decrece de izquierda a derecha)
|
||||
return current_slope_->x1 < current_slope_->x2;
|
||||
}
|
||||
|
||||
// Actualiza current_slope_ con la rampa correcta según el pie que toca
|
||||
void Player::updateCurrentSlope() {
|
||||
updateFeet();
|
||||
|
||||
// Left slopes (\) ascendentes a izquierda tocan el pie izquierdo
|
||||
if (room_->checkLeftSlopes(under_left_foot_)) {
|
||||
current_slope_ = room_->getSlopeAtPoint(under_left_foot_);
|
||||
}
|
||||
// Right slopes (/) ascendentes a derecha tocan el pie derecho
|
||||
else if (room_->checkRightSlopes(under_right_foot_)) {
|
||||
current_slope_ = room_->getSlopeAtPoint(under_right_foot_);
|
||||
}
|
||||
// Fallback para casos edge
|
||||
else {
|
||||
current_slope_ = room_->getSlopeAtPoint(under_left_foot_);
|
||||
if (current_slope_ == nullptr) {
|
||||
current_slope_ = room_->getSlopeAtPoint(under_right_foot_);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
if (current_slope_ != nullptr) {
|
||||
Debug::get()->set("sl.type", isLeftSlope() ? "L\\" : "R/");
|
||||
Debug::get()->set("sl.p1", std::to_string(current_slope_->x1) + "," + std::to_string(current_slope_->y1));
|
||||
Debug::get()->set("sl.p2", std::to_string(current_slope_->x2) + "," + std::to_string(current_slope_->y2));
|
||||
} else {
|
||||
Debug::get()->set("sl.type", "null");
|
||||
Debug::get()->unset("sl.p1");
|
||||
Debug::get()->unset("sl.p2");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Comprueba que el jugador no toque ningun tile de los que matan
|
||||
auto Player::handleKillingTiles() -> bool {
|
||||
// Comprueba si hay contacto con algún tile que mata
|
||||
if (std::ranges::any_of(collider_points_, [this](const auto& c) -> bool {
|
||||
return room_->getTile(c) == Room::Tile::KILL;
|
||||
})) {
|
||||
markAsDead(); // Mata al jugador inmediatamente
|
||||
return true; // Retorna en cuanto se detecta una colisión
|
||||
}
|
||||
|
||||
return false; // No se encontró ninguna colisión
|
||||
}
|
||||
|
||||
// Establece el color del jugador (0 = automático según options)
|
||||
void Player::setColor(Uint8 color) {
|
||||
if (color != 0) {
|
||||
color_ = color;
|
||||
return;
|
||||
}
|
||||
|
||||
// Color personalizado desde opciones
|
||||
if (Options::game.player_color >= 0) {
|
||||
color_ = static_cast<Uint8>(Options::game.player_color);
|
||||
} else {
|
||||
color_ = static_cast<Uint8>(PaletteColor::WHITE);
|
||||
}
|
||||
|
||||
// Si el color coincide con el fondo de la habitación, usar fallback
|
||||
if (room_ != nullptr && color_ == room_->getBGColor()) {
|
||||
color_ = (room_->getBGColor() != static_cast<Uint8>(PaletteColor::WHITE))
|
||||
? static_cast<Uint8>(PaletteColor::WHITE)
|
||||
: static_cast<Uint8>(PaletteColor::BRIGHT_BLACK);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los puntos de colisión
|
||||
void Player::updateColliderPoints() {
|
||||
const SDL_FRect RECT = getRect();
|
||||
collider_points_[0] = {.x = RECT.x, .y = RECT.y};
|
||||
collider_points_[1] = {.x = RECT.x + 7, .y = RECT.y};
|
||||
collider_points_[2] = {.x = RECT.x + 7, .y = RECT.y + 7};
|
||||
collider_points_[3] = {.x = RECT.x, .y = RECT.y + 7};
|
||||
collider_points_[4] = {.x = RECT.x, .y = RECT.y + 8};
|
||||
collider_points_[5] = {.x = RECT.x + 7, .y = RECT.y + 8};
|
||||
collider_points_[6] = {.x = RECT.x + 7, .y = RECT.y + 15};
|
||||
collider_points_[7] = {.x = RECT.x, .y = RECT.y + 15};
|
||||
}
|
||||
|
||||
// Actualiza los puntos de los pies
|
||||
void Player::updateFeet() {
|
||||
under_left_foot_ = {
|
||||
.x = x_,
|
||||
.y = y_ + HEIGHT};
|
||||
under_right_foot_ = {
|
||||
.x = x_ + WIDTH - 1,
|
||||
.y = y_ + HEIGHT};
|
||||
}
|
||||
|
||||
// Inicializa los sonidos de salto y caida
|
||||
void Player::initSounds() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (int i = 0; i < 24; ++i) {
|
||||
std::string sound_file = "jump" + std::to_string(i + 1) + ".wav";
|
||||
jumping_sound_[i] = Resource::Cache::get()->getSound(sound_file);
|
||||
|
||||
if (i >= 10) { // i+1 >= 11
|
||||
falling_sound_[i - 10] = Resource::Cache::get()->getSound(sound_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implementación de JumpSoundController::start
|
||||
void Player::JumpSoundController::start() {
|
||||
current_index = 0;
|
||||
elapsed_time = 0.0F;
|
||||
active = true;
|
||||
}
|
||||
|
||||
// Implementación de JumpSoundController::reset
|
||||
void Player::JumpSoundController::reset() {
|
||||
active = false;
|
||||
current_index = 0;
|
||||
elapsed_time = 0.0F;
|
||||
}
|
||||
|
||||
// Implementación de JumpSoundController::shouldPlay
|
||||
auto Player::JumpSoundController::shouldPlay(float delta_time, size_t& out_index) -> bool {
|
||||
if (!active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Acumula el tiempo transcurrido durante el salto
|
||||
elapsed_time += delta_time;
|
||||
|
||||
// Calcula qué sonido debería estar sonando según el tiempo
|
||||
size_t target_index = FIRST_SOUND + static_cast<size_t>((elapsed_time / SECONDS_PER_SOUND));
|
||||
target_index = std::min(target_index, LAST_SOUND);
|
||||
|
||||
// Reproduce si hemos avanzado a un nuevo sonido
|
||||
if (target_index > current_index) {
|
||||
current_index = target_index;
|
||||
out_index = current_index;
|
||||
return true; // NOLINT(readability-simplify-boolean-expr)
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Implementación de FallSoundController::start
|
||||
void Player::FallSoundController::start(float start_y) {
|
||||
current_index = 0;
|
||||
distance_traveled = 0.0F;
|
||||
last_y = start_y;
|
||||
active = true;
|
||||
}
|
||||
|
||||
// Implementación de FallSoundController::reset
|
||||
void Player::FallSoundController::reset() {
|
||||
active = false;
|
||||
current_index = 0;
|
||||
distance_traveled = 0.0F;
|
||||
}
|
||||
|
||||
// Implementación de FallSoundController::shouldPlay
|
||||
auto Player::FallSoundController::shouldPlay(float delta_time, float current_y, size_t& out_index) -> bool {
|
||||
(void)delta_time; // No usado actualmente, pero recibido por consistencia
|
||||
|
||||
if (!active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Acumula la distancia recorrida (solo hacia abajo)
|
||||
if (current_y > last_y) {
|
||||
distance_traveled += (current_y - last_y);
|
||||
}
|
||||
last_y = current_y;
|
||||
|
||||
// Calcula qué sonido debería estar sonando según el intervalo
|
||||
size_t target_index = FIRST_SOUND + static_cast<size_t>((distance_traveled / PIXELS_PER_SOUND));
|
||||
|
||||
// El sonido a reproducir se limita a LAST_SOUND (13), pero el índice interno sigue creciendo
|
||||
size_t sound_to_play = std::min(target_index, LAST_SOUND);
|
||||
|
||||
// Reproduce si hemos avanzado a un nuevo índice (permite repetición de sonido 13)
|
||||
if (target_index > current_index) {
|
||||
current_index = target_index; // Guardamos el índice real (puede ser > LAST_SOUND)
|
||||
out_index = sound_to_play; // Pero reproducimos LAST_SOUND cuando corresponde
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Aplica los valores de spawn al jugador
|
||||
void Player::applySpawnValues(const SpawnData& spawn) {
|
||||
x_ = spawn.x;
|
||||
y_ = spawn.y;
|
||||
y_prev_ = spawn.y; // Inicializar y_prev_ igual a y_ para evitar saltos en primer frame
|
||||
vx_ = spawn.vx;
|
||||
vy_ = spawn.vy;
|
||||
last_grounded_position_ = spawn.last_grounded_position;
|
||||
state_ = spawn.state;
|
||||
sprite_->setFlip(spawn.flip);
|
||||
}
|
||||
|
||||
// Resuelve nombre de skin a fichero de animación
|
||||
auto Player::skinToAnimationPath(const std::string& skin_name) -> std::string {
|
||||
if (skin_name == "default") {
|
||||
return "player.yaml";
|
||||
}
|
||||
return skin_name + ".yaml";
|
||||
}
|
||||
|
||||
// Cambia la skin del jugador en caliente preservando la orientación actual
|
||||
void Player::setSkin(const std::string& skin_name) {
|
||||
const auto FLIP = sprite_->getFlip();
|
||||
initSprite(skinToAnimationPath(skin_name));
|
||||
sprite_->setFlip(FLIP);
|
||||
}
|
||||
|
||||
// Inicializa el sprite del jugador
|
||||
void Player::initSprite(const std::string& animations_path) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
const auto& animation_data = Resource::Cache::get()->getAnimationData(animations_path);
|
||||
sprite_ = std::make_unique<AnimatedSprite>(animation_data);
|
||||
sprite_->setWidth(WIDTH);
|
||||
sprite_->setHeight(HEIGHT);
|
||||
sprite_->setCurrentAnimation("default");
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite y las colisiones
|
||||
void Player::syncSpriteAndCollider() {
|
||||
placeSprite(); // Coloca el sprite en la posición del jugador
|
||||
collider_box_ = getRect(); // Actualiza el rectangulo de colisión
|
||||
updateColliderPoints(); // Actualiza los puntos de colisión
|
||||
#ifdef _DEBUG
|
||||
updateFeet();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Coloca el sprite en la posición del jugador
|
||||
void Player::placeSprite() {
|
||||
sprite_->setPos(x_, y_);
|
||||
}
|
||||
|
||||
// Gestiona la muerta al ccaer desde muy alto
|
||||
void Player::handleDeathByFalling() {
|
||||
const int FALL_DISTANCE = static_cast<int>(y_) - last_grounded_position_;
|
||||
if (previous_state_ == State::FALLING && FALL_DISTANCE > MAX_FALLING_HEIGHT) {
|
||||
markAsDead(); // Muere si cae más de 32 píxeles
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula la velocidad en x
|
||||
void Player::updateVelocity() {
|
||||
if (auto_movement_) {
|
||||
// La cinta transportadora tiene el control
|
||||
vx_ = HORIZONTAL_VELOCITY * room_->getConveyorBeltDirection();
|
||||
sprite_->setFlip(vx_ < 0.0F ? Flip::LEFT : Flip::RIGHT);
|
||||
} else {
|
||||
// El jugador tiene el control
|
||||
switch (wanna_go_) {
|
||||
case Direction::LEFT:
|
||||
vx_ = -HORIZONTAL_VELOCITY;
|
||||
sprite_->setFlip(Flip::LEFT);
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
vx_ = HORIZONTAL_VELOCITY;
|
||||
sprite_->setFlip(Flip::RIGHT);
|
||||
break;
|
||||
case Direction::NONE:
|
||||
vx_ = 0.0F;
|
||||
break;
|
||||
default:
|
||||
vx_ = 0.0F;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica movimiento horizontal con colisión de muros
|
||||
void Player::applyHorizontalMovement(float delta_time) {
|
||||
if (vx_ == 0.0F) { return; }
|
||||
|
||||
const float DISPLACEMENT = vx_ * delta_time;
|
||||
if (vx_ < 0.0F) {
|
||||
const SDL_FRect PROJECTION = getProjection(Direction::LEFT, DISPLACEMENT);
|
||||
const int POS = room_->checkRightSurfaces(PROJECTION);
|
||||
if (POS == Collision::NONE) {
|
||||
x_ += DISPLACEMENT;
|
||||
} else {
|
||||
x_ = POS + 1;
|
||||
}
|
||||
} else {
|
||||
const SDL_FRect PROJECTION = getProjection(Direction::RIGHT, DISPLACEMENT);
|
||||
const int POS = room_->checkLeftSurfaces(PROJECTION);
|
||||
if (POS == Collision::NONE) {
|
||||
x_ += DISPLACEMENT;
|
||||
} else {
|
||||
x_ = POS - WIDTH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Detecta aterrizaje en superficies y rampas
|
||||
auto Player::handleLandingFromAir(float displacement, const SDL_FRect& projection) -> bool {
|
||||
// Comprueba la colisión con las superficies y las cintas transportadoras
|
||||
const float POS = std::max(room_->checkTopSurfaces(projection), room_->checkAutoSurfaces(projection));
|
||||
if (POS != Collision::NONE) {
|
||||
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
|
||||
y_ = POS - HEIGHT;
|
||||
transitionToState(State::ON_GROUND);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Comprueba la colisión con las rampas
|
||||
auto rect = toSDLRect(projection);
|
||||
const LineVertical LEFT_SIDE = {.x = rect.x, .y1 = rect.y, .y2 = rect.y + rect.h};
|
||||
const LineVertical RIGHT_SIDE = {.x = rect.x + rect.w - 1, .y1 = rect.y, .y2 = rect.y + rect.h};
|
||||
const float POINT = std::max(room_->checkRightSlopes(RIGHT_SIDE), room_->checkLeftSlopes(LEFT_SIDE));
|
||||
if (POINT != Collision::NONE) {
|
||||
y_ = POINT - HEIGHT;
|
||||
transitionToState(State::ON_SLOPE);
|
||||
return true;
|
||||
}
|
||||
|
||||
// No hay colisión
|
||||
y_ += displacement;
|
||||
#ifdef _DEBUG
|
||||
// Guarda por si en debug el jugador se sale de la pantalla, para que no esté cayendo infinitamente
|
||||
if (y_ > PlayArea::BOTTOM + HEIGHT) { y_ = PlayArea::TOP + 2; }
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
// Resetea los controladores de sonido al aterrizar
|
||||
void Player::resetSoundControllersOnLanding() {
|
||||
jump_sound_ctrl_.reset();
|
||||
fall_sound_ctrl_.reset();
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de proyeccion
|
||||
auto Player::getProjection(Direction direction, float displacement) -> SDL_FRect { // NOLINT(readability-convert-member-functions-to-static)
|
||||
switch (direction) {
|
||||
case Direction::LEFT:
|
||||
return {
|
||||
.x = x_ + displacement,
|
||||
.y = y_,
|
||||
.w = std::ceil(std::fabs(displacement)), // Para evitar que tenga una anchura de 0 pixels
|
||||
.h = HEIGHT - 1}; // -1 para dar ventana de 2px en aperturas de altura exacta
|
||||
|
||||
case Direction::RIGHT:
|
||||
return {
|
||||
.x = x_ + WIDTH,
|
||||
.y = y_,
|
||||
.w = std::ceil(displacement), // Para evitar que tenga una anchura de 0 pixels
|
||||
.h = HEIGHT - 1}; // -1 para dar ventana de 2px en aperturas de altura exacta
|
||||
|
||||
case Direction::UP:
|
||||
return {
|
||||
.x = x_,
|
||||
.y = y_ + displacement,
|
||||
.w = WIDTH,
|
||||
.h = std::ceil(std::fabs(displacement)) // Para evitar que tenga una altura de 0 pixels
|
||||
};
|
||||
|
||||
case Direction::DOWN:
|
||||
return {
|
||||
.x = x_,
|
||||
.y = y_ + HEIGHT,
|
||||
.w = WIDTH,
|
||||
.h = std::ceil(displacement) // Para evitar que tenga una altura de 0 pixels
|
||||
};
|
||||
|
||||
default:
|
||||
return {
|
||||
.x = 0.0F,
|
||||
.y = 0.0F,
|
||||
.w = 0.0F,
|
||||
.h = 0.0F};
|
||||
}
|
||||
}
|
||||
|
||||
// Marca al jugador como muerto
|
||||
void Player::markAsDead() {
|
||||
is_alive_ = (Options::cheats.invincible == Options::Cheat::State::ENABLED);
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Establece la posición del jugador directamente (debug)
|
||||
void Player::setDebugPosition(float x, float y) {
|
||||
x_ = x;
|
||||
y_ = y;
|
||||
syncSpriteAndCollider();
|
||||
}
|
||||
|
||||
// Fija estado ON_GROUND, velocidades a 0, actualiza last_grounded_position_ (debug)
|
||||
void Player::finalizeDebugTeleport() {
|
||||
vx_ = 0.0F;
|
||||
vy_ = 0.0F;
|
||||
last_grounded_position_ = static_cast<int>(y_);
|
||||
transitionToState(State::ON_GROUND);
|
||||
syncSpriteAndCollider();
|
||||
}
|
||||
#endif
|
||||
226
source/game/entities/player.hpp
Normal file
226
source/game/entities/player.hpp
Normal file
@@ -0,0 +1,226 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array> // Para array
|
||||
#include <limits> // Para numeric_limits
|
||||
#include <memory> // Para shared_ptr, __shared_ptr_access
|
||||
#include <string> // Para string
|
||||
#include <utility>
|
||||
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "game/gameplay/room.hpp"
|
||||
#include "game/options.hpp" // Para Cheat, Options, options
|
||||
#include "utils/defines.hpp" // Para BORDER_TOP, BLOCK
|
||||
#include "utils/utils.hpp" // Para Color
|
||||
struct JA_Sound_t; // lines 13-13
|
||||
|
||||
class Player {
|
||||
public:
|
||||
// --- Enums y Structs ---
|
||||
enum class State {
|
||||
ON_GROUND, // En suelo plano o conveyor belt
|
||||
ON_SLOPE, // En rampa/pendiente
|
||||
JUMPING,
|
||||
FALLING,
|
||||
};
|
||||
|
||||
enum class Direction {
|
||||
LEFT,
|
||||
RIGHT,
|
||||
UP,
|
||||
DOWN,
|
||||
NONE
|
||||
};
|
||||
|
||||
// --- Constantes de física (públicas para permitir cálculos en structs) ---
|
||||
static constexpr float HORIZONTAL_VELOCITY = 40.0F; // Velocidad horizontal en pixels/segundo (0.6 * 66.67fps)
|
||||
static constexpr float MAX_VY = 80.0F; // Velocidad vertical máxima en pixels/segundo (1.2 * 66.67fps)
|
||||
static constexpr float JUMP_VELOCITY = -80.0F; // Velocidad inicial del salto en pixels/segundo
|
||||
static constexpr float GRAVITY_FORCE = 155.6F; // Fuerza de gravedad en pixels/segundo² (0.035 * 66.67²)
|
||||
|
||||
struct SpawnData {
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
float vx = 0;
|
||||
float vy = 0;
|
||||
int last_grounded_position = 0;
|
||||
State state = State::ON_GROUND;
|
||||
SDL_FlipMode flip = SDL_FLIP_NONE;
|
||||
};
|
||||
|
||||
struct Data {
|
||||
SpawnData spawn_data;
|
||||
std::string animations_path;
|
||||
std::shared_ptr<Room> room = nullptr;
|
||||
};
|
||||
|
||||
struct JumpSoundController {
|
||||
// Duración del salto calculada automáticamente con física: t = 2 * v0 / g
|
||||
static constexpr float JUMP_DURATION = (2.0F * MAX_VY) / GRAVITY_FORCE;
|
||||
static constexpr size_t FIRST_SOUND = 1; // Primer sonido a reproducir (índice 1)
|
||||
static constexpr size_t LAST_SOUND = 17; // Último sonido a reproducir (índice 17)
|
||||
static constexpr float SECONDS_PER_SOUND = JUMP_DURATION / (LAST_SOUND - FIRST_SOUND + 1);
|
||||
|
||||
size_t current_index = 0; // Índice del sonido actual
|
||||
float elapsed_time = 0.0F; // Tiempo transcurrido durante el salto
|
||||
bool active = false; // Indica si el controlador está activo
|
||||
|
||||
void start(); // Inicia el controlador
|
||||
void reset(); // Resetea el controlador
|
||||
auto shouldPlay(float delta_time, size_t& out_index) -> bool; // Comprueba si debe reproducir un sonido
|
||||
};
|
||||
|
||||
struct FallSoundController {
|
||||
static constexpr float PIXELS_PER_SOUND = 5.0F; // Intervalo de píxeles por sonido (configurable)
|
||||
static constexpr size_t FIRST_SOUND = 1; // Primer sonido a reproducir (índice 1)
|
||||
static constexpr size_t LAST_SOUND = 13; // Último sonido a reproducir (índice 13)
|
||||
|
||||
size_t current_index = 0; // Índice del sonido actual
|
||||
float distance_traveled = 0.0F; // Distancia acumulada durante la caída
|
||||
float last_y = 0.0F; // Última posición Y registrada
|
||||
bool active = false; // Indica si el controlador está activo
|
||||
|
||||
void start(float start_y); // Inicia el controlador
|
||||
void reset(); // Resetea el controlador
|
||||
auto shouldPlay(float delta_time, float current_y, size_t& out_index) -> bool; // Comprueba si debe reproducir un sonido
|
||||
};
|
||||
|
||||
// --- Constructor y Destructor ---
|
||||
explicit Player(const Data& player);
|
||||
~Player() = default;
|
||||
|
||||
// --- Funciones ---
|
||||
void render(); // Pinta el enemigo en pantalla
|
||||
void update(float delta_time); // Actualiza las variables del objeto
|
||||
[[nodiscard]] auto isOnBorder() const -> bool { return border_ != Room::Border::NONE; } // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
|
||||
[[nodiscard]] auto getBorder() const -> Room::Border { return border_; } // Indica en cual de los cuatro bordes se encuentra
|
||||
void switchBorders(); // Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
|
||||
auto getRect() -> SDL_FRect { return {.x = x_, .y = y_, .w = WIDTH, .h = HEIGHT}; } // Obtiene el rectangulo que delimita al jugador
|
||||
auto getCollider() -> SDL_FRect& { return collider_box_; } // Obtiene el rectangulo de colision del jugador
|
||||
auto getSpawnParams() -> SpawnData { return {.x = x_, .y = y_, .vx = vx_, .vy = vy_, .last_grounded_position = last_grounded_position_, .state = state_, .flip = sprite_->getFlip()}; } // Obtiene el estado de reaparición del jugador
|
||||
void setColor(Uint8 color = 0); // Establece el color del jugador (0 = automático según cheats)
|
||||
void setSkin(const std::string& skin_name); // Cambia la skin del jugador en caliente ("default" o nombre de enemigo)
|
||||
static auto skinToAnimationPath(const std::string& skin_name) -> std::string; // Resuelve nombre de skin a fichero de animación
|
||||
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); } // Establece la habitación en la que se encuentra el jugador
|
||||
//[[nodiscard]] auto isAlive() const -> bool { return is_alive_ || (Options::cheats.invincible == Options::Cheat::State::ENABLED); } // Comprueba si el jugador esta vivo
|
||||
[[nodiscard]] auto isAlive() const -> bool { return is_alive_; } // Comprueba si el jugador esta vivo
|
||||
void setPaused(bool value) { is_paused_ = value; } // Pone el jugador en modo pausa
|
||||
void setIgnoreInput(bool value) { ignore_input_ = value; } // Ignora inputs del jugador (física sigue activa)
|
||||
[[nodiscard]] auto getIgnoreInput() const -> bool { return ignore_input_; }
|
||||
|
||||
#ifdef _DEBUG
|
||||
// --- Funciones de debug ---
|
||||
void setDebugPosition(float x, float y); // Establece la posición del jugador directamente (debug)
|
||||
void finalizeDebugTeleport(); // Fija estado ON_GROUND, velocidades a 0, actualiza last_grounded_position_ (debug)
|
||||
#endif
|
||||
|
||||
private:
|
||||
// --- Constantes ---
|
||||
static constexpr int WIDTH = 8; // Ancho del jugador
|
||||
static constexpr int HEIGHT = 16; // ALto del jugador
|
||||
static constexpr int MAX_FALLING_HEIGHT = Tile::SIZE * 4; // Altura maxima permitida de caída en pixels
|
||||
|
||||
// --- Objetos y punteros ---
|
||||
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
|
||||
std::unique_ptr<AnimatedSprite> sprite_; // Sprite del jugador
|
||||
|
||||
// --- Variables de posición y física ---
|
||||
float x_ = 0.0F; // Posición del jugador en el eje X
|
||||
float y_ = 0.0F; // Posición del jugador en el eje Y
|
||||
float y_prev_ = 0.0F; // Posición Y del frame anterior (para detectar hitos de distancia en sonidos)
|
||||
float vx_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje X
|
||||
float vy_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje Y
|
||||
|
||||
Direction wanna_go_ = Direction::NONE;
|
||||
bool wanna_jump_ = false;
|
||||
|
||||
// --- Variables de estado ---
|
||||
State state_ = State::ON_GROUND; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
|
||||
State previous_state_ = State::ON_GROUND; // Estado previo en el que se encontraba el jugador
|
||||
|
||||
// --- Variables de colisión ---
|
||||
SDL_FRect collider_box_{}; // Caja de colisión con los enemigos u objetos
|
||||
std::array<SDL_FPoint, 8> collider_points_{}; // Puntos de colisión con el mapa
|
||||
SDL_FPoint under_left_foot_ = {.x = 0.0F, .y = 0.0F}; // El punto bajo la esquina inferior izquierda del jugador
|
||||
SDL_FPoint under_right_foot_ = {.x = 0.0F, .y = 0.0F}; // El punto bajo la esquina inferior derecha del jugador
|
||||
const LineDiagonal* current_slope_{nullptr}; // Rampa actual sobe la que está el jugador
|
||||
|
||||
// --- Variables de juego ---
|
||||
bool is_alive_ = true; // Indica si el jugador esta vivo o no
|
||||
bool is_paused_ = false; // Indica si el jugador esta en modo pausa
|
||||
bool ignore_input_ = false; // Ignora inputs pero mantiene la física activa
|
||||
bool auto_movement_ = false; // Indica si esta siendo arrastrado por una superficie automatica
|
||||
Room::Border border_ = Room::Border::TOP; // Indica en cual de los cuatro bordes se encuentra
|
||||
int last_grounded_position_ = 0; // Ultima posición en Y en la que se estaba en contacto con el suelo (hace doble función: tracking de caída + altura inicial del salto)
|
||||
|
||||
// --- Variables de renderizado y sonido ---
|
||||
Uint8 color_ = 0; // Color del jugador
|
||||
std::array<JA_Sound_t*, 24> jumping_sound_{}; // Array con todos los sonidos del salto
|
||||
std::array<JA_Sound_t*, 14> falling_sound_{}; // Array con todos los sonidos de la caída
|
||||
JumpSoundController jump_sound_ctrl_; // Controlador de sonidos de salto
|
||||
FallSoundController fall_sound_ctrl_; // Controlador de sonidos de caída
|
||||
int fall_start_position_ = 0; // Posición Y al iniciar la caída
|
||||
|
||||
void handleConveyorBelts();
|
||||
void handleShouldFall();
|
||||
void updateState(float delta_time);
|
||||
|
||||
// --- Métodos de actualización por estado ---
|
||||
void updateOnGround(float delta_time); // Actualización lógica estado ON_GROUND
|
||||
void updateOnSlope(float delta_time); // Actualización lógica estado ON_SLOPE
|
||||
void updateJumping(float delta_time); // Actualización lógica estado JUMPING
|
||||
void updateFalling(float delta_time); // Actualización lógica estado FALLING
|
||||
|
||||
// --- Métodos de movimiento por estado ---
|
||||
void moveOnGround(float delta_time); // Movimiento físico estado ON_GROUND
|
||||
void moveOnSlope(float delta_time); // Movimiento físico estado ON_SLOPE
|
||||
void moveJumping(float delta_time); // Movimiento físico estado JUMPING
|
||||
void moveFalling(float delta_time); // Movimiento físico estado FALLING
|
||||
|
||||
// --- Funciones de inicialización ---
|
||||
void initSprite(const std::string& animations_path); // Inicializa el sprite del jugador
|
||||
void initSounds(); // Inicializa los sonidos de salto y caida
|
||||
void applySpawnValues(const SpawnData& spawn); // Aplica los valores de spawn al jugador
|
||||
|
||||
// --- Funciones de procesamiento de entrada ---
|
||||
void handleInput(); // Comprueba las entradas y modifica variables
|
||||
|
||||
// --- Funciones de gestión de estado ---
|
||||
void transitionToState(State state); // Cambia el estado del jugador
|
||||
|
||||
// --- Funciones de física ---
|
||||
void applyGravity(float delta_time); // Aplica gravedad al jugador
|
||||
|
||||
// --- Funciones de movimiento y colisión ---
|
||||
void move(float delta_time); // Orquesta el movimiento del jugador
|
||||
auto getProjection(Direction direction, float displacement) -> SDL_FRect; // Devuelve el rectangulo de proyeccion
|
||||
void applyHorizontalMovement(float delta_time); // Aplica movimiento horizontal con colisión de muros
|
||||
auto handleLandingFromAir(float displacement, const SDL_FRect& projection) -> bool; // Detecta aterrizaje en superficies y rampas
|
||||
void resetSoundControllersOnLanding(); // Resetea los controladores de sonido al aterrizar
|
||||
|
||||
// --- Funciones de detección de superficies ---
|
||||
auto isOnFloor() -> bool; // Comprueba si el jugador tiene suelo debajo de los pies
|
||||
auto isOnTopSurface() -> bool; // Comprueba si el jugador está sobre una superficie
|
||||
auto isOnConveyorBelt() -> bool; // Comprueba si el jugador esta sobre una cinta transportadora
|
||||
auto isOnSlope() -> bool; // Comprueba si el jugador está sobre una rampa
|
||||
auto isLeftSlope() -> bool; // Comprueba si current_slope_ es una rampa izquierda (ascendente a la izquierda)
|
||||
void updateCurrentSlope(); // Actualiza current_slope_ con la rampa correcta y muestra debug info
|
||||
|
||||
// --- Funciones de actualización de geometría ---
|
||||
void syncSpriteAndCollider(); // Actualiza collider_box y collision points
|
||||
void updateColliderPoints(); // Actualiza los puntos de colisión
|
||||
void updateFeet(); // Actualiza los puntos de los pies
|
||||
void placeSprite(); // Coloca el sprite en la posición del jugador
|
||||
|
||||
// --- Funciones de finalización ---
|
||||
void animate(float delta_time); // Establece la animación del jugador
|
||||
auto handleBorders() -> Room::Border; // Comprueba si se halla en alguno de los cuatro bordes
|
||||
void handleJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||
auto handleKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
|
||||
void playJumpSound(float delta_time); // Calcula y reproduce el sonido de salto
|
||||
void playFallSound(float delta_time); // Calcula y reproduce el sonido de caer
|
||||
void handleDeathByFalling(); // Gestiona la muerte al caer desde muy alto
|
||||
void updateVelocity(); // Calcula la velocidad en x
|
||||
void markAsDead(); // Marca al jugador como muerto
|
||||
};
|
||||
33
source/game/game_control.hpp
Normal file
33
source/game/game_control.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace GameControl {
|
||||
// Disponible en todos los builds — cambia la skin del jugador ("default" o nombre de enemigo)
|
||||
inline std::function<void(const std::string&)> change_player_skin;
|
||||
// Disponible en todos los builds — cambia el color del jugador (-1 = automático, 0-15 = color fijo)
|
||||
inline std::function<void(int)> change_player_color;
|
||||
} // namespace GameControl
|
||||
|
||||
#ifdef _DEBUG
|
||||
namespace GameControl {
|
||||
// Registrada por Game::Game() — cambia la habitación activa
|
||||
inline std::function<bool(const std::string&)> change_room;
|
||||
// Registrada por Game::Game() — devuelve el fichero de la habitación activa (ej. "03.yaml")
|
||||
inline std::function<std::string()> get_current_room;
|
||||
// Registrada por Game::Game() — fija el contador de items recogidos
|
||||
inline std::function<void(int)> set_items;
|
||||
// Registrada por Game::Game() — hace toggle del modo debug (equivale a tecla 0)
|
||||
inline std::function<void()> toggle_debug_mode;
|
||||
// Registrada por Game::Game() — guarda la habitación actual como habitación de inicio en debug.yaml
|
||||
inline std::function<std::string()> set_initial_room;
|
||||
// Registrada por Game::Game() — guarda la posición/flip actuales del jugador como posición de inicio en debug.yaml
|
||||
inline std::function<std::string()> set_initial_pos;
|
||||
// Registradas por Game::Game() — control del editor de mapas
|
||||
inline std::function<void()> enter_editor;
|
||||
inline std::function<void()> exit_editor;
|
||||
inline std::function<std::string()> revert_editor;
|
||||
inline std::function<void()> reload_current_room; // Recarga la habitación actual desde disco
|
||||
inline std::function<std::string(const std::string&)> get_adjacent_room; // Obtiene la room adyacente (UP/DOWN/LEFT/RIGHT)
|
||||
} // namespace GameControl
|
||||
#endif
|
||||
163
source/game/gameplay/cheevos.cpp
Normal file
163
source/game/gameplay/cheevos.cpp
Normal file
@@ -0,0 +1,163 @@
|
||||
#include "game/gameplay/cheevos.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // Para std::count_if
|
||||
#include <cstddef> // Para NULL
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ofstream
|
||||
#include <iostream> // Para cout, cerr
|
||||
#include <utility>
|
||||
|
||||
#include "core/locale/locale.hpp" // Para Locale
|
||||
#include "game/options.hpp" // Para Options, options
|
||||
#include "game/ui/notifier.hpp" // Para Notifier
|
||||
|
||||
// [SINGLETON]
|
||||
Cheevos* Cheevos::cheevos = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Cheevos::init(const std::string& file) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
Cheevos::cheevos = new Cheevos(file);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Cheevos::destroy() {
|
||||
delete Cheevos::cheevos;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
auto Cheevos::get() -> Cheevos* {
|
||||
return Cheevos::cheevos;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Cheevos::Cheevos(std::string file)
|
||||
: file_(std::move(file)) {
|
||||
init();
|
||||
loadFromFile();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Cheevos::~Cheevos() {
|
||||
saveToFile();
|
||||
}
|
||||
|
||||
// Inicializa los logros
|
||||
void Cheevos::init() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
cheevos_list_.clear();
|
||||
auto* loc = Locale::get();
|
||||
cheevos_list_.emplace_back(Achievement{.id = 1, .caption = loc->get("achievements.c1"), .description = loc->get("achievements.d1"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 2, .caption = loc->get("achievements.c2"), .description = loc->get("achievements.d2"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 3, .caption = loc->get("achievements.c3"), .description = loc->get("achievements.d3"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 4, .caption = loc->get("achievements.c4"), .description = loc->get("achievements.d4"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 5, .caption = loc->get("achievements.c5"), .description = loc->get("achievements.d5"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 6, .caption = loc->get("achievements.c6"), .description = loc->get("achievements.d6"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 7, .caption = loc->get("achievements.c7"), .description = loc->get("achievements.d7"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 8, .caption = loc->get("achievements.c8"), .description = loc->get("achievements.d8"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 9, .caption = loc->get("achievements.c9"), .description = loc->get("achievements.d9"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 10, .caption = loc->get("achievements.c10"), .description = loc->get("achievements.d10"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 11, .caption = loc->get("achievements.c11"), .description = loc->get("achievements.d11"), .icon = 2});
|
||||
cheevos_list_.emplace_back(Achievement{.id = 12, .caption = loc->get("achievements.c12"), .description = loc->get("achievements.d12"), .icon = 2});
|
||||
}
|
||||
|
||||
// Busca un logro por id y devuelve el indice
|
||||
auto Cheevos::find(int id) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (int i = 0; i < (int)cheevos_list_.size(); ++i) {
|
||||
if (cheevos_list_[i].id == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Desbloquea un logro
|
||||
void Cheevos::unlock(int id) {
|
||||
const int INDEX = find(id);
|
||||
|
||||
// Si el índice es inválido, el logro no es válido, ya está completado o el sistema de logros no está habilitado, no hacemos nada
|
||||
if (INDEX == -1 || !cheevos_list_.at(INDEX).obtainable || cheevos_list_.at(INDEX).completed || !enabled_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Marcar el logro como completado
|
||||
cheevos_list_.at(INDEX).completed = true;
|
||||
|
||||
// Mostrar notificación en la pantalla
|
||||
Notifier::get()->show({Locale::get()->get("achievements.header"), cheevos_list_.at(INDEX).caption}, Notifier::Style::CHEEVO, -1, false);
|
||||
|
||||
// Guardar el estado de los logros
|
||||
saveToFile();
|
||||
}
|
||||
|
||||
// Invalida un logro
|
||||
void Cheevos::setUnobtainable(int id) {
|
||||
const int INDEX = find(id);
|
||||
|
||||
// Si el índice es válido, se invalida el logro
|
||||
if (INDEX != -1) {
|
||||
cheevos_list_.at(INDEX).obtainable = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Carga el estado de los logros desde un fichero
|
||||
void Cheevos::loadFromFile() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
std::ifstream file(file_, std::ios::binary);
|
||||
|
||||
// El fichero no existe
|
||||
if (!file) {
|
||||
std::cout << "Warning: Unable to open " << file_ << "! Creating new file..." << '\n';
|
||||
|
||||
// Crea el fichero en modo escritura (binario)
|
||||
std::ofstream new_file(file_, std::ios::binary);
|
||||
|
||||
if (new_file) {
|
||||
std::cout << "New " << file_ << " created!" << '\n';
|
||||
|
||||
// Guarda la información
|
||||
for (const auto& cheevo : cheevos_list_) {
|
||||
new_file.write(reinterpret_cast<const char*>(&cheevo.completed), sizeof(bool));
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Error: Unable to create " << file_ << "!" << '\n';
|
||||
}
|
||||
}
|
||||
// El fichero existe
|
||||
else {
|
||||
std::cout << "Reading " << file_ << '\n';
|
||||
|
||||
// Carga los datos
|
||||
for (auto& cheevo : cheevos_list_) {
|
||||
file.read(reinterpret_cast<char*>(&cheevo.completed), sizeof(bool));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Guarda el estado de los logros en un fichero
|
||||
void Cheevos::saveToFile() {
|
||||
// Abre el fichero en modo escritura (binario)
|
||||
SDL_IOStream* file = SDL_IOFromFile(this->file_.c_str(), "w+b");
|
||||
if (file != nullptr) {
|
||||
// Guarda la información
|
||||
for (auto& i : cheevos_list_) {
|
||||
SDL_WriteIO(file, &i.completed, sizeof(bool));
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_CloseIO(file);
|
||||
} else {
|
||||
std::cout << "Error: Unable to save file! " << SDL_GetError() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve el número total de logros desbloqueados
|
||||
auto Cheevos::getTotalUnlockedAchievements() -> int {
|
||||
return std::count_if(cheevos_list_.begin(), cheevos_list_.end(), [](const auto& cheevo) -> bool { return cheevo.completed; });
|
||||
}
|
||||
|
||||
// Elimina el estado "no obtenible"
|
||||
void Cheevos::clearUnobtainableState() {
|
||||
for (auto& cheevo : cheevos_list_) {
|
||||
cheevo.obtainable = true;
|
||||
}
|
||||
}
|
||||
55
source/game/gameplay/cheevos.hpp
Normal file
55
source/game/gameplay/cheevos.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <string> // Para string
|
||||
#include <utility>
|
||||
#include <vector> // Para vector
|
||||
|
||||
class Cheevos {
|
||||
public:
|
||||
// Tipos anidados (públicos porque se usan en la interfaz)
|
||||
struct Achievement {
|
||||
int id{0}; // Identificador del logro
|
||||
std::string caption; // Texto con el nombre del logro
|
||||
std::string description; // Texto que describe el logro
|
||||
int icon{0}; // Indice del icono a utilizar en la notificación
|
||||
bool completed{false}; // Indica si se ha obtenido el logro
|
||||
bool obtainable{true}; // Indica si se puede obtener el logro
|
||||
};
|
||||
|
||||
using Achievements = std::vector<Achievement>; // Type alias para vector de logros
|
||||
|
||||
// Gestión singleton
|
||||
static void init(const std::string& file); // Inicialización
|
||||
static void destroy(); // Destrucción
|
||||
static auto get() -> Cheevos*; // Acceso al singleton
|
||||
|
||||
// Gestión de logros
|
||||
void unlock(int id); // Desbloquea un logro
|
||||
void setUnobtainable(int id); // Invalida un logro
|
||||
void clearUnobtainableState(); // Elimina el estado "no obtenible"
|
||||
void enable(bool value) { enabled_ = value; } // Habilita o deshabilita los logros
|
||||
|
||||
// Consultas
|
||||
[[nodiscard]] auto list() const -> const Achievements& { return cheevos_list_; } // Lista los logros
|
||||
auto getTotalUnlockedAchievements() -> int; // Devuelve logros desbloqueados
|
||||
auto size() -> int { return cheevos_list_.size(); } // Devuelve número total de logros
|
||||
|
||||
private:
|
||||
// Constantes singleton
|
||||
static Cheevos* cheevos; // [SINGLETON] Objeto privado
|
||||
|
||||
// Métodos privados
|
||||
void init(); // Inicializa los logros
|
||||
auto find(int id) -> int; // Busca un logro por id y devuelve el índice
|
||||
void loadFromFile(); // Carga el estado de los logros desde un fichero
|
||||
void saveToFile(); // Guarda el estado de los logros en un fichero
|
||||
|
||||
// Constructor y destructor privados [SINGLETON]
|
||||
explicit Cheevos(std::string file);
|
||||
~Cheevos();
|
||||
|
||||
// Variables miembro
|
||||
Achievements cheevos_list_; // Listado de logros
|
||||
bool enabled_{true}; // Indica si los logros se pueden obtener
|
||||
std::string file_; // Fichero donde leer/almacenar el estado de los logros
|
||||
};
|
||||
510
source/game/gameplay/collision_map.cpp
Normal file
510
source/game/gameplay/collision_map.cpp
Normal file
@@ -0,0 +1,510 @@
|
||||
#include "collision_map.hpp"
|
||||
|
||||
#include <algorithm> // Para std::ranges::any_of
|
||||
|
||||
#ifdef _DEBUG
|
||||
#include "core/system/debug.hpp" // Para Debug
|
||||
#endif
|
||||
#include "utils/defines.hpp" // Para Collision
|
||||
|
||||
// Constructor
|
||||
CollisionMap::CollisionMap(std::vector<int> tile_map, int tile_set_width, int conveyor_belt_direction)
|
||||
: tile_map_(std::move(tile_map)),
|
||||
tile_set_width_(tile_set_width),
|
||||
conveyor_belt_direction_(conveyor_belt_direction) {
|
||||
// Inicializa todas las superficies de colisión
|
||||
initializeSurfaces();
|
||||
}
|
||||
|
||||
// Inicializa todas las superficies de colisión
|
||||
void CollisionMap::initializeSurfaces() {
|
||||
setBottomSurfaces();
|
||||
setTopSurfaces();
|
||||
setLeftSurfaces();
|
||||
setRightSurfaces();
|
||||
setLeftSlopes();
|
||||
setRightSlopes();
|
||||
setAutoSurfaces();
|
||||
}
|
||||
|
||||
// Devuelve el tipo de tile que hay en ese pixel
|
||||
auto CollisionMap::getTile(SDL_FPoint point) const -> Tile {
|
||||
const int ROW = static_cast<int>(point.y / TILE_SIZE);
|
||||
const int COL = static_cast<int>(point.x / TILE_SIZE);
|
||||
const int POS = (ROW * MAP_WIDTH) + COL;
|
||||
return getTile(POS);
|
||||
}
|
||||
|
||||
// Devuelve el tipo de tile que hay en ese indice
|
||||
auto CollisionMap::getTile(int index) const -> Tile { // NOLINT(readability-convert-member-functions-to-static)
|
||||
const bool ON_RANGE = (index > -1) && (index < (int)tile_map_.size());
|
||||
|
||||
if (ON_RANGE) {
|
||||
// Las filas 0-8 son de tiles t_wall
|
||||
if ((tile_map_[index] >= 0) && (tile_map_[index] < 9 * tile_set_width_)) {
|
||||
return Tile::WALL;
|
||||
}
|
||||
|
||||
// Las filas 9-17 son de tiles t_passable
|
||||
if ((tile_map_[index] >= 9 * tile_set_width_) && (tile_map_[index] < 18 * tile_set_width_)) {
|
||||
return Tile::PASSABLE;
|
||||
}
|
||||
|
||||
// Las filas 18-20 es de tiles t_animated
|
||||
if ((tile_map_[index] >= 18 * tile_set_width_) && (tile_map_[index] < 21 * tile_set_width_)) {
|
||||
return Tile::ANIMATED;
|
||||
}
|
||||
|
||||
// La fila 21 es de tiles t_slope_r
|
||||
if ((tile_map_[index] >= 21 * tile_set_width_) && (tile_map_[index] < 22 * tile_set_width_)) {
|
||||
return Tile::SLOPE_R;
|
||||
}
|
||||
|
||||
// La fila 22 es de tiles t_slope_l
|
||||
if ((tile_map_[index] >= 22 * tile_set_width_) && (tile_map_[index] < 23 * tile_set_width_)) {
|
||||
return Tile::SLOPE_L;
|
||||
}
|
||||
|
||||
// La fila 23 es de tiles t_kill
|
||||
if ((tile_map_[index] >= 23 * tile_set_width_) && (tile_map_[index] < 24 * tile_set_width_)) {
|
||||
return Tile::KILL;
|
||||
}
|
||||
}
|
||||
|
||||
return Tile::EMPTY;
|
||||
}
|
||||
|
||||
// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
|
||||
auto CollisionMap::getSlopeHeight(SDL_FPoint p, Tile slope) -> int {
|
||||
// Calcula la base del tile
|
||||
int base = ((p.y / TILE_SIZE) * TILE_SIZE) + TILE_SIZE;
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("slope.BASE", std::to_string(base));
|
||||
#endif
|
||||
|
||||
// Calcula cuanto se ha entrado en el tile horizontalmente
|
||||
const int POS = (static_cast<int>(p.x) % TILE_SIZE); // Esto da un valor entre 0 y 7
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("slope.POS", std::to_string(POS));
|
||||
#endif
|
||||
|
||||
// Se resta a la base la cantidad de pixeles pos en funcion de la rampa
|
||||
if (slope == Tile::SLOPE_R) {
|
||||
base -= POS + 1;
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("slope.result", "BASE_R=" + std::to_string(base));
|
||||
#endif
|
||||
} else {
|
||||
base -= (TILE_SIZE - POS);
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->set("slope.result", "BASE_L=" + std::to_string(base));
|
||||
#endif
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// === Queries de colisión ===
|
||||
|
||||
// Comprueba las colisiones con paredes derechas
|
||||
auto CollisionMap::checkRightSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (const auto& s : right_walls_) {
|
||||
if (checkCollision(s, rect)) {
|
||||
return s.x;
|
||||
}
|
||||
}
|
||||
return Collision::NONE;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con paredes izquierdas
|
||||
auto CollisionMap::checkLeftSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (const auto& s : left_walls_) {
|
||||
if (checkCollision(s, rect)) {
|
||||
return s.x;
|
||||
}
|
||||
}
|
||||
return Collision::NONE;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con techos
|
||||
auto CollisionMap::checkTopSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (const auto& s : top_floors_) {
|
||||
if (checkCollision(s, rect)) {
|
||||
return s.y;
|
||||
}
|
||||
}
|
||||
return Collision::NONE;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con techos
|
||||
auto CollisionMap::checkTopSurfaces(const SDL_FPoint& p) -> bool {
|
||||
return std::ranges::any_of(top_floors_, [&](const auto& s) -> bool {
|
||||
return checkCollision(s, p);
|
||||
});
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con suelos
|
||||
auto CollisionMap::checkBottomSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (const auto& s : bottom_floors_) {
|
||||
if (checkCollision(s, rect)) {
|
||||
return s.y;
|
||||
}
|
||||
}
|
||||
return Collision::NONE;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con conveyor belts
|
||||
auto CollisionMap::checkAutoSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (const auto& s : conveyor_belt_floors_) {
|
||||
if (checkCollision(s, rect)) {
|
||||
return s.y;
|
||||
}
|
||||
}
|
||||
return Collision::NONE;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con conveyor belts
|
||||
auto CollisionMap::checkConveyorBelts(const SDL_FPoint& p) -> bool {
|
||||
return std::ranges::any_of(conveyor_belt_floors_, [&](const auto& s) -> bool {
|
||||
return checkCollision(s, p);
|
||||
});
|
||||
}
|
||||
|
||||
// Comprueba las colisiones línea con rampas izquierdas
|
||||
auto CollisionMap::checkLeftSlopes(const LineVertical& line) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (const auto& slope : left_slopes_) {
|
||||
const auto P = checkCollision(slope, line);
|
||||
if (P.x != -1) {
|
||||
return P.y;
|
||||
}
|
||||
}
|
||||
return Collision::NONE;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con rampas izquierdas
|
||||
auto CollisionMap::checkLeftSlopes(const SDL_FPoint& p) -> bool {
|
||||
return std::ranges::any_of(left_slopes_, [&](const auto& slope) -> bool {
|
||||
return checkCollision(p, slope);
|
||||
});
|
||||
}
|
||||
|
||||
// Comprueba las colisiones línea con rampas derechas
|
||||
auto CollisionMap::checkRightSlopes(const LineVertical& line) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (const auto& slope : right_slopes_) {
|
||||
const auto P = checkCollision(slope, line);
|
||||
if (P.x != -1) {
|
||||
return P.y;
|
||||
}
|
||||
}
|
||||
return Collision::NONE;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con rampas derechas
|
||||
auto CollisionMap::checkRightSlopes(const SDL_FPoint& p) -> bool {
|
||||
return std::ranges::any_of(right_slopes_, [&](const auto& slope) -> bool {
|
||||
return checkCollision(p, slope);
|
||||
});
|
||||
}
|
||||
|
||||
// Obtiene puntero a slope en un punto (prioriza left_slopes_ sobre right_slopes_)
|
||||
auto CollisionMap::getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal* { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Primero busca en rampas izquierdas
|
||||
for (const auto& slope : left_slopes_) {
|
||||
if (checkCollision(p, slope)) {
|
||||
return &slope;
|
||||
}
|
||||
}
|
||||
|
||||
// Luego busca en rampas derechas
|
||||
for (const auto& slope : right_slopes_) {
|
||||
if (checkCollision(p, slope)) {
|
||||
return &slope;
|
||||
}
|
||||
}
|
||||
|
||||
// No hay colisión con ninguna slope
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// === Helpers para recopilar tiles ===
|
||||
|
||||
// Helper: recopila tiles inferiores (muros sin muro debajo)
|
||||
auto CollisionMap::collectBottomTiles() -> std::vector<int> { // NOLINT(readability-make-member-function-const)
|
||||
std::vector<int> tile;
|
||||
|
||||
// Busca todos los tiles de tipo muro que no tengan debajo otro muro
|
||||
// Hay que recorrer la habitación por filas (excepto los de la última fila)
|
||||
for (int i = 0; i < (int)tile_map_.size() - MAP_WIDTH; ++i) {
|
||||
if (getTile(i) == Tile::WALL && getTile(i + MAP_WIDTH) != Tile::WALL) {
|
||||
tile.push_back(i);
|
||||
|
||||
// Si llega al final de la fila, introduce un separador
|
||||
if (i % MAP_WIDTH == MAP_WIDTH - 1) {
|
||||
tile.push_back(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un terminador
|
||||
tile.push_back(-1);
|
||||
return tile;
|
||||
}
|
||||
|
||||
// Helper: recopila tiles superiores (muros o pasables sin muro encima)
|
||||
auto CollisionMap::collectTopTiles() -> std::vector<int> { // NOLINT(readability-make-member-function-const)
|
||||
std::vector<int> tile;
|
||||
|
||||
// Busca todos los tiles de tipo muro o pasable que no tengan encima un muro
|
||||
// Hay que recorrer la habitación por filas (excepto los de la primera fila)
|
||||
for (int i = MAP_WIDTH; i < (int)tile_map_.size(); ++i) {
|
||||
if ((getTile(i) == Tile::WALL || getTile(i) == Tile::PASSABLE) && getTile(i - MAP_WIDTH) != Tile::WALL) {
|
||||
tile.push_back(i);
|
||||
|
||||
// Si llega al final de la fila, introduce un separador
|
||||
if (i % MAP_WIDTH == MAP_WIDTH - 1) {
|
||||
tile.push_back(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un terminador
|
||||
tile.push_back(-1);
|
||||
return tile;
|
||||
}
|
||||
|
||||
// Helper: recopila tiles animados (para superficies automaticas/conveyor belts)
|
||||
auto CollisionMap::collectAnimatedTiles() -> std::vector<int> { // NOLINT(readability-make-member-function-const)
|
||||
std::vector<int> tile;
|
||||
|
||||
// Busca todos los tiles de tipo animado
|
||||
// Hay que recorrer la habitación por filas (excepto los de la primera fila)
|
||||
for (int i = MAP_WIDTH; i < (int)tile_map_.size(); ++i) {
|
||||
if (getTile(i) == Tile::ANIMATED) {
|
||||
tile.push_back(i);
|
||||
|
||||
// Si llega al final de la fila, introduce un separador
|
||||
if (i % MAP_WIDTH == MAP_WIDTH - 1) {
|
||||
tile.push_back(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un terminador si hay tiles
|
||||
if (!tile.empty()) {
|
||||
tile.push_back(-1);
|
||||
}
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
// Helper: construye lineas horizontales a partir de tiles consecutivos
|
||||
void CollisionMap::buildHorizontalLines(const std::vector<int>& tiles, std::vector<LineHorizontal>& lines, bool is_bottom_surface) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (tiles.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (i < static_cast<int>(tiles.size()) - 1) {
|
||||
LineHorizontal line;
|
||||
line.x1 = (tiles[i] % MAP_WIDTH) * TILE_SIZE;
|
||||
|
||||
// Calcula Y segun si es superficie inferior o superior
|
||||
if (is_bottom_surface) {
|
||||
line.y = ((tiles[i] / MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
} else {
|
||||
line.y = (tiles[i] / MAP_WIDTH) * TILE_SIZE;
|
||||
}
|
||||
|
||||
int last_one = i;
|
||||
i++;
|
||||
|
||||
// Encuentra tiles consecutivos
|
||||
if (i < static_cast<int>(tiles.size())) {
|
||||
while (tiles[i] == tiles[i - 1] + 1) {
|
||||
last_one = i;
|
||||
i++;
|
||||
if (i >= static_cast<int>(tiles.size())) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
line.x2 = ((tiles[last_one] % MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
lines.push_back(line);
|
||||
|
||||
// Salta separadores
|
||||
if (i < static_cast<int>(tiles.size()) && tiles[i] == -1) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// === Métodos de generación de geometría ===
|
||||
|
||||
// Calcula las superficies inferiores
|
||||
void CollisionMap::setBottomSurfaces() {
|
||||
std::vector<int> tile = collectBottomTiles();
|
||||
buildHorizontalLines(tile, bottom_floors_, true);
|
||||
}
|
||||
|
||||
// Calcula las superficies superiores
|
||||
void CollisionMap::setTopSurfaces() {
|
||||
std::vector<int> tile = collectTopTiles();
|
||||
buildHorizontalLines(tile, top_floors_, false);
|
||||
}
|
||||
|
||||
// Calcula las superficies laterales izquierdas
|
||||
void CollisionMap::setLeftSurfaces() { // NOLINT(readability-make-member-function-const)
|
||||
std::vector<int> tile;
|
||||
|
||||
// Busca todos los tiles de tipo muro que no tienen a su izquierda un tile de tipo muro
|
||||
// Hay que recorrer la habitación por columnas (excepto los de la primera columna)
|
||||
for (int i = 1; i < MAP_WIDTH; ++i) {
|
||||
for (int j = 0; j < MAP_HEIGHT; ++j) {
|
||||
const int POS = ((j * MAP_WIDTH) + i);
|
||||
if (getTile(POS) == Tile::WALL && getTile(POS - 1) != Tile::WALL) {
|
||||
tile.push_back(POS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un terminador
|
||||
tile.push_back(-1);
|
||||
|
||||
// Recorre el vector de tiles buscando tiles consecutivos
|
||||
// (Los tiles de la misma columna, la diferencia entre ellos es de mapWidth)
|
||||
// para localizar las superficies
|
||||
if ((int)tile.size() > 1) {
|
||||
int i = 0;
|
||||
do {
|
||||
LineVertical line;
|
||||
line.x = (tile[i] % MAP_WIDTH) * TILE_SIZE;
|
||||
line.y1 = ((tile[i] / MAP_WIDTH) * TILE_SIZE);
|
||||
while (tile[i] + MAP_WIDTH == tile[i + 1]) {
|
||||
if (i == (int)tile.size() - 1) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
line.y2 = ((tile[i] / MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
left_walls_.push_back(line);
|
||||
i++;
|
||||
} while (i < (int)tile.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula las superficies laterales derechas
|
||||
void CollisionMap::setRightSurfaces() { // NOLINT(readability-make-member-function-const)
|
||||
std::vector<int> tile;
|
||||
|
||||
// Busca todos los tiles de tipo muro que no tienen a su derecha un tile de tipo muro
|
||||
// Hay que recorrer la habitación por columnas (excepto los de la última columna)
|
||||
for (int i = 0; i < MAP_WIDTH - 1; ++i) {
|
||||
for (int j = 0; j < MAP_HEIGHT; ++j) {
|
||||
const int POS = ((j * MAP_WIDTH) + i);
|
||||
if (getTile(POS) == Tile::WALL && getTile(POS + 1) != Tile::WALL) {
|
||||
tile.push_back(POS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Añade un terminador
|
||||
tile.push_back(-1);
|
||||
|
||||
// Recorre el vector de tiles buscando tiles consecutivos
|
||||
// (Los tiles de la misma columna, la diferencia entre ellos es de mapWidth)
|
||||
// para localizar las superficies
|
||||
if ((int)tile.size() > 1) {
|
||||
int i = 0;
|
||||
do {
|
||||
LineVertical line;
|
||||
line.x = ((tile[i] % MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
line.y1 = ((tile[i] / MAP_WIDTH) * TILE_SIZE);
|
||||
while (tile[i] + MAP_WIDTH == tile[i + 1]) {
|
||||
if (i == (int)tile.size() - 1) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
line.y2 = ((tile[i] / MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
right_walls_.push_back(line);
|
||||
i++;
|
||||
} while (i < (int)tile.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Encuentra todas las rampas que suben hacia la izquierda
|
||||
void CollisionMap::setLeftSlopes() { // NOLINT(readability-make-member-function-const)
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_slope_l
|
||||
std::vector<int> found;
|
||||
for (int i = 0; i < (int)tile_map_.size(); ++i) {
|
||||
if (getTile(i) == Tile::SLOPE_L) {
|
||||
found.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
// El primer elemento es el inicio de una rampa. Se añade ese elemento y se buscan los siguientes,
|
||||
// que seran i + mapWidth + 1. Conforme se añaden se eliminan y se vuelve a escudriñar el vector de
|
||||
// tiles encontrados hasta que esté vacío
|
||||
|
||||
while (!found.empty()) {
|
||||
LineDiagonal line;
|
||||
line.x1 = (found[0] % MAP_WIDTH) * TILE_SIZE;
|
||||
line.y1 = (found[0] / MAP_WIDTH) * TILE_SIZE;
|
||||
int looking_for = found[0] + MAP_WIDTH + 1;
|
||||
int last_one_found = found[0];
|
||||
found.erase(found.begin());
|
||||
for (int i = 0; i < (int)found.size(); ++i) {
|
||||
if (found[i] == looking_for) {
|
||||
last_one_found = looking_for;
|
||||
looking_for += MAP_WIDTH + 1;
|
||||
found.erase(found.begin() + i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
line.x2 = ((last_one_found % MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
line.y2 = ((last_one_found / MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
left_slopes_.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Encuentra todas las rampas que suben hacia la derecha
|
||||
void CollisionMap::setRightSlopes() { // NOLINT(readability-make-member-function-const)
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_slope_r
|
||||
std::vector<int> found;
|
||||
for (int i = 0; i < (int)tile_map_.size(); ++i) {
|
||||
if (getTile(i) == Tile::SLOPE_R) {
|
||||
found.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
// El primer elemento es el inicio de una rampa. Se añade ese elemento y se buscan los siguientes,
|
||||
// que seran i + mapWidth - 1. Conforme se añaden se eliminan y se vuelve a escudriñar el vector de
|
||||
// tiles encontrados hasta que esté vacío
|
||||
|
||||
while (!found.empty()) {
|
||||
LineDiagonal line;
|
||||
line.x1 = ((found[0] % MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
line.y1 = (found[0] / MAP_WIDTH) * TILE_SIZE;
|
||||
int looking_for = found[0] + MAP_WIDTH - 1;
|
||||
int last_one_found = found[0];
|
||||
found.erase(found.begin());
|
||||
for (int i = 0; i < (int)found.size(); ++i) {
|
||||
if (found[i] == looking_for) {
|
||||
last_one_found = looking_for;
|
||||
looking_for += MAP_WIDTH - 1;
|
||||
found.erase(found.begin() + i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
line.x2 = (last_one_found % MAP_WIDTH) * TILE_SIZE;
|
||||
line.y2 = ((last_one_found / MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
right_slopes_.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula las superficies automaticas (conveyor belts)
|
||||
void CollisionMap::setAutoSurfaces() {
|
||||
std::vector<int> tile = collectAnimatedTiles();
|
||||
buildHorizontalLines(tile, conveyor_belt_floors_, false);
|
||||
}
|
||||
121
source/game/gameplay/collision_map.hpp
Normal file
121
source/game/gameplay/collision_map.hpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical
|
||||
|
||||
/**
|
||||
* @brief Mapa de colisiones de una habitación
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - Almacenar la geometría de colisión (superficies, rampas, conveyor belts)
|
||||
* - Generar geometría a partir del tilemap
|
||||
* - Proporcionar queries de colisión para Player y otras entidades
|
||||
* - Determinar tipo de tile en posiciones específicas
|
||||
*/
|
||||
class CollisionMap {
|
||||
public:
|
||||
// Enumeración de tipos de tile (para colisiones)
|
||||
enum class Tile {
|
||||
EMPTY,
|
||||
WALL,
|
||||
PASSABLE,
|
||||
SLOPE_L,
|
||||
SLOPE_R,
|
||||
KILL,
|
||||
ANIMATED
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor
|
||||
* @param tile_map Vector con índices de tiles de la habitación
|
||||
* @param tile_set_width Ancho del tileset en tiles (para calcular tipo de tile)
|
||||
* @param conveyor_belt_direction Dirección de las cintas transportadoras (-1, 0, +1)
|
||||
*/
|
||||
CollisionMap(std::vector<int> tile_map, int tile_set_width, int conveyor_belt_direction);
|
||||
~CollisionMap() = default;
|
||||
|
||||
// Prohibir copia y movimiento
|
||||
CollisionMap(const CollisionMap&) = delete;
|
||||
auto operator=(const CollisionMap&) -> CollisionMap& = delete;
|
||||
CollisionMap(CollisionMap&&) = delete;
|
||||
auto operator=(CollisionMap&&) -> CollisionMap& = delete;
|
||||
|
||||
// --- Queries de tipo de tile ---
|
||||
[[nodiscard]] auto getTile(SDL_FPoint point) const -> Tile; // Devuelve el tipo de tile en un punto (pixel)
|
||||
[[nodiscard]] auto getTile(int index) const -> Tile; // Devuelve el tipo de tile en un índice del tilemap
|
||||
|
||||
// --- Queries de colisión con superficies ---
|
||||
auto checkRightSurfaces(const SDL_FRect& rect) -> int; // Colisión con paredes derechas (retorna X)
|
||||
auto checkLeftSurfaces(const SDL_FRect& rect) -> int; // Colisión con paredes izquierdas (retorna X)
|
||||
auto checkTopSurfaces(const SDL_FRect& rect) -> int; // Colisión con techos (retorna Y)
|
||||
auto checkTopSurfaces(const SDL_FPoint& p) -> bool; // Colisión punto con techos
|
||||
auto checkBottomSurfaces(const SDL_FRect& rect) -> int; // Colisión con suelos (retorna Y)
|
||||
|
||||
// --- Queries de colisión con superficies automáticas (conveyor belts) ---
|
||||
auto checkAutoSurfaces(const SDL_FRect& rect) -> int; // Colisión con conveyor belts (retorna Y)
|
||||
auto checkConveyorBelts(const SDL_FPoint& p) -> bool; // Colisión punto con conveyor belts
|
||||
|
||||
// --- Queries de colisión con rampas ---
|
||||
auto checkLeftSlopes(const LineVertical& line) -> int; // Colisión línea con rampas izquierdas (retorna Y)
|
||||
auto checkLeftSlopes(const SDL_FPoint& p) -> bool; // Colisión punto con rampas izquierdas
|
||||
auto checkRightSlopes(const LineVertical& line) -> int; // Colisión línea con rampas derechas (retorna Y)
|
||||
auto checkRightSlopes(const SDL_FPoint& p) -> bool; // Colisión punto con rampas derechas
|
||||
[[nodiscard]] auto getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal*; // Obtiene puntero a slope en un punto
|
||||
|
||||
// --- Métodos estáticos ---
|
||||
static auto getTileSize() -> int { return TILE_SIZE; } // Tamaño del tile en pixels
|
||||
static auto getSlopeHeight(SDL_FPoint p, Tile slope) -> int; // Altura de rampa en un punto
|
||||
|
||||
// --- Getters ---
|
||||
[[nodiscard]] auto getConveyorBeltDirection() const -> int { return conveyor_belt_direction_; }
|
||||
|
||||
// Getters para debug visualization
|
||||
[[nodiscard]] auto getBottomFloors() const -> const std::vector<LineHorizontal>& { return bottom_floors_; }
|
||||
[[nodiscard]] auto getTopFloors() const -> const std::vector<LineHorizontal>& { return top_floors_; }
|
||||
[[nodiscard]] auto getLeftWalls() const -> const std::vector<LineVertical>& { return left_walls_; }
|
||||
[[nodiscard]] auto getRightWalls() const -> const std::vector<LineVertical>& { return right_walls_; }
|
||||
[[nodiscard]] auto getLeftSlopes() const -> const std::vector<LineDiagonal>& { return left_slopes_; }
|
||||
[[nodiscard]] auto getRightSlopes() const -> const std::vector<LineDiagonal>& { return right_slopes_; }
|
||||
[[nodiscard]] auto getConveyorBeltFloors() const -> const std::vector<LineHorizontal>& { return conveyor_belt_floors_; }
|
||||
|
||||
private:
|
||||
// --- Constantes ---
|
||||
static constexpr int TILE_SIZE = 8; // Tamaño del tile en pixels
|
||||
static constexpr int MAP_WIDTH = 32; // Ancho del mapa en tiles
|
||||
static constexpr int MAP_HEIGHT = 16; // Alto del mapa en tiles
|
||||
|
||||
// --- Datos de la habitación ---
|
||||
std::vector<int> tile_map_; // Índices de tiles de la habitación
|
||||
int tile_set_width_; // Ancho del tileset en tiles
|
||||
int conveyor_belt_direction_; // Dirección de conveyor belts
|
||||
|
||||
// --- Geometría de colisión ---
|
||||
std::vector<LineHorizontal> bottom_floors_; // Superficies inferiores (suelos)
|
||||
std::vector<LineHorizontal> top_floors_; // Superficies superiores (techos)
|
||||
std::vector<LineVertical> left_walls_; // Paredes izquierdas
|
||||
std::vector<LineVertical> right_walls_; // Paredes derechas
|
||||
std::vector<LineDiagonal> left_slopes_; // Rampas que suben hacia la izquierda
|
||||
std::vector<LineDiagonal> right_slopes_; // Rampas que suben hacia la derecha
|
||||
std::vector<LineHorizontal> conveyor_belt_floors_; // Superficies automáticas (conveyor belts)
|
||||
|
||||
// --- Métodos privados de generación de geometría ---
|
||||
void initializeSurfaces(); // Inicializa todas las superficies de colisión
|
||||
|
||||
// Helpers para recopilar tiles
|
||||
auto collectBottomTiles() -> std::vector<int>; // Tiles con superficie inferior
|
||||
auto collectTopTiles() -> std::vector<int>; // Tiles con superficie superior
|
||||
auto collectAnimatedTiles() -> std::vector<int>; // Tiles animados (conveyor belts)
|
||||
|
||||
// Construcción de geometría
|
||||
static void buildHorizontalLines(const std::vector<int>& tiles, std::vector<LineHorizontal>& lines, bool is_bottom_surface);
|
||||
void setBottomSurfaces(); // Calcula superficies inferiores
|
||||
void setTopSurfaces(); // Calcula superficies superiores
|
||||
void setLeftSurfaces(); // Calcula paredes izquierdas
|
||||
void setRightSurfaces(); // Calcula paredes derechas
|
||||
void setLeftSlopes(); // Calcula rampas izquierdas
|
||||
void setRightSlopes(); // Calcula rampas derechas
|
||||
void setAutoSurfaces(); // Calcula conveyor belts
|
||||
};
|
||||
76
source/game/gameplay/enemy_manager.cpp
Normal file
76
source/game/gameplay/enemy_manager.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "enemy_manager.hpp"
|
||||
|
||||
#include <algorithm> // Para std::ranges::any_of
|
||||
|
||||
#include "game/entities/enemy.hpp" // Para Enemy
|
||||
#include "utils/utils.hpp" // Para checkCollision
|
||||
|
||||
// Añade un enemigo a la colección
|
||||
void EnemyManager::addEnemy(std::shared_ptr<Enemy> enemy) { // NOLINT(readability-identifier-naming)
|
||||
enemies_.push_back(std::move(enemy));
|
||||
}
|
||||
|
||||
// Elimina todos los enemigos
|
||||
void EnemyManager::clear() {
|
||||
enemies_.clear();
|
||||
}
|
||||
|
||||
// Elimina el último enemigo de la colección
|
||||
void EnemyManager::removeLastEnemy() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (!enemies_.empty()) {
|
||||
enemies_.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si no hay enemigos
|
||||
auto EnemyManager::isEmpty() const -> bool {
|
||||
return enemies_.empty();
|
||||
}
|
||||
|
||||
// Actualiza todos los enemigos
|
||||
void EnemyManager::update(float delta_time) {
|
||||
for (const auto& enemy : enemies_) {
|
||||
enemy->update(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
// Renderiza todos los enemigos
|
||||
void EnemyManager::render() {
|
||||
for (const auto& enemy : enemies_) {
|
||||
enemy->render();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Solo actualiza animaciones sin mover enemigos
|
||||
void EnemyManager::updateAnimations(float delta_time) {
|
||||
for (const auto& enemy : enemies_) {
|
||||
enemy->updateAnimation(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
// Resetea todos los enemigos a su posición inicial
|
||||
void EnemyManager::resetPositions(const std::vector<Enemy::Data>& enemy_data) {
|
||||
const int COUNT = std::min(static_cast<int>(enemies_.size()), static_cast<int>(enemy_data.size()));
|
||||
for (int i = 0; i < COUNT; ++i) {
|
||||
enemies_[i]->resetToInitialPosition(enemy_data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Número de enemigos
|
||||
auto EnemyManager::getCount() const -> int {
|
||||
return static_cast<int>(enemies_.size());
|
||||
}
|
||||
|
||||
// Acceso a un enemigo por índice
|
||||
auto EnemyManager::getEnemy(int index) -> std::shared_ptr<Enemy>& {
|
||||
return enemies_.at(index);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Comprueba si hay colisión con algún enemigo
|
||||
auto EnemyManager::checkCollision(SDL_FRect& rect) -> bool {
|
||||
return std::ranges::any_of(enemies_, [&rect](const auto& enemy) {
|
||||
return ::checkCollision(rect, enemy->getCollider());
|
||||
});
|
||||
}
|
||||
52
source/game/gameplay/enemy_manager.hpp
Normal file
52
source/game/gameplay/enemy_manager.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "game/entities/enemy.hpp" // Para Enemy, Enemy::Data
|
||||
|
||||
/**
|
||||
* @brief Gestor de enemigos de una habitación
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - Almacenar y gestionar la colección de enemigos
|
||||
* - Actualizar todos los enemigos
|
||||
* - Renderizar todos los enemigos
|
||||
* - Detectar colisiones con enemigos
|
||||
*/
|
||||
class EnemyManager {
|
||||
public:
|
||||
EnemyManager() = default;
|
||||
~EnemyManager() = default;
|
||||
|
||||
// Prohibir copia y movimiento para evitar duplicación accidental
|
||||
EnemyManager(const EnemyManager&) = delete;
|
||||
auto operator=(const EnemyManager&) -> EnemyManager& = delete;
|
||||
EnemyManager(EnemyManager&&) = delete;
|
||||
auto operator=(EnemyManager&&) -> EnemyManager& = delete;
|
||||
|
||||
// Gestión de enemigos
|
||||
void addEnemy(std::shared_ptr<Enemy> enemy); // Añade un enemigo a la colección
|
||||
void clear(); // Elimina todos los enemigos
|
||||
void removeLastEnemy(); // Elimina el último enemigo de la colección
|
||||
[[nodiscard]] auto isEmpty() const -> bool; // Comprueba si no hay enemigos
|
||||
|
||||
// Actualización y renderizado
|
||||
void update(float delta_time); // Actualiza todos los enemigos
|
||||
void render(); // Renderiza todos los enemigos
|
||||
|
||||
// Detección de colisiones
|
||||
auto checkCollision(SDL_FRect& rect) -> bool; // Comprueba si hay colisión con algún enemigo
|
||||
|
||||
#ifdef _DEBUG
|
||||
void updateAnimations(float delta_time); // Solo actualiza animaciones sin mover enemigos
|
||||
void resetPositions(const std::vector<Enemy::Data>& enemy_data); // Resetea todos los enemigos a su posición inicial
|
||||
[[nodiscard]] auto getCount() const -> int; // Número de enemigos
|
||||
auto getEnemy(int index) -> std::shared_ptr<Enemy>&; // Acceso a un enemigo por índice
|
||||
#endif
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<Enemy>> enemies_; // Colección de enemigos
|
||||
};
|
||||
69
source/game/gameplay/item_manager.cpp
Normal file
69
source/game/gameplay/item_manager.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "item_manager.hpp"
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "game/entities/item.hpp" // Para Item
|
||||
#include "game/options.hpp" // Para Options
|
||||
#include "item_tracker.hpp" // Para ItemTracker
|
||||
#include "scoreboard.hpp" // Para Scoreboard::Data
|
||||
#include "utils/utils.hpp" // Para checkCollision
|
||||
|
||||
// Constructor
|
||||
ItemManager::ItemManager(std::string room_name, std::shared_ptr<Scoreboard::Data> scoreboard_data)
|
||||
: room_name_(std::move(room_name)),
|
||||
data_(std::move(scoreboard_data)) {
|
||||
}
|
||||
|
||||
// Añade un item a la colección
|
||||
void ItemManager::addItem(std::shared_ptr<Item> item) { // NOLINT(readability-identifier-naming)
|
||||
items_.push_back(std::move(item));
|
||||
}
|
||||
|
||||
// Elimina todos los items
|
||||
void ItemManager::clear() {
|
||||
items_.clear();
|
||||
}
|
||||
|
||||
// Actualiza todos los items
|
||||
void ItemManager::update(float delta_time) {
|
||||
for (const auto& item : items_) {
|
||||
item->update(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
// Renderiza todos los items
|
||||
void ItemManager::render() {
|
||||
for (const auto& item : items_) {
|
||||
item->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Pausa/despausa todos los items
|
||||
void ItemManager::setPaused(bool paused) {
|
||||
for (const auto& item : items_) {
|
||||
item->setPaused(paused);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si hay colisión con algún item
|
||||
auto ItemManager::checkCollision(SDL_FRect& rect) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (int i = 0; i < static_cast<int>(items_.size()); ++i) {
|
||||
if (::checkCollision(rect, items_.at(i)->getCollider())) {
|
||||
// Registra el item como recogido
|
||||
ItemTracker::get()->addItem(room_name_, items_.at(i)->getPos());
|
||||
|
||||
// Elimina el item de la colección
|
||||
items_.erase(items_.begin() + i);
|
||||
|
||||
// Reproduce el sonido de pickup
|
||||
Audio::get()->playSound("item.wav", Audio::Group::GAME);
|
||||
|
||||
// Actualiza el scoreboard y estadísticas
|
||||
data_->items++;
|
||||
Options::stats.items = data_->items;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
73
source/game/gameplay/item_manager.hpp
Normal file
73
source/game/gameplay/item_manager.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "game/entities/item.hpp" // Para Item, Item::Data
|
||||
#include "scoreboard.hpp" // Para Scoreboard::Data
|
||||
|
||||
/**
|
||||
* @brief Gestor de items de una habitación
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - Almacenar y gestionar la colección de items
|
||||
* - Actualizar todos los items
|
||||
* - Renderizar todos los items
|
||||
* - Detectar colisiones con items y gestionar pickup
|
||||
* - Integración con ItemTracker, Scoreboard y Audio
|
||||
*/
|
||||
class ItemManager {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor
|
||||
* @param room_name Nombre de la habitación (para ItemTracker)
|
||||
* @param scoreboard_data Puntero compartido a los datos del scoreboard
|
||||
*/
|
||||
ItemManager(std::string room_name, std::shared_ptr<Scoreboard::Data> scoreboard_data);
|
||||
~ItemManager() = default;
|
||||
|
||||
// Prohibir copia y movimiento para evitar duplicación accidental
|
||||
ItemManager(const ItemManager&) = delete;
|
||||
auto operator=(const ItemManager&) -> ItemManager& = delete;
|
||||
ItemManager(ItemManager&&) = delete;
|
||||
auto operator=(ItemManager&&) -> ItemManager& = delete;
|
||||
|
||||
// Gestión de items
|
||||
void addItem(std::shared_ptr<Item> item); // Añade un item a la colección
|
||||
void clear(); // Elimina todos los items
|
||||
|
||||
// Actualización y renderizado
|
||||
void update(float delta_time); // Actualiza todos los items
|
||||
void render(); // Renderiza todos los items
|
||||
|
||||
// Estado
|
||||
void setPaused(bool paused); // Pausa/despausa todos los items
|
||||
|
||||
#ifdef _DEBUG
|
||||
[[nodiscard]] auto getCount() const -> int { return static_cast<int>(items_.size()); } // Número de items
|
||||
auto getItem(int index) -> std::shared_ptr<Item>& { return items_.at(index); } // Acceso a un item por índice
|
||||
#endif
|
||||
|
||||
// Detección de colisiones
|
||||
/**
|
||||
* @brief Comprueba si hay colisión con algún item
|
||||
*
|
||||
* Si hay colisión:
|
||||
* - Añade el item a ItemTracker
|
||||
* - Elimina el item de la colección
|
||||
* - Reproduce el sonido de pickup
|
||||
* - Actualiza el scoreboard y estadísticas
|
||||
*
|
||||
* @param rect Rectángulo de colisión
|
||||
* @return true si hubo colisión, false en caso contrario
|
||||
*/
|
||||
auto checkCollision(SDL_FRect& rect) -> bool;
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<Item>> items_; // Colección de items
|
||||
std::string room_name_; // Nombre de la habitación
|
||||
std::shared_ptr<Scoreboard::Data> data_; // Datos del scoreboard
|
||||
};
|
||||
75
source/game/gameplay/item_tracker.cpp
Normal file
75
source/game/gameplay/item_tracker.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "game/gameplay/item_tracker.hpp"
|
||||
|
||||
// [SINGLETON]
|
||||
ItemTracker* ItemTracker::item_tracker = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void ItemTracker::init() {
|
||||
ItemTracker::item_tracker = new ItemTracker();
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void ItemTracker::destroy() {
|
||||
delete ItemTracker::item_tracker;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
auto ItemTracker::get() -> ItemTracker* {
|
||||
return ItemTracker::item_tracker;
|
||||
}
|
||||
|
||||
// Comprueba si el objeto ya ha sido cogido
|
||||
auto ItemTracker::hasBeenPicked(const std::string& name, SDL_FPoint pos) -> bool {
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
if (const int INDEX = findByName(name); INDEX != NOT_FOUND) {
|
||||
// Luego busca si existe ya una entrada con esa posición
|
||||
if (findByPos(INDEX, pos) != NOT_FOUND) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Añade el objeto a la lista de objetos cogidos
|
||||
void ItemTracker::addItem(const std::string& name, SDL_FPoint pos) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Comprueba si el objeto no ha sido recogido con anterioridad
|
||||
if (!hasBeenPicked(name, pos)) {
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
if (const int INDEX = findByName(name); INDEX != NOT_FOUND) {
|
||||
items_.at(INDEX).pos.push_back(pos);
|
||||
}
|
||||
// En caso contrario crea la entrada
|
||||
else {
|
||||
items_.emplace_back(name, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Busca una entrada en la lista por nombre
|
||||
auto ItemTracker::findByName(const std::string& name) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
int i = 0;
|
||||
|
||||
for (const auto& item : items_) {
|
||||
if (item.name == name) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
// Busca una entrada en la lista por posición
|
||||
auto ItemTracker::findByPos(int index, SDL_FPoint pos) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
int i = 0;
|
||||
|
||||
for (const auto& item : items_[index].pos) {
|
||||
if ((item.x == pos.x) && (item.y == pos.y)) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return NOT_FOUND;
|
||||
}
|
||||
49
source/game/gameplay/item_tracker.hpp
Normal file
49
source/game/gameplay/item_tracker.hpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string> // Para string, basic_string
|
||||
#include <utility>
|
||||
#include <vector> // Para vector
|
||||
|
||||
class ItemTracker {
|
||||
public:
|
||||
// Gestión singleton
|
||||
static void init(); // Inicialización
|
||||
static void destroy(); // Destrucción
|
||||
static auto get() -> ItemTracker*; // Acceso al singleton
|
||||
|
||||
// Gestión de items
|
||||
auto hasBeenPicked(const std::string& name, SDL_FPoint pos) -> bool; // Comprueba si el objeto ya ha sido cogido
|
||||
void addItem(const std::string& name, SDL_FPoint pos); // Añade el objeto a la lista
|
||||
|
||||
private:
|
||||
// Tipos anidados privados
|
||||
struct Data {
|
||||
std::string name; // Nombre de la habitación donde se encuentra el objeto
|
||||
std::vector<SDL_FPoint> pos; // Lista de objetos cogidos de la habitación
|
||||
|
||||
// Constructor para facilitar creación con posición inicial
|
||||
Data(std::string name, const SDL_FPoint& position)
|
||||
: name(std::move(name)) {
|
||||
pos.push_back(position);
|
||||
}
|
||||
};
|
||||
|
||||
// Constantes privadas
|
||||
static constexpr int NOT_FOUND = -1; // Valor de retorno cuando no se encuentra un elemento
|
||||
|
||||
// Constantes singleton
|
||||
static ItemTracker* item_tracker; // [SINGLETON] Objeto privado
|
||||
|
||||
// Métodos privados
|
||||
auto findByName(const std::string& name) -> int; // Busca una entrada en la lista por nombre
|
||||
auto findByPos(int index, SDL_FPoint pos) -> int; // Busca una entrada en la lista por posición
|
||||
|
||||
// Constructor y destructor privados [SINGLETON]
|
||||
ItemTracker() = default;
|
||||
~ItemTracker() = default;
|
||||
|
||||
// Variables miembro
|
||||
std::vector<Data> items_; // Lista con todos los objetos recogidos
|
||||
};
|
||||
301
source/game/gameplay/room.cpp
Normal file
301
source/game/gameplay/room.cpp
Normal file
@@ -0,0 +1,301 @@
|
||||
#include "game/gameplay/room.hpp"
|
||||
|
||||
#include <utility> // Para std::move
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "game/defaults.hpp" // Para Defaults::Game
|
||||
#include "game/gameplay/collision_map.hpp" // Para CollisionMap
|
||||
#include "game/gameplay/enemy_manager.hpp" // Para EnemyManager
|
||||
#include "game/gameplay/item_manager.hpp" // Para ItemManager
|
||||
#include "game/gameplay/item_tracker.hpp" // Para ItemTracker
|
||||
#include "game/gameplay/room_loader.hpp" // Para RoomLoader
|
||||
#include "game/gameplay/scoreboard.hpp" // Para Scoreboard::Data
|
||||
#include "game/gameplay/tilemap_renderer.hpp" // Para TilemapRenderer
|
||||
#include "utils/defines.hpp" // Para TILE_SIZE
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
// Constructor
|
||||
Room::Room(const std::string& room_path, std::shared_ptr<Scoreboard::Data> data)
|
||||
: data_(std::move(data)) {
|
||||
auto room = Resource::Cache::get()->getRoom(room_path);
|
||||
|
||||
// Crea los managers de enemigos e items
|
||||
enemy_manager_ = std::make_unique<EnemyManager>();
|
||||
item_manager_ = std::make_unique<ItemManager>(room->name, data_);
|
||||
|
||||
initializeRoom(*room);
|
||||
openTheJail(); // Abre la Jail si se da el caso
|
||||
|
||||
// Crea el mapa de colisiones (necesita tile_map_, tile_set_width_, conveyor_belt_direction_)
|
||||
collision_map_ = std::make_unique<CollisionMap>(tile_map_, tile_set_width_, conveyor_belt_direction_);
|
||||
|
||||
// Crea el renderizador del tilemap (necesita tile_map_, tile_set_width_, surface_, bg_color_, conveyor_belt_direction_)
|
||||
tilemap_renderer_ = std::make_unique<TilemapRenderer>(tile_map_, tile_set_width_, surface_, bg_color_, conveyor_belt_direction_);
|
||||
tilemap_renderer_->initialize(collision_map_.get()); // Inicializa (crea map_surface, pinta tiles, busca animados)
|
||||
|
||||
Screen::get()->setBorderColor(stringToColor(border_color_)); // Establece el color del borde
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Room::~Room() = default;
|
||||
|
||||
void Room::initializeRoom(const Data& room) {
|
||||
// Asignar valores a las variables miembro
|
||||
number_ = room.number;
|
||||
name_ = room.name;
|
||||
bg_color_ = room.bg_color;
|
||||
border_color_ = room.border_color;
|
||||
item_color1_ = room.item_color1.empty() ? "yellow" : room.item_color1;
|
||||
item_color2_ = room.item_color2.empty() ? "magenta" : room.item_color2;
|
||||
upper_room_ = room.upper_room;
|
||||
lower_room_ = room.lower_room;
|
||||
left_room_ = room.left_room;
|
||||
right_room_ = room.right_room;
|
||||
tile_set_file_ = room.tile_set_file;
|
||||
conveyor_belt_direction_ = room.conveyor_belt_direction;
|
||||
tile_map_ = room.tile_map; // Tilemap viene embebido en el YAML
|
||||
surface_ = Resource::Cache::get()->getSurface(room.tile_set_file);
|
||||
tile_set_width_ = surface_->getWidth() / TILE_SIZE;
|
||||
is_paused_ = false;
|
||||
|
||||
// Crear los enemigos usando el manager
|
||||
for (const auto& enemy_data : room.enemies) {
|
||||
enemy_manager_->addEnemy(std::make_shared<Enemy>(enemy_data));
|
||||
}
|
||||
|
||||
// Crear los items usando el manager
|
||||
for (const auto& item : room.items) {
|
||||
const SDL_FPoint ITEM_POS = {item.x, item.y};
|
||||
|
||||
if (!ItemTracker::get()->hasBeenPicked(room.name, ITEM_POS)) {
|
||||
// Crear una copia local de los datos del item
|
||||
Item::Data item_copy = item;
|
||||
item_copy.color1 = stringToColor(item_color1_);
|
||||
item_copy.color2 = stringToColor(item_color2_);
|
||||
|
||||
// Crear el objeto Item usando la copia modificada
|
||||
item_manager_->addItem(std::make_shared<Item>(item_copy));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Abre la jail para poder entrar
|
||||
void Room::openTheJail() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (data_->jail_is_open && number_ == Defaults::Game::Room::END_ROOM) {
|
||||
// Elimina el último enemigo (Bry debe ser el último enemigo definido en el fichero)
|
||||
if (!enemy_manager_->isEmpty()) {
|
||||
enemy_manager_->removeLastEnemy();
|
||||
}
|
||||
|
||||
// Abre las puertas
|
||||
constexpr int TILE_A = 16 + (13 * 32);
|
||||
constexpr int TILE_B = 16 + (14 * 32);
|
||||
if (TILE_A < tile_map_.size()) {
|
||||
tile_map_[TILE_A] = -1;
|
||||
}
|
||||
if (TILE_B < tile_map_.size()) {
|
||||
tile_map_[TILE_B] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el mapa en pantalla
|
||||
void Room::renderMap() {
|
||||
tilemap_renderer_->render();
|
||||
}
|
||||
|
||||
// Dibuja los enemigos en pantalla
|
||||
void Room::renderEnemies() {
|
||||
enemy_manager_->render();
|
||||
}
|
||||
|
||||
// Dibuja los objetos en pantalla
|
||||
void Room::renderItems() {
|
||||
item_manager_->render();
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Redibuja el mapa (para actualizar modo debug)
|
||||
void Room::redrawMap() {
|
||||
tilemap_renderer_->redrawMap(collision_map_.get());
|
||||
}
|
||||
|
||||
// Actualiza animaciones sin mover enemigos (para editor de mapas)
|
||||
void Room::updateEditorMode(float delta_time) {
|
||||
tilemap_renderer_->update(delta_time);
|
||||
enemy_manager_->updateAnimations(delta_time);
|
||||
item_manager_->update(delta_time);
|
||||
}
|
||||
|
||||
// Resetea enemigos a posiciones iniciales (para editor de mapas)
|
||||
void Room::resetEnemyPositions(const std::vector<Enemy::Data>& enemy_data) {
|
||||
enemy_manager_->resetPositions(enemy_data);
|
||||
}
|
||||
|
||||
// Cambia un tile y repinta la celda (para editor)
|
||||
void Room::setTile(int index, int tile_value) {
|
||||
if (index >= 0 && index < static_cast<int>(tile_map_.size())) {
|
||||
tile_map_[index] = tile_value;
|
||||
tilemap_renderer_->setTile(index, tile_value);
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia color de fondo y redibuja el mapa (para editor)
|
||||
void Room::setBgColor(const std::string& color) {
|
||||
bg_color_ = color;
|
||||
tilemap_renderer_->setBgColor(color);
|
||||
tilemap_renderer_->redrawMap(collision_map_.get());
|
||||
}
|
||||
|
||||
// Cambia colores de items en vivo (para editor)
|
||||
void Room::setItemColors(const std::string& color1, const std::string& color2) {
|
||||
item_color1_ = color1;
|
||||
item_color2_ = color2;
|
||||
Uint8 c1 = stringToColor(color1);
|
||||
Uint8 c2 = stringToColor(color2);
|
||||
auto* item_mgr = item_manager_.get();
|
||||
for (int i = 0; i < item_mgr->getCount(); ++i) {
|
||||
item_mgr->getItem(i)->setColors(c1, c2);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Actualiza las variables y objetos de la habitación
|
||||
void Room::update(float delta_time) { // NOLINT(readability-make-member-function-const)
|
||||
if (is_paused_) {
|
||||
// Si está en modo pausa no se actualiza nada
|
||||
return;
|
||||
}
|
||||
|
||||
// Actualiza los tiles animados usando el renderer
|
||||
tilemap_renderer_->update(delta_time);
|
||||
|
||||
// Actualiza los enemigos usando el manager
|
||||
enemy_manager_->update(delta_time);
|
||||
|
||||
// Actualiza los items usando el manager
|
||||
item_manager_->update(delta_time);
|
||||
}
|
||||
|
||||
// Pone el mapa en modo pausa
|
||||
void Room::setPaused(bool value) {
|
||||
is_paused_ = value;
|
||||
tilemap_renderer_->setPaused(value);
|
||||
item_manager_->setPaused(value);
|
||||
}
|
||||
|
||||
// Devuelve la cadena del fichero de la habitación contigua segun el borde
|
||||
auto Room::getRoom(Border border) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
||||
switch (border) {
|
||||
case Border::TOP:
|
||||
return upper_room_;
|
||||
case Border::BOTTOM:
|
||||
return lower_room_;
|
||||
case Border::RIGHT:
|
||||
return right_room_;
|
||||
case Border::LEFT:
|
||||
return left_room_;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve el tipo de tile que hay en ese pixel
|
||||
auto Room::getTile(SDL_FPoint point) -> Tile { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Delega a CollisionMap y convierte el resultado
|
||||
const auto COLLISION_TILE = collision_map_->getTile(point);
|
||||
return static_cast<Tile>(COLLISION_TILE);
|
||||
}
|
||||
|
||||
// Devuelve el tipo de tile en un índice del tilemap
|
||||
auto Room::getTile(int index) -> Tile { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Delega a CollisionMap y convierte el resultado
|
||||
const auto COLLISION_TILE = collision_map_->getTile(index);
|
||||
return static_cast<Tile>(COLLISION_TILE);
|
||||
}
|
||||
|
||||
// Indica si hay colision con un enemigo a partir de un rectangulo
|
||||
auto Room::enemyCollision(SDL_FRect& rect) -> bool {
|
||||
return enemy_manager_->checkCollision(rect);
|
||||
}
|
||||
|
||||
// Indica si hay colision con un objeto a partir de un rectangulo
|
||||
auto Room::itemCollision(SDL_FRect& rect) -> bool {
|
||||
return item_manager_->checkCollision(rect);
|
||||
}
|
||||
|
||||
// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
|
||||
auto Room::getSlopeHeight(SDL_FPoint p, Tile slope) -> int {
|
||||
// Delega a CollisionMap (método estático)
|
||||
const auto COLLISION_TILE = static_cast<CollisionMap::Tile>(slope);
|
||||
return CollisionMap::getSlopeHeight(p, COLLISION_TILE);
|
||||
}
|
||||
|
||||
// === Métodos de colisión (delegados a CollisionMap) ===
|
||||
|
||||
// Comprueba las colisiones con paredes derechas
|
||||
auto Room::checkRightSurfaces(const SDL_FRect& rect) -> int {
|
||||
return collision_map_->checkRightSurfaces(rect);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con paredes izquierdas
|
||||
auto Room::checkLeftSurfaces(const SDL_FRect& rect) -> int {
|
||||
return collision_map_->checkLeftSurfaces(rect);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con techos
|
||||
auto Room::checkTopSurfaces(const SDL_FRect& rect) -> int {
|
||||
return collision_map_->checkTopSurfaces(rect);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con techos
|
||||
auto Room::checkTopSurfaces(const SDL_FPoint& p) -> bool {
|
||||
return collision_map_->checkTopSurfaces(p);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con suelos
|
||||
auto Room::checkBottomSurfaces(const SDL_FRect& rect) -> int {
|
||||
return collision_map_->checkBottomSurfaces(rect);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con conveyor belts
|
||||
auto Room::checkAutoSurfaces(const SDL_FRect& rect) -> int {
|
||||
return collision_map_->checkAutoSurfaces(rect);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con conveyor belts
|
||||
auto Room::checkConveyorBelts(const SDL_FPoint& p) -> bool {
|
||||
return collision_map_->checkConveyorBelts(p);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones línea con rampas izquierdas
|
||||
auto Room::checkLeftSlopes(const LineVertical& line) -> int {
|
||||
return collision_map_->checkLeftSlopes(line);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con rampas izquierdas
|
||||
auto Room::checkLeftSlopes(const SDL_FPoint& p) -> bool {
|
||||
return collision_map_->checkLeftSlopes(p);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones línea con rampas derechas
|
||||
auto Room::checkRightSlopes(const LineVertical& line) -> int {
|
||||
return collision_map_->checkRightSlopes(line);
|
||||
}
|
||||
|
||||
// Comprueba las colisiones punto con rampas derechas
|
||||
auto Room::checkRightSlopes(const SDL_FPoint& p) -> bool {
|
||||
return collision_map_->checkRightSlopes(p);
|
||||
}
|
||||
|
||||
// Obtiene puntero a slope en un punto
|
||||
auto Room::getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal* {
|
||||
return collision_map_->getSlopeAtPoint(p);
|
||||
}
|
||||
|
||||
// Carga una habitación desde un archivo YAML (delegado a RoomLoader)
|
||||
auto Room::loadYAML(const std::string& file_path, bool verbose) -> Data { // NOLINT(readability-convert-member-functions-to-static)
|
||||
return RoomLoader::loadYAML(file_path, verbose);
|
||||
}
|
||||
143
source/game/gameplay/room.hpp
Normal file
143
source/game/gameplay/room.hpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "game/entities/enemy.hpp" // Para EnemyData
|
||||
#include "game/entities/item.hpp" // Para ItemData
|
||||
#include "game/gameplay/scoreboard.hpp" // Para Scoreboard::Data
|
||||
#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical
|
||||
class Sprite; // lines 12-12
|
||||
class Surface; // lines 13-13
|
||||
class EnemyManager;
|
||||
class ItemManager;
|
||||
class CollisionMap;
|
||||
class TilemapRenderer;
|
||||
|
||||
class Room {
|
||||
public:
|
||||
// -- Enumeraciones y estructuras ---
|
||||
enum class Border : int {
|
||||
TOP = 0,
|
||||
RIGHT = 1,
|
||||
BOTTOM = 2,
|
||||
LEFT = 3,
|
||||
NONE = 4
|
||||
};
|
||||
|
||||
enum class Tile {
|
||||
EMPTY,
|
||||
WALL,
|
||||
PASSABLE,
|
||||
SLOPE_L,
|
||||
SLOPE_R,
|
||||
KILL,
|
||||
ANIMATED
|
||||
};
|
||||
|
||||
struct Data {
|
||||
std::string number; // Numero de la habitación
|
||||
std::string name; // Nombre de la habitación
|
||||
std::string bg_color; // Color de fondo de la habitación
|
||||
std::string border_color; // Color del borde de la pantalla
|
||||
std::string item_color1; // Color 1 para los items de la habitación
|
||||
std::string item_color2; // Color 2 para los items de la habitación
|
||||
std::string upper_room; // Identificador de la habitación que se encuentra arriba
|
||||
std::string lower_room; // Identificador de la habitación que se encuentra abajo
|
||||
std::string left_room; // Identificador de la habitación que se encuentra a la izquierda
|
||||
std::string right_room; // Identificador de la habitación que se encuentra a la derecha
|
||||
std::string tile_set_file; // Imagen con los gráficos para la habitación
|
||||
int conveyor_belt_direction{0}; // Sentido en el que arrastran las superficies automáticas de la habitación
|
||||
std::vector<int> tile_map; // Índice de los tiles a dibujar en la habitación (embebido desde YAML)
|
||||
std::vector<Enemy::Data> enemies; // Listado con los enemigos de la habitación
|
||||
std::vector<Item::Data> items; // Listado con los items que hay en la habitación
|
||||
};
|
||||
|
||||
// Constructor y destructor
|
||||
Room(const std::string& room_path, std::shared_ptr<Scoreboard::Data> data);
|
||||
~Room(); // NOLINT(modernize-use-equals-default, performance-trivially-destructible) -- defined in .cpp for unique_ptr with forward declarations
|
||||
|
||||
// --- Funciones ---
|
||||
[[nodiscard]] auto getNumber() const -> const std::string& { return number_; } // Devuelve el numero de la habitación
|
||||
[[nodiscard]] auto getName() const -> const std::string& { return name_; } // Devuelve el nombre de la habitación
|
||||
[[nodiscard]] auto getBGColor() const -> Uint8 { return stringToColor(bg_color_); } // Devuelve el color de la habitación
|
||||
[[nodiscard]] auto getBorderColor() const -> Uint8 { return stringToColor(border_color_); } // Devuelve el color del borde
|
||||
void renderMap(); // Dibuja el mapa en pantalla
|
||||
void renderEnemies(); // Dibuja los enemigos en pantalla
|
||||
void renderItems(); // Dibuja los objetos en pantalla
|
||||
#ifdef _DEBUG
|
||||
void redrawMap(); // Redibuja el mapa (para actualizar modo debug)
|
||||
void updateEditorMode(float delta_time); // Actualiza animaciones sin mover enemigos (para editor)
|
||||
void resetEnemyPositions(const std::vector<Enemy::Data>& enemy_data); // Resetea enemigos a posiciones iniciales
|
||||
auto getEnemyManager() -> EnemyManager* { return enemy_manager_.get(); } // Acceso al gestor de enemigos (para editor)
|
||||
auto getItemManager() -> ItemManager* { return item_manager_.get(); } // Acceso al gestor de items (para editor)
|
||||
void setBgColor(const std::string& color); // Cambia color de fondo y redibuja (para editor)
|
||||
void setItemColors(const std::string& color1, const std::string& color2); // Cambia colores de items (para editor)
|
||||
void setTile(int index, int tile_value); // Cambia un tile y redibuja (para editor)
|
||||
[[nodiscard]] auto getTileSetFile() const -> const std::string& { return tile_set_file_; }
|
||||
[[nodiscard]] auto getTileSetWidth() const -> int { return tile_set_width_; }
|
||||
#endif
|
||||
void update(float delta_time); // Actualiza las variables y objetos de la habitación
|
||||
auto getRoom(Border border) -> std::string; // Devuelve la cadena del fichero de la habitación contigua segun el borde
|
||||
auto getTile(SDL_FPoint point) -> Tile; // Devuelve el tipo de tile que hay en ese pixel
|
||||
auto getTile(int index) -> Tile; // Devuelve el tipo de tile en un índice del tilemap
|
||||
auto enemyCollision(SDL_FRect& rect) -> bool; // Indica si hay colision con un enemigo a partir de un rectangulo
|
||||
auto itemCollision(SDL_FRect& rect) -> bool; // Indica si hay colision con un objeto a partir de un rectangulo
|
||||
static auto getTileSize() -> int { return TILE_SIZE; } // Obten el tamaño del tile
|
||||
static auto getSlopeHeight(SDL_FPoint p, Tile slope) -> int; // Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
|
||||
auto checkRightSurfaces(const SDL_FRect& rect) -> int; // Comprueba las colisiones
|
||||
auto checkLeftSurfaces(const SDL_FRect& rect) -> int; // Comprueba las colisiones
|
||||
auto checkTopSurfaces(const SDL_FRect& rect) -> int; // Comprueba las colisiones
|
||||
auto checkBottomSurfaces(const SDL_FRect& rect) -> int; // Comprueba las colisiones
|
||||
auto checkAutoSurfaces(const SDL_FRect& rect) -> int; // Comprueba las colisiones
|
||||
auto checkTopSurfaces(const SDL_FPoint& p) -> bool; // Comprueba las colisiones
|
||||
auto checkConveyorBelts(const SDL_FPoint& p) -> bool; // Comprueba las colisiones
|
||||
auto checkLeftSlopes(const LineVertical& line) -> int; // Comprueba las colisiones
|
||||
auto checkLeftSlopes(const SDL_FPoint& p) -> bool; // Comprueba las colisiones
|
||||
auto checkRightSlopes(const LineVertical& line) -> int; // Comprueba las colisiones
|
||||
auto checkRightSlopes(const SDL_FPoint& p) -> bool; // Comprueba las colisiones
|
||||
[[nodiscard]] auto getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal*; // Obtiene puntero a slope en un punto
|
||||
void setPaused(bool value); // Pone el mapa en modo pausa
|
||||
[[nodiscard]] auto getConveyorBeltDirection() const -> int { return conveyor_belt_direction_; } // Obten la direccion de las superficies automaticas
|
||||
|
||||
// Método de carga de archivos YAML (delegado a RoomLoader)
|
||||
static auto loadYAML(const std::string& file_path, bool verbose = false) -> Data; // Carga habitación desde archivo YAML unificado
|
||||
|
||||
private:
|
||||
// Constantes
|
||||
static constexpr int TILE_SIZE = 8; // Ancho del tile en pixels
|
||||
static constexpr int MAP_WIDTH = 32; // Ancho del mapa en tiles
|
||||
static constexpr int MAP_HEIGHT = 16; // Alto del mapa en tiles
|
||||
|
||||
// Objetos y punteros
|
||||
std::unique_ptr<EnemyManager> enemy_manager_; // Gestor de enemigos de la habitación
|
||||
std::unique_ptr<ItemManager> item_manager_; // Gestor de items de la habitación
|
||||
std::unique_ptr<CollisionMap> collision_map_; // Mapa de colisiones de la habitación
|
||||
std::unique_ptr<TilemapRenderer> tilemap_renderer_; // Renderizador del mapa de tiles
|
||||
std::shared_ptr<Surface> surface_; // Textura con los graficos de la habitación
|
||||
std::shared_ptr<Scoreboard::Data> data_; // Puntero a los datos del marcador
|
||||
|
||||
// --- Variables ---
|
||||
std::string number_; // Numero de la habitación
|
||||
std::string name_; // Nombre de la habitación
|
||||
std::string bg_color_; // Color de fondo de la habitación
|
||||
std::string border_color_; // Color del borde de la pantalla
|
||||
std::string item_color1_; // Color 1 para los items de la habitación
|
||||
std::string item_color2_; // Color 2 para los items de la habitación
|
||||
std::string upper_room_; // Identificador de la habitación que se encuentra arriba
|
||||
std::string lower_room_; // Identificador de la habitación que se encuentra abajp
|
||||
std::string left_room_; // Identificador de la habitación que se encuentra a la izquierda
|
||||
std::string right_room_; // Identificador de la habitación que se encuentra a la derecha
|
||||
std::string tile_set_file_; // Imagen con los graficos para la habitación
|
||||
std::vector<int> tile_map_; // Indice de los tiles a dibujar en la habitación (embebido desde YAML)
|
||||
int conveyor_belt_direction_{0}; // Sentido en el que arrastran las superficies automáticas de la habitación
|
||||
bool is_paused_{false}; // Indica si el mapa esta en modo pausa
|
||||
int tile_set_width_{0}; // Ancho del tileset en tiles
|
||||
|
||||
// --- Funciones ---
|
||||
void initializeRoom(const Data& room); // Inicializa los valores
|
||||
void openTheJail(); // Abre la jail para poder entrar
|
||||
};
|
||||
375
source/game/gameplay/room_loader.cpp
Normal file
375
source/game/gameplay/room_loader.cpp
Normal file
@@ -0,0 +1,375 @@
|
||||
#include "room_loader.hpp"
|
||||
|
||||
#include <exception> // Para exception
|
||||
#include <iostream> // Para cout, cerr
|
||||
|
||||
#include "core/resources/resource_helper.hpp" // Para Resource::Helper
|
||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||
#include "game/options.hpp" // Para Options::language
|
||||
#include "utils/defines.hpp" // Para Tile::SIZE
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
// Convierte room connection de YAML a formato interno
|
||||
auto RoomLoader::convertRoomConnection(const std::string& value) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (value == "null" || value.empty()) {
|
||||
return "0";
|
||||
}
|
||||
// Si ya tiene .yaml, devolverlo tal cual; si no, añadirlo
|
||||
if (value.size() > 5 && value.substr(value.size() - 5) == ".yaml") {
|
||||
return value;
|
||||
}
|
||||
return value + ".yaml";
|
||||
}
|
||||
|
||||
// Convierte string de autoSurface a int
|
||||
auto RoomLoader::convertAutoSurface(const fkyaml::node& node) -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (node.is_integer()) {
|
||||
return node.get_value<int>();
|
||||
}
|
||||
if (node.is_string()) {
|
||||
const auto VALUE = node.get_value<std::string>();
|
||||
if (VALUE == "left") {
|
||||
return -1;
|
||||
}
|
||||
if (VALUE == "right") {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0; // "none" o default
|
||||
}
|
||||
|
||||
// Convierte un tilemap 2D a vector 1D flat
|
||||
auto RoomLoader::flattenTilemap(const std::vector<std::vector<int>>& tilemap_2d) -> std::vector<int> { // NOLINT(readability-convert-member-functions-to-static, readability-named-parameter)
|
||||
std::vector<int> tilemap_flat;
|
||||
tilemap_flat.reserve(512); // 16 rows × 32 cols
|
||||
|
||||
for (const auto& row : tilemap_2d) {
|
||||
for (int tile : row) {
|
||||
tilemap_flat.push_back(tile);
|
||||
}
|
||||
}
|
||||
|
||||
return tilemap_flat;
|
||||
}
|
||||
|
||||
// Parsea la configuración general de la habitación
|
||||
void RoomLoader::parseRoomConfig(const fkyaml::node& yaml, Room::Data& room, const std::string& file_name) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (!yaml.contains("room")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& room_node = yaml["room"];
|
||||
|
||||
// Extract room number from filename (e.g., "01.yaml" → "01")
|
||||
room.number = file_name.substr(0, file_name.find_last_of('.'));
|
||||
|
||||
// Basic properties
|
||||
const std::string LANG_KEY = "name_" + Options::language;
|
||||
if (room_node.contains(LANG_KEY)) {
|
||||
room.name = room_node[LANG_KEY].get_value<std::string>();
|
||||
} else if (room_node.contains("name")) {
|
||||
room.name = room_node["name"].get_value<std::string>();
|
||||
}
|
||||
if (room_node.contains("bgColor")) {
|
||||
room.bg_color = room_node["bgColor"].get_value<std::string>();
|
||||
}
|
||||
if (room_node.contains("border")) {
|
||||
room.border_color = room_node["border"].get_value<std::string>();
|
||||
}
|
||||
if (room_node.contains("tileSetFile")) {
|
||||
room.tile_set_file = room_node["tileSetFile"].get_value<std::string>();
|
||||
}
|
||||
|
||||
// Room connections
|
||||
if (room_node.contains("connections")) {
|
||||
parseRoomConnections(room_node["connections"], room);
|
||||
}
|
||||
|
||||
// Item colors
|
||||
room.item_color1 = room_node.contains("itemColor1")
|
||||
? room_node["itemColor1"].get_value_or<std::string>("yellow")
|
||||
: "yellow";
|
||||
|
||||
room.item_color2 = room_node.contains("itemColor2")
|
||||
? room_node["itemColor2"].get_value_or<std::string>("magenta")
|
||||
: "magenta";
|
||||
|
||||
// Dirección de la cinta transportadora (left/none/right)
|
||||
room.conveyor_belt_direction = room_node.contains("conveyorBelt")
|
||||
? convertAutoSurface(room_node["conveyorBelt"])
|
||||
: 0;
|
||||
}
|
||||
|
||||
// Parsea las conexiones de la habitación (arriba/abajo/izq/der)
|
||||
void RoomLoader::parseRoomConnections(const fkyaml::node& conn_node, Room::Data& room) {
|
||||
room.upper_room = conn_node.contains("up")
|
||||
? convertRoomConnection(conn_node["up"].get_value_or<std::string>("null"))
|
||||
: "0";
|
||||
|
||||
room.lower_room = conn_node.contains("down")
|
||||
? convertRoomConnection(conn_node["down"].get_value_or<std::string>("null"))
|
||||
: "0";
|
||||
|
||||
room.left_room = conn_node.contains("left")
|
||||
? convertRoomConnection(conn_node["left"].get_value_or<std::string>("null"))
|
||||
: "0";
|
||||
|
||||
room.right_room = conn_node.contains("right")
|
||||
? convertRoomConnection(conn_node["right"].get_value_or<std::string>("null"))
|
||||
: "0";
|
||||
}
|
||||
|
||||
// Parsea el tilemap de la habitación
|
||||
void RoomLoader::parseTilemap(const fkyaml::node& yaml, Room::Data& room, const std::string& file_name, bool verbose) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (!yaml.contains("tilemap")) {
|
||||
std::cerr << "Warning: No tilemap found in " << file_name << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& tilemap_node = yaml["tilemap"];
|
||||
|
||||
// Read 2D array
|
||||
std::vector<std::vector<int>> tilemap_2d;
|
||||
tilemap_2d.reserve(16);
|
||||
|
||||
for (const auto& row_node : tilemap_node) {
|
||||
std::vector<int> row;
|
||||
row.reserve(32);
|
||||
|
||||
for (const auto& tile_node : row_node) {
|
||||
row.push_back(tile_node.get_value<int>());
|
||||
}
|
||||
|
||||
tilemap_2d.push_back(row);
|
||||
}
|
||||
|
||||
// Convert to 1D flat array
|
||||
room.tile_map = flattenTilemap(tilemap_2d);
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Loaded tilemap: " << room.tile_map.size() << " tiles\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Parsea los límites de movimiento de un enemigo
|
||||
void RoomLoader::parseEnemyBoundaries(const fkyaml::node& bounds_node, Enemy::Data& enemy) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Nuevo formato: position1 y position2
|
||||
if (bounds_node.contains("position1")) {
|
||||
const auto& pos1 = bounds_node["position1"];
|
||||
if (pos1.contains("x")) {
|
||||
enemy.x1 = pos1["x"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
if (pos1.contains("y")) {
|
||||
enemy.y1 = pos1["y"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
if (bounds_node.contains("position2")) {
|
||||
const auto& pos2 = bounds_node["position2"];
|
||||
if (pos2.contains("x")) {
|
||||
enemy.x2 = pos2["x"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
if (pos2.contains("y")) {
|
||||
enemy.y2 = pos2["y"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
// Formato antiguo: x1/y1/x2/y2 (compatibilidad)
|
||||
if (bounds_node.contains("x1")) {
|
||||
enemy.x1 = bounds_node["x1"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
if (bounds_node.contains("y1")) {
|
||||
enemy.y1 = bounds_node["y1"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
if (bounds_node.contains("x2")) {
|
||||
enemy.x2 = bounds_node["x2"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
if (bounds_node.contains("y2")) {
|
||||
enemy.y2 = bounds_node["y2"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
// Parsea los datos de un enemigo individual
|
||||
auto RoomLoader::parseEnemyData(const fkyaml::node& enemy_node) -> Enemy::Data { // NOLINT(readability-convert-member-functions-to-static)
|
||||
Enemy::Data enemy;
|
||||
|
||||
// Animation path
|
||||
if (enemy_node.contains("animation")) {
|
||||
enemy.animation_path = enemy_node["animation"].get_value<std::string>();
|
||||
}
|
||||
|
||||
// Position (in tiles, convert to pixels)
|
||||
if (enemy_node.contains("position")) {
|
||||
const auto& pos = enemy_node["position"];
|
||||
if (pos.contains("x")) {
|
||||
enemy.x = pos["x"].get_value<float>() * Tile::SIZE;
|
||||
}
|
||||
if (pos.contains("y")) {
|
||||
enemy.y = pos["y"].get_value<float>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
// Velocity (already in pixels/second)
|
||||
if (enemy_node.contains("velocity")) {
|
||||
const auto& vel = enemy_node["velocity"];
|
||||
if (vel.contains("x")) {
|
||||
enemy.vx = vel["x"].get_value<float>();
|
||||
}
|
||||
if (vel.contains("y")) {
|
||||
enemy.vy = vel["y"].get_value<float>();
|
||||
}
|
||||
}
|
||||
|
||||
// Boundaries (in tiles, convert to pixels)
|
||||
if (enemy_node.contains("boundaries")) {
|
||||
parseEnemyBoundaries(enemy_node["boundaries"], enemy);
|
||||
}
|
||||
|
||||
// Color
|
||||
enemy.color = enemy_node.contains("color")
|
||||
? enemy_node["color"].get_value_or<std::string>("white")
|
||||
: "white";
|
||||
|
||||
// Optional fields
|
||||
enemy.flip = enemy_node.contains("flip")
|
||||
? enemy_node["flip"].get_value_or<bool>(false)
|
||||
: false;
|
||||
|
||||
enemy.mirror = enemy_node.contains("mirror")
|
||||
? enemy_node["mirror"].get_value_or<bool>(false)
|
||||
: false;
|
||||
|
||||
enemy.frame = enemy_node.contains("frame")
|
||||
? enemy_node["frame"].get_value_or<int>(-1)
|
||||
: -1;
|
||||
|
||||
return enemy;
|
||||
}
|
||||
|
||||
// Parsea la lista de enemigos de la habitación
|
||||
void RoomLoader::parseEnemies(const fkyaml::node& yaml, Room::Data& room, bool verbose) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (!yaml.contains("enemies") || yaml["enemies"].is_null()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& enemies_node = yaml["enemies"];
|
||||
|
||||
for (const auto& enemy_node : enemies_node) {
|
||||
room.enemies.push_back(parseEnemyData(enemy_node));
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Loaded " << room.enemies.size() << " enemies\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Parsea los datos de un item individual
|
||||
auto RoomLoader::parseItemData(const fkyaml::node& item_node, const Room::Data& room) -> Item::Data { // NOLINT(readability-convert-member-functions-to-static)
|
||||
Item::Data item;
|
||||
|
||||
// Tileset file
|
||||
if (item_node.contains("tileSetFile")) {
|
||||
item.tile_set_file = item_node["tileSetFile"].get_value<std::string>();
|
||||
}
|
||||
|
||||
// Tile index
|
||||
if (item_node.contains("tile")) {
|
||||
item.tile = item_node["tile"].get_value<int>();
|
||||
}
|
||||
|
||||
// Position (in tiles, convert to pixels)
|
||||
if (item_node.contains("position")) {
|
||||
const auto& pos = item_node["position"];
|
||||
if (pos.contains("x")) {
|
||||
item.x = pos["x"].get_value<float>() * Tile::SIZE;
|
||||
}
|
||||
if (pos.contains("y")) {
|
||||
item.y = pos["y"].get_value<float>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
// Counter
|
||||
item.counter = item_node.contains("counter")
|
||||
? item_node["counter"].get_value_or<int>(0)
|
||||
: 0;
|
||||
|
||||
// Colors (assigned from room defaults)
|
||||
item.color1 = stringToColor(room.item_color1);
|
||||
item.color2 = stringToColor(room.item_color2);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
// Parsea la lista de items de la habitación
|
||||
void RoomLoader::parseItems(const fkyaml::node& yaml, Room::Data& room, bool verbose) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
if (!yaml.contains("items") || yaml["items"].is_null()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& items_node = yaml["items"];
|
||||
|
||||
for (const auto& item_node : items_node) {
|
||||
room.items.push_back(parseItemData(item_node, room));
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Loaded " << room.items.size() << " items\n";
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Carga una habitación desde un string YAML (para el editor de mapas, evita el resource pack)
|
||||
auto RoomLoader::loadFromString(const std::string& yaml_content, const std::string& file_name) -> Room::Data {
|
||||
Room::Data room;
|
||||
try {
|
||||
auto yaml = fkyaml::node::deserialize(yaml_content);
|
||||
parseRoomConfig(yaml, room, file_name);
|
||||
parseTilemap(yaml, room, file_name, false);
|
||||
parseEnemies(yaml, room, false);
|
||||
parseItems(yaml, room, false);
|
||||
} catch (const fkyaml::exception& e) {
|
||||
std::cerr << "YAML parsing error in " << file_name << ": " << e.what() << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error loading room " << file_name << ": " << e.what() << '\n';
|
||||
}
|
||||
return room;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Carga un archivo de room en formato YAML
|
||||
auto RoomLoader::loadYAML(const std::string& file_path, bool verbose) -> Room::Data { // NOLINT(readability-convert-member-functions-to-static)
|
||||
Room::Data room;
|
||||
|
||||
// Extract filename for logging
|
||||
const std::string FILE_NAME = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
|
||||
try {
|
||||
// Load YAML file using ResourceHelper (supports both filesystem and pack)
|
||||
auto file_data = Resource::Helper::loadFile(file_path);
|
||||
|
||||
if (file_data.empty()) {
|
||||
std::cerr << "Error: Unable to load file " << FILE_NAME << '\n';
|
||||
return room;
|
||||
}
|
||||
|
||||
// Parse YAML from string
|
||||
std::string yaml_content(file_data.begin(), file_data.end());
|
||||
auto yaml = fkyaml::node::deserialize(yaml_content);
|
||||
|
||||
// Delegación a funciones especializadas
|
||||
parseRoomConfig(yaml, room, FILE_NAME);
|
||||
parseTilemap(yaml, room, FILE_NAME, verbose);
|
||||
parseEnemies(yaml, room, verbose);
|
||||
parseItems(yaml, room, verbose);
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Room loaded successfully: " << FILE_NAME << '\n';
|
||||
}
|
||||
|
||||
} catch (const fkyaml::exception& e) {
|
||||
std::cerr << "YAML parsing error in " << FILE_NAME << ": " << e.what() << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error loading room " << FILE_NAME << ": " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return room;
|
||||
}
|
||||
136
source/game/gameplay/room_loader.hpp
Normal file
136
source/game/gameplay/room_loader.hpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#pragma once
|
||||
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||
#include "game/entities/enemy.hpp" // Para Enemy::Data
|
||||
#include "game/entities/item.hpp" // Para Item::Data
|
||||
#include "game/gameplay/room.hpp" // Para Room::Data
|
||||
|
||||
/**
|
||||
* @brief Cargador de archivos de habitaciones en formato YAML
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - Cargar archivos de room en formato YAML unificado (.yaml)
|
||||
* - Parsear datos de room, tilemap, enemies e items
|
||||
* - Convertir tipos de datos (tiles, posiciones, colores)
|
||||
* - Validar y propagar errores de carga
|
||||
*
|
||||
* Esta clase contiene solo métodos estáticos y no debe instanciarse.
|
||||
*/
|
||||
class RoomLoader {
|
||||
public:
|
||||
// Constructor eliminado para prevenir instanciación
|
||||
RoomLoader() = delete;
|
||||
~RoomLoader() = delete;
|
||||
RoomLoader(const RoomLoader&) = delete;
|
||||
auto operator=(const RoomLoader&) -> RoomLoader& = delete;
|
||||
RoomLoader(RoomLoader&&) = delete;
|
||||
auto operator=(RoomLoader&&) -> RoomLoader& = delete;
|
||||
|
||||
/**
|
||||
* @brief Carga un archivo de room en formato YAML
|
||||
* @param file_path Ruta al archivo YAML de habitación
|
||||
* @param verbose Si true, muestra información de debug
|
||||
* @return Room::Data con todos los datos de la habitación incluyendo:
|
||||
* - Configuración de la habitación (nombre, colores, etc.)
|
||||
* - Tilemap completo (convertido de 2D a 1D)
|
||||
* - Lista de enemigos con todas sus propiedades
|
||||
* - Lista de items con todas sus propiedades
|
||||
*
|
||||
* El formato YAML esperado incluye:
|
||||
* - room: configuración general
|
||||
* - tilemap: array 2D de 16x32 tiles (convertido a vector 1D de 512 elementos)
|
||||
* - enemies: lista de enemigos (opcional)
|
||||
* - items: lista de items (opcional)
|
||||
*/
|
||||
static auto loadYAML(const std::string& file_path, bool verbose = false) -> Room::Data;
|
||||
#ifdef _DEBUG
|
||||
static auto loadFromString(const std::string& yaml_content, const std::string& file_name) -> Room::Data;
|
||||
#endif
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Convierte room connection de YAML a formato interno
|
||||
* @param value Valor del YAML (vacío, "02" o "02.yaml")
|
||||
* @return "0" para sin conexión, o nombre del archivo con extensión
|
||||
*/
|
||||
static auto convertRoomConnection(const std::string& value) -> std::string;
|
||||
|
||||
/**
|
||||
* @brief Convierte autoSurface de YAML a int
|
||||
* @param node Nodo YAML (puede ser int o string "left"/"none"/"right")
|
||||
* @return -1 para left, 0 para none, 1 para right
|
||||
*/
|
||||
static auto convertAutoSurface(const fkyaml::node& node) -> int;
|
||||
|
||||
/**
|
||||
* @brief Convierte un tilemap 2D a vector 1D flat
|
||||
* @param tilemap_2d Array 2D de tiles (16 rows × 32 cols)
|
||||
* @return Vector 1D flat con 512 elementos
|
||||
*/
|
||||
static auto flattenTilemap(const std::vector<std::vector<int>>& tilemap_2d) -> std::vector<int>; // NOLINT(readability-avoid-const-params-in-decls)
|
||||
|
||||
/**
|
||||
* @brief Parsea la configuración general de la habitación
|
||||
* @param yaml Nodo raíz del YAML
|
||||
* @param room Estructura de datos de la habitación a rellenar
|
||||
* @param file_name Nombre del archivo para logging
|
||||
*/
|
||||
static void parseRoomConfig(const fkyaml::node& yaml, Room::Data& room, const std::string& file_name);
|
||||
|
||||
/**
|
||||
* @brief Parsea las conexiones de la habitación (arriba/abajo/izq/der)
|
||||
* @param conn_node Nodo YAML con las conexiones
|
||||
* @param room Estructura de datos de la habitación a rellenar
|
||||
*/
|
||||
static void parseRoomConnections(const fkyaml::node& conn_node, Room::Data& room);
|
||||
|
||||
/**
|
||||
* @brief Parsea el tilemap de la habitación
|
||||
* @param yaml Nodo raíz del YAML
|
||||
* @param room Estructura de datos de la habitación a rellenar
|
||||
* @param file_name Nombre del archivo para logging
|
||||
* @param verbose Si true, muestra información de debug
|
||||
*/
|
||||
static void parseTilemap(const fkyaml::node& yaml, Room::Data& room, const std::string& file_name, bool verbose);
|
||||
|
||||
/**
|
||||
* @brief Parsea la lista de enemigos de la habitación
|
||||
* @param yaml Nodo raíz del YAML
|
||||
* @param room Estructura de datos de la habitación a rellenar
|
||||
* @param verbose Si true, muestra información de debug
|
||||
*/
|
||||
static void parseEnemies(const fkyaml::node& yaml, Room::Data& room, bool verbose);
|
||||
|
||||
/**
|
||||
* @brief Parsea los datos de un enemigo individual
|
||||
* @param enemy_node Nodo YAML del enemigo
|
||||
* @return Estructura Enemy::Data con los datos parseados
|
||||
*/
|
||||
static auto parseEnemyData(const fkyaml::node& enemy_node) -> Enemy::Data;
|
||||
|
||||
/**
|
||||
* @brief Parsea los límites de movimiento de un enemigo
|
||||
* @param bounds_node Nodo YAML con los límites
|
||||
* @param enemy Estructura del enemigo a rellenar
|
||||
*/
|
||||
static void parseEnemyBoundaries(const fkyaml::node& bounds_node, Enemy::Data& enemy);
|
||||
|
||||
/**
|
||||
* @brief Parsea la lista de items de la habitación
|
||||
* @param yaml Nodo raíz del YAML
|
||||
* @param room Estructura de datos de la habitación a rellenar
|
||||
* @param verbose Si true, muestra información de debug
|
||||
*/
|
||||
static void parseItems(const fkyaml::node& yaml, Room::Data& room, bool verbose);
|
||||
|
||||
/**
|
||||
* @brief Parsea los datos de un item individual
|
||||
* @param item_node Nodo YAML del item
|
||||
* @param room Datos de la habitación (para colores por defecto)
|
||||
* @return Estructura Item::Data con los datos parseados
|
||||
*/
|
||||
static auto parseItemData(const fkyaml::node& item_node, const Room::Data& room) -> Item::Data;
|
||||
};
|
||||
20
source/game/gameplay/room_tracker.cpp
Normal file
20
source/game/gameplay/room_tracker.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "game/gameplay/room_tracker.hpp"
|
||||
|
||||
#include <algorithm> // Para std::ranges::any_of
|
||||
|
||||
// Comprueba si la habitación ya ha sido visitada
|
||||
auto RoomTracker::hasBeenVisited(const std::string& name) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
||||
return std::ranges::any_of(rooms_, [&name](const auto& l) -> bool { return l == name; });
|
||||
}
|
||||
|
||||
// Añade la habitación a la lista
|
||||
auto RoomTracker::addRoom(const std::string& name) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Comprueba si la habitación ya ha sido visitada
|
||||
if (!hasBeenVisited(name)) {
|
||||
// En caso contrario añádela a la lista
|
||||
rooms_.push_back(name);
|
||||
return true; // NOLINT(readability-simplify-boolean-expr)
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
17
source/game/gameplay/room_tracker.hpp
Normal file
17
source/game/gameplay/room_tracker.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
class RoomTracker {
|
||||
public:
|
||||
RoomTracker() = default; // Constructor
|
||||
~RoomTracker() = default; // Destructor
|
||||
|
||||
auto addRoom(const std::string& name) -> bool; // Añade la habitación a la lista
|
||||
|
||||
private:
|
||||
auto hasBeenVisited(const std::string& name) -> bool; // Comprueba si ya ha sido visitada
|
||||
|
||||
std::vector<std::string> rooms_; // Lista con habitaciones visitadas
|
||||
};
|
||||
183
source/game/gameplay/scoreboard.cpp
Normal file
183
source/game/gameplay/scoreboard.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "game/gameplay/scoreboard.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "core/locale/locale.hpp" // Para Locale
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "game/entities/player.hpp" // Para Player::skinToAnimationPath
|
||||
#include "game/options.hpp" // Para Options, options, Cheat, OptionsGame
|
||||
#include "utils/defines.hpp" // Para BLOCK
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
// Constructor
|
||||
Scoreboard::Scoreboard(std::shared_ptr<Data> data)
|
||||
: item_surface_(Resource::Cache::get()->getSurface("items.gif")),
|
||||
data_(std::move(std::move(data))) {
|
||||
const float SURFACE_WIDTH = Options::game.width;
|
||||
constexpr float SURFACE_HEIGHT = 6.0F * Tile::SIZE;
|
||||
|
||||
// Reserva memoria para los objetos
|
||||
const std::string PLAYER_ANIM_PATH = Player::skinToAnimationPath(Options::game.player_skin);
|
||||
const auto& player_animation_data = Resource::Cache::get()->getAnimationData(PLAYER_ANIM_PATH);
|
||||
player_sprite_ = std::make_shared<AnimatedSprite>(player_animation_data);
|
||||
player_sprite_->setCurrentAnimation("default");
|
||||
|
||||
surface_ = std::make_shared<Surface>(SURFACE_WIDTH, SURFACE_HEIGHT);
|
||||
surface_dest_ = {.x = 0, .y = Options::game.height - SURFACE_HEIGHT, .w = SURFACE_WIDTH, .h = SURFACE_HEIGHT};
|
||||
|
||||
// Inicializa el color de items
|
||||
items_color_ = stringToColor("white");
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> COLORS = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"};
|
||||
for (const auto& color : COLORS) {
|
||||
color_.push_back(stringToColor(color));
|
||||
}
|
||||
}
|
||||
|
||||
// Pinta el objeto en pantalla
|
||||
void Scoreboard::render() {
|
||||
surface_->render(nullptr, &surface_dest_);
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Scoreboard::update(float delta_time) {
|
||||
// Acumular tiempo para animaciones
|
||||
time_accumulator_ += delta_time;
|
||||
|
||||
// Actualiza el color de la cantidad de items recogidos
|
||||
updateItemsColor(delta_time);
|
||||
|
||||
// Dibuja la textura
|
||||
fillTexture();
|
||||
|
||||
if (!is_paused_) {
|
||||
// Si está en pausa no se actualiza el reloj
|
||||
clock_ = getTime();
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el tiempo transcurrido de partida
|
||||
auto Scoreboard::getTime() -> Scoreboard::ClockData { // NOLINT(readability-convert-member-functions-to-static)
|
||||
const Uint32 TIME_ELAPSED = SDL_GetTicks() - data_->ini_clock - paused_time_elapsed_;
|
||||
|
||||
ClockData time;
|
||||
time.hours = TIME_ELAPSED / 3600000;
|
||||
time.minutes = TIME_ELAPSED / 60000;
|
||||
time.seconds = TIME_ELAPSED / 1000;
|
||||
time.separator = (TIME_ELAPSED % 1000 <= 500) ? ":" : " ";
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
// Actualiza el sprite del jugador con la skin actual
|
||||
void Scoreboard::refreshPlayerSkin() {
|
||||
const std::string PLAYER_ANIM_PATH = Player::skinToAnimationPath(Options::game.player_skin);
|
||||
const auto& player_animation_data = Resource::Cache::get()->getAnimationData(PLAYER_ANIM_PATH);
|
||||
player_sprite_ = std::make_shared<AnimatedSprite>(player_animation_data);
|
||||
player_sprite_->setCurrentAnimation("default");
|
||||
}
|
||||
|
||||
// Pone el marcador en modo pausa
|
||||
void Scoreboard::setPaused(bool value) {
|
||||
if (is_paused_ == value) {
|
||||
// Evita ejecutar lógica si el estado no cambia
|
||||
return;
|
||||
}
|
||||
|
||||
is_paused_ = value;
|
||||
|
||||
if (is_paused_) {
|
||||
// Guarda el tiempo actual al pausar
|
||||
paused_time_ = SDL_GetTicks();
|
||||
} else {
|
||||
// Calcula el tiempo pausado acumulado al reanudar
|
||||
paused_time_elapsed_ += SDL_GetTicks() - paused_time_;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el color de la cantidad de items recogidos
|
||||
void Scoreboard::updateItemsColor(float delta_time) {
|
||||
if (!data_->jail_is_open) {
|
||||
return;
|
||||
}
|
||||
|
||||
items_color_timer_ += delta_time;
|
||||
|
||||
// Resetear timer cada 2 ciclos (0.666s total)
|
||||
if (items_color_timer_ >= ITEMS_COLOR_BLINK_DURATION * 2.0F) {
|
||||
items_color_timer_ = 0.0F;
|
||||
}
|
||||
|
||||
// Alternar color cada ITEMS_COLOR_BLINK_DURATION
|
||||
if (items_color_timer_ < ITEMS_COLOR_BLINK_DURATION) {
|
||||
items_color_ = stringToColor("white");
|
||||
} else {
|
||||
items_color_ = stringToColor("magenta");
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve la cantidad de minutos de juego transcurridos
|
||||
auto Scoreboard::getMinutes() -> int {
|
||||
return getTime().minutes;
|
||||
}
|
||||
|
||||
// Dibuja los elementos del marcador en la textura
|
||||
void Scoreboard::fillTexture() {
|
||||
// Empieza a dibujar en la textura
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface_);
|
||||
|
||||
// Limpia la textura
|
||||
surface_->clear(stringToColor("black"));
|
||||
|
||||
// Dibuja las vidas
|
||||
const int WALK_FRAMES = player_sprite_->getCurrentAnimationSize();
|
||||
const int DESP = static_cast<int>(time_accumulator_ / SPRITE_WALK_CYCLE_DURATION) % (WALK_FRAMES * 2);
|
||||
const int FRAME = DESP % WALK_FRAMES;
|
||||
player_sprite_->setCurrentAnimationFrame(FRAME);
|
||||
player_sprite_->setPosY(LINE2_Y);
|
||||
for (int i = 0; i < data_->lives; ++i) {
|
||||
player_sprite_->setPosX(LIVES_START_X + (LIVES_SPACING * i) + DESP);
|
||||
const int INDEX = i % color_.size();
|
||||
player_sprite_->render(1, color_.at(INDEX));
|
||||
}
|
||||
|
||||
// Muestra si suena la música
|
||||
if (data_->music) {
|
||||
const Uint8 C = data_->color;
|
||||
SDL_FRect clip = {.x = 0, .y = 8, .w = 8, .h = 8};
|
||||
item_surface_->renderWithColorReplace(MUSIC_ICON_X, LINE2_Y, 1, C, &clip);
|
||||
}
|
||||
|
||||
// Escribe los textos
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
const std::string TIME_TEXT = std::to_string((clock_.minutes % 100) / 10) + std::to_string(clock_.minutes % 10) + clock_.separator + std::to_string((clock_.seconds % 60) / 10) + std::to_string(clock_.seconds % 10);
|
||||
const std::string ITEMS_TEXT = std::to_string(data_->items / 100) + std::to_string((data_->items % 100) / 10) + std::to_string(data_->items % 10);
|
||||
text->writeColored(ITEMS_LABEL_X, LINE1_Y, Locale::get()->get("scoreboard.items"), data_->color); // NOLINT(readability-static-accessed-through-instance)
|
||||
text->writeColored(ITEMS_VALUE_X, LINE1_Y, ITEMS_TEXT, items_color_);
|
||||
text->writeColored(TIME_LABEL_X, LINE1_Y, Locale::get()->get("scoreboard.time"), data_->color); // NOLINT(readability-static-accessed-through-instance)
|
||||
text->writeColored(TIME_VALUE_X, LINE1_Y, TIME_TEXT, stringToColor("white"));
|
||||
|
||||
const std::string ROOMS_TEXT = std::to_string(data_->rooms / 100) + std::to_string((data_->rooms % 100) / 10) + std::to_string(data_->rooms % 10);
|
||||
text->writeColored(ROOMS_LABEL_X, LINE2_Y, Locale::get()->get("scoreboard.rooms"), stringToColor("white")); // NOLINT(readability-static-accessed-through-instance)
|
||||
text->writeColored(ROOMS_VALUE_X, LINE2_Y, ROOMS_TEXT, stringToColor("white"));
|
||||
|
||||
// Indicadores de trucos activos (fuente 8bithud)
|
||||
auto cheat_text = Resource::Cache::get()->getText("8bithud");
|
||||
if (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED) {
|
||||
cheat_text->writeColored(CHEAT_INF_LIVES_X, CHEAT_INF_LIVES_Y, Locale::get()->get("scoreboard.cheat_infinite_lives"), data_->color); // NOLINT(readability-static-accessed-through-instance)
|
||||
}
|
||||
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
|
||||
cheat_text->writeColored(CHEAT_INVINCIBLE_X, CHEAT_INVINCIBLE_Y, Locale::get()->get("scoreboard.cheat_invincibility"), data_->color); // NOLINT(readability-static-accessed-through-instance)
|
||||
}
|
||||
|
||||
// Deja el renderizador como estaba
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
85
source/game/gameplay/scoreboard.hpp
Normal file
85
source/game/gameplay/scoreboard.hpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string, basic_string
|
||||
#include <utility>
|
||||
#include <vector> // Para vector
|
||||
class AnimatedSprite; // lines 10-10
|
||||
class Surface; // lines 11-11
|
||||
|
||||
class Scoreboard {
|
||||
public:
|
||||
// Tipos anidados
|
||||
struct Data {
|
||||
int items{0}; // Lleva la cuenta de los objetos recogidos
|
||||
int lives{0}; // Lleva la cuenta de las vidas restantes del jugador
|
||||
int rooms{0}; // Lleva la cuenta de las habitaciones visitadas
|
||||
bool music{true}; // Indica si ha de sonar la música durante el juego
|
||||
Uint8 color{0}; // Color para escribir el texto del marcador
|
||||
Uint32 ini_clock{0}; // Tiempo inicial para calcular el tiempo transcurrido
|
||||
bool jail_is_open{false}; // Indica si se puede entrar a la Jail
|
||||
};
|
||||
|
||||
// Métodos públicos
|
||||
explicit Scoreboard(std::shared_ptr<Data> data); // Constructor
|
||||
~Scoreboard() = default; // Destructor
|
||||
void render(); // Pinta el objeto en pantalla
|
||||
void update(float delta_time); // Actualiza las variables del objeto
|
||||
void setPaused(bool value); // Pone el marcador en modo pausa
|
||||
void refreshPlayerSkin(); // Actualiza el sprite del jugador con la skin actual
|
||||
auto getMinutes() -> int; // Devuelve la cantidad de minutos de juego transcurridos
|
||||
|
||||
private:
|
||||
// Tipos anidados
|
||||
struct ClockData {
|
||||
int hours{0}; // Horas transcurridas
|
||||
int minutes{0}; // Minutos transcurridos
|
||||
int seconds{0}; // Segundos transcurridos
|
||||
std::string separator{":"}; // Separador para mostrar el tiempo
|
||||
};
|
||||
|
||||
// Constantes de tiempo
|
||||
static constexpr float ITEMS_COLOR_BLINK_DURATION = 0.333F; // Duración de cada estado del parpadeo (era 10 frames @ 60fps)
|
||||
static constexpr float SPRITE_WALK_CYCLE_DURATION = 0.667F; // Duración del ciclo de caminar (era 40 frames @ 60fps)
|
||||
|
||||
// Posición de los elementos del marcador (en pixels, Tile::SIZE = 8)
|
||||
static constexpr int LINE1_Y = 8; // Fila superior (1 * Tile::SIZE)
|
||||
static constexpr int LINE2_Y = 24; // Fila inferior (3 * Tile::SIZE)
|
||||
static constexpr int ITEMS_LABEL_X = 8; // "TRESORS PILLATS" / "ITEMS COLLECTED"
|
||||
static constexpr int ITEMS_VALUE_X = 136; // Valor numérico de items
|
||||
static constexpr int TIME_LABEL_X = 160; // "HORA" / "TIME"
|
||||
static constexpr int TIME_VALUE_X = 208; // Valor numérico del tiempo
|
||||
static constexpr int LIVES_START_X = 8; // Primera vida (sprite)
|
||||
static constexpr int LIVES_SPACING = 16; // Separación entre vidas
|
||||
static constexpr int MUSIC_ICON_X = 160; // Icono de música
|
||||
static constexpr int ROOMS_LABEL_X = 176; // "SALES" / "ROOMS"
|
||||
static constexpr int ROOMS_VALUE_X = 224; // Valor numérico de salas
|
||||
static constexpr int CHEAT_INF_LIVES_X = 176; // Indicador "vides inf" / "inf lives"
|
||||
static constexpr int CHEAT_INF_LIVES_Y = 34; // Posición Y del indicador de vidas infinitas
|
||||
static constexpr int CHEAT_INVINCIBLE_X = 231; // Indicador "inv"
|
||||
static constexpr int CHEAT_INVINCIBLE_Y = 34; // Posición Y del indicador de invencibilidad
|
||||
|
||||
// Métodos privados
|
||||
auto getTime() -> ClockData; // Obtiene el tiempo transcurrido de partida
|
||||
void updateItemsColor(float delta_time); // Actualiza el color de la cantidad de items recogidos
|
||||
void fillTexture(); // Dibuja los elementos del marcador en la surface
|
||||
|
||||
// Objetos y punteros
|
||||
std::shared_ptr<AnimatedSprite> player_sprite_; // Sprite para mostrar las vidas en el marcador
|
||||
std::shared_ptr<Surface> item_surface_; // Surface con los graficos para los elementos del marcador
|
||||
std::shared_ptr<Data> data_; // Contiene las variables a mostrar en el marcador
|
||||
std::shared_ptr<Surface> surface_; // Surface donde dibujar el marcador
|
||||
|
||||
// Variables de estado
|
||||
std::vector<Uint8> color_; // Vector con los colores del objeto
|
||||
bool is_paused_{false}; // Indica si el marcador esta en modo pausa
|
||||
Uint32 paused_time_{0}; // Milisegundos que ha estado el marcador en pausa
|
||||
Uint32 paused_time_elapsed_{0}; // Tiempo acumulado en pausa
|
||||
ClockData clock_{}; // Contiene las horas, minutos y segundos transcurridos desde el inicio de la partida
|
||||
Uint8 items_color_{0}; // Color de la cantidad de items recogidos
|
||||
SDL_FRect surface_dest_{}; // Rectangulo donde dibujar la surface del marcador
|
||||
float time_accumulator_{0.0F}; // Acumulador de tiempo para animaciones
|
||||
float items_color_timer_{0.0F}; // Timer para parpadeo de color de items
|
||||
};
|
||||
228
source/game/gameplay/tilemap_renderer.cpp
Normal file
228
source/game/gameplay/tilemap_renderer.cpp
Normal file
@@ -0,0 +1,228 @@
|
||||
#include "tilemap_renderer.hpp"
|
||||
|
||||
#include "core/rendering/screen.hpp"
|
||||
#include "core/rendering/sprite/sprite.hpp"
|
||||
#include "core/rendering/surface.hpp"
|
||||
#ifdef _DEBUG
|
||||
#include "core/system/debug.hpp"
|
||||
#endif
|
||||
#include "game/gameplay/collision_map.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
|
||||
// Constructor
|
||||
TilemapRenderer::TilemapRenderer(std::vector<int> tile_map, int tile_set_width, std::shared_ptr<Surface> tileset_surface, std::string bg_color, int conveyor_belt_direction)
|
||||
: tile_map_(std::move(tile_map)),
|
||||
tile_set_width_(tile_set_width),
|
||||
tileset_surface_(std::move(tileset_surface)),
|
||||
bg_color_(std::move(bg_color)),
|
||||
conveyor_belt_direction_(conveyor_belt_direction) {
|
||||
// Crear la surface del mapa
|
||||
map_surface_ = std::make_shared<Surface>(PlayArea::WIDTH, PlayArea::HEIGHT);
|
||||
}
|
||||
|
||||
// Inicializa el renderizador
|
||||
void TilemapRenderer::initialize(const CollisionMap* collision_map) {
|
||||
setAnimatedTiles(collision_map);
|
||||
fillMapTexture(collision_map);
|
||||
}
|
||||
|
||||
// Actualiza las animaciones de tiles
|
||||
void TilemapRenderer::update(float delta_time) {
|
||||
if (is_paused_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Actualiza el acumulador de tiempo
|
||||
time_accumulator_ += delta_time;
|
||||
|
||||
// Actualiza los tiles animados
|
||||
updateAnimatedTiles();
|
||||
}
|
||||
|
||||
// Renderiza el mapa completo en pantalla
|
||||
void TilemapRenderer::render() {
|
||||
// Dibuja la textura con el mapa en pantalla
|
||||
SDL_FRect dest = {.x = 0, .y = 0, .w = PlayArea::WIDTH, .h = PlayArea::HEIGHT};
|
||||
map_surface_->render(nullptr, &dest);
|
||||
|
||||
// Dibuja los tiles animados
|
||||
#ifdef _DEBUG
|
||||
if (!Debug::get()->isEnabled()) {
|
||||
renderAnimatedTiles();
|
||||
}
|
||||
#else
|
||||
renderAnimatedTiles();
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Renderiza las superficies de colisión en modo debug (función helper estática)
|
||||
static void renderDebugCollisionSurfaces(const CollisionMap* collision_map) {
|
||||
auto surface = Screen::get()->getRendererSurface();
|
||||
|
||||
// BottomSurfaces
|
||||
for (auto l : collision_map->getBottomFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, static_cast<Uint8>(PaletteColor::BLUE));
|
||||
}
|
||||
|
||||
// TopSurfaces
|
||||
for (auto l : collision_map->getTopFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, static_cast<Uint8>(PaletteColor::RED));
|
||||
}
|
||||
|
||||
// LeftSurfaces
|
||||
for (auto l : collision_map->getLeftWalls()) {
|
||||
surface->drawLine(l.x, l.y1, l.x, l.y2, static_cast<Uint8>(PaletteColor::GREEN));
|
||||
}
|
||||
|
||||
// RightSurfaces
|
||||
for (auto l : collision_map->getRightWalls()) {
|
||||
surface->drawLine(l.x, l.y1, l.x, l.y2, static_cast<Uint8>(PaletteColor::MAGENTA));
|
||||
}
|
||||
|
||||
// LeftSlopes
|
||||
for (auto l : collision_map->getLeftSlopes()) {
|
||||
surface->drawLine(l.x1, l.y1, l.x2, l.y2, static_cast<Uint8>(PaletteColor::CYAN));
|
||||
}
|
||||
|
||||
// RightSlopes
|
||||
for (auto l : collision_map->getRightSlopes()) {
|
||||
surface->drawLine(l.x1, l.y1, l.x2, l.y2, static_cast<Uint8>(PaletteColor::YELLOW));
|
||||
}
|
||||
|
||||
// AutoSurfaces (Conveyor Belts)
|
||||
for (auto l : collision_map->getConveyorBeltFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, static_cast<Uint8>(PaletteColor::WHITE));
|
||||
}
|
||||
}
|
||||
|
||||
// Redibuja el tilemap (para actualizar modo debug)
|
||||
void TilemapRenderer::redrawMap(const CollisionMap* collision_map) {
|
||||
fillMapTexture(collision_map);
|
||||
}
|
||||
|
||||
// Cambia un tile y repinta solo esa celda en la map_surface
|
||||
void TilemapRenderer::setTile(int index, int tile_value) {
|
||||
if (index < 0 || index >= static_cast<int>(tile_map_.size())) { return; }
|
||||
|
||||
tile_map_[index] = tile_value;
|
||||
|
||||
int col = index % MAP_WIDTH;
|
||||
int row = index / MAP_WIDTH;
|
||||
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(map_surface_);
|
||||
|
||||
// Borrar la celda con el color de fondo
|
||||
SDL_FRect cell = {.x = static_cast<float>(col * TILE_SIZE), .y = static_cast<float>(row * TILE_SIZE), .w = static_cast<float>(TILE_SIZE), .h = static_cast<float>(TILE_SIZE)};
|
||||
map_surface_->fillRect(&cell, stringToColor(bg_color_));
|
||||
|
||||
// Dibujar el nuevo tile (si no es vacío ni animado)
|
||||
if (tile_value > -1) {
|
||||
const bool IS_ANIMATED = (tile_value >= 18 * tile_set_width_) && (tile_value < 19 * tile_set_width_);
|
||||
if (!IS_ANIMATED) {
|
||||
SDL_FRect clip = {.x = static_cast<float>((tile_value % tile_set_width_) * TILE_SIZE),
|
||||
.y = static_cast<float>((tile_value / tile_set_width_) * TILE_SIZE),
|
||||
.w = static_cast<float>(TILE_SIZE),
|
||||
.h = static_cast<float>(TILE_SIZE)};
|
||||
tileset_surface_->render(col * TILE_SIZE, row * TILE_SIZE, &clip);
|
||||
}
|
||||
}
|
||||
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Pinta el mapa estático y debug lines
|
||||
void TilemapRenderer::fillMapTexture(const CollisionMap* collision_map) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
const Uint8 COLOR = stringToColor(bg_color_);
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(map_surface_);
|
||||
map_surface_->clear(COLOR);
|
||||
|
||||
// Los tileSetFiles son de 20x20 tiles. El primer tile es el 0. Cuentan hacia la derecha y hacia abajo
|
||||
|
||||
SDL_FRect clip = {.x = 0, .y = 0, .w = TILE_SIZE, .h = TILE_SIZE};
|
||||
for (int y = 0; y < MAP_HEIGHT; ++y) {
|
||||
for (int x = 0; x < MAP_WIDTH; ++x) {
|
||||
// Tiled pone los tiles vacios del mapa como cero y empieza a contar de 1 a n.
|
||||
// Al cargar el mapa en memoria, se resta uno, por tanto los tiles vacios son -1
|
||||
// Tampoco hay que dibujar los tiles animados que estan en la fila 19 (indices)
|
||||
const int INDEX = (y * MAP_WIDTH) + x;
|
||||
const bool A = (tile_map_[INDEX] >= 18 * tile_set_width_) && (tile_map_[INDEX] < 19 * tile_set_width_);
|
||||
const bool B = tile_map_[INDEX] > -1;
|
||||
|
||||
if (B && !A) {
|
||||
clip.x = (tile_map_[INDEX] % tile_set_width_) * TILE_SIZE;
|
||||
clip.y = (tile_map_[INDEX] / tile_set_width_) * TILE_SIZE;
|
||||
#ifdef _DEBUG
|
||||
if (!Debug::get()->isEnabled()) {
|
||||
tileset_surface_->render(x * TILE_SIZE, y * TILE_SIZE, &clip);
|
||||
}
|
||||
#else
|
||||
tileset_surface_->render(x * TILE_SIZE, y * TILE_SIZE, &clip);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Pinta las superficies en el modo debug
|
||||
if (Debug::get()->isEnabled()) {
|
||||
renderDebugCollisionSurfaces(collision_map);
|
||||
}
|
||||
#endif // _DEBUG
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
}
|
||||
|
||||
// Localiza todos los tiles animados
|
||||
void TilemapRenderer::setAnimatedTiles(const CollisionMap* collision_map) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_animated
|
||||
for (int i = 0; i < (int)tile_map_.size(); ++i) {
|
||||
const auto TILE_TYPE = collision_map->getTile(i);
|
||||
if (TILE_TYPE == CollisionMap::Tile::ANIMATED) {
|
||||
// La i es la ubicación
|
||||
const int X = (i % MAP_WIDTH) * TILE_SIZE;
|
||||
const int Y = (i / MAP_WIDTH) * TILE_SIZE;
|
||||
|
||||
// TileMap[i] es el tile a poner
|
||||
const int XC = (tile_map_[i] % tile_set_width_) * TILE_SIZE;
|
||||
const int YC = (tile_map_[i] / tile_set_width_) * TILE_SIZE;
|
||||
|
||||
AnimatedTile at;
|
||||
at.sprite = std::make_shared<Sprite>(tileset_surface_, X, Y, 8, 8);
|
||||
at.sprite->setClip(XC, YC, 8, 8);
|
||||
at.x_orig = XC;
|
||||
animated_tiles_.push_back(at);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza tiles animados
|
||||
void TilemapRenderer::updateAnimatedTiles() { // NOLINT(readability-make-member-function-const)
|
||||
const int NUM_FRAMES = 4;
|
||||
|
||||
// Calcular frame actual basado en tiempo
|
||||
const int CURRENT_FRAME = static_cast<int>(time_accumulator_ / CONVEYOR_FRAME_DURATION) % NUM_FRAMES;
|
||||
|
||||
// Calcular offset basado en dirección
|
||||
int offset = 0;
|
||||
if (conveyor_belt_direction_ == -1) {
|
||||
offset = CURRENT_FRAME * TILE_SIZE;
|
||||
} else {
|
||||
offset = (NUM_FRAMES - 1 - CURRENT_FRAME) * TILE_SIZE;
|
||||
}
|
||||
|
||||
for (auto& a : animated_tiles_) {
|
||||
SDL_FRect rect = a.sprite->getClip();
|
||||
rect.x = a.x_orig + offset;
|
||||
a.sprite->setClip(rect);
|
||||
}
|
||||
}
|
||||
|
||||
// Renderiza tiles animados
|
||||
void TilemapRenderer::renderAnimatedTiles() {
|
||||
for (const auto& a : animated_tiles_) {
|
||||
a.sprite->render();
|
||||
}
|
||||
}
|
||||
120
source/game/gameplay/tilemap_renderer.hpp
Normal file
120
source/game/gameplay/tilemap_renderer.hpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "utils/defines.hpp"
|
||||
|
||||
class Surface;
|
||||
class Sprite;
|
||||
class CollisionMap;
|
||||
|
||||
/**
|
||||
* @brief Renderizador de tilemap de una habitación
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - Renderizar el mapa de tiles estático
|
||||
* - Gestionar tiles animados (conveyor belts)
|
||||
* - Actualizar animaciones basadas en tiempo
|
||||
* - Renderizar debug visualization (en modo DEBUG)
|
||||
*/
|
||||
class TilemapRenderer {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor
|
||||
* @param tile_map Vector con índices de tiles de la habitación
|
||||
* @param tile_set_width Ancho del tileset en tiles
|
||||
* @param tileset_surface Surface con los gráficos del tileset
|
||||
* @param bg_color Color de fondo de la habitación (como string)
|
||||
* @param conveyor_belt_direction Dirección de las cintas transportadoras (-1, 0, +1)
|
||||
*/
|
||||
TilemapRenderer(std::vector<int> tile_map, int tile_set_width, std::shared_ptr<Surface> tileset_surface, std::string bg_color, int conveyor_belt_direction);
|
||||
~TilemapRenderer() = default;
|
||||
|
||||
// Prohibir copia y movimiento
|
||||
TilemapRenderer(const TilemapRenderer&) = delete;
|
||||
auto operator=(const TilemapRenderer&) -> TilemapRenderer& = delete;
|
||||
TilemapRenderer(TilemapRenderer&&) = delete;
|
||||
auto operator=(TilemapRenderer&&) -> TilemapRenderer& = delete;
|
||||
|
||||
/**
|
||||
* @brief Inicializa el renderizador
|
||||
* @param collision_map Mapa de colisiones para determinar tiles animados
|
||||
*
|
||||
* Crea la textura del mapa, pinta los tiles estáticos, y localiza tiles animados
|
||||
*/
|
||||
void initialize(const CollisionMap* collision_map);
|
||||
|
||||
/**
|
||||
* @brief Actualiza las animaciones de tiles
|
||||
* @param delta_time Tiempo transcurrido desde el último frame (segundos)
|
||||
*/
|
||||
void update(float delta_time);
|
||||
|
||||
/**
|
||||
* @brief Renderiza el mapa completo en pantalla
|
||||
*
|
||||
* Dibuja la textura del mapa y los tiles animados
|
||||
*/
|
||||
void render();
|
||||
|
||||
#ifdef _DEBUG
|
||||
/**
|
||||
* @brief Redibuja el tilemap (para actualizar modo debug)
|
||||
* @param collision_map Mapa de colisiones para dibujar líneas de debug
|
||||
*
|
||||
* Llamado cuando se activa/desactiva el modo debug para actualizar la visualización
|
||||
*/
|
||||
void redrawMap(const CollisionMap* collision_map);
|
||||
void setBgColor(const std::string& color) { bg_color_ = color; }
|
||||
void setTile(int index, int tile_value); // Cambia un tile y repinta esa celda
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Activa/desactiva modo pausa
|
||||
* @param paused true para pausar, false para reanudar
|
||||
*
|
||||
* Nota: Actualmente no afecta al renderizado, pero mantiene consistencia con Room
|
||||
*/
|
||||
void setPaused(bool paused) { is_paused_ = paused; }
|
||||
|
||||
// Getter para la surface del mapa (usado por Room para acceso directo si es necesario)
|
||||
[[nodiscard]] auto getMapSurface() const -> std::shared_ptr<Surface> { return map_surface_; }
|
||||
|
||||
private:
|
||||
// Estructura para tiles animados (conveyor belts)
|
||||
struct AnimatedTile {
|
||||
std::shared_ptr<Sprite> sprite{nullptr}; // SurfaceSprite para dibujar el tile
|
||||
int x_orig{0}; // Posición X del primer tile de la animación en tilesheet
|
||||
};
|
||||
|
||||
// === Constantes ===
|
||||
static constexpr int TILE_SIZE = Tile::SIZE; // Ancho del tile en pixels
|
||||
static constexpr int MAP_WIDTH = PlayArea::WIDTH / Tile::SIZE; // Ancho del mapa en tiles
|
||||
static constexpr int MAP_HEIGHT = PlayArea::HEIGHT / Tile::SIZE; // Alto del mapa en tiles
|
||||
static constexpr int PLAY_AREA_WIDTH = PlayArea::WIDTH; // Ancho del área de juego en pixels
|
||||
static constexpr int PLAY_AREA_HEIGHT = PlayArea::HEIGHT; // Alto del área de juego en pixels
|
||||
static constexpr float CONVEYOR_FRAME_DURATION = 0.05F; // Duración de cada frame (3 frames @ 60fps)
|
||||
|
||||
// === Datos de la habitación ===
|
||||
std::vector<int> tile_map_; // Índices de tiles de la habitación
|
||||
int tile_set_width_; // Ancho del tileset en tiles
|
||||
std::shared_ptr<Surface> tileset_surface_; // Gráficos del tileset
|
||||
std::string bg_color_; // Color de fondo
|
||||
int conveyor_belt_direction_; // Dirección de conveyor belts
|
||||
|
||||
// === Renderizado ===
|
||||
std::shared_ptr<Surface> map_surface_; // Textura para el mapa de la habitación
|
||||
std::vector<AnimatedTile> animated_tiles_; // Tiles animados (conveyor belts)
|
||||
float time_accumulator_{0.0F}; // Acumulador de tiempo para animaciones
|
||||
bool is_paused_{false}; // Indica si está en modo pausa
|
||||
|
||||
// === Métodos privados ===
|
||||
void fillMapTexture(const CollisionMap* collision_map); // Pinta el mapa estático y debug lines
|
||||
void setAnimatedTiles(const CollisionMap* collision_map); // Localiza todos los tiles animados
|
||||
void updateAnimatedTiles(); // Actualiza tiles animados
|
||||
void renderAnimatedTiles(); // Renderiza tiles animados
|
||||
};
|
||||
1205
source/game/options.cpp
Normal file
1205
source/game/options.cpp
Normal file
File diff suppressed because it is too large
Load Diff
215
source/game/options.hpp
Normal file
215
source/game/options.hpp
Normal file
@@ -0,0 +1,215 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <string> // Para string, basic_string
|
||||
#include <utility>
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen::Filter
|
||||
#include "core/rendering/shader_backend.hpp" // Para Rendering::ShaderType
|
||||
#include "game/defaults.hpp"
|
||||
#include "utils/defines.hpp" // Para WINDOW_CAPTION
|
||||
#include "utils/utils.hpp" // Para Color, Palette
|
||||
|
||||
// --- Namespace Options: gestión de configuración y opciones del juego ---
|
||||
namespace Options {
|
||||
|
||||
// Estructura para las opciones de control de teclado
|
||||
struct KeyboardControls {
|
||||
SDL_Scancode key_left{Defaults::Controls::KEY_LEFT}; // Tecla para mover a la izquierda
|
||||
SDL_Scancode key_right{Defaults::Controls::KEY_RIGHT}; // Tecla para mover a la derecha
|
||||
SDL_Scancode key_jump{Defaults::Controls::KEY_JUMP}; // Tecla para saltar
|
||||
};
|
||||
|
||||
// Estructura para las opciones de control del gamepad/joystick
|
||||
struct GamepadControls {
|
||||
int button_left{Defaults::Controls::GAMEPAD_BUTTON_LEFT}; // Botón para mover a la izquierda (por defecto: DPAD_LEFT)
|
||||
int button_right{Defaults::Controls::GAMEPAD_BUTTON_RIGHT}; // Botón para mover a la derecha (por defecto: DPAD_RIGHT)
|
||||
int button_jump{Defaults::Controls::GAMEPAD_BUTTON_JUMP}; // Botón para saltar (por defecto: WEST/X button)
|
||||
};
|
||||
|
||||
// Estructura para albergar trucos
|
||||
struct Cheat {
|
||||
enum class State : bool {
|
||||
DISABLED = false,
|
||||
ENABLED = true
|
||||
};
|
||||
|
||||
State infinite_lives{Defaults::Cheat::INFINITE_LIVES ? State::ENABLED : State::DISABLED}; // Indica si el jugador dispone de vidas infinitas
|
||||
State invincible{Defaults::Cheat::INVINCIBLE ? State::ENABLED : State::DISABLED}; // Indica si el jugador puede morir
|
||||
State jail_is_open{Defaults::Cheat::JAIL_IS_OPEN ? State::ENABLED : State::DISABLED}; // Indica si la Jail está abierta
|
||||
|
||||
// Método para comprobar si alguno de los tres primeros trucos está activo
|
||||
[[nodiscard]] auto enabled() const -> bool {
|
||||
return infinite_lives == State::ENABLED || invincible == State::ENABLED || jail_is_open == State::ENABLED;
|
||||
}
|
||||
};
|
||||
|
||||
// Estructura para almacenar estadísticas
|
||||
struct Stats {
|
||||
int rooms{Defaults::Stats::ROOMS}; // Cantidad de habitaciones visitadas
|
||||
int items{Defaults::Stats::ITEMS}; // Cantidad de items obtenidos
|
||||
};
|
||||
|
||||
// Estructura para el modo kiosko
|
||||
struct Kiosk {
|
||||
bool enabled{Defaults::Kiosk::ENABLED}; // Indica si el modo kiosko está activo
|
||||
std::string text{Defaults::Kiosk::TEXT}; // Texto a mostrar en el modo kiosko
|
||||
bool infinite_lives{Defaults::Kiosk::INFINITE_LIVES}; // Indica si el jugador tiene vidas infinitas en modo kiosko
|
||||
};
|
||||
|
||||
// Estructura con opciones de la ventana
|
||||
struct Window {
|
||||
std::string caption{Texts::WINDOW_CAPTION}; // Texto que aparece en la barra de título de la ventana
|
||||
int zoom{Defaults::Window::ZOOM}; // Zoom de la ventana
|
||||
int max_zoom{Defaults::Window::ZOOM}; // Máximo tamaño de zoom para la ventana
|
||||
};
|
||||
|
||||
// Estructura para gestionar el borde de la pantalla
|
||||
struct Border {
|
||||
bool enabled{Defaults::Border::ENABLED}; // Indica si se ha de mostrar el borde
|
||||
float width{Defaults::Border::WIDTH}; // Ancho del borde
|
||||
float height{Defaults::Border::HEIGHT}; // Alto del borde
|
||||
};
|
||||
|
||||
// Estructura para las opciones de GPU
|
||||
struct GPU {
|
||||
bool acceleration{Defaults::Video::GPU_ACCELERATION}; // Usar aceleración GPU; false = path SDL fallback directo
|
||||
std::string preferred_driver; // Driver GPU preferido; vacío = auto. Aplica en el próximo arranque.
|
||||
};
|
||||
|
||||
// Estructura para las opciones de supersampling
|
||||
struct Supersampling {
|
||||
bool enabled{Defaults::Video::SUPERSAMPLING}; // Indica si el supersampling 3× está activo
|
||||
bool linear_upscale{Defaults::Video::LINEAR_UPSCALE}; // Upscale LINEAR (true) o NEAREST (false)
|
||||
int downscale_algo{Defaults::Video::DOWNSCALE_ALGO}; // 0=bilinear, 1=Lanczos2, 2=Lanczos3
|
||||
};
|
||||
|
||||
// Estructura para las opciones de shader (dentro de Video)
|
||||
struct ShaderConfig {
|
||||
bool enabled{Defaults::Video::SHADER_ENABLED}; // Indica si se usan shaders de post-procesado
|
||||
Rendering::ShaderType current_shader{Rendering::ShaderType::POSTFX}; // Shader de post-procesado activo
|
||||
std::string current_postfx_preset_name; // Nombre del preset PostFX leído del config
|
||||
std::string current_crtpi_preset_name; // Nombre del preset CrtPi leído del config
|
||||
int current_postfx_preset{0}; // Índice resuelto del preset PostFX
|
||||
int current_crtpi_preset{0}; // Índice resuelto del preset CrtPi
|
||||
};
|
||||
|
||||
// Estructura para las opciones de video
|
||||
struct Video {
|
||||
bool fullscreen{Defaults::Video::FULLSCREEN}; // Contiene el valor del modo de pantalla completa
|
||||
Screen::Filter filter{Defaults::Video::FILTER}; // Filtro usado para el escalado de la imagen
|
||||
bool vertical_sync{Defaults::Video::VERTICAL_SYNC}; // Indica si se quiere usar vsync o no
|
||||
bool integer_scale{Defaults::Video::INTEGER_SCALE}; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
|
||||
bool keep_aspect{Defaults::Video::KEEP_ASPECT}; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
|
||||
std::string palette{Defaults::Video::PALETTE_NAME}; // Paleta de colores a usar en el juego
|
||||
std::string palette_sort{Defaults::Video::PALETTE_SORT}; // Modo de ordenación de la paleta (original/luminance/spectrum)
|
||||
std::string info; // Información sobre el modo de vídeo
|
||||
Border border{}; // Borde de la pantalla
|
||||
GPU gpu{}; // Opciones de aceleración GPU
|
||||
Supersampling supersampling{}; // Opciones de supersampling
|
||||
ShaderConfig shader{}; // Opciones de shader post-procesado
|
||||
};
|
||||
|
||||
// Estructura para las opciones de musica
|
||||
struct Music {
|
||||
bool enabled{Defaults::Music::ENABLED}; // Indica si la música suena o no
|
||||
float volume{Defaults::Music::VOLUME}; // Volumen al que suena la música
|
||||
};
|
||||
|
||||
// Estructura para las opciones de sonido
|
||||
struct Sound {
|
||||
bool enabled{Defaults::Sound::ENABLED}; // Indica si los sonidos suenan o no
|
||||
float volume{Defaults::Sound::VOLUME}; // Volumen al que suenan los sonidos (0 a 128 internamente)
|
||||
};
|
||||
|
||||
// Estructura para las opciones de audio
|
||||
struct Audio {
|
||||
Music music{}; // Opciones para la música
|
||||
Sound sound{}; // Opciones para los efectos de sonido
|
||||
bool enabled{Defaults::Audio::ENABLED}; // Indica si el audio está activo o no
|
||||
float volume{Defaults::Audio::VOLUME}; // Volumen al que suenan el audio (0-128 internamente)
|
||||
};
|
||||
|
||||
// Estructura para las opciones de juego
|
||||
struct Game {
|
||||
float width{Defaults::Canvas::WIDTH}; // Ancho de la resolucion del juego
|
||||
float height{Defaults::Canvas::HEIGHT}; // Alto de la resolucion del juego
|
||||
std::string player_skin{Defaults::Game::Player::SKIN}; // Skin del jugador ("default" o nombre de enemigo)
|
||||
int player_color{Defaults::Game::Player::COLOR}; // Color del jugador (-1 = automático, 0-15 = color fijo)
|
||||
};
|
||||
|
||||
// Estructura para un preset de PostFX
|
||||
struct PostFXPreset {
|
||||
std::string name; // Nombre del preset
|
||||
float vignette{0.6F}; // Intensidad de la viñeta (0.0 = ninguna, 1.0 = máxima)
|
||||
float scanlines{0.7F}; // Intensidad de las scanlines (0.0 = desactivadas, 1.0 = máximas)
|
||||
float chroma{0.15F}; // Intensidad de la aberración cromática (0.0 = ninguna, 1.0 = máxima)
|
||||
float mask{0.0F}; // Intensidad de la máscara de fósforo RGB (0.0 = desactivada, 1.0 = máxima)
|
||||
float gamma{0.0F}; // Corrección gamma input 2.4 / output 2.2 (0.0 = off, 1.0 = plena)
|
||||
float curvature{0.0F}; // Distorsión barrel CRT (0.0 = plana, 1.0 = máxima curvatura)
|
||||
float bleeding{0.0F}; // Sangrado de color NTSC horizontal Y/C (0.0 = off, 1.0 = máximo)
|
||||
float flicker{0.0F}; // Parpadeo de fósforo CRT ~50 Hz (0.0 = off, 1.0 = máximo)
|
||||
};
|
||||
|
||||
// Estructura para un preset del shader CRT-Pi
|
||||
struct CrtPiPreset {
|
||||
std::string name;
|
||||
float scanline_weight{6.0F}; // Ajuste gaussiano (mayor = scanlines más estrechas)
|
||||
float scanline_gap_brightness{0.12F}; // Brillo mínimo en las ranuras entre scanlines
|
||||
float bloom_factor{3.5F}; // Factor de brillo para zonas iluminadas
|
||||
float input_gamma{2.4F}; // Gamma de entrada (linealización)
|
||||
float output_gamma{2.2F}; // Gamma de salida (codificación)
|
||||
float mask_brightness{0.80F}; // Brillo sub-píxeles en la máscara de fósforo
|
||||
float curvature_x{0.05F}; // Distorsión barrel eje X
|
||||
float curvature_y{0.10F}; // Distorsión barrel eje Y
|
||||
int mask_type{2}; // 0=ninguna, 1=verde/magenta, 2=RGB fósforo
|
||||
bool enable_scanlines{true}; // Activar efecto de scanlines
|
||||
bool enable_multisample{true}; // Antialiasing analítico de scanlines
|
||||
bool enable_gamma{true}; // Corrección gamma
|
||||
bool enable_curvature{false}; // Distorsión barrel CRT
|
||||
bool enable_sharper{false}; // Submuestreo más nítido (modo SHARPER)
|
||||
};
|
||||
|
||||
// --- Variables globales ---
|
||||
inline std::string version{}; // Versión del fichero de configuración. Sirve para saber si las opciones son compatibles
|
||||
inline Cheat cheats{}; // Contiene trucos y ventajas para el juego
|
||||
inline Game game{}; // Opciones de juego
|
||||
inline Video video{}; // Opciones de video
|
||||
inline Stats stats{}; // Datos con las estadisticas de juego
|
||||
inline Window window{}; // Opciones relativas a la ventana
|
||||
inline Audio audio{}; // Opciones relativas al audio
|
||||
inline KeyboardControls keyboard_controls{}; // Teclas usadas para jugar
|
||||
inline GamepadControls gamepad_controls{}; // Botones del gamepad usados para jugar
|
||||
inline Kiosk kiosk{}; // Opciones del modo kiosko
|
||||
|
||||
// Idioma del juego (establecido al inicio, sin cambio en caliente)
|
||||
inline std::string language{Defaults::Localization::LANGUAGE};
|
||||
|
||||
// Ruta completa del fichero de configuración (establecida mediante setConfigFile)
|
||||
inline std::string config_file_path{};
|
||||
|
||||
// --- Variables PostFX ---
|
||||
inline std::vector<PostFXPreset> postfx_presets{}; // Lista de presets de PostFX
|
||||
inline std::string postfx_file_path{}; // Ruta del fichero postfx.yaml
|
||||
|
||||
// --- Variables CrtPi ---
|
||||
inline std::vector<CrtPiPreset> crtpi_presets{}; // Lista de presets del shader CRT-Pi
|
||||
inline std::string crtpi_file_path{}; // Ruta del fichero crtpi.yaml
|
||||
|
||||
// --- Funciones públicas ---
|
||||
void setConfigFile(const std::string& path); // Establece la ruta del fichero de configuración
|
||||
auto loadFromFile() -> bool; // Carga las opciones desde el fichero configurado
|
||||
auto saveToFile() -> bool; // Guarda las opciones al fichero configurado
|
||||
void setPostFXFile(const std::string& path); // Establece la ruta del fichero de PostFX
|
||||
auto loadPostFXFromFile() -> bool; // Carga los presets de PostFX desde el fichero
|
||||
auto savePostFXToFile() -> bool; // Guarda los presets de PostFX por defecto
|
||||
void setCrtPiFile(const std::string& path); // Establece la ruta del fichero de CrtPi
|
||||
auto loadCrtPiFromFile() -> bool; // Carga los presets de CrtPi desde el fichero (crea defaults si no existe)
|
||||
|
||||
void resolvePostFXPresetName(); // Resuelve el nombre del preset PostFX a índice
|
||||
void resolveCrtPiPresetName(); // Resuelve el nombre del preset CrtPi a índice
|
||||
|
||||
} // namespace Options
|
||||
41
source/game/scene_manager.hpp
Normal file
41
source/game/scene_manager.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
Namespace SceneManager: gestiona el flujo entre las diferentes escenas del juego.
|
||||
|
||||
Define las escenas principales del programa y las opciones de transición entre ellas.
|
||||
Proporciona variables globales inline para gestionar el estado actual de la escena.
|
||||
*/
|
||||
|
||||
namespace SceneManager {
|
||||
|
||||
// --- Escenas del programa ---
|
||||
enum class Scene {
|
||||
LOGO, // Pantalla del logo
|
||||
LOADING_SCREEN, // Pantalla de carga
|
||||
TITLE, // Pantalla de título/menú principal
|
||||
CREDITS, // Créditos del juego
|
||||
GAME, // Juego principal
|
||||
DEMO, // Modo demostración
|
||||
GAME_OVER, // Pantalla de game over
|
||||
ENDING, // Final del juego (ending 1)
|
||||
ENDING2, // Final del juego (ending 2)
|
||||
RESTART_CURRENT, // Especial: reinicia la escena que estaba corriendo
|
||||
QUIT // Salir del programa
|
||||
};
|
||||
|
||||
// --- Opciones para transiciones entre escenas ---
|
||||
enum class Options {
|
||||
NONE, // Sin opciones especiales
|
||||
LOGO_TO_LOADING_SCREEN, // Del logo a la intro
|
||||
LOGO_TO_TITLE, // Del logo al título
|
||||
TITLE_WITH_LOADING_SCREEN, // Al título mostrando pantalla de carga
|
||||
TITLE_WITHOUT_LOADING_SCREEN // Al título sin pantalla de carga
|
||||
};
|
||||
|
||||
// --- Variables de estado globales ---
|
||||
inline Scene current = Scene::LOGO; // Escena actual (en _DEBUG sobrescrito por Director tras cargar debug.yaml)
|
||||
inline Options options = Options::LOGO_TO_LOADING_SCREEN; // Opciones de la escena actual
|
||||
inline Scene scene_before_restart = Scene::LOGO; // escena a relanzar tras RESTART_CURRENT
|
||||
|
||||
} // namespace SceneManager
|
||||
247
source/game/scenes/credits.cpp
Normal file
247
source/game/scenes/credits.cpp
Normal file
@@ -0,0 +1,247 @@
|
||||
#include "game/scenes/credits.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/global_inputs.hpp" // Para check
|
||||
#include "core/input/input.hpp" // Para Input
|
||||
#include "core/locale/locale.hpp" // Para Locale
|
||||
#include "core/rendering/pixel_reveal.hpp" // Para PixelReveal
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/options.hpp" // Para Options, options, OptionsGame, Sectio...
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "utils/defines.hpp" // Para GAME_SPEED, PlayArea::CENTER_X, PLAY_...
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/utils.hpp" // Para PaletteColor
|
||||
|
||||
// Destructor
|
||||
Credits::~Credits() = default;
|
||||
|
||||
// Constructor
|
||||
Credits::Credits()
|
||||
: text_surface_(std::make_shared<Surface>(Options::game.width, Options::game.height)),
|
||||
shining_sprite_(std::make_shared<AnimatedSprite>(Resource::Cache::get()->getAnimationData("shine.yaml"))),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()) {
|
||||
// Configura la escena
|
||||
SceneManager::current = SceneManager::Scene::CREDITS;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
shining_sprite_->setPos({.x = 194, .y = 174, .w = 8, .h = 8});
|
||||
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
|
||||
fillTexture(); // Escribe el texto en la textura
|
||||
Audio::get()->playMusic("574071_EA_DTV.ogg"); // Inicia la musica
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Credits::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Credits::handleInput() {
|
||||
Input::get()->update();
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Inicializa los textos
|
||||
void Credits::iniTexts() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto* loc = Locale::get();
|
||||
|
||||
texts_.clear();
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.instructions"), .color = static_cast<Uint8>(PaletteColor::YELLOW)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.l0"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.l1"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.l2"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
|
||||
texts_.push_back({.label = loc->get("credits.keys"), .color = static_cast<Uint8>(PaletteColor::YELLOW)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.keys_move"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.f8"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.f11"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.f1f2"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.f3"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = loc->get("credits.f9"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
|
||||
texts_.push_back({.label = loc->get("credits.author"), .color = static_cast<Uint8>(PaletteColor::YELLOW)});
|
||||
texts_.push_back({.label = loc->get("credits.date"), .color = static_cast<Uint8>(PaletteColor::YELLOW)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
|
||||
texts_.push_back({.label = loc->get("credits.love"), .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
texts_.push_back({.label = "", .color = static_cast<Uint8>(PaletteColor::WHITE)});
|
||||
}
|
||||
|
||||
// Escribe el texto en la textura
|
||||
void Credits::fillTexture() {
|
||||
// Inicializa los textos
|
||||
iniTexts();
|
||||
|
||||
// Rellena la textura de texto
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(text_surface_);
|
||||
text_surface_->clear(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
|
||||
// Escribe el texto en la textura
|
||||
const int SIZE = text->getCharacterSize();
|
||||
int pos_y = 0;
|
||||
|
||||
for (const auto& t : texts_) {
|
||||
text->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, pos_y * SIZE, t.label, 1, t.color);
|
||||
pos_y++;
|
||||
}
|
||||
|
||||
// Escribe el corazón
|
||||
const int TEXT_LENGHT = text->length(texts_[22].label, 1) - text->length(" ", 1); // Se resta el ultimo caracter que es un espacio
|
||||
const int POS_X = ((PlayArea::WIDTH - TEXT_LENGHT) / 2) + TEXT_LENGHT;
|
||||
text->writeColored(POS_X, 176, "ä", static_cast<Uint8>(PaletteColor::BRIGHT_RED));
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
|
||||
// Recoloca el sprite del brillo
|
||||
shining_sprite_->setPosX(POS_X + 2);
|
||||
|
||||
// Crea el efecto de revelado pixel a pixel
|
||||
pixel_reveal_ = std::make_unique<PixelReveal>(Options::game.width, Options::game.height, PIXELS_PER_SECOND, STEP_DURATION, REVEAL_STEPS);
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Credits::update() {
|
||||
const float DELTA_TIME = delta_timer_->tick();
|
||||
total_time_ += DELTA_TIME; // Actualiza el tiempo total
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
updateState(DELTA_TIME); // Actualiza la máquina de estados
|
||||
|
||||
pixel_reveal_->update(reveal_time_); // Actualiza el efecto de revelado
|
||||
|
||||
// Actualiza el sprite con el brillo si está después del tiempo de inicio
|
||||
if (reveal_time_ > SHINE_START_TIME) {
|
||||
shining_sprite_->update(DELTA_TIME);
|
||||
}
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
}
|
||||
|
||||
// Transición entre estados
|
||||
void Credits::transitionToState(State new_state) {
|
||||
state_ = new_state;
|
||||
state_time_ = 0.0F;
|
||||
}
|
||||
|
||||
// Actualiza la máquina de estados
|
||||
void Credits::updateState(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
|
||||
switch (state_) {
|
||||
case State::REVEALING_TEXT:
|
||||
reveal_time_ += delta_time; // Incrementa reveal_time durante revelación
|
||||
if (state_time_ >= REVEAL_PHASE_1_DURATION) {
|
||||
transitionToState(State::PAUSE_1);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::PAUSE_1:
|
||||
// reveal_time_ NO incrementa durante pausa (se congela)
|
||||
if (state_time_ >= PAUSE_DURATION) {
|
||||
transitionToState(State::REVEALING_TEXT_2);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::REVEALING_TEXT_2:
|
||||
reveal_time_ += delta_time; // Incrementa reveal_time durante revelación
|
||||
if (state_time_ >= REVEAL_PHASE_2_DURATION) {
|
||||
transitionToState(State::PAUSE_2);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::PAUSE_2:
|
||||
// reveal_time_ NO incrementa durante pausa (se congela)
|
||||
if (state_time_ >= PAUSE_DURATION) {
|
||||
transitionToState(State::REVEALING_TEXT_3);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::REVEALING_TEXT_3:
|
||||
reveal_time_ += delta_time; // Incrementa reveal_time durante revelación
|
||||
if (state_time_ >= REVEAL_PHASE_3_DURATION) {
|
||||
transitionToState(State::PAUSE_3);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::PAUSE_3:
|
||||
// reveal_time_ NO incrementa durante pausa (se congela)
|
||||
if (state_time_ >= PAUSE_DURATION) {
|
||||
transitionToState(State::DISPLAYING_WITH_SHINE);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::DISPLAYING_WITH_SHINE:
|
||||
reveal_time_ += delta_time; // Incrementa reveal_time durante revelación
|
||||
if (state_time_ >= DISPLAY_WITH_SHINE_DURATION) {
|
||||
transitionToState(State::FADING_OUT);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::FADING_OUT:
|
||||
reveal_time_ += delta_time; // Incrementa reveal_time durante fade
|
||||
if (state_time_ >= FADE_OUT_DURATION) {
|
||||
transitionToState(State::EXITING);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::EXITING:
|
||||
SceneManager::current = SceneManager::Scene::DEMO;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Credits::render() {
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
if (state_ != State::EXITING) {
|
||||
// Dibuja la textura con el texto en pantalla
|
||||
text_surface_->render(0, 0);
|
||||
|
||||
// Dibuja la máscara de revelado pixel a pixel
|
||||
pixel_reveal_->render(0, 0);
|
||||
|
||||
// Dibuja el sprite con el brillo
|
||||
if (reveal_time_ > SHINE_START_TIME) {
|
||||
shining_sprite_->render(1, static_cast<Uint8>(PaletteColor::BRIGHT_WHITE));
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Credits::run() {
|
||||
while (SceneManager::current == SceneManager::Scene::CREDITS) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
}
|
||||
80
source/game/scenes/credits.hpp
Normal file
80
source/game/scenes/credits.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
class AnimatedSprite; // lines 11-11
|
||||
class Surface;
|
||||
class PixelReveal;
|
||||
class DeltaTimer;
|
||||
|
||||
class Credits {
|
||||
public:
|
||||
// --- Constructor y Destructor ---
|
||||
Credits();
|
||||
~Credits(); // NOLINT(modernize-use-equals-default, performance-trivially-destructible) -- defined in .cpp for unique_ptr with forward declarations
|
||||
|
||||
// --- Bucle principal ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Tipos anidados ---
|
||||
enum class State {
|
||||
REVEALING_TEXT,
|
||||
PAUSE_1,
|
||||
REVEALING_TEXT_2,
|
||||
PAUSE_2,
|
||||
REVEALING_TEXT_3,
|
||||
PAUSE_3,
|
||||
DISPLAYING_WITH_SHINE,
|
||||
FADING_OUT,
|
||||
EXITING
|
||||
};
|
||||
|
||||
struct Captions {
|
||||
std::string label; // Texto a escribir
|
||||
Uint8 color{0}; // Color del texto
|
||||
};
|
||||
|
||||
// --- Constantes de tiempo (basado en 60 FPS) ---
|
||||
static constexpr float REVEAL_PHASE_1_DURATION = 3.733F; // 224 frames @ 60fps
|
||||
static constexpr float PAUSE_DURATION = 1.667F; // 100 frames @ 60fps
|
||||
static constexpr float REVEAL_PHASE_2_DURATION = 5.333F; // 320 frames (544-224) @ 60fps
|
||||
static constexpr float REVEAL_PHASE_3_DURATION = 2.133F; // 128 frames (672-544) @ 60fps
|
||||
static constexpr float DISPLAY_WITH_SHINE_DURATION = 7.967F; // 478 frames (1150-672) @ 60fps
|
||||
static constexpr float FADE_OUT_DURATION = 0.833F; // 50 frames (1200-1150) @ 60fps
|
||||
static constexpr float TOTAL_DURATION = 20.0F; // 1200 frames @ 60fps
|
||||
static constexpr float SHINE_START_TIME = 12.833F; // 770 frames @ 60fps
|
||||
static constexpr float FADE_OUT_START = 19.167F; // 1150 frames @ 60fps
|
||||
static constexpr float PIXELS_PER_SECOND = 15.0F; // Filas reveladas por segundo (REVEAL_SPEED/8*2 = 60/8*2 = 15)
|
||||
static constexpr float STEP_DURATION = 2.0F / 60.0F; // Segundos por paso de revelado (2 frames @ 60fps)
|
||||
static constexpr int REVEAL_STEPS = 16; // Pasos de revelado por fila (más pasos = efecto más visible)
|
||||
|
||||
// --- Métodos privados ---
|
||||
void update(); // Actualiza las variables
|
||||
void render(); // Dibuja en pantalla
|
||||
static void handleEvents(); // Comprueba el manejador de eventos
|
||||
static void handleInput(); // Comprueba las entradas
|
||||
void updateState(float delta_time); // Actualiza la máquina de estados
|
||||
void transitionToState(State new_state); // Transición entre estados
|
||||
void iniTexts(); // Inicializa los textos
|
||||
void fillTexture(); // Escribe el texto en la textura
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Recursos gráficos
|
||||
std::shared_ptr<Surface> text_surface_; // Textura para dibujar el texto
|
||||
std::unique_ptr<PixelReveal> pixel_reveal_; // Efecto de revelado pixel a pixel
|
||||
std::shared_ptr<AnimatedSprite> shining_sprite_; // Sprite para el brillo del corazón
|
||||
|
||||
// Temporizadores y estado
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Temporizador delta para time-based update
|
||||
State state_{State::REVEALING_TEXT}; // Estado actual
|
||||
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
|
||||
float total_time_{0.0F}; // Tiempo total acumulado
|
||||
float reveal_time_{0.0F}; // Tiempo acumulado solo durante revelación (se congela en pausas)
|
||||
|
||||
// Textos
|
||||
std::vector<Captions> texts_; // Vector con los textos
|
||||
};
|
||||
434
source/game/scenes/ending.cpp
Normal file
434
source/game/scenes/ending.cpp
Normal file
@@ -0,0 +1,434 @@
|
||||
#include "game/scenes/ending.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/global_inputs.hpp" // Para check
|
||||
#include "core/input/input.hpp" // Para Input
|
||||
#include "core/locale/locale.hpp" // Para Locale
|
||||
#include "core/rendering/pixel_reveal.hpp" // Para PixelReveal
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/sprite.hpp" // Para SSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text, TEXT_STROKE
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/options.hpp" // Para Options, options, OptionsGame, SectionS...
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/utils.hpp" // Para PaletteColor
|
||||
|
||||
// Destructor
|
||||
Ending::~Ending() = default;
|
||||
|
||||
// Constructor
|
||||
Ending::Ending()
|
||||
: delta_timer_(std::make_unique<DeltaTimer>()) {
|
||||
SceneManager::current = SceneManager::Scene::ENDING;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
|
||||
iniTexts(); // Inicializa los textos
|
||||
iniPics(); // Inicializa las imagenes
|
||||
iniScenes(); // Inicializa las escenas
|
||||
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
|
||||
}
|
||||
|
||||
// Actualiza el objeto
|
||||
void Ending::update() {
|
||||
const float DELTA_TIME = delta_timer_->tick();
|
||||
total_time_ += DELTA_TIME; // Actualiza el tiempo total
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
updateState(DELTA_TIME); // Actualiza la máquina de estados
|
||||
updateSpriteCovers(); // Actualiza las cortinillas de los elementos
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
}
|
||||
|
||||
// Dibuja el final en pantalla
|
||||
void Ending::render() {
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
// Skip rendering durante WARMING_UP
|
||||
if (state_ != State::WARMING_UP) {
|
||||
// Dibuja las imagenes de la escena
|
||||
const auto& pic = sprite_pics_.at(current_scene_);
|
||||
pic.image_sprite->render();
|
||||
pic.pixel_reveal->render(pic.pos_x, pic.pos_y);
|
||||
|
||||
// Dibuja los textos de la escena
|
||||
for (const auto& ti : scenes_.at(current_scene_).text_index) {
|
||||
// Convertir trigger de frames a segundos @ 60fps
|
||||
const float TRIGGER_TIME = static_cast<float>(ti.trigger) / 60.0F;
|
||||
|
||||
if (state_time_ > TRIGGER_TIME) {
|
||||
const auto& txt = sprite_texts_.at(ti.index);
|
||||
txt.image_sprite->render();
|
||||
txt.pixel_reveal->render(txt.pos_x, txt.pos_y);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la cortinilla de cambio de escena
|
||||
if (scene_cover_) {
|
||||
scene_cover_->render(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Ending::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Ending::handleInput() {
|
||||
Input::get()->update();
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Transición entre estados
|
||||
void Ending::transitionToState(State new_state) {
|
||||
state_ = new_state;
|
||||
state_time_ = 0.0F;
|
||||
|
||||
// Al cambiar a una escena, resetear la cortinilla de salida y el contador de fade
|
||||
if (new_state != State::WARMING_UP && new_state != State::ENDING) {
|
||||
fadeout_time_ = 0.0F;
|
||||
scene_cover_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
// Lógica de fade común a los estados SCENE_N
|
||||
void Ending::handleSceneFadeout(float scene_duration, float delta_time) {
|
||||
if (state_time_ >= scene_duration - FADEOUT_START_OFFSET) {
|
||||
fadeout_time_ += delta_time;
|
||||
if (!scene_cover_) {
|
||||
scene_cover_ = std::make_unique<PixelReveal>(Options::game.width, Options::game.height, COVER_PIXELS_PER_SECOND, STEP_DURATION, COVER_STEPS, true);
|
||||
}
|
||||
scene_cover_->update(fadeout_time_);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la máquina de estados
|
||||
void Ending::updateState(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
|
||||
switch (state_) {
|
||||
case State::WARMING_UP:
|
||||
if (state_time_ >= WARMUP_DURATION) {
|
||||
transitionToState(State::SCENE_0);
|
||||
current_scene_ = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case State::SCENE_0:
|
||||
checkChangeScene();
|
||||
handleSceneFadeout(SCENE_0_DURATION, delta_time);
|
||||
break;
|
||||
|
||||
case State::SCENE_1:
|
||||
checkChangeScene();
|
||||
handleSceneFadeout(SCENE_1_DURATION, delta_time);
|
||||
break;
|
||||
|
||||
case State::SCENE_2:
|
||||
checkChangeScene();
|
||||
handleSceneFadeout(SCENE_2_DURATION, delta_time);
|
||||
break;
|
||||
|
||||
case State::SCENE_3:
|
||||
checkChangeScene();
|
||||
handleSceneFadeout(SCENE_3_DURATION, delta_time);
|
||||
break;
|
||||
|
||||
case State::SCENE_4:
|
||||
checkChangeScene();
|
||||
handleSceneFadeout(SCENE_4_DURATION, delta_time);
|
||||
break;
|
||||
|
||||
case State::ENDING:
|
||||
// Esperar ENDING_DURATION y luego transicionar a ENDING2
|
||||
if (state_time_ >= ENDING_DURATION) {
|
||||
SceneManager::current = SceneManager::Scene::ENDING2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa los textos
|
||||
void Ending::iniTexts() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Vector con los textos (traducidos según el idioma activo)
|
||||
std::vector<TextAndPosition> texts;
|
||||
auto* loc = Locale::get();
|
||||
|
||||
// Escena #0
|
||||
texts.push_back({.caption = loc->get("ending.t0"), .pos = 32});
|
||||
texts.push_back({.caption = loc->get("ending.t1"), .pos = 42});
|
||||
texts.push_back({.caption = loc->get("ending.t2"), .pos = 142});
|
||||
texts.push_back({.caption = loc->get("ending.t3"), .pos = 152});
|
||||
|
||||
// Escena #1
|
||||
texts.push_back({.caption = loc->get("ending.t4"), .pos = 1});
|
||||
texts.push_back({.caption = loc->get("ending.t5"), .pos = 11});
|
||||
texts.push_back({.caption = loc->get("ending.t6"), .pos = 21});
|
||||
|
||||
texts.push_back({.caption = loc->get("ending.t7"), .pos = 161});
|
||||
texts.push_back({.caption = loc->get("ending.t8"), .pos = 171});
|
||||
|
||||
texts.push_back({.caption = loc->get("ending.t9"), .pos = 181});
|
||||
|
||||
// Escena #2
|
||||
texts.push_back({.caption = loc->get("ending.t10"), .pos = 19});
|
||||
texts.push_back({.caption = loc->get("ending.t11"), .pos = 29});
|
||||
|
||||
// Escena #3
|
||||
texts.push_back({.caption = loc->get("ending.t12"), .pos = 36});
|
||||
texts.push_back({.caption = loc->get("ending.t13"), .pos = 46});
|
||||
|
||||
// Escena #4
|
||||
texts.push_back({.caption = loc->get("ending.t14"), .pos = 36});
|
||||
texts.push_back({.caption = loc->get("ending.t15"), .pos = 46});
|
||||
texts.push_back({.caption = loc->get("ending.t16"), .pos = 158});
|
||||
|
||||
// Crea los sprites
|
||||
sprite_texts_.clear();
|
||||
|
||||
for (const auto& txt : texts) {
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
|
||||
const float WIDTH = text->length(txt.caption, 1) + 2 + 2;
|
||||
const float HEIGHT = text->getCharacterSize() + 2 + 2;
|
||||
auto text_color = static_cast<Uint8>(PaletteColor::WHITE);
|
||||
auto shadow_color = static_cast<Uint8>(PaletteColor::BLACK);
|
||||
|
||||
EndingSurface st;
|
||||
|
||||
// Crea la textura
|
||||
st.image_surface = std::make_shared<Surface>(WIDTH, HEIGHT);
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(st.image_surface);
|
||||
text->writeDX(Text::STROKE_FLAG, 2, 2, txt.caption, 1, text_color, 2, shadow_color);
|
||||
|
||||
// Crea el sprite
|
||||
st.image_sprite = std::make_shared<Sprite>(st.image_surface, 0, 0, st.image_surface->getWidth(), st.image_surface->getHeight());
|
||||
st.pos_x = static_cast<int>((Options::game.width - st.image_surface->getWidth()) / 2);
|
||||
st.pos_y = txt.pos;
|
||||
st.image_sprite->setPosition(st.pos_x, st.pos_y);
|
||||
|
||||
// Crea el efecto de revelado pixel a pixel
|
||||
st.pixel_reveal = std::make_unique<PixelReveal>(static_cast<int>(WIDTH), static_cast<int>(HEIGHT), TEXT_PIXELS_PER_SECOND, STEP_DURATION, REVEAL_STEPS);
|
||||
|
||||
sprite_texts_.push_back(std::move(st));
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa las imagenes
|
||||
void Ending::iniPics() {
|
||||
// Vector con las rutas y la posición
|
||||
std::vector<TextAndPosition> pics;
|
||||
|
||||
pics.push_back({.caption = "ending1.gif", .pos = 48});
|
||||
pics.push_back({.caption = "ending2.gif", .pos = 26});
|
||||
pics.push_back({.caption = "ending3.gif", .pos = 29});
|
||||
pics.push_back({.caption = "ending4.gif", .pos = 63});
|
||||
pics.push_back({.caption = "ending5.gif", .pos = 53});
|
||||
|
||||
// Crea los sprites
|
||||
sprite_pics_.clear();
|
||||
|
||||
for (const auto& pic : pics) {
|
||||
EndingSurface sp;
|
||||
|
||||
// Crea la texture
|
||||
sp.image_surface = Resource::Cache::get()->getSurface(pic.caption);
|
||||
sp.image_surface->setTransparentColor();
|
||||
const float WIDTH = sp.image_surface->getWidth();
|
||||
const float HEIGHT = sp.image_surface->getHeight();
|
||||
|
||||
// Crea el sprite
|
||||
sp.pos_x = static_cast<int>((Options::game.width - WIDTH) / 2);
|
||||
sp.pos_y = pic.pos;
|
||||
sp.image_sprite = std::make_shared<Sprite>(sp.image_surface, 0, 0, WIDTH, HEIGHT);
|
||||
sp.image_sprite->setPosition(sp.pos_x, sp.pos_y);
|
||||
|
||||
// Crea el efecto de revelado pixel a pixel
|
||||
sp.pixel_reveal = std::make_unique<PixelReveal>(static_cast<int>(WIDTH), static_cast<int>(HEIGHT), IMAGE_PIXELS_PER_SECOND, STEP_DURATION, REVEAL_STEPS);
|
||||
|
||||
sprite_pics_.push_back(std::move(sp));
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa las escenas
|
||||
void Ending::iniScenes() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Variable para los tiempos
|
||||
int trigger;
|
||||
constexpr int LAPSE = 80;
|
||||
|
||||
// Crea el contenedor
|
||||
SceneData sc;
|
||||
|
||||
// Inicializa el vector
|
||||
scenes_.clear();
|
||||
|
||||
// Crea la escena #0
|
||||
sc.counter_end = 1000;
|
||||
sc.picture_index = 0;
|
||||
sc.text_index.clear();
|
||||
trigger = 85 * 2;
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 0, .trigger = trigger});
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 1, .trigger = trigger});
|
||||
trigger += LAPSE * 3;
|
||||
sc.text_index.push_back({.index = 2, .trigger = trigger});
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 3, .trigger = trigger});
|
||||
scenes_.push_back(sc);
|
||||
|
||||
// Crea la escena #1
|
||||
sc.counter_end = 1400;
|
||||
sc.picture_index = 1;
|
||||
sc.text_index.clear();
|
||||
trigger = 140 * 2;
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 4, .trigger = trigger});
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 5, .trigger = trigger});
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 6, .trigger = trigger});
|
||||
trigger += LAPSE * 3;
|
||||
sc.text_index.push_back({.index = 7, .trigger = trigger});
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 8, .trigger = trigger});
|
||||
trigger += LAPSE * 3;
|
||||
sc.text_index.push_back({.index = 9, .trigger = trigger});
|
||||
scenes_.push_back(sc);
|
||||
|
||||
// Crea la escena #2
|
||||
sc.counter_end = 1000;
|
||||
sc.picture_index = 2;
|
||||
sc.text_index.clear();
|
||||
trigger = 148 / 2;
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 10, .trigger = trigger});
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 11, .trigger = trigger});
|
||||
scenes_.push_back(sc);
|
||||
|
||||
// Crea la escena #3
|
||||
sc.counter_end = 800;
|
||||
sc.picture_index = 3;
|
||||
sc.text_index.clear();
|
||||
trigger = 87 / 2;
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 12, .trigger = trigger});
|
||||
trigger += LAPSE / 2;
|
||||
sc.text_index.push_back({.index = 13, .trigger = trigger});
|
||||
scenes_.push_back(sc);
|
||||
|
||||
// Crea la escena #4
|
||||
sc.counter_end = 1000;
|
||||
sc.picture_index = 4;
|
||||
sc.text_index.clear();
|
||||
trigger = 91 * 2;
|
||||
trigger += LAPSE;
|
||||
sc.text_index.push_back({.index = 14, .trigger = trigger});
|
||||
trigger += LAPSE * 2;
|
||||
sc.text_index.push_back({.index = 15, .trigger = trigger});
|
||||
trigger += LAPSE * 3;
|
||||
sc.text_index.push_back({.index = 16, .trigger = trigger});
|
||||
scenes_.push_back(sc);
|
||||
}
|
||||
|
||||
// Bucle principal
|
||||
void Ending::run() {
|
||||
Audio::get()->playMusic("574070_KUVO_Farewell_to_school.ogg");
|
||||
|
||||
while (SceneManager::current == SceneManager::Scene::ENDING) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
|
||||
// Actualiza las cortinillas de los elementos
|
||||
void Ending::updateSpriteCovers() {
|
||||
// Skip durante WARMING_UP
|
||||
if (state_ == State::WARMING_UP) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Actualiza el revelado de los textos
|
||||
for (const auto& ti : scenes_.at(current_scene_).text_index) {
|
||||
const float TRIGGER_TIME = static_cast<float>(ti.trigger) / 60.0F;
|
||||
|
||||
if (state_time_ > TRIGGER_TIME) {
|
||||
const float TIME_SINCE_TRIGGER = state_time_ - TRIGGER_TIME;
|
||||
sprite_texts_.at(ti.index).pixel_reveal->update(TIME_SINCE_TRIGGER);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el revelado de la imagen (desde el inicio de la escena)
|
||||
sprite_pics_.at(current_scene_).pixel_reveal->update(state_time_);
|
||||
}
|
||||
|
||||
// Comprueba si se ha de cambiar de escena
|
||||
void Ending::checkChangeScene() {
|
||||
// Obtener duración de la escena actual
|
||||
float current_duration = 0.0F;
|
||||
State next_state = State::ENDING;
|
||||
|
||||
switch (state_) {
|
||||
case State::SCENE_0:
|
||||
current_duration = SCENE_0_DURATION;
|
||||
next_state = State::SCENE_1;
|
||||
break;
|
||||
case State::SCENE_1:
|
||||
current_duration = SCENE_1_DURATION;
|
||||
next_state = State::SCENE_2;
|
||||
break;
|
||||
case State::SCENE_2:
|
||||
current_duration = SCENE_2_DURATION;
|
||||
next_state = State::SCENE_3;
|
||||
break;
|
||||
case State::SCENE_3:
|
||||
current_duration = SCENE_3_DURATION;
|
||||
next_state = State::SCENE_4;
|
||||
break;
|
||||
case State::SCENE_4:
|
||||
current_duration = SCENE_4_DURATION;
|
||||
next_state = State::ENDING;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// Comprobar si ha pasado la duración de la escena
|
||||
if (state_time_ >= current_duration) {
|
||||
if (next_state == State::ENDING) {
|
||||
// Transición al estado ENDING con fade de audio
|
||||
transitionToState(State::ENDING);
|
||||
Audio::get()->fadeOutMusic(MUSIC_FADE_DURATION);
|
||||
} else {
|
||||
// Transición a la siguiente escena
|
||||
current_scene_++;
|
||||
transitionToState(next_state);
|
||||
}
|
||||
}
|
||||
}
|
||||
106
source/game/scenes/ending.hpp
Normal file
106
source/game/scenes/ending.hpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
class Sprite; // lines 8-8
|
||||
class Surface; // lines 9-9
|
||||
class PixelReveal;
|
||||
class DeltaTimer;
|
||||
|
||||
class Ending {
|
||||
public:
|
||||
// --- Constructor y Destructor ---
|
||||
Ending();
|
||||
~Ending(); // NOLINT(modernize-use-equals-default, performance-trivially-destructible) -- defined in .cpp for unique_ptr with forward declarations
|
||||
|
||||
// --- Bucle principal ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Enumeraciones ---
|
||||
enum class State {
|
||||
WARMING_UP,
|
||||
SCENE_0,
|
||||
SCENE_1,
|
||||
SCENE_2,
|
||||
SCENE_3,
|
||||
SCENE_4,
|
||||
ENDING
|
||||
};
|
||||
|
||||
// --- Estructuras ---
|
||||
struct EndingSurface {
|
||||
std::shared_ptr<Surface> image_surface; // Surface a mostrar
|
||||
std::shared_ptr<Sprite> image_sprite; // SSprite para mostrar la textura
|
||||
std::unique_ptr<PixelReveal> pixel_reveal; // Efecto de revelado pixel a pixel
|
||||
int pos_x{0}; // Posición X de renderizado
|
||||
int pos_y{0}; // Posición Y de renderizado
|
||||
};
|
||||
|
||||
struct TextAndPosition {
|
||||
std::string caption; // Texto
|
||||
int pos{0}; // Posición
|
||||
};
|
||||
|
||||
struct TextIndex {
|
||||
int index{0}; // Índice del texto
|
||||
int trigger{0}; // Disparador temporal
|
||||
};
|
||||
|
||||
struct SceneData {
|
||||
std::vector<TextIndex> text_index; // Índices del vector de textos a mostrar y su disparador
|
||||
int picture_index{0}; // Índice del vector de imágenes a mostrar
|
||||
int counter_end{0}; // Valor del contador en el que finaliza la escena
|
||||
};
|
||||
|
||||
// --- Constantes de tiempo (basado en 60 FPS) ---
|
||||
static constexpr float WARMUP_DURATION = 3.333F; // 200 frames @ 60fps
|
||||
static constexpr float SCENE_0_DURATION = 16.667F; // 1000 frames @ 60fps
|
||||
static constexpr float SCENE_1_DURATION = 23.333F; // 1400 frames @ 60fps
|
||||
static constexpr float SCENE_2_DURATION = 16.667F; // 1000 frames @ 60fps
|
||||
static constexpr float SCENE_3_DURATION = 13.333F; // 800 frames @ 60fps
|
||||
static constexpr float SCENE_4_DURATION = 16.667F; // 1000 frames @ 60fps
|
||||
static constexpr float TEXT_PIXELS_PER_SECOND = 30.0F; // Filas de texto reveladas por segundo
|
||||
static constexpr float IMAGE_PIXELS_PER_SECOND = 60.0F; // Filas de imagen reveladas por segundo
|
||||
static constexpr float STEP_DURATION = 2.0F / 60.0F; // Segundos por paso de revelado (2 frames @ 60fps)
|
||||
static constexpr int REVEAL_STEPS = 4; // Pasos de revelado por fila
|
||||
static constexpr float TEXT_LAPSE = 1.333F; // 80 frames @ 60fps
|
||||
static constexpr float FADEOUT_START_OFFSET = 1.667F; // Inicio cortinilla 100 frames antes del fin
|
||||
static constexpr float COVER_PIXELS_PER_SECOND = 120.0F; // Filas cubiertas por segundo
|
||||
static constexpr int COVER_STEPS = 4; // Pasos por fila
|
||||
static constexpr float ENDING_DURATION = 2.0F; // Duración del estado ENDING (2 segundos)
|
||||
static constexpr int MUSIC_FADE_DURATION = 1800; // Fade de audio en milisegundos (1.8 segundos)
|
||||
|
||||
// --- Métodos ---
|
||||
void update(); // Actualiza el objeto
|
||||
void render(); // Dibuja el final en pantalla
|
||||
static void handleEvents(); // Comprueba el manejador de eventos
|
||||
static void handleInput(); // Comprueba las entradas
|
||||
void iniTexts(); // Inicializa los textos
|
||||
void iniPics(); // Inicializa las imágenes
|
||||
void iniScenes(); // Inicializa las escenas
|
||||
void updateState(float delta_time); // Actualiza la máquina de estados
|
||||
void handleSceneFadeout(float scene_duration, float delta_time); // Lógica de fade común a los estados SCENE_N
|
||||
void transitionToState(State new_state); // Transición entre estados
|
||||
void updateSpriteCovers(); // Actualiza las cortinillas de los elementos
|
||||
void checkChangeScene(); // Comprueba si se ha de cambiar de escena
|
||||
void updateMusicVolume() const; // Actualiza el volumen de la música
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Objetos y punteros a recursos
|
||||
std::unique_ptr<PixelReveal> scene_cover_; // Cortinilla de salida (negro sobre la escena)
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para time-based update
|
||||
std::vector<EndingSurface> sprite_texts_; // Vector con los sprites de texto con su cortinilla
|
||||
std::vector<EndingSurface> sprite_pics_; // Vector con los sprites de imágenes con su cortinilla
|
||||
std::vector<SceneData> scenes_; // Vector con los textos e imágenes de cada escena
|
||||
|
||||
// Variables de estado
|
||||
State state_{State::WARMING_UP}; // Estado actual
|
||||
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
|
||||
float total_time_{0.0F}; // Tiempo total acumulado desde el inicio
|
||||
float fadeout_time_{0.0F}; // Tiempo acumulado para la cortinilla de salida
|
||||
int current_scene_{0}; // Escena actual (0-4)
|
||||
};
|
||||
505
source/game/scenes/ending2.cpp
Normal file
505
source/game/scenes/ending2.cpp
Normal file
@@ -0,0 +1,505 @@
|
||||
#include "game/scenes/ending2.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // Para max, replace
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/global_inputs.hpp" // Para check
|
||||
#include "core/input/input.hpp" // Para Input
|
||||
#include "core/locale/locale.hpp" // Para Locale
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/dissolve_sprite.hpp" // Para SurfaceDissolveSprite
|
||||
#include "core/rendering/sprite/moving_sprite.hpp" // Para SMovingSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/options.hpp" // Para Options, options, OptionsGame, Sectio...
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "utils/defines.hpp" // Para GameCanvas::CENTER_X, GameCanvas::CENTER_Y
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/utils.hpp" // Para PaletteColor, stringToColor
|
||||
|
||||
// Constructor
|
||||
Ending2::Ending2()
|
||||
: delta_timer_(std::make_unique<DeltaTimer>()),
|
||||
state_{.state = EndingState::PRE_CREDITS, .duration = STATE_PRE_CREDITS_DURATION} {
|
||||
// Establece la escena
|
||||
SceneManager::current = SceneManager::Scene::ENDING2;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
|
||||
for (const auto& color : COLORS) {
|
||||
colors_.push_back(stringToColor(color));
|
||||
}
|
||||
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
|
||||
iniSpriteList(); // Inicializa la lista de sprites
|
||||
loadSprites(); // Carga todos los sprites desde una lista
|
||||
placeSprites(); // Coloca los sprites en su sito
|
||||
createSpriteTexts(); // Crea los sprites con las texturas con los textos
|
||||
createTexts(); // Crea los sprites con las texturas con los textos del final
|
||||
}
|
||||
|
||||
// Actualiza el objeto
|
||||
void Ending2::update() {
|
||||
const float DELTA_TIME = delta_timer_->tick();
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
updateState(DELTA_TIME); // Actualiza el estado
|
||||
|
||||
switch (state_.state) {
|
||||
case EndingState::CREDITS:
|
||||
// Actualiza los sprites, los textos y los textos del final
|
||||
updateSprites(DELTA_TIME);
|
||||
updateTextSprites(DELTA_TIME);
|
||||
updateTexts(DELTA_TIME);
|
||||
break;
|
||||
|
||||
case EndingState::FADING:
|
||||
// Actualiza el fade final
|
||||
updateFinalFade();
|
||||
break;
|
||||
|
||||
default:
|
||||
// No hacer nada si el estado no corresponde a un caso manejado
|
||||
break;
|
||||
}
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
}
|
||||
|
||||
// Dibuja el final en pantalla
|
||||
void Ending2::render() {
|
||||
// Prepara para empezar a dibujar en la surface de juego
|
||||
Screen::get()->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
// Dibuja los sprites
|
||||
renderSprites();
|
||||
|
||||
// Dibuja los sprites con el texto
|
||||
renderSpriteTexts();
|
||||
|
||||
// Dibuja los sprites con el texto del final
|
||||
renderTexts();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Ending2::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Ending2::handleInput() {
|
||||
Input::get()->update();
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Bucle principal
|
||||
void Ending2::run() {
|
||||
Audio::get()->playMusic("574071_EA_DTV.ogg");
|
||||
|
||||
while (SceneManager::current == SceneManager::Scene::ENDING2) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
|
||||
// Actualiza el estado
|
||||
void Ending2::updateState(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
|
||||
switch (state_.state) {
|
||||
case EndingState::PRE_CREDITS:
|
||||
if (state_time_ >= STATE_PRE_CREDITS_DURATION) {
|
||||
transitionToState(EndingState::CREDITS);
|
||||
}
|
||||
break;
|
||||
|
||||
case EndingState::CREDITS:
|
||||
if (texts_.back()->getPosY() <= GameCanvas::CENTER_Y) {
|
||||
transitionToState(EndingState::POST_CREDITS);
|
||||
}
|
||||
break;
|
||||
|
||||
case EndingState::POST_CREDITS:
|
||||
if (state_time_ >= STATE_POST_CREDITS_DURATION) {
|
||||
transitionToState(EndingState::FADING);
|
||||
}
|
||||
break;
|
||||
|
||||
case EndingState::FADING:
|
||||
if (state_time_ >= STATE_FADE_DURATION) {
|
||||
SceneManager::current = SceneManager::Scene::LOGO;
|
||||
SceneManager::options = SceneManager::Options::LOGO_TO_TITLE;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Transición entre estados
|
||||
void Ending2::transitionToState(EndingState new_state) {
|
||||
state_.state = new_state;
|
||||
state_time_ = 0.0F;
|
||||
|
||||
// Actualizar duración según el nuevo estado
|
||||
switch (new_state) {
|
||||
case EndingState::PRE_CREDITS:
|
||||
state_.duration = STATE_PRE_CREDITS_DURATION;
|
||||
break;
|
||||
case EndingState::POST_CREDITS:
|
||||
state_.duration = STATE_POST_CREDITS_DURATION;
|
||||
break;
|
||||
case EndingState::FADING:
|
||||
state_.duration = STATE_FADE_DURATION;
|
||||
// Al entrar en FADING, iniciar fade de audio
|
||||
Audio::get()->fadeOutMusic(MUSIC_FADE_DURATION);
|
||||
break;
|
||||
case EndingState::CREDITS:
|
||||
state_.duration = 0.0F; // CREDITS no tiene duración fija, termina cuando el último texto llega al centro
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa la lista de sprites
|
||||
void Ending2::iniSpriteList() {
|
||||
// Reinicia el vector
|
||||
sprite_list_.clear();
|
||||
|
||||
// Añade los valores
|
||||
sprite_list_.emplace_back("bin");
|
||||
sprite_list_.emplace_back("floppy");
|
||||
sprite_list_.emplace_back("bird");
|
||||
sprite_list_.emplace_back("chip");
|
||||
sprite_list_.emplace_back("jeannine");
|
||||
sprite_list_.emplace_back("spark");
|
||||
sprite_list_.emplace_back("code");
|
||||
sprite_list_.emplace_back("paco");
|
||||
sprite_list_.emplace_back("elsa");
|
||||
sprite_list_.emplace_back("z80");
|
||||
|
||||
sprite_list_.emplace_back("bell");
|
||||
sprite_list_.emplace_back("dong");
|
||||
|
||||
sprite_list_.emplace_back("amstrad_cs");
|
||||
sprite_list_.emplace_back("breakout");
|
||||
|
||||
sprite_list_.emplace_back("flying_arounder");
|
||||
sprite_list_.emplace_back("stopped_arounder");
|
||||
sprite_list_.emplace_back("walking_arounder");
|
||||
sprite_list_.emplace_back("arounders_door");
|
||||
sprite_list_.emplace_back("arounders_machine");
|
||||
|
||||
sprite_list_.emplace_back("abad");
|
||||
sprite_list_.emplace_back("abad_bell");
|
||||
sprite_list_.emplace_back("lord_abad");
|
||||
|
||||
sprite_list_.emplace_back("bat");
|
||||
sprite_list_.emplace_back("batman_bell");
|
||||
sprite_list_.emplace_back("batman_fire");
|
||||
sprite_list_.emplace_back("batman");
|
||||
|
||||
sprite_list_.emplace_back("demon");
|
||||
sprite_list_.emplace_back("heavy");
|
||||
sprite_list_.emplace_back("dimallas");
|
||||
sprite_list_.emplace_back("guitar");
|
||||
|
||||
sprite_list_.emplace_back("jailbattle_alien");
|
||||
sprite_list_.emplace_back("jailbattle_human");
|
||||
|
||||
sprite_list_.emplace_back("jailer1");
|
||||
sprite_list_.emplace_back("jailer2");
|
||||
sprite_list_.emplace_back("jailer3");
|
||||
sprite_list_.emplace_back("bry");
|
||||
sprite_list_.emplace_back("upv_student");
|
||||
|
||||
sprite_list_.emplace_back("lamp");
|
||||
sprite_list_.emplace_back("robot");
|
||||
sprite_list_.emplace_back("congo");
|
||||
sprite_list_.emplace_back("crosshair");
|
||||
sprite_list_.emplace_back("tree_thing");
|
||||
|
||||
sprite_list_.emplace_back("matatunos");
|
||||
sprite_list_.emplace_back("tuno");
|
||||
|
||||
sprite_list_.emplace_back("mummy");
|
||||
sprite_list_.emplace_back("sam");
|
||||
|
||||
sprite_list_.emplace_back("qvoid");
|
||||
sprite_list_.emplace_back("sigmasua");
|
||||
|
||||
sprite_list_.emplace_back("tv_panel");
|
||||
sprite_list_.emplace_back("tv");
|
||||
|
||||
sprite_list_.emplace_back("spider");
|
||||
sprite_list_.emplace_back("shock");
|
||||
sprite_list_.emplace_back("wave");
|
||||
|
||||
sprite_list_.emplace_back("player");
|
||||
}
|
||||
|
||||
// Carga todos los sprites desde una lista
|
||||
void Ending2::loadSprites() {
|
||||
// Inicializa variables
|
||||
sprite_max_width_ = 0;
|
||||
sprite_max_height_ = 0;
|
||||
|
||||
// Carga los sprites
|
||||
for (const auto& file : sprite_list_) {
|
||||
const auto& animation_data = Resource::Cache::get()->getAnimationData(file + ".yaml");
|
||||
sprites_.emplace_back(std::make_shared<DissolveSprite>(animation_data));
|
||||
sprites_.back()->setColorReplace(1, static_cast<Uint8>(PaletteColor::RED));
|
||||
sprites_.back()->setProgress(1.0F); // comença invisible
|
||||
sprite_max_width_ = std::max(sprites_.back()->getWidth(), sprite_max_width_);
|
||||
sprite_max_height_ = std::max(sprites_.back()->getHeight(), sprite_max_height_);
|
||||
}
|
||||
|
||||
// El último sprite (player) va en blanco, no en rojo
|
||||
sprites_.back()->setColorReplace(1, static_cast<Uint8>(PaletteColor::WHITE));
|
||||
}
|
||||
|
||||
// Actualiza los sprites
|
||||
void Ending2::updateSprites(float delta) {
|
||||
for (const auto& sprite : sprites_) {
|
||||
sprite->update(delta);
|
||||
|
||||
const float Y = sprite->getPosY();
|
||||
const float H = sprite->getHeight();
|
||||
const auto CANVAS_H = Options::game.height;
|
||||
|
||||
// Checkpoint inferior: sprite entra per baix → generar de dalt a baix
|
||||
if (Y > static_cast<float>(ENTRY_EXIT_PADDING) && Y <= CANVAS_H - H - ENTRY_EXIT_PADDING && sprite->getProgress() >= 1.0F && sprite->isTransitionDone()) {
|
||||
sprite->startGenerate(TRANSITION_DURATION_MS, DissolveDirection::UP);
|
||||
}
|
||||
|
||||
// Checkpoint superior: sprite surt per dalt → dissoldre de dalt a baix
|
||||
if (Y <= static_cast<float>(ENTRY_EXIT_PADDING) && sprite->getProgress() <= 0.0F && sprite->isTransitionDone()) {
|
||||
sprite->startDissolve(TRANSITION_DURATION_MS, DissolveDirection::DOWN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los sprites de texto
|
||||
void Ending2::updateTextSprites(float delta) {
|
||||
for (const auto& sprite : sprite_texts_) {
|
||||
sprite->update(delta);
|
||||
|
||||
const float Y = sprite->getPosY();
|
||||
const float H = sprite->getHeight();
|
||||
const auto CANVAS_H = Options::game.height;
|
||||
|
||||
if (Y > static_cast<float>(ENTRY_EXIT_PADDING) && Y <= CANVAS_H - H - ENTRY_EXIT_PADDING && sprite->getProgress() >= 1.0F && sprite->isTransitionDone()) {
|
||||
sprite->startGenerate(TRANSITION_DURATION_MS, DissolveDirection::UP);
|
||||
}
|
||||
|
||||
if (Y <= static_cast<float>(ENTRY_EXIT_PADDING) && sprite->getProgress() <= 0.0F && sprite->isTransitionDone()) {
|
||||
sprite->startDissolve(TRANSITION_DURATION_MS, DissolveDirection::DOWN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los sprites de texto del final
|
||||
void Ending2::updateTexts(float delta) {
|
||||
for (const auto& sprite : texts_) {
|
||||
sprite->update(delta);
|
||||
|
||||
const float Y = sprite->getPosY();
|
||||
const float H = sprite->getHeight();
|
||||
const auto CANVAS_H = Options::game.height;
|
||||
|
||||
// Checkpoint inferior: text entra per baix → generar de dalt a baix
|
||||
if (Y > static_cast<float>(ENTRY_EXIT_PADDING) && Y <= CANVAS_H - H - ENTRY_EXIT_PADDING && sprite->getProgress() >= 1.0F && sprite->isTransitionDone()) {
|
||||
sprite->startGenerate(TRANSITION_DURATION_MS, DissolveDirection::UP);
|
||||
}
|
||||
|
||||
// Checkpoint superior: text surt per dalt → dissoldre de dalt a baix
|
||||
if (Y <= static_cast<float>(ENTRY_EXIT_PADDING) && sprite->getProgress() <= 0.0F && sprite->isTransitionDone()) {
|
||||
sprite->startDissolve(TRANSITION_DURATION_MS, DissolveDirection::DOWN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja los sprites
|
||||
void Ending2::renderSprites() {
|
||||
for (const auto& sprite : sprites_) {
|
||||
const bool A = sprite->getRect().y + sprite->getRect().h > 0;
|
||||
const bool B = sprite->getRect().y < Options::game.height;
|
||||
if (A && B) {
|
||||
sprite->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja los sprites con el texto
|
||||
void Ending2::renderSpriteTexts() {
|
||||
for (const auto& sprite : sprite_texts_) {
|
||||
const bool A = sprite->getRect().y + sprite->getRect().h > 0;
|
||||
const bool B = sprite->getRect().y < Options::game.height;
|
||||
if (A && B) {
|
||||
sprite->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja los sprites con el texto del final
|
||||
void Ending2::renderTexts() {
|
||||
for (const auto& sprite : texts_) {
|
||||
const bool A = sprite->getRect().y + sprite->getRect().h > 0;
|
||||
const bool B = sprite->getRect().y < Options::game.height;
|
||||
if (A && B) {
|
||||
sprite->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Coloca los sprites en su sito
|
||||
void Ending2::placeSprites() const {
|
||||
for (int i = 0; i < static_cast<int>(sprites_.size()); ++i) {
|
||||
const float X = i % 2 == 0 ? FIRST_COL : SECOND_COL;
|
||||
const float Y = ((i / 1) * (sprite_max_height_ + DIST_SPRITE_TEXT + Resource::Cache::get()->getText("smb2")->getCharacterSize() + DIST_SPRITE_SPRITE)) + Options::game.height + INITIAL_Y_OFFSET;
|
||||
const float W = sprites_.at(i)->getWidth();
|
||||
const float H = sprites_.at(i)->getHeight();
|
||||
const float DX = -(W / 2);
|
||||
const float DY = sprite_max_height_ - H;
|
||||
|
||||
sprites_.at(i)->setPos({.x = X + DX, .y = Y + DY, .w = W, .h = H});
|
||||
sprites_.at(i)->setVelY(SPRITE_DESP_SPEED);
|
||||
}
|
||||
|
||||
// Recoloca el sprite del jugador, que es el último de la lista
|
||||
const float X = (Options::game.width - sprites_.back()->getWidth()) / 2;
|
||||
const float Y = sprites_.back()->getPosY() + (sprite_max_height_ * 2);
|
||||
sprites_.back()->setPos(X, Y);
|
||||
sprites_.back()->setCurrentAnimation("default");
|
||||
}
|
||||
|
||||
// Crea los sprites con las texturas con los textos
|
||||
void Ending2::createSpriteTexts() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Crea los sprites de texto a partir de la lista
|
||||
for (size_t i = 0; i < sprite_list_.size(); ++i) {
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
|
||||
// Procesa y ajusta el texto del sprite actual
|
||||
std::string txt = sprite_list_[i];
|
||||
std::ranges::replace(txt, '_', ' '); // Reemplaza '_' por ' '
|
||||
if (txt == "player") {
|
||||
txt = Locale::get()->get("ending2.jaildoctor"); // NOLINT(readability-static-accessed-through-instance) Reemplaza "player" por nombre localizado
|
||||
}
|
||||
|
||||
// Calcula las dimensiones del texto
|
||||
const float W = text->length(txt, 1);
|
||||
const float H = text->getCharacterSize();
|
||||
|
||||
// Determina la columna y la posición X del texto
|
||||
const float X = (i == sprite_list_.size() - 1)
|
||||
? (GameCanvas::CENTER_X - (W / 2))
|
||||
: ((i % 2 == 0 ? FIRST_COL : SECOND_COL) - (W / 2));
|
||||
|
||||
// Calcula la posición Y del texto en base a la posición y altura del sprite
|
||||
const float Y = sprites_.at(i)->getPosY() + sprites_.at(i)->getHeight() + DIST_SPRITE_TEXT;
|
||||
|
||||
// Crea la surface
|
||||
auto surface = std::make_shared<Surface>(W, H);
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface);
|
||||
text->write(0, 0, txt);
|
||||
|
||||
// Crea el sprite
|
||||
SDL_FRect pos = {.x = X, .y = Y, .w = W, .h = H};
|
||||
sprite_texts_.emplace_back(std::make_shared<DissolveSprite>(surface, pos));
|
||||
sprite_texts_.back()->setColorReplace(1, static_cast<Uint8>(PaletteColor::WHITE));
|
||||
sprite_texts_.back()->setProgress(1.0F); // comença invisible
|
||||
sprite_texts_.back()->setVelY(SPRITE_DESP_SPEED);
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
}
|
||||
|
||||
// Crea los sprites con las texturas con los textos del final
|
||||
void Ending2::createTexts() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Crea los primeros textos
|
||||
std::vector<std::string> list;
|
||||
list.emplace_back(Locale::get()->get("ending2.starring"));
|
||||
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
|
||||
// Crea los sprites de texto a partir de la lista
|
||||
for (size_t i = 0; i < list.size(); ++i) {
|
||||
// Calcula constantes
|
||||
const float W = text->length(list[i], 1);
|
||||
const float H = text->getCharacterSize();
|
||||
const float X = GameCanvas::CENTER_X;
|
||||
const float DX = -(W / 2);
|
||||
const float Y = Options::game.height + (text->getCharacterSize() * (i * TEXT_SPACING_MULTIPLIER));
|
||||
|
||||
// Crea la surface
|
||||
auto surface = std::make_shared<Surface>(W, H);
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface);
|
||||
text->write(0, 0, list[i]);
|
||||
|
||||
// Crea el sprite
|
||||
SDL_FRect pos = {.x = X + DX, .y = Y, .w = W, .h = H};
|
||||
texts_.emplace_back(std::make_shared<DissolveSprite>(surface, pos));
|
||||
texts_.back()->setProgress(1.0F); // comença invisible
|
||||
texts_.back()->setVelY(SPRITE_DESP_SPEED);
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
|
||||
// Crea los últimos textos
|
||||
// El primer texto va a continuación del ultimo spriteText
|
||||
const int START = sprite_texts_.back()->getPosY() + (text->getCharacterSize() * 15);
|
||||
list.clear();
|
||||
list.emplace_back(Locale::get()->get("ending2.thank_you"));
|
||||
list.emplace_back(Locale::get()->get("ending2.for_playing"));
|
||||
|
||||
// Crea los sprites de texto a partir de la lista
|
||||
for (size_t i = 0; i < list.size(); ++i) {
|
||||
// Calcula constantes
|
||||
const float W = text->length(list[i], 1);
|
||||
const float H = text->getCharacterSize();
|
||||
const float X = GameCanvas::CENTER_X;
|
||||
const float DX = -(W / 2);
|
||||
const float Y = START + (text->getCharacterSize() * (i * TEXT_SPACING_MULTIPLIER));
|
||||
|
||||
// Crea la surface
|
||||
auto surface = std::make_shared<Surface>(W, H);
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface);
|
||||
text->write(0, 0, list[i]);
|
||||
|
||||
// Crea el sprite
|
||||
SDL_FRect pos = {.x = X + DX, .y = Y, .w = W, .h = H};
|
||||
texts_.emplace_back(std::make_shared<DissolveSprite>(surface, pos));
|
||||
texts_.back()->setProgress(1.0F); // comença invisible
|
||||
texts_.back()->setVelY(SPRITE_DESP_SPEED);
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el fade final
|
||||
void Ending2::updateFinalFade() {
|
||||
for (const auto& sprite : texts_) {
|
||||
sprite->getSurface()->fadeSubPalette(0);
|
||||
}
|
||||
}
|
||||
94
source/game/scenes/ending2.hpp
Normal file
94
source/game/scenes/ending2.hpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "core/rendering/sprite/dissolve_sprite.hpp" // Para SurfaceDissolveSprite
|
||||
#include "utils/defines.hpp" // Para GameCanvas::WIDTH, GameCanvas::FIRST_QUAR...
|
||||
|
||||
class MovingSprite;
|
||||
class DeltaTimer;
|
||||
|
||||
class Ending2 {
|
||||
public:
|
||||
// --- Constructor y Destructor ---
|
||||
Ending2();
|
||||
~Ending2() = default;
|
||||
|
||||
// --- Bucle principal ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Enumeraciones ---
|
||||
enum class EndingState : int {
|
||||
PRE_CREDITS, // Estado previo a los créditos
|
||||
CREDITS, // Estado de los créditos
|
||||
POST_CREDITS, // Estado posterior a los créditos
|
||||
FADING, // Estado de fundido de los textos a negro
|
||||
};
|
||||
|
||||
// --- Estructuras ---
|
||||
struct State {
|
||||
EndingState state{EndingState::PRE_CREDITS}; // Estado actual
|
||||
float duration{0.0F}; // Duración en segundos para el estado actual
|
||||
};
|
||||
|
||||
// --- Constantes ---
|
||||
static constexpr int FIRST_COL = GameCanvas::FIRST_QUARTER_X + (GameCanvas::WIDTH / 16); // Primera columna por donde desfilan los sprites
|
||||
static constexpr int SECOND_COL = GameCanvas::THIRD_QUARTER_X - (GameCanvas::WIDTH / 16); // Segunda columna por donde desfilan los sprites
|
||||
static constexpr int DIST_SPRITE_TEXT = 8; // Distancia entre el sprite y el texto que lo acompaña
|
||||
static constexpr int DIST_SPRITE_SPRITE = 0; // Distancia entre dos sprites de la misma columna
|
||||
static constexpr int INITIAL_Y_OFFSET = 40; // Offset inicial en Y para posicionar sprites
|
||||
static constexpr int SCREEN_MESH_HEIGHT = 8; // Altura de la malla superior/inferior de la pantalla
|
||||
static constexpr int FADE_H = 24; // Alçada de la zona de dissolució als cantons (files)
|
||||
static constexpr float TRANSITION_DURATION_MS = 500.0F; // ms per canviar d'estat (generar o dissoldre)
|
||||
static constexpr int ENTRY_EXIT_PADDING = 2; // px de padding als bordes per activar dissolució/generació
|
||||
static constexpr int TEXT_SPACING_MULTIPLIER = 2; // Multiplicador para espaciado entre líneas de texto
|
||||
|
||||
// Constantes de tiempo (basadas en tiempo real, no en frames)
|
||||
static constexpr float SPRITE_DESP_SPEED = -12.0F; // Velocidad de desplazamiento en pixels/segundo (era -0.2 px/frame @ 60fps)
|
||||
static constexpr float STATE_PRE_CREDITS_DURATION = 3.0F; // Duración del estado previo a créditos en segundos
|
||||
static constexpr float STATE_POST_CREDITS_DURATION = 5.0F; // Duración del estado posterior a créditos en segundos
|
||||
static constexpr float STATE_FADE_DURATION = 5.0F; // Duración del fade final en segundos
|
||||
static constexpr int MUSIC_FADE_DURATION = 3000; // Duración del fade de música en milisegundos (para Audio API)
|
||||
|
||||
// --- Métodos ---
|
||||
void update(); // Actualiza el objeto
|
||||
void render(); // Dibuja el final en pantalla
|
||||
static void handleEvents(); // Comprueba el manejador de eventos
|
||||
static void handleInput(); // Comprueba las entradas
|
||||
void updateState(float delta_time); // Actualiza el estado
|
||||
void transitionToState(EndingState new_state); // Transición entre estados
|
||||
void iniSpriteList(); // Inicializa la lista de sprites
|
||||
void loadSprites(); // Carga todos los sprites desde una lista
|
||||
void updateSprites(float delta); // Actualiza los sprites
|
||||
void updateTextSprites(float delta); // Actualiza los sprites de texto
|
||||
void updateTexts(float delta); // Actualiza los sprites de texto del final
|
||||
void renderSprites(); // Dibuja los sprites
|
||||
void renderSpriteTexts(); // Dibuja los sprites con el texto
|
||||
void renderTexts(); // Dibuja los sprites con el texto del final
|
||||
void placeSprites() const; // Coloca los sprites en su sitio
|
||||
void createSpriteTexts(); // Crea los sprites con las texturas con los textos
|
||||
void createTexts(); // Crea los sprites con las texturas con los textos del final
|
||||
void updateFinalFade(); // Actualiza el fade final
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Objetos y punteros a recursos
|
||||
std::vector<std::shared_ptr<DissolveSprite>> sprites_; // Vector con todos los sprites a dibujar
|
||||
std::vector<std::shared_ptr<DissolveSprite>> sprite_texts_; // Vector con los sprites de texto de los sprites
|
||||
std::vector<std::shared_ptr<DissolveSprite>> texts_; // Vector con los sprites de texto
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para time-based update
|
||||
|
||||
// Variables de estado
|
||||
State state_; // Controla el estado de la clase
|
||||
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
|
||||
|
||||
// Variables auxiliares
|
||||
std::vector<std::string> sprite_list_; // Lista con todos los sprites a dibujar
|
||||
std::vector<Uint8> colors_; // Vector con los colores para el fade
|
||||
float sprite_max_width_{0.0F}; // El valor de ancho del sprite más ancho
|
||||
float sprite_max_height_{0.0F}; // El valor de alto del sprite más alto
|
||||
};
|
||||
1034
source/game/scenes/game.cpp
Normal file
1034
source/game/scenes/game.cpp
Normal file
File diff suppressed because it is too large
Load Diff
133
source/game/scenes/game.hpp
Normal file
133
source/game/scenes/game.hpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <initializer_list> // Para initializer_list
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "game/entities/player.hpp" // Para PlayerSpawn
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
class Room; // lines 12-12
|
||||
class RoomTracker; // lines 13-13
|
||||
class Scoreboard; // lines 14-14
|
||||
class Surface;
|
||||
|
||||
class Game {
|
||||
public:
|
||||
// --- Estructuras ---
|
||||
enum class Mode {
|
||||
DEMO,
|
||||
GAME
|
||||
};
|
||||
|
||||
enum class State {
|
||||
PLAYING, // Normal gameplay
|
||||
BLACK_SCREEN, // Black screen after death (0.30s)
|
||||
GAME_OVER, // Intermediate state before changing scene
|
||||
FADE_TO_ENDING, // Fade out transition
|
||||
POST_FADE_ENDING, // Black screen delay before ending
|
||||
};
|
||||
|
||||
// --- Constructor y Destructor ---
|
||||
explicit Game(Mode mode);
|
||||
~Game();
|
||||
|
||||
// --- Bucle para el juego ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Constantes de tiempo ---
|
||||
static constexpr float BLACK_SCREEN_DURATION = 0.30F; // Duración de la pantalla negra en segundos (20 frames a 66.67fps)
|
||||
static constexpr float GAME_OVER_THRESHOLD = 0.255F; // Tiempo antes del game over en segundos (17 frames a 66.67fps)
|
||||
static constexpr float DEMO_ROOM_DURATION = 6.0F; // Duración de cada habitación en modo demo en segundos (400 frames)
|
||||
static constexpr float JAIL_RESTORE_INTERVAL = 1.5F; // Intervalo de restauración de vidas en la Jail en segundos (100 frames)
|
||||
static constexpr float FADE_STEP_INTERVAL = 0.05F; // Intervalo entre pasos de fade en segundos
|
||||
static constexpr float POST_FADE_DELAY = 2.0F; // Duración de la pantalla negra después del fade
|
||||
|
||||
// --- Estructuras ---
|
||||
struct DemoData {
|
||||
float time_accumulator{0.0F}; // Acumulador de tiempo para el modo demo
|
||||
int room_index{0}; // Índice para el vector de habitaciones
|
||||
std::vector<std::string> rooms; // Listado con los mapas de la demo
|
||||
};
|
||||
|
||||
// --- Métodos ---
|
||||
void update(); // Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||
void render(); // Pinta los objetos en pantalla
|
||||
void handleEvents(); // Comprueba los eventos de la cola
|
||||
void renderRoomName(); // Escribe el nombre de la pantalla
|
||||
void transitionToState(State new_state); // Cambia al estado especificado y resetea los timers
|
||||
void updatePlaying(float delta_time); // Actualiza el juego en estado PLAYING
|
||||
void updateBlackScreen(float delta_time); // Actualiza el juego en estado BLACK_SCREEN
|
||||
void updateGameOver(float delta_time); // Actualiza el juego en estado GAME_OVER
|
||||
void updateFadeToEnding(float delta_time); // Actualiza el juego en estado FADE_TO_ENDING
|
||||
void updatePostFadeEnding(float delta_time); // Actualiza el juego en estado POST_FADE_ENDING
|
||||
void renderPlaying(); // Renderiza el juego en estado PLAYING (directo a pantalla)
|
||||
static void renderBlackScreen(); // Renderiza el juego en estado BLACK_SCREEN (pantalla negra)
|
||||
static void renderGameOver(); // Renderiza el juego en estado GAME_OVER (pantalla negra)
|
||||
void renderFadeToEnding(); // Renderiza el juego en estado FADE_TO_ENDING (via backbuffer)
|
||||
static void renderPostFadeEnding(); // Renderiza el juego en estado POST_FADE_ENDING (pantalla negra)
|
||||
auto changeRoom(const std::string& room_path) -> bool; // Cambia de habitación
|
||||
void handleInput(); // Comprueba el teclado
|
||||
void checkPlayerIsOnBorder(); // Comprueba si el jugador esta en el borde de la pantalla y actua
|
||||
auto checkPlayerAndEnemies() -> bool; // Comprueba las colisiones del jugador con los enemigos
|
||||
void checkPlayerAndItems(); // Comprueba las colisiones del jugador con los objetos
|
||||
void checkIfPlayerIsAlive(); // Comprueba si el jugador esta vivo
|
||||
void killPlayer(); // Mata al jugador
|
||||
void setScoreBoardColor(); // Pone el color del marcador en función del color del borde de la habitación
|
||||
auto checkEndGame() -> bool; // Comprueba si ha finalizado el juego
|
||||
static auto getTotalItems() -> int; // Obtiene la cantidad total de items que hay en el mapeado del juego
|
||||
void togglePause(); // Pone el juego en pausa
|
||||
void checkRestoringJail(float delta_time); // Da vidas al jugador cuando está en la Jail
|
||||
void fillRoomNameTexture(); // Pone el nombre de la habitación en la textura
|
||||
void checkSomeCheevos(); // Comprueba algunos logros
|
||||
void checkEndGameCheevos(); // Comprueba los logros de completar el juego
|
||||
void initPlayer(const Player::SpawnData& spawn_point, std::shared_ptr<Room> room); // Inicializa al jugador
|
||||
void createRoomNameTexture(); // Crea la textura para poner el nombre de la habitación
|
||||
void keepMusicPlaying(); // Hace sonar la música
|
||||
void demoInit(); // DEMO MODE: Inicializa las variables para el modo demo
|
||||
void demoCheckRoomChange(float delta_time); // DEMO MODE: Comprueba si se ha de cambiar de habitación
|
||||
#ifdef _DEBUG
|
||||
static void renderDebugInfo(); // Pone la información de debug en pantalla
|
||||
void handleDebugEvents(const SDL_Event& event); // Comprueba los eventos
|
||||
void handleDebugMouseDrag(float delta_time); // Maneja el arrastre del jugador con el ratón (debug)
|
||||
#endif
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Objetos y punteros a recursos
|
||||
std::shared_ptr<Scoreboard::Data> scoreboard_data_; // Estructura con los datos del marcador
|
||||
std::shared_ptr<Scoreboard> scoreboard_; // Objeto encargado de gestionar el marcador
|
||||
std::shared_ptr<RoomTracker> room_tracker_; // Lleva el control de las habitaciones visitadas
|
||||
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
|
||||
std::shared_ptr<Player> player_; // Objeto con el jugador
|
||||
std::shared_ptr<Surface> room_name_surface_; // Textura para escribir el nombre de la habitación
|
||||
std::shared_ptr<Surface> game_backbuffer_surface_; // Backbuffer para efectos de fade
|
||||
|
||||
// Variables de estado del juego
|
||||
Mode mode_; // Modo del juego
|
||||
State state_{State::PLAYING}; // Estado actual de la escena
|
||||
DeltaTimer delta_timer_; // Timer para calcular delta time
|
||||
std::string current_room_; // Fichero de la habitación actual
|
||||
Player::SpawnData spawn_data_; // Lugar de la habitación donde aparece el jugador
|
||||
int total_items_; // Cantidad total de items que hay en el mapeado del juego
|
||||
bool paused_{false}; // Indica si el juego se encuentra en pausa
|
||||
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
|
||||
float fade_accumulator_{0.0F}; // Acumulador de tiempo para el fade
|
||||
|
||||
// Variables de demo mode
|
||||
DemoData demo_; // Variables para el modo demo
|
||||
|
||||
// Variables de efectos visuales
|
||||
SDL_FRect room_name_rect_; // Rectangulo donde pintar la textura con el nombre de la habitación
|
||||
float jail_restore_time_{0.0F}; // Tiempo acumulado para restauración de vidas en la Jail
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Variables de debug para arrastre con ratón
|
||||
bool debug_dragging_player_{false}; // Indica si estamos arrastrando al jugador con el ratón
|
||||
float debug_drag_speed_{0.0F}; // Velocidad actual del arrastre (ease-in)
|
||||
// Estado previo de invencibilidad antes de entrar en modo debug
|
||||
bool invincible_before_debug_{false};
|
||||
#endif
|
||||
};
|
||||
210
source/game/scenes/game_over.cpp
Normal file
210
source/game/scenes/game_over.cpp
Normal file
@@ -0,0 +1,210 @@
|
||||
#include "game/scenes/game_over.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // Para min, max
|
||||
#include <string> // Para basic_string, operator+, to_string
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/global_inputs.hpp" // Para check
|
||||
#include "core/input/input.hpp" // Para Input
|
||||
#include "core/locale/locale.hpp" // Para Locale
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "core/rendering/text.hpp" // Para Text::CENTER_FLAG, Text::COLOR_FLAG, Text
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/options.hpp" // Para Options, options, OptionsStats, Secti...
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "utils/defines.hpp" // Para GameCanvas::CENTER_X
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/utils.hpp" // Para PaletteColor, stringToColor
|
||||
|
||||
// Constructor
|
||||
GameOver::GameOver()
|
||||
: player_sprite_(std::make_shared<AnimatedSprite>(Resource::Cache::get()->getAnimationData("player_game_over.yaml"))),
|
||||
tv_sprite_(std::make_shared<AnimatedSprite>(Resource::Cache::get()->getAnimationData("tv.yaml"))),
|
||||
delta_timer_(std::make_shared<DeltaTimer>()) {
|
||||
SceneManager::current = SceneManager::Scene::GAME_OVER;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
|
||||
// Inicializa las posiciones de los sprites usando las constantes
|
||||
player_sprite_->setPosX(GameCanvas::CENTER_X + PLAYER_X_OFFSET);
|
||||
player_sprite_->setPosY(TEXT_Y + SPRITE_Y_OFFSET);
|
||||
tv_sprite_->setPosX(GameCanvas::CENTER_X - tv_sprite_->getWidth() - TV_X_OFFSET);
|
||||
tv_sprite_->setPosY(TEXT_Y + SPRITE_Y_OFFSET);
|
||||
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
// Inicializa el vector de colores (de brillante a oscuro para fade)
|
||||
const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
|
||||
for (const auto& color : COLORS) {
|
||||
colors_.push_back(stringToColor(color));
|
||||
}
|
||||
color_ = colors_.back(); // Empieza en black
|
||||
}
|
||||
|
||||
// Actualiza el objeto
|
||||
void GameOver::update() {
|
||||
const float DELTA_TIME = delta_timer_->tick();
|
||||
elapsed_time_ += DELTA_TIME;
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
updateState(); // Actualiza el estado de la escena
|
||||
updateColor(); // Actualiza el color usado para renderizar los textos e imagenes
|
||||
player_sprite_->update(DELTA_TIME); // Actualiza el sprite
|
||||
tv_sprite_->update(DELTA_TIME); // Actualiza el sprite
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
}
|
||||
|
||||
// Dibuja el final en pantalla
|
||||
void GameOver::render() {
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
|
||||
// Escribe el texto de GAME OVER
|
||||
auto* loc = Locale::get();
|
||||
text->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, TEXT_Y, loc->get("game_over.title"), 1, color_); // NOLINT(readability-static-accessed-through-instance)
|
||||
|
||||
// Dibuja los sprites (ya posicionados en el constructor, solo ajustamos Y)
|
||||
player_sprite_->setPosY(TEXT_Y + SPRITE_Y_OFFSET);
|
||||
tv_sprite_->setPosY(TEXT_Y + SPRITE_Y_OFFSET);
|
||||
renderSprites();
|
||||
|
||||
// Escribe el texto con las habitaciones y los items
|
||||
const std::string ITEMS_TEXT = std::to_string(Options::stats.items / 100) + std::to_string((Options::stats.items % 100) / 10) + std::to_string(Options::stats.items % 10);
|
||||
const std::string ROOMS_TEXT = std::to_string(Options::stats.rooms / 100) + std::to_string((Options::stats.rooms % 100) / 10) + std::to_string(Options::stats.rooms % 10);
|
||||
text->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, TEXT_Y + ITEMS_Y_OFFSET, loc->get("game_over.items") + ITEMS_TEXT, 1, color_); // NOLINT(readability-static-accessed-through-instance)
|
||||
text->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, TEXT_Y + ROOMS_Y_OFFSET, loc->get("game_over.rooms") + ROOMS_TEXT, 1, color_); // NOLINT(readability-static-accessed-through-instance)
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void GameOver::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void GameOver::handleInput() {
|
||||
Input::get()->update();
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Bucle principal
|
||||
void GameOver::run() {
|
||||
while (SceneManager::current == SceneManager::Scene::GAME_OVER) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el color usado para renderizar los textos e imagenes
|
||||
void GameOver::updateColor() {
|
||||
// Calcula el color basado en el estado actual
|
||||
switch (state_) {
|
||||
case State::WAITING:
|
||||
// Durante la espera, mantener en black
|
||||
color_ = colors_.back(); // black
|
||||
break;
|
||||
|
||||
case State::FADE_IN: {
|
||||
// Fade in: de black (último color) a white (primer color)
|
||||
// Progreso: 0.0 (black) -> 1.0 (white)
|
||||
const float PROGRESS = std::min(elapsed_time_ / FADE_IN_DURATION, 1.0F);
|
||||
const int INDEX = (colors_.size() - 1) - static_cast<int>((colors_.size() - 1) * PROGRESS);
|
||||
color_ = colors_[std::clamp(INDEX, 0, static_cast<int>(colors_.size() - 1))];
|
||||
break;
|
||||
}
|
||||
|
||||
case State::DISPLAY:
|
||||
// Durante display, mantener el color más brillante
|
||||
color_ = colors_[0]; // white
|
||||
break;
|
||||
|
||||
case State::FADE_OUT: {
|
||||
// Fade out: de white (primer color) a black (último color)
|
||||
// Progreso: 0.0 (white) -> 1.0 (black)
|
||||
const float PROGRESS = std::min(elapsed_time_ / FADE_OUT_DURATION, 1.0F);
|
||||
const int INDEX = static_cast<int>((colors_.size() - 1) * PROGRESS);
|
||||
color_ = colors_[std::clamp(INDEX, 0, static_cast<int>(colors_.size() - 1))];
|
||||
break;
|
||||
}
|
||||
|
||||
case State::ENDING:
|
||||
case State::TRANSITION:
|
||||
// Al final, mantener en black
|
||||
color_ = colors_.back(); // black
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja los sprites
|
||||
void GameOver::renderSprites() {
|
||||
player_sprite_->render(1, color_);
|
||||
tv_sprite_->render(1, color_);
|
||||
}
|
||||
|
||||
// Actualiza el estado de la escena y gestiona transiciones
|
||||
void GameOver::updateState() {
|
||||
// Máquina de estados basada en tiempo transcurrido
|
||||
switch (state_) {
|
||||
case State::WAITING:
|
||||
// Espera inicial antes de empezar
|
||||
if (elapsed_time_ >= WAITING_DURATION) {
|
||||
state_ = State::FADE_IN;
|
||||
elapsed_time_ = 0.0F;
|
||||
// Hace sonar la música cuando termina la espera
|
||||
Audio::get()->playMusic("574070_KUVO_Farewell_to_school.ogg", 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::FADE_IN:
|
||||
// Fade in de colores desde black
|
||||
if (elapsed_time_ >= FADE_IN_DURATION) {
|
||||
state_ = State::DISPLAY;
|
||||
elapsed_time_ = 0.0F;
|
||||
}
|
||||
break;
|
||||
|
||||
case State::DISPLAY:
|
||||
// Mostrando contenido con color brillante
|
||||
if (elapsed_time_ >= DISPLAY_DURATION) {
|
||||
state_ = State::FADE_OUT;
|
||||
elapsed_time_ = 0.0F;
|
||||
}
|
||||
break;
|
||||
|
||||
case State::FADE_OUT:
|
||||
// Fade out hacia black
|
||||
if (elapsed_time_ >= FADE_OUT_DURATION) {
|
||||
state_ = State::ENDING;
|
||||
elapsed_time_ = 0.0F;
|
||||
}
|
||||
break;
|
||||
|
||||
case State::ENDING:
|
||||
// Pantalla en negro antes de salir
|
||||
if (elapsed_time_ >= ENDING_DURATION) {
|
||||
state_ = State::TRANSITION;
|
||||
elapsed_time_ = 0.0F;
|
||||
}
|
||||
break;
|
||||
|
||||
case State::TRANSITION:
|
||||
// Transición a la escena de logo
|
||||
SceneManager::current = SceneManager::Scene::LOGO;
|
||||
SceneManager::options = SceneManager::Options::LOGO_TO_TITLE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
67
source/game/scenes/game_over.hpp
Normal file
67
source/game/scenes/game_over.hpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <vector> // Para vector
|
||||
class AnimatedSprite; // lines 7-7
|
||||
class DeltaTimer; // Forward declaration
|
||||
|
||||
class GameOver {
|
||||
public:
|
||||
// Constructor y Destructor
|
||||
GameOver();
|
||||
~GameOver() = default;
|
||||
|
||||
// Bucle principal
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Enumeraciones ---
|
||||
enum class State {
|
||||
WAITING, // Espera inicial antes de empezar
|
||||
FADE_IN, // Fade in de colores desde black
|
||||
DISPLAY, // Mostrando contenido con color brillante
|
||||
FADE_OUT, // Fade out hacia black
|
||||
ENDING, // Pantalla en negro antes de salir
|
||||
TRANSITION // Cambio a logo
|
||||
};
|
||||
|
||||
// --- Constantes de duración (segundos) ---
|
||||
static constexpr float WAITING_DURATION = 0.8F; // Espera inicial
|
||||
static constexpr float FADE_IN_DURATION = 0.32F; // Duración del fade in
|
||||
static constexpr float DISPLAY_DURATION = 4.64F; // Duración mostrando contenido
|
||||
static constexpr float FADE_OUT_DURATION = 0.32F; // Duración del fade out
|
||||
static constexpr float ENDING_DURATION = 1.12F; // Espera en negro antes de salir
|
||||
|
||||
// --- Constantes de posición ---
|
||||
static constexpr int TEXT_Y = 32; // Posición Y del texto principal
|
||||
static constexpr int SPRITE_Y_OFFSET = 30; // Offset Y para sprites desde TEXT_Y
|
||||
static constexpr int PLAYER_X_OFFSET = 10; // Offset X del jugador desde el centro
|
||||
static constexpr int TV_X_OFFSET = 10; // Offset X del TV desde el centro
|
||||
static constexpr int ITEMS_Y_OFFSET = 80; // Offset Y del texto de items desde TEXT_Y
|
||||
static constexpr int ROOMS_Y_OFFSET = 90; // Offset Y del texto de rooms desde TEXT_Y
|
||||
|
||||
// --- Métodos ---
|
||||
void update(); // Actualiza el objeto
|
||||
void render(); // Dibuja el final en pantalla
|
||||
static void handleEvents(); // Comprueba el manejador de eventos
|
||||
static void handleInput(); // Comprueba las entradas
|
||||
void updateState(); // Actualiza el estado y transiciones
|
||||
void updateColor(); // Actualiza el color usado para renderizar
|
||||
void renderSprites(); // Dibuja los sprites
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Objetos y punteros a recursos
|
||||
std::shared_ptr<AnimatedSprite> player_sprite_; // Sprite con el jugador
|
||||
std::shared_ptr<AnimatedSprite> tv_sprite_; // Sprite con el televisor
|
||||
std::shared_ptr<DeltaTimer> delta_timer_; // Timer para time-based logic
|
||||
|
||||
// Variables de estado de la escena
|
||||
State state_{State::WAITING}; // Estado actual de la escena
|
||||
float elapsed_time_{0.0F}; // Tiempo transcurrido en el estado actual
|
||||
|
||||
// Variables de efectos visuales
|
||||
std::vector<Uint8> colors_; // Vector con los colores para el fade
|
||||
Uint8 color_{0}; // Color actual para texto y sprites
|
||||
};
|
||||
498
source/game/scenes/loading_screen.cpp
Normal file
498
source/game/scenes/loading_screen.cpp
Normal file
@@ -0,0 +1,498 @@
|
||||
#include "game/scenes/loading_screen.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cmath> // Para std::sin
|
||||
#include <cstdlib> // Para rand
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/global_inputs.hpp" // Para check
|
||||
#include "core/input/input.hpp" // Para Input
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/sprite.hpp" // Para SSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/options.hpp" // Para Options, options, SectionState, Options...
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "utils/defines.hpp" // Para GAME_SPEED
|
||||
#include "utils/utils.hpp" // Para stringToColor, PaletteColor
|
||||
|
||||
// Constructor
|
||||
LoadingScreen::LoadingScreen()
|
||||
: mono_loading_screen_surface_(Resource::Cache::get()->getSurface("loading_screen_bn.gif")),
|
||||
color_loading_screen_surface_(Resource::Cache::get()->getSurface("loading_screen_color.gif")),
|
||||
mono_loading_screen_sprite_(std::make_unique<Sprite>(mono_loading_screen_surface_, 0, 0, mono_loading_screen_surface_->getWidth(), mono_loading_screen_surface_->getHeight())),
|
||||
color_loading_screen_sprite_(std::make_unique<Sprite>(color_loading_screen_surface_, 0, 0, color_loading_screen_surface_->getWidth(), color_loading_screen_surface_->getHeight())),
|
||||
program_sprite_(std::make_unique<Sprite>(Resource::Cache::get()->getSurface("program_jaildoc.gif"))),
|
||||
screen_surface_(std::make_shared<Surface>(Options::game.width, Options::game.height)),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()) {
|
||||
// Configura la superficie donde se van a pintar los sprites
|
||||
screen_surface_->clear(static_cast<Uint8>(PaletteColor::WHITE));
|
||||
|
||||
// Inicializa variables
|
||||
SceneManager::current = SceneManager::Scene::LOADING_SCREEN;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
program_sprite_->setPosition(0.0F, 8.0F);
|
||||
|
||||
// Inicializa el array de índices de líneas
|
||||
initLineIndexArray();
|
||||
|
||||
// Cambia el color del borde
|
||||
Screen::get()->setBorderColor(stringToColor("white"));
|
||||
transitionToState(State::SILENT1);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
LoadingScreen::~LoadingScreen() {
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void LoadingScreen::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void LoadingScreen::handleInput() {
|
||||
Input::get()->update();
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Inicializa el array de índices de líneas (imita el direccionamiento de memoria del Spectrum)
|
||||
void LoadingScreen::initLineIndexArray() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (int i = 0; i < MONO_TOTAL_LINES; ++i) {
|
||||
if (i < 64) { // Primer bloque de 2K
|
||||
line_index_[i] = ((i % 8) * 8) + (i / 8);
|
||||
} else if (i < 128) { // Segundo bloque de 2K
|
||||
line_index_[i] = 64 + ((i % 8) * 8) + ((i - 64) / 8);
|
||||
} else { // Tercer bloque de 2K
|
||||
line_index_[i] = 128 + ((i % 8) * 8) + ((i - 128) / 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Transiciona a un nuevo estado
|
||||
void LoadingScreen::transitionToState(State new_state) {
|
||||
state_ = new_state;
|
||||
state_time_ = 0.0F;
|
||||
|
||||
// Acciones específicas al entrar en cada estado
|
||||
switch (new_state) {
|
||||
case State::SILENT1:
|
||||
case State::SILENT2:
|
||||
current_border_type_ = Border::RED;
|
||||
Audio::get()->stopMusic();
|
||||
break;
|
||||
|
||||
case State::HEADER1:
|
||||
case State::HEADER2:
|
||||
current_border_type_ = Border::RED_AND_CYAN;
|
||||
Audio::get()->playMusic("574071_EA_DTV.ogg", 0);
|
||||
break;
|
||||
|
||||
case State::DATA1:
|
||||
printProgramName();
|
||||
current_border_type_ = Border::YELLOW_AND_BLUE;
|
||||
Audio::get()->playMusic("574071_EA_DTV.ogg", 0);
|
||||
break;
|
||||
case State::DATA2:
|
||||
current_border_type_ = Border::YELLOW_AND_BLUE;
|
||||
Audio::get()->playMusic("574070_KUVO_Farewell_to_school.ogg", 0);
|
||||
break;
|
||||
case State::LOADING_MONO:
|
||||
current_border_type_ = Border::YELLOW_AND_BLUE;
|
||||
Audio::get()->playMusic("574071_EA_DTV.ogg", 0);
|
||||
last_mono_step_ = -1; // Resetear contador de pasos mono
|
||||
break;
|
||||
|
||||
case State::LOADING_COLOR:
|
||||
current_border_type_ = Border::YELLOW_AND_BLUE;
|
||||
Audio::get()->playMusic("574070_KUVO_Farewell_to_school.ogg", 0);
|
||||
last_color_block_ = -1; // Resetear contador de bloques color
|
||||
break;
|
||||
|
||||
case State::COMPLETE:
|
||||
current_border_type_ = Border::BLACK;
|
||||
// Transicionar a la pantalla de título
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
SceneManager::options = SceneManager::Options::TITLE_WITH_LOADING_SCREEN;
|
||||
Audio::get()->stopMusic();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado actual
|
||||
void LoadingScreen::updateState(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
|
||||
// Transiciones automáticas por tiempo para los estados iniciales
|
||||
// LOADING_MONO y LOADING_COLOR transicionan en sus propias funciones
|
||||
switch (state_) {
|
||||
case State::SILENT1:
|
||||
if (state_time_ >= SILENT1_DURATION) {
|
||||
transitionToState(State::HEADER1);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::HEADER1:
|
||||
if (state_time_ >= HEADER1_DURATION) {
|
||||
transitionToState(State::DATA1);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::DATA1:
|
||||
if (state_time_ >= DATA1_DURATION) {
|
||||
transitionToState(State::SILENT2);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::SILENT2:
|
||||
if (state_time_ >= SILENT2_DURATION) {
|
||||
transitionToState(State::HEADER2);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::HEADER2:
|
||||
if (state_time_ >= HEADER2_DURATION) {
|
||||
transitionToState(State::LOADING_MONO);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::DATA2:
|
||||
if (state_time_ >= DATA2_DURATION) {
|
||||
transitionToState(State::COMPLETE);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::LOADING_MONO:
|
||||
case State::LOADING_COLOR:
|
||||
case State::COMPLETE:
|
||||
// Estos estados se gestionan en updateMonoLoad/updateColorLoad
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona la carga monocromática (time-based simplificado)
|
||||
void LoadingScreen::updateMonoLoad(float delta_time) {
|
||||
// Calcular progreso lineal (0.0 - 1.0)
|
||||
float progress = state_time_ / LOADING_MONO_DURATION;
|
||||
progress = std::min(progress, 1.0F);
|
||||
|
||||
// Calcular paso total actual (0-959)
|
||||
const int TOTAL_STEPS = MONO_TOTAL_LINES * MONO_STEPS_PER_LINE; // 192 * 5 = 960
|
||||
const int CURRENT_STEP = static_cast<int>(progress * TOTAL_STEPS);
|
||||
|
||||
// Verificar si ha completado todas las líneas
|
||||
if (CURRENT_STEP >= TOTAL_STEPS) {
|
||||
transitionToState(State::LOADING_COLOR);
|
||||
return;
|
||||
}
|
||||
|
||||
// Dibujar todos los pasos intermedios desde el último dibujado
|
||||
const float TEXTURE_WIDTH = mono_loading_screen_surface_->getWidth();
|
||||
const float CLIP_WIDTH = TEXTURE_WIDTH / MONO_STEPS_PER_LINE;
|
||||
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(screen_surface_);
|
||||
|
||||
for (int step = last_mono_step_ + 1; step <= CURRENT_STEP; ++step) {
|
||||
// Calcular línea y sub-paso para este paso
|
||||
const int CURRENT_LINE = step / MONO_STEPS_PER_LINE; // 0-191
|
||||
const int CURRENT_SUBSTEP = step % MONO_STEPS_PER_LINE; // 0-4
|
||||
|
||||
// Saltar si excede el total de líneas
|
||||
if (CURRENT_LINE >= MONO_TOTAL_LINES) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Calcular rectángulo de clip para este paso
|
||||
const float CLIP_X = CURRENT_SUBSTEP * CLIP_WIDTH;
|
||||
|
||||
load_rect_.x = CLIP_X;
|
||||
load_rect_.y = static_cast<float>(line_index_[CURRENT_LINE]);
|
||||
load_rect_.w = CLIP_WIDTH;
|
||||
load_rect_.h = 1.0F;
|
||||
|
||||
// Configurar y dibujar sobre screen_surface_
|
||||
mono_loading_screen_sprite_->setClip(load_rect_);
|
||||
mono_loading_screen_sprite_->setPosition(load_rect_);
|
||||
mono_loading_screen_sprite_->render();
|
||||
}
|
||||
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
|
||||
// Actualizar el último paso dibujado
|
||||
last_mono_step_ = CURRENT_STEP;
|
||||
}
|
||||
|
||||
// Gestiona la carga en color
|
||||
void LoadingScreen::updateColorLoad(float delta_time) {
|
||||
// Calcular progreso lineal (0.0 - 1.0)
|
||||
float progress = state_time_ / LOADING_COLOR_DURATION;
|
||||
progress = std::min(progress, 1.0F);
|
||||
|
||||
// Calcular bloque actual (0-767) - ahora pinta de 1 en 1 en lugar de 2 en 2
|
||||
const int CURRENT_BLOCK = static_cast<int>(progress * COLOR_TOTAL_BLOCKS);
|
||||
|
||||
// Verificar si ha completado todos los bloques
|
||||
if (CURRENT_BLOCK >= COLOR_TOTAL_BLOCKS) {
|
||||
transitionToState(State::DATA2);
|
||||
return;
|
||||
}
|
||||
|
||||
// Dibujar todos los bloques intermedios desde el último dibujado
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(screen_surface_);
|
||||
|
||||
// Iterar desde el último bloque + 1 hasta el bloque actual (de 1 en 1)
|
||||
for (int block = last_color_block_ + 1; block <= CURRENT_BLOCK; ++block) {
|
||||
// Saltar si excede el total de bloques
|
||||
if (block >= COLOR_TOTAL_BLOCKS) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Calcular posición del bloque
|
||||
load_rect_.x = static_cast<float>((block * COLOR_BLOCK_SPACING) % 256);
|
||||
load_rect_.y = static_cast<float>((block / COLOR_BLOCKS_PER_ROW) * COLOR_BLOCK_SPACING);
|
||||
load_rect_.w = static_cast<float>(COLOR_BLOCK_WIDTH);
|
||||
load_rect_.h = static_cast<float>(COLOR_BLOCK_HEIGHT);
|
||||
|
||||
// Configurar y dibujar sobre screen_surface_
|
||||
color_loading_screen_sprite_->setClip(load_rect_);
|
||||
color_loading_screen_sprite_->setPosition(load_rect_);
|
||||
color_loading_screen_sprite_->render();
|
||||
}
|
||||
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
|
||||
// Actualizar el último bloque dibujado
|
||||
last_color_block_ = CURRENT_BLOCK;
|
||||
}
|
||||
|
||||
// Dibuja el efecto de carga amarillo y azul en el borde
|
||||
void LoadingScreen::renderDataBorder() {
|
||||
// Obtiene la Surface del borde
|
||||
auto border = Screen::get()->getBorderSurface();
|
||||
|
||||
// Pinta el borde de color azul
|
||||
border->clear(static_cast<Uint8>(PaletteColor::BLUE));
|
||||
|
||||
// Añade lineas amarillas
|
||||
const auto COLOR = static_cast<Uint8>(PaletteColor::YELLOW);
|
||||
const int WIDTH = Options::game.width + (Options::video.border.width * 2);
|
||||
const int HEIGHT = Options::game.height + (Options::video.border.height * 2);
|
||||
bool draw_enabled = rand() % 2 == 0;
|
||||
|
||||
int row = 0;
|
||||
while (row < HEIGHT) {
|
||||
const int ROW_HEIGHT = (rand() % 4) + 3;
|
||||
if (draw_enabled) {
|
||||
for (int i = row; i < row + ROW_HEIGHT; ++i) {
|
||||
border->drawLine(0, i, WIDTH, i, COLOR);
|
||||
}
|
||||
}
|
||||
row += ROW_HEIGHT;
|
||||
draw_enabled = !draw_enabled;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el efecto de carga rojo y azul en el borde
|
||||
void LoadingScreen::renderHeaderBorder() const {
|
||||
// Obtiene la Surface del borde
|
||||
auto border = Screen::get()->getBorderSurface();
|
||||
|
||||
// Pinta el borde de color azul o rojo
|
||||
border->clear(carrier_.toggle ? static_cast<Uint8>(PaletteColor::CYAN) : static_cast<Uint8>(PaletteColor::RED));
|
||||
|
||||
// Añade lineas rojas o azules
|
||||
const auto COLOR = carrier_.toggle ? static_cast<Uint8>(PaletteColor::RED) : static_cast<Uint8>(PaletteColor::CYAN);
|
||||
const int WIDTH = Options::game.width + (Options::video.border.width * 2);
|
||||
const int HEIGHT = Options::game.height + (Options::video.border.height * 2);
|
||||
|
||||
// Primera linea (con el color y tamaño de la portadora)
|
||||
int row = 0;
|
||||
const int FIRST_ROW_HEIGHT = static_cast<int>(carrier_.offset);
|
||||
for (int i = row; i < row + FIRST_ROW_HEIGHT; ++i) {
|
||||
border->drawLine(0, i, WIDTH, i, COLOR);
|
||||
}
|
||||
row += FIRST_ROW_HEIGHT;
|
||||
|
||||
// Resto de lineas (siguen a la portadora)
|
||||
bool draw_enabled = false;
|
||||
while (row < HEIGHT) {
|
||||
if (draw_enabled) {
|
||||
for (int i = row; i < row + HEADER_DATAROW_HEIGHT; ++i) {
|
||||
border->drawLine(0, i, WIDTH, i, COLOR);
|
||||
}
|
||||
}
|
||||
row += HEADER_DATAROW_HEIGHT;
|
||||
draw_enabled = !draw_enabled;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el borde de color
|
||||
void LoadingScreen::renderColoredBorder(PaletteColor color) {
|
||||
// Obtiene la Surface del borde
|
||||
auto border = Screen::get()->getBorderSurface();
|
||||
|
||||
// Pinta el borde de color azul
|
||||
border->clear(static_cast<Uint8>(color));
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void LoadingScreen::update() {
|
||||
const float DELTA_TIME = delta_timer_->tick();
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones
|
||||
|
||||
// Actualizar la carga según el estado actual
|
||||
switch (state_) {
|
||||
case State::DATA1:
|
||||
case State::DATA2:
|
||||
// Por ahora no hacen nada específico
|
||||
break;
|
||||
case State::SILENT1:
|
||||
case State::SILENT2:
|
||||
updateSilent(DELTA_TIME);
|
||||
break;
|
||||
case State::HEADER1:
|
||||
case State::HEADER2:
|
||||
updateCarrier(DELTA_TIME);
|
||||
break;
|
||||
|
||||
case State::LOADING_MONO:
|
||||
updateMonoLoad(DELTA_TIME);
|
||||
break;
|
||||
|
||||
case State::LOADING_COLOR:
|
||||
updateColorLoad(DELTA_TIME);
|
||||
break;
|
||||
|
||||
case State::COMPLETE:
|
||||
// No hay más actualizaciones
|
||||
break;
|
||||
}
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void LoadingScreen::render() {
|
||||
// Pinta el borde
|
||||
renderBorder();
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(stringToColor("white"));
|
||||
|
||||
// Copia la surface a la surface de Screen
|
||||
screen_surface_->render(0, 0);
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void LoadingScreen::run() {
|
||||
// Ajusta el volumen
|
||||
Audio::get()->setMusicVolume(50);
|
||||
|
||||
// Limpia la pantalla
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearRenderer();
|
||||
Screen::get()->render();
|
||||
|
||||
while (SceneManager::current == SceneManager::Scene::LOADING_SCREEN) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
Audio::get()->setMusicVolume(100);
|
||||
}
|
||||
|
||||
// Pinta el borde
|
||||
void LoadingScreen::renderBorder() {
|
||||
if (Options::video.border.enabled) {
|
||||
// Dibuja el efecto de carga en el borde según el tipo actual
|
||||
switch (current_border_type_) {
|
||||
case Border::YELLOW_AND_BLUE:
|
||||
renderDataBorder();
|
||||
break;
|
||||
case Border::RED_AND_CYAN:
|
||||
renderHeaderBorder();
|
||||
break;
|
||||
case Border::WHITE:
|
||||
renderColoredBorder(PaletteColor::WHITE);
|
||||
break;
|
||||
case Border::BLACK:
|
||||
renderColoredBorder(PaletteColor::BLACK);
|
||||
break;
|
||||
case Border::RED:
|
||||
renderColoredBorder(PaletteColor::RED);
|
||||
break;
|
||||
case Border::CYAN:
|
||||
renderColoredBorder(PaletteColor::CYAN);
|
||||
break;
|
||||
case Border::NONE:
|
||||
// No renderizar borde
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe el nombre del programa
|
||||
void LoadingScreen::printProgramName() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(screen_surface_);
|
||||
program_sprite_->render();
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
}
|
||||
|
||||
// Actualiza la portadora
|
||||
void LoadingScreen::updateCarrier(float delta_time) {
|
||||
constexpr float CARRIER_BASE_SPEED = -250.0F;
|
||||
constexpr float CARRIER_HEIGHT = HEADER_DATAROW_HEIGHT;
|
||||
|
||||
// Oscilación compuesta: mezcla de dos frecuencias para evitar patrón predecible
|
||||
const float MODULATION = std::sin(carrier_.total_time * 1.2F) * std::sin((carrier_.total_time * 0.35F) + 1.0F);
|
||||
const float SPEED = CARRIER_BASE_SPEED * (0.5F + (0.5F * MODULATION)); // rango [-200, 0]
|
||||
|
||||
carrier_.offset += SPEED * delta_time;
|
||||
|
||||
if (carrier_.offset < 0.0F) {
|
||||
carrier_.offset += CARRIER_HEIGHT; // reinicia al rango [0,HEADER_DATAROW_HEIGHT]
|
||||
carrier_.toggle = !carrier_.toggle;
|
||||
}
|
||||
|
||||
carrier_.total_time += delta_time;
|
||||
}
|
||||
|
||||
// Actualiza el ruido durante el tiempo de silencio
|
||||
void LoadingScreen::updateSilent(float delta_time) {
|
||||
constexpr float NOISE_THRESHOLD = 0.35F;
|
||||
|
||||
// Oscilación compuesta para simular picos de ruido
|
||||
const float MODULATION = std::sin(noise_.total_time * 4.2F) * std::sin((noise_.total_time * 1.7F) + 0.5F);
|
||||
noise_.value = std::fabs(MODULATION); // rango [0.0, 1.0]
|
||||
|
||||
// Detecta cruce de umbral solo si venía de abajo
|
||||
if (noise_.value > NOISE_THRESHOLD && !noise_.crossed) {
|
||||
noise_.crossed = true;
|
||||
current_border_type_ = (current_border_type_ == Border::RED) ? Border::CYAN : Border::RED;
|
||||
}
|
||||
|
||||
// Restablece el flag cuando baja del umbral
|
||||
if (noise_.value < NOISE_THRESHOLD) {
|
||||
noise_.crossed = false;
|
||||
}
|
||||
|
||||
noise_.total_time += delta_time;
|
||||
}
|
||||
123
source/game/scenes/loading_screen.hpp
Normal file
123
source/game/scenes/loading_screen.hpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array> // Para std::array
|
||||
#include <memory> // Para shared_ptr
|
||||
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/utils.hpp" // Para PaletteColor
|
||||
class Sprite; // Forward declaration
|
||||
class Surface; // Forward declaration
|
||||
|
||||
class LoadingScreen {
|
||||
public:
|
||||
// --- Constructor y Destructor ---
|
||||
LoadingScreen();
|
||||
~LoadingScreen();
|
||||
|
||||
// --- Bucle principal ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Enumeraciones ---
|
||||
// Estados de la secuencia de carga
|
||||
enum class State {
|
||||
SILENT1, // Pausa inicial antes de empezar
|
||||
HEADER1, // Cabecera
|
||||
DATA1, // Datos
|
||||
SILENT2, // Segunda pausa
|
||||
HEADER2, // Cabecera pantalla
|
||||
LOADING_MONO, // Carga de pantalla monocromática (escaneo de líneas)
|
||||
LOADING_COLOR, // Carga de pantalla en color (bloques)
|
||||
DATA2, // Datos
|
||||
COMPLETE // Carga completa
|
||||
};
|
||||
|
||||
// Tipos de borde para la pantalla de carga
|
||||
enum class Border {
|
||||
NONE,
|
||||
YELLOW_AND_BLUE,
|
||||
RED_AND_CYAN,
|
||||
WHITE,
|
||||
BLACK,
|
||||
RED,
|
||||
CYAN
|
||||
};
|
||||
|
||||
// --- Estructuras ---
|
||||
struct Carrier {
|
||||
float offset{0.0F}; // Offset para la carga de cabeceras
|
||||
bool toggle{false}; // Para cambiar el color inicial
|
||||
float total_time{0.0F}; // Tiempo acumulado para modulación de velocidad
|
||||
};
|
||||
|
||||
struct Noise {
|
||||
float value{0.0F}; // Nivel actual de ruido (0.0 a 1.0)
|
||||
float total_time{0.0F}; // Tiempo acumulado para modulación
|
||||
bool crossed{false}; // Flag para detectar cruce de umbral
|
||||
};
|
||||
|
||||
// --- Constantes de tiempo (en segundos) ---
|
||||
static constexpr float SILENT1_DURATION = 2.0F; // Pausa inicial
|
||||
static constexpr float HEADER1_DURATION = 4.0F; // Cabecera
|
||||
static constexpr float DATA1_DURATION = 0.18F; // Datos
|
||||
static constexpr float SILENT2_DURATION = 1.6F; // Segunda pausa
|
||||
static constexpr float HEADER2_DURATION = 2.0F; // Cabecera pantalla
|
||||
static constexpr float LOADING_MONO_DURATION = 16.0F; // Duración total de la carga monocromática
|
||||
static constexpr float LOADING_COLOR_DURATION = 4.0F; // Duración total de la carga en color
|
||||
static constexpr float DATA2_DURATION = 5.0F; // Datos
|
||||
|
||||
// --- Constantes de geometría ---
|
||||
static constexpr int MONO_TOTAL_LINES = 192; // Total de líneas en carga monocromática
|
||||
static constexpr int MONO_STEPS_PER_LINE = 5; // Pasos de animación por línea
|
||||
static constexpr int COLOR_TOTAL_BLOCKS = 768; // Total de bloques en carga color
|
||||
static constexpr int COLOR_BLOCK_WIDTH = 16; // Ancho del bloque de color
|
||||
static constexpr int COLOR_BLOCK_HEIGHT = 8; // Alto del bloque de color
|
||||
static constexpr int COLOR_BLOCKS_PER_ROW = 32; // Bloques por fila (256 / 8)
|
||||
static constexpr int COLOR_BLOCK_SPACING = 8; // Espaciado entre bloques
|
||||
static constexpr int HEADER_DATAROW_HEIGHT = 9.0F; // Alto de las barras del borde de la carga de las cabeceras
|
||||
|
||||
// --- Métodos ---
|
||||
void update(); // Actualiza las variables
|
||||
void render(); // Dibuja en pantalla
|
||||
static void handleEvents(); // Comprueba el manejador de eventos
|
||||
static void handleInput(); // Comprueba las entradas
|
||||
void updateState(float delta_time); // Actualiza el estado actual
|
||||
void transitionToState(State new_state); // Transiciona a un nuevo estado
|
||||
void updateMonoLoad(float delta_time); // Gestiona la carga monocromática (time-based)
|
||||
void updateColorLoad(float delta_time); // Gestiona la carga en color (time-based)
|
||||
void renderBorder(); // Pinta el borde
|
||||
static void renderDataBorder(); // Dibuja el efecto de carga amarillo y azul en el borde
|
||||
void renderHeaderBorder() const; // Dibuja el efecto de carga rojo y azul en el borde
|
||||
static void renderColoredBorder(PaletteColor color); // Dibuja el borde de color
|
||||
void initLineIndexArray(); // Inicializa el array de índices de líneas
|
||||
void printProgramName(); // Escribe el nombre del programa
|
||||
void updateCarrier(float delta_time); // Actualiza la portadora
|
||||
void updateSilent(float delta_time); // Actualiza el ruido durante el tiempo de silencio
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Objetos y punteros a recursos
|
||||
std::shared_ptr<Surface> mono_loading_screen_surface_; // Surface con la pantalla de carga en blanco y negro
|
||||
std::shared_ptr<Surface> color_loading_screen_surface_; // Surface con la pantalla de carga en color
|
||||
std::unique_ptr<Sprite> mono_loading_screen_sprite_; // SurfaceSprite para manejar la textura mono_loading_screen_surface_
|
||||
std::unique_ptr<Sprite> color_loading_screen_sprite_; // SurfaceSprite para manejar la textura color_loading_screen_surface_
|
||||
std::unique_ptr<Sprite> program_sprite_; // SurfaceSprite para manejar la textura con el nombre del programa
|
||||
std::shared_ptr<Surface> screen_surface_; // Surface para dibujar la pantalla de carga
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para delta time
|
||||
|
||||
// Variables de estado de la secuencia
|
||||
State state_{State::SILENT1}; // Estado actual de la secuencia
|
||||
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
|
||||
Border current_border_type_{Border::NONE}; // Tipo de borde actual
|
||||
|
||||
// Arrays y estructuras auxiliares
|
||||
std::array<int, MONO_TOTAL_LINES> line_index_; // El orden en el que se procesan las 192 líneas de la pantalla de carga
|
||||
SDL_FRect load_rect_{.x = 0.0F, .y = 0.0F, .w = 0.0F, .h = 1.0F}; // Rectángulo para dibujar la pantalla de carga
|
||||
Carrier carrier_; // Estructura para los efectos de la carga de cabeceras
|
||||
Noise noise_; // Variaciones de ruido durante los silencios
|
||||
|
||||
// Variables de seguimiento para evitar saltos de pasos/bloques
|
||||
int last_mono_step_{-1}; // Último paso mono dibujado
|
||||
int last_color_block_{-1}; // Último bloque color dibujado
|
||||
};
|
||||
290
source/game/scenes/logo.cpp
Normal file
290
source/game/scenes/logo.cpp
Normal file
@@ -0,0 +1,290 @@
|
||||
#include "game/scenes/logo.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // Para std::clamp
|
||||
#include <array> // Para std::array
|
||||
#include <random> // Para generador aleatorio
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/global_inputs.hpp" // Para check
|
||||
#include "core/input/input.hpp" // Para Input
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/sprite.hpp" // Para SSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/options.hpp" // Para Options, SectionState, options, Section
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/easing_functions.hpp" // Para funciones de suavizado
|
||||
#include "utils/utils.hpp" // Para PaletteColor
|
||||
|
||||
// Constructor
|
||||
Logo::Logo()
|
||||
: jailgames_surface_(Resource::Cache::get()->getSurface("jailgames.gif")),
|
||||
since_1998_surface_(Resource::Cache::get()->getSurface("since_1998.gif")),
|
||||
since_1998_sprite_(std::make_shared<Sprite>(since_1998_surface_, (256 - since_1998_surface_->getWidth()) / 2, 83 + jailgames_surface_->getHeight() + 5, since_1998_surface_->getWidth(), since_1998_surface_->getHeight())),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()) {
|
||||
// Configura variables
|
||||
since_1998_sprite_->setClip(0, 0, since_1998_surface_->getWidth(), since_1998_surface_->getHeight());
|
||||
since_1998_color_ = static_cast<Uint8>(PaletteColor::BLACK);
|
||||
jailgames_color_ = static_cast<Uint8>(PaletteColor::BRIGHT_WHITE);
|
||||
|
||||
// Inicializa variables
|
||||
SceneManager::current = SceneManager::Scene::LOGO;
|
||||
|
||||
initSprites(); // Crea los sprites de cada linea
|
||||
initColors(); // Inicializa el vector de colores
|
||||
|
||||
// Seleccionar función de easing aleatoria para la animación del logo
|
||||
// Usamos lambdas para funciones con parámetros opcionales
|
||||
static const std::array<EasingFunction, 4> EASING_OPTIONS = {
|
||||
[](float t) -> float { return Easing::backOut(t); }, // Overshoot retro
|
||||
[](float t) -> float { return Easing::elasticOut(t); }, // Rebote múltiple con oscilación
|
||||
Easing::bounceOut, // Rebote físico decreciente
|
||||
Easing::cubicOut // Suavizado sin overshoot (para variedad)
|
||||
};
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<size_t> dist(0, EASING_OPTIONS.size() - 1);
|
||||
easing_function_ = EASING_OPTIONS[dist(gen)];
|
||||
|
||||
// Cambia el color del borde
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Logo::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Logo::handleInput() {
|
||||
Input::get()->update();
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
void Logo::updateJAILGAMES(float delta_time) {
|
||||
// Solo actualizar durante el estado JAILGAMES_SLIDE_IN
|
||||
if (state_ != State::JAILGAMES_SLIDE_IN) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calcular el progreso de la animación (0.0 a 1.0)
|
||||
const float PROGRESS = std::clamp(state_time_ / JAILGAMES_SLIDE_DURATION, 0.0F, 1.0F);
|
||||
|
||||
// Aplicar función de suavizado seleccionada aleatoriamente (permite overshoot para efecto de rebote)
|
||||
// La posición final exacta se garantiza en updateState() antes de transicionar
|
||||
const float EASED_PROGRESS = easing_function_(PROGRESS);
|
||||
|
||||
// Actualizar cada línea del sprite JAILGAMES interpolando con easing
|
||||
for (size_t i = 0; i < jailgames_sprite_.size(); ++i) {
|
||||
// Interpolar entre posición inicial y destino usando el progreso suavizado
|
||||
const auto INITIAL_X = static_cast<float>(jailgames_initial_x_[i]);
|
||||
const auto DEST_X = static_cast<float>(JAILGAMES_DEST_X);
|
||||
const float NEW_X = INITIAL_X + ((DEST_X - INITIAL_X) * EASED_PROGRESS);
|
||||
|
||||
jailgames_sprite_[i]->setX(NEW_X);
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula el índice de color según el progreso (0.0-1.0)
|
||||
auto Logo::getColorIndex(float progress) const -> int { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Asegurar que progress esté en el rango [0.0, 1.0]
|
||||
progress = std::clamp(progress, 0.0F, 1.0F);
|
||||
|
||||
// Mapear el progreso al índice de color (0-7)
|
||||
const int MAX_INDEX = static_cast<int>(color_.size()) - 1;
|
||||
const int INDEX = static_cast<int>(progress * MAX_INDEX);
|
||||
|
||||
return INDEX;
|
||||
}
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
void Logo::updateTextureColors() {
|
||||
switch (state_) {
|
||||
case State::SINCE_1998_FADE_IN: {
|
||||
// Fade-in de "Since 1998" de negro a blanco
|
||||
const float PROGRESS = state_time_ / SINCE_1998_FADE_DURATION;
|
||||
since_1998_color_ = color_[getColorIndex(PROGRESS)];
|
||||
break;
|
||||
}
|
||||
|
||||
case State::DISPLAY: {
|
||||
// Asegurar que ambos logos estén en blanco durante el display
|
||||
jailgames_color_ = color_.back(); // BRIGHT_WHITE
|
||||
since_1998_color_ = color_.back(); // BRIGHT_WHITE
|
||||
break;
|
||||
}
|
||||
|
||||
case State::FADE_OUT: {
|
||||
// Fade-out de ambos logos de blanco a negro
|
||||
const float PROGRESS = 1.0F - (state_time_ / FADE_OUT_DURATION);
|
||||
const int COLOR_INDEX = getColorIndex(PROGRESS);
|
||||
jailgames_color_ = color_[COLOR_INDEX];
|
||||
since_1998_color_ = color_[COLOR_INDEX];
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// En otros estados, mantener los colores actuales
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Transiciona a un nuevo estado
|
||||
void Logo::transitionToState(State new_state) {
|
||||
state_ = new_state;
|
||||
state_time_ = 0.0F;
|
||||
}
|
||||
|
||||
// Actualiza el estado actual
|
||||
void Logo::updateState(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
|
||||
// Gestionar transiciones entre estados basándose en el tiempo
|
||||
switch (state_) {
|
||||
case State::INITIAL:
|
||||
if (state_time_ >= INITIAL_DELAY) {
|
||||
transitionToState(State::JAILGAMES_SLIDE_IN);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::JAILGAMES_SLIDE_IN:
|
||||
if (state_time_ >= JAILGAMES_SLIDE_DURATION) {
|
||||
// Garantizar que todas las líneas estén exactamente en la posición final
|
||||
// antes de transicionar (previene race condition con updateJAILGAMES)
|
||||
for (auto& sprite : jailgames_sprite_) {
|
||||
sprite->setX(JAILGAMES_DEST_X);
|
||||
}
|
||||
transitionToState(State::SINCE_1998_FADE_IN);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::SINCE_1998_FADE_IN:
|
||||
if (state_time_ >= SINCE_1998_FADE_DURATION) {
|
||||
transitionToState(State::DISPLAY);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::DISPLAY:
|
||||
if (state_time_ >= DISPLAY_DURATION) {
|
||||
transitionToState(State::FADE_OUT);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::FADE_OUT:
|
||||
if (state_time_ >= FADE_OUT_DURATION) {
|
||||
transitionToState(State::BLACK_SCREEN);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::BLACK_SCREEN:
|
||||
if (state_time_ >= BLACK_SCREEN_DURATION) {
|
||||
transitionToState(State::END);
|
||||
endSection();
|
||||
}
|
||||
break;
|
||||
|
||||
case State::END:
|
||||
// Estado final, no hacer nada
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Logo::update() {
|
||||
const float DELTA_TIME = delta_timer_->tick();
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones
|
||||
updateJAILGAMES(DELTA_TIME); // Gestiona el logo de JAILGAME
|
||||
updateTextureColors(); // Gestiona el color de las texturas
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Logo::render() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
// Dibuja los objetos
|
||||
for (const auto& sprite : jailgames_sprite_) {
|
||||
sprite->render(1, jailgames_color_);
|
||||
}
|
||||
since_1998_sprite_->render(1, since_1998_color_);
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Logo::run() {
|
||||
while (SceneManager::current == SceneManager::Scene::LOGO) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Termina la sección
|
||||
void Logo::endSection() {
|
||||
switch (SceneManager::options) {
|
||||
case SceneManager::Options::LOGO_TO_TITLE:
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
break;
|
||||
|
||||
case SceneManager::Options::LOGO_TO_LOADING_SCREEN:
|
||||
SceneManager::current = SceneManager::Scene::LOADING_SCREEN;
|
||||
break;
|
||||
|
||||
default:
|
||||
SceneManager::current = SceneManager::Scene::LOADING_SCREEN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa el vector de colores
|
||||
void Logo::initColors() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<Uint8> COLORS = {
|
||||
static_cast<Uint8>(PaletteColor::BLACK),
|
||||
static_cast<Uint8>(PaletteColor::BLUE),
|
||||
static_cast<Uint8>(PaletteColor::RED),
|
||||
static_cast<Uint8>(PaletteColor::MAGENTA),
|
||||
static_cast<Uint8>(PaletteColor::GREEN),
|
||||
static_cast<Uint8>(PaletteColor::CYAN),
|
||||
static_cast<Uint8>(PaletteColor::YELLOW),
|
||||
static_cast<Uint8>(PaletteColor::BRIGHT_WHITE)};
|
||||
for (const auto& color : COLORS) {
|
||||
color_.push_back(color);
|
||||
}
|
||||
}
|
||||
|
||||
// Crea los sprites de cada linea
|
||||
void Logo::initSprites() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
const float WIDTH = jailgames_surface_->getWidth();
|
||||
jailgames_initial_x_.reserve(jailgames_surface_->getHeight());
|
||||
|
||||
for (int i = 0; i < jailgames_surface_->getHeight(); ++i) {
|
||||
jailgames_sprite_.push_back(std::make_shared<Sprite>(jailgames_surface_, 0, i, jailgames_surface_->getWidth(), 1));
|
||||
jailgames_sprite_.back()->setClip(0, i, jailgames_surface_->getWidth(), 1);
|
||||
|
||||
// Calcular posición inicial (alternando entre derecha e izquierda)
|
||||
constexpr int LINE_OFFSET = 6;
|
||||
const int INITIAL_X = (i % 2 == 0) ? (256 + (i * LINE_OFFSET)) : (static_cast<int>(-WIDTH) - (i * LINE_OFFSET));
|
||||
jailgames_initial_x_.push_back(INITIAL_X);
|
||||
|
||||
jailgames_sprite_.at(i)->setX(INITIAL_X);
|
||||
jailgames_sprite_.at(i)->setY(83 + i);
|
||||
}
|
||||
}
|
||||
80
source/game/scenes/logo.hpp
Normal file
80
source/game/scenes/logo.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <functional> // Para std::function
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
class Sprite; // Forward declaration
|
||||
class Surface; // Forward declaration
|
||||
|
||||
class Logo {
|
||||
public:
|
||||
// --- Tipos ---
|
||||
using EasingFunction = std::function<float(float)>; // Función de easing (permite lambdas)
|
||||
|
||||
// --- Enumeraciones ---
|
||||
enum class State {
|
||||
INITIAL, // Espera inicial
|
||||
JAILGAMES_SLIDE_IN, // Las líneas de JAILGAMES se deslizan hacia el centro
|
||||
SINCE_1998_FADE_IN, // Aparición gradual del texto "Since 1998"
|
||||
DISPLAY, // Logo completo visible
|
||||
FADE_OUT, // Desaparición gradual
|
||||
BLACK_SCREEN, // Pantalla en negro antes de terminar
|
||||
END // Fin de la secuencia
|
||||
};
|
||||
|
||||
// --- Constructor y Destructor ---
|
||||
Logo();
|
||||
~Logo() = default;
|
||||
|
||||
// --- Bucle principal ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Constantes de tiempo (en segundos) ---
|
||||
static constexpr float INITIAL_DELAY = 0.5F; // Tiempo antes de que empiece la animación
|
||||
static constexpr float SINCE_1998_FADE_DURATION = 0.5F; // Duración del fade-in de "Since 1998"
|
||||
static constexpr float DISPLAY_DURATION = 3.5F; // Tiempo que el logo permanece visible
|
||||
static constexpr float FADE_OUT_DURATION = 0.5F; // Duración del fade-out final
|
||||
static constexpr float BLACK_SCREEN_DURATION = 1.0F; // Duración de la pantalla negra final
|
||||
|
||||
// --- Constantes de animación ---
|
||||
static constexpr float JAILGAMES_SLIDE_DURATION = 0.8F; // Duración de la animación de slide-in (segundos)
|
||||
static constexpr int JAILGAMES_DEST_X = 37; // Posición X de destino para JAILGAMES
|
||||
|
||||
// --- Métodos ---
|
||||
void update(); // Actualiza las variables
|
||||
void render(); // Dibuja en pantalla
|
||||
static void handleEvents(); // Comprueba el manejador de eventos
|
||||
static void handleInput(); // Comprueba las entradas
|
||||
void updateJAILGAMES(float delta_time); // Gestiona el logo de JAILGAME (time-based)
|
||||
void updateTextureColors(); // Gestiona el color de las texturas
|
||||
void updateState(float delta_time); // Actualiza el estado actual
|
||||
void transitionToState(State new_state); // Transiciona a un nuevo estado
|
||||
[[nodiscard]] auto getColorIndex(float progress) const -> int; // Calcula el índice de color según el progreso (0.0-1.0)
|
||||
static void endSection(); // Termina la sección
|
||||
void initColors(); // Inicializa el vector de colores
|
||||
void initSprites(); // Crea los sprites de cada linea
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Objetos y punteros a recursos
|
||||
std::shared_ptr<Surface> jailgames_surface_; // Textura con los graficos "JAILGAMES"
|
||||
std::shared_ptr<Surface> since_1998_surface_; // Textura con los graficos "Since 1998"
|
||||
std::vector<std::shared_ptr<Sprite>> jailgames_sprite_; // Vector con los sprites de cada linea que forman el bitmap JAILGAMES
|
||||
std::vector<int> jailgames_initial_x_; // Posiciones X iniciales de cada línea (para interpolación con easing)
|
||||
std::shared_ptr<Sprite> since_1998_sprite_; // SSprite para manejar la textura2
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para delta time
|
||||
|
||||
// Variables de estado de colores
|
||||
std::vector<Uint8> color_; // Vector con los colores para el fade
|
||||
Uint8 jailgames_color_{0}; // Color para el sprite de "JAILGAMES"
|
||||
Uint8 since_1998_color_{0}; // Color para el sprite de "Since 1998"
|
||||
|
||||
// Variables de estado de la secuencia
|
||||
State state_{State::INITIAL}; // Estado actual de la secuencia
|
||||
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
|
||||
EasingFunction easing_function_; // Función de easing para la animación del logo
|
||||
};
|
||||
876
source/game/scenes/title.cpp
Normal file
876
source/game/scenes/title.cpp
Normal file
@@ -0,0 +1,876 @@
|
||||
#include "game/scenes/title.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // Para clamp
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/input/global_inputs.hpp" // Para check
|
||||
#include "core/input/input.hpp" // Para Input, InputAction, Input::DO_NOT_ALLOW_REPEAT, REP...
|
||||
#include "core/locale/locale.hpp" // Para Locale
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/sprite.hpp" // Para SSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/resources/resource_list.hpp" // Para Asset
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/gameplay/cheevos.hpp" // Para Cheevos, Achievement
|
||||
#include "game/options.hpp" // Para Options, options, SectionState, Section
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "game/ui/console.hpp" // Para Console
|
||||
#include "utils/defines.hpp" // Para PlayArea::CENTER_X, GameCanvas::WIDTH
|
||||
#include "utils/utils.hpp" // Para stringToColor, PaletteColor, playMusic
|
||||
|
||||
// Constructor
|
||||
Title::Title()
|
||||
: game_logo_surface_(Resource::Cache::get()->getSurface("title_logo.gif")),
|
||||
game_logo_sprite_(std::make_unique<Sprite>(game_logo_surface_, 29, 9, game_logo_surface_->getWidth(), game_logo_surface_->getHeight())),
|
||||
loading_screen_surface_(Resource::Cache::get()->getSurface("loading_screen_color.gif")),
|
||||
loading_screen_sprite_(std::make_unique<Sprite>(loading_screen_surface_, 0, 0, loading_screen_surface_->getWidth(), loading_screen_surface_->getHeight())),
|
||||
title_surface_(std::make_shared<Surface>(Options::game.width, Options::game.height)),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()),
|
||||
marquee_text_(Resource::Cache::get()->getText("gauntlet")),
|
||||
menu_text_(Resource::Cache::get()->getText("gauntlet")) {
|
||||
// Inicializa arrays con valores por defecto
|
||||
temp_keys_.fill(SDL_SCANCODE_UNKNOWN);
|
||||
temp_buttons_.fill(-1);
|
||||
|
||||
// Determina el estado inicial basado en opciones
|
||||
state_ = SceneManager::options == SceneManager::Options::TITLE_WITH_LOADING_SCREEN ? State::SHOW_LOADING_SCREEN : State::MAIN_MENU;
|
||||
|
||||
// Establece SceneManager
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
|
||||
// Acciones iniciales
|
||||
initMarquee(); // Inicializa la marquesina
|
||||
createCheevosTexture(); // Crea y rellena la textura para mostrar los logros
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
|
||||
Audio::get()->playMusic("574071_EA_DTV.ogg"); // Inicia la musica
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Title::~Title() { // NOLINT(modernize-use-equals-default)
|
||||
loading_screen_surface_->resetSubPalette();
|
||||
title_surface_->resetSubPalette();
|
||||
}
|
||||
|
||||
// Inicializa la marquesina
|
||||
void Title::initMarquee() {
|
||||
letters_.clear();
|
||||
long_text_ = Locale::get()->get("title.marquee");
|
||||
|
||||
// Pre-calcular anchos de caracteres para eficiencia (iteración por codepoints UTF-8)
|
||||
size_t pos = 0;
|
||||
while (pos < long_text_.size()) {
|
||||
uint32_t cp = Text::nextCodepoint(long_text_, pos);
|
||||
Glyph l;
|
||||
l.codepoint = cp;
|
||||
l.clip = marquee_text_->getGlyphClip(cp); // Pre-calcular clip rect (evita búsqueda por frame)
|
||||
l.x = MARQUEE_START_X;
|
||||
l.width = static_cast<float>(marquee_text_->glyphWidth(cp, 0)); // Pre-calcular ancho visual del glifo
|
||||
l.enabled = false;
|
||||
letters_.push_back(l);
|
||||
}
|
||||
|
||||
if (!letters_.empty()) {
|
||||
letters_[0].enabled = true;
|
||||
}
|
||||
first_active_letter_ = 0;
|
||||
last_active_letter_ = 0;
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Title::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
|
||||
// Manejo especial para captura de botones de gamepad
|
||||
if (is_remapping_joystick_ && !remap_completed_ &&
|
||||
(event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION)) {
|
||||
handleJoystickRemap(event);
|
||||
continue; // No procesar más este evento
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && !Console::get()->isActive()) {
|
||||
// Si estamos en modo remap de teclado, capturar tecla
|
||||
if (is_remapping_keyboard_ && !remap_completed_) {
|
||||
handleKeyboardRemap(event);
|
||||
}
|
||||
// Si estamos en el menú principal normal
|
||||
else if (state_ == State::MAIN_MENU && !is_remapping_keyboard_ && !is_remapping_joystick_) {
|
||||
handleMainMenuKeyPress(event.key.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Maneja las teclas del menu principal
|
||||
void Title::handleMainMenuKeyPress(SDL_Keycode key) {
|
||||
switch (key) {
|
||||
case SDLK_1:
|
||||
// PLAY
|
||||
exit_scene_ = SceneManager::Scene::GAME;
|
||||
transitionToState(State::FADE_MENU);
|
||||
Audio::get()->fadeOutMusic(1000);
|
||||
break;
|
||||
|
||||
case SDLK_2:
|
||||
// REDEFINE KEYBOARD
|
||||
is_remapping_keyboard_ = true;
|
||||
is_remapping_joystick_ = false;
|
||||
remap_step_ = 0;
|
||||
remap_completed_ = false;
|
||||
remap_error_message_.clear();
|
||||
state_time_ = 0.0F;
|
||||
break;
|
||||
|
||||
case SDLK_3:
|
||||
// REDEFINE JOYSTICK (siempre visible, pero solo funciona si hay gamepad)
|
||||
if (Input::get()->gameControllerFound()) {
|
||||
is_remapping_keyboard_ = false;
|
||||
is_remapping_joystick_ = true;
|
||||
remap_step_ = 0;
|
||||
remap_completed_ = false;
|
||||
remap_error_message_.clear();
|
||||
axis_cooldown_ = 0.0F;
|
||||
state_time_ = 0.0F;
|
||||
}
|
||||
// Si no hay gamepad, simplemente no hacer nada
|
||||
break;
|
||||
|
||||
case SDLK_4:
|
||||
// PROJECTS
|
||||
transitionToState(State::CHEEVOS_MENU);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Title::handleInput(float delta_time) {
|
||||
Input::get()->update();
|
||||
|
||||
// Permitir cancelar remap con ESC/CANCEL
|
||||
if ((is_remapping_keyboard_ || is_remapping_joystick_) && !remap_completed_) {
|
||||
if (Input::get()->checkAction(InputAction::CANCEL, Input::DO_NOT_ALLOW_REPEAT)) {
|
||||
is_remapping_keyboard_ = false;
|
||||
is_remapping_joystick_ = false;
|
||||
remap_step_ = 0;
|
||||
remap_completed_ = false;
|
||||
remap_error_message_.clear();
|
||||
}
|
||||
// Durante el remap, no procesar otras entradas
|
||||
GlobalInputs::handle();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (state_) {
|
||||
case State::SHOW_LOADING_SCREEN:
|
||||
if (Input::get()->checkAction(InputAction::ACCEPT, Input::DO_NOT_ALLOW_REPEAT)) {
|
||||
transitionToState(State::FADE_LOADING_SCREEN);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::CHEEVOS_MENU:
|
||||
if (Input::get()->checkAction(InputAction::ACCEPT, Input::DO_NOT_ALLOW_REPEAT) ||
|
||||
Input::get()->checkAction(InputAction::CANCEL, Input::DO_NOT_ALLOW_REPEAT)) {
|
||||
resetCheevosScroll();
|
||||
transitionToState(State::MAIN_MENU);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Actualiza la marquesina
|
||||
void Title::updateMarquee(float delta_time) {
|
||||
const float DISPLACEMENT = MARQUEE_SPEED * delta_time;
|
||||
|
||||
// Solo procesar letras en rango activo + 1 para poder activar la siguiente
|
||||
for (int i = first_active_letter_; i <= last_active_letter_ + 1 && i < (int)letters_.size(); ++i) {
|
||||
auto& letter = letters_[i];
|
||||
|
||||
if (letter.enabled) {
|
||||
letter.x -= DISPLACEMENT;
|
||||
|
||||
// Desactivar si sale de pantalla
|
||||
if (letter.x < MARQUEE_EXIT_X) {
|
||||
letter.enabled = false;
|
||||
if (i == first_active_letter_) {
|
||||
first_active_letter_++; // Avanzar inicio del rango
|
||||
}
|
||||
}
|
||||
} else if (i > 0 && letters_[i - 1].x < MARQUEE_START_X && letters_[i - 1].enabled) {
|
||||
// Activar siguiente letra usando ancho pre-calculado
|
||||
letter.enabled = true;
|
||||
letter.x = letters_[i - 1].x + letters_[i - 1].width + MARQUEE_LETTER_SPACING;
|
||||
last_active_letter_ = i; // Expandir fin del rango
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la marquesina y la reinicia
|
||||
if (letters_[letters_.size() - 1].x < MARQUEE_EXIT_X) {
|
||||
initMarquee();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la marquesina
|
||||
void Title::renderMarquee() const {
|
||||
auto* sprite = marquee_text_->getSprite();
|
||||
sprite->setY(MARQUEE_Y);
|
||||
// Solo renderizar letras activas (optimización: usa cache y rangos)
|
||||
for (int i = first_active_letter_; i <= last_active_letter_ + 1 && i < (int)letters_.size(); ++i) {
|
||||
const auto& letter = letters_[i];
|
||||
if (letter.enabled && letter.clip.w > 0.0F) {
|
||||
sprite->setClip(letter.clip);
|
||||
sprite->setX(letter.x);
|
||||
sprite->render(1, static_cast<Uint8>(PaletteColor::MAGENTA));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Title::update() {
|
||||
const float DELTA_TIME = delta_timer_->tick();
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(DELTA_TIME); // Comprueba las entradas
|
||||
|
||||
updateState(DELTA_TIME); // Actualiza el estado actual
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
}
|
||||
|
||||
// Actualiza el estado actual
|
||||
void Title::updateState(float delta_time) {
|
||||
switch (state_) {
|
||||
case State::SHOW_LOADING_SCREEN:
|
||||
updateShowLoadingScreen(delta_time);
|
||||
break;
|
||||
|
||||
case State::FADE_LOADING_SCREEN:
|
||||
updateFadeLoadingScreen(delta_time);
|
||||
break;
|
||||
|
||||
case State::MAIN_MENU:
|
||||
updateMainMenu(delta_time);
|
||||
break;
|
||||
|
||||
case State::CHEEVOS_MENU:
|
||||
updateCheevosMenu(delta_time);
|
||||
break;
|
||||
|
||||
case State::FADE_MENU:
|
||||
updateFadeMenu(delta_time);
|
||||
break;
|
||||
|
||||
case State::POST_FADE_MENU:
|
||||
updatePostFadeMenu(delta_time);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Transiciona a un nuevo estado
|
||||
void Title::transitionToState(State new_state) {
|
||||
state_ = new_state;
|
||||
state_time_ = 0.0F;
|
||||
fade_accumulator_ = 0.0F;
|
||||
}
|
||||
|
||||
// Actualiza el estado SHOW_LOADING_SCREEN
|
||||
void Title::updateShowLoadingScreen(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
if (state_time_ >= SHOW_LOADING_DURATION) {
|
||||
transitionToState(State::FADE_LOADING_SCREEN);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado FADE_LOADING_SCREEN
|
||||
void Title::updateFadeLoadingScreen(float delta_time) {
|
||||
fade_accumulator_ += delta_time;
|
||||
if (fade_accumulator_ >= FADE_STEP_INTERVAL) {
|
||||
fade_accumulator_ = 0.0F;
|
||||
if (loading_screen_surface_->fadeSubPalette()) {
|
||||
transitionToState(State::MAIN_MENU);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado MAIN_MENU
|
||||
void Title::updateMainMenu(float delta_time) {
|
||||
// Actualiza la marquesina
|
||||
updateMarquee(delta_time);
|
||||
|
||||
// Si estamos en modo remap, manejar la lógica específica
|
||||
if (is_remapping_keyboard_ || is_remapping_joystick_) {
|
||||
// Decrementar cooldown de ejes si estamos capturando botones de joystick
|
||||
if (is_remapping_joystick_ && axis_cooldown_ > 0.0F) {
|
||||
axis_cooldown_ -= delta_time;
|
||||
axis_cooldown_ = std::max(axis_cooldown_, 0.0F);
|
||||
}
|
||||
|
||||
// Si el remap está completado, esperar antes de guardar
|
||||
if (remap_completed_) {
|
||||
state_time_ += delta_time;
|
||||
if (state_time_ >= KEYBOARD_REMAP_DISPLAY_DELAY) {
|
||||
if (is_remapping_keyboard_) {
|
||||
applyKeyboardRemap();
|
||||
} else if (is_remapping_joystick_) {
|
||||
applyJoystickRemap();
|
||||
}
|
||||
// Resetear estado de remap
|
||||
is_remapping_keyboard_ = false;
|
||||
is_remapping_joystick_ = false;
|
||||
remap_completed_ = false;
|
||||
state_time_ = 0.0F;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Incrementa el temporizador solo en el menú principal normal
|
||||
state_time_ += delta_time;
|
||||
|
||||
// Si el tiempo alcanza el timeout, va a créditos con fade
|
||||
if (state_time_ >= MAIN_MENU_IDLE_TIMEOUT) {
|
||||
exit_scene_ = SceneManager::Scene::CREDITS;
|
||||
transitionToState(State::FADE_MENU);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado CHEEVOS_MENU
|
||||
void Title::updateCheevosMenu(float delta_time) {
|
||||
// Actualiza la marquesina (sigue visible en fondo)
|
||||
updateMarquee(delta_time);
|
||||
|
||||
// Determina la velocidad objetivo basada en el input
|
||||
float target_velocity = 0.0F;
|
||||
if (Input::get()->checkAction(InputAction::RIGHT, Input::ALLOW_REPEAT)) {
|
||||
target_velocity = CHEEVOS_SCROLL_MAX_SPEED; // Scroll hacia abajo
|
||||
} else if (Input::get()->checkAction(InputAction::LEFT, Input::ALLOW_REPEAT)) {
|
||||
target_velocity = -CHEEVOS_SCROLL_MAX_SPEED; // Scroll hacia arriba
|
||||
}
|
||||
|
||||
// Interpola suavemente la velocidad actual hacia la velocidad objetivo
|
||||
if (target_velocity != 0.0F) {
|
||||
// Acelerando hacia la velocidad objetivo
|
||||
const float ACCELERATION_STEP = CHEEVOS_SCROLL_ACCELERATION * delta_time;
|
||||
if (cheevos_scroll_velocity_ < target_velocity) {
|
||||
cheevos_scroll_velocity_ = std::min(cheevos_scroll_velocity_ + ACCELERATION_STEP, target_velocity);
|
||||
} else if (cheevos_scroll_velocity_ > target_velocity) {
|
||||
cheevos_scroll_velocity_ = std::max(cheevos_scroll_velocity_ - ACCELERATION_STEP, target_velocity);
|
||||
}
|
||||
} else {
|
||||
// Desacelerando hacia 0
|
||||
const float DECELERATION_STEP = CHEEVOS_SCROLL_DECELERATION * delta_time;
|
||||
if (cheevos_scroll_velocity_ > 0.0F) {
|
||||
cheevos_scroll_velocity_ = std::max(cheevos_scroll_velocity_ - DECELERATION_STEP, 0.0F);
|
||||
} else if (cheevos_scroll_velocity_ < 0.0F) {
|
||||
cheevos_scroll_velocity_ = std::min(cheevos_scroll_velocity_ + DECELERATION_STEP, 0.0F);
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica la velocidad actual al scroll position
|
||||
if (cheevos_scroll_velocity_ != 0.0F) {
|
||||
cheevos_surface_view_.y += cheevos_scroll_velocity_ * delta_time;
|
||||
|
||||
// Ajusta los límites
|
||||
const float BOTTOM = cheevos_surface_->getHeight() - cheevos_surface_view_.h;
|
||||
cheevos_surface_view_.y = std::clamp(cheevos_surface_view_.y, 0.0F, BOTTOM);
|
||||
|
||||
cheevos_sprite_->setClip(cheevos_surface_view_);
|
||||
}
|
||||
|
||||
// No incrementar state_time_ (no timeout en este estado)
|
||||
}
|
||||
|
||||
// Actualiza el estado FADE_MENU
|
||||
void Title::updateFadeMenu(float delta_time) {
|
||||
fade_accumulator_ += delta_time;
|
||||
if (fade_accumulator_ >= FADE_STEP_INTERVAL) {
|
||||
fade_accumulator_ = 0.0F;
|
||||
if (title_surface_->fadeSubPalette()) {
|
||||
transitionToState(State::POST_FADE_MENU);
|
||||
}
|
||||
}
|
||||
// Actualiza la marquesina (sigue visible en fondo)
|
||||
updateMarquee(delta_time);
|
||||
}
|
||||
|
||||
// Actualiza el estado POST_FADE_MENU
|
||||
void Title::updatePostFadeMenu(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
if (state_time_ >= POST_FADE_DELAY) {
|
||||
SceneManager::current = exit_scene_;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Title::render() {
|
||||
// Rellena la surface
|
||||
fillTitleSurface();
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
// Dibuja en pantalla la surface con la composicion
|
||||
title_surface_->render();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Title::run() {
|
||||
while (SceneManager::current == SceneManager::Scene::TITLE) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Crea y rellena la textura para mostrar los logros
|
||||
void Title::createCheevosTexture() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Define la zona central del menu (entre el logo y la marquesina)
|
||||
constexpr int MENU_ZONE_Y = 73; // Top of menu zone
|
||||
constexpr int MENU_ZONE_HEIGHT = 102; // Height of menu zone
|
||||
|
||||
// Crea la textura con el listado de logros
|
||||
const auto CHEEVOS_LIST = Cheevos::get()->list();
|
||||
const auto TEXT = Resource::Cache::get()->getText("subatomic");
|
||||
constexpr int CHEEVOS_TEXTURE_WIDTH = 200;
|
||||
constexpr int CHEEVOS_TEXTURE_VIEW_HEIGHT = MENU_ZONE_HEIGHT;
|
||||
constexpr int CHEEVOS_PADDING = 10;
|
||||
const int CHEEVO_HEIGHT = CHEEVOS_PADDING + (TEXT->getCharacterSize() * 2) + 1;
|
||||
const int CHEEVOS_TEXTURE_HEIGHT = (CHEEVO_HEIGHT * CHEEVOS_LIST.size()) + 2 + TEXT->getCharacterSize() + 8;
|
||||
cheevos_surface_ = std::make_shared<Surface>(CHEEVOS_TEXTURE_WIDTH, CHEEVOS_TEXTURE_HEIGHT);
|
||||
|
||||
// Prepara para dibujar sobre la textura
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(cheevos_surface_);
|
||||
|
||||
// Rellena la textura con color sólido
|
||||
const auto CHEEVOS_BG_COLOR = static_cast<Uint8>(PaletteColor::BLACK);
|
||||
cheevos_surface_->clear(CHEEVOS_BG_COLOR);
|
||||
|
||||
// Escribe la lista de logros en la textura
|
||||
const std::string CHEEVOS_OWNER = Locale::get()->get("title.projects"); // NOLINT(readability-static-accessed-through-instance)
|
||||
const std::string CHEEVOS_LIST_CAPTION = CHEEVOS_OWNER + " (" + std::to_string(Cheevos::get()->getTotalUnlockedAchievements()) + " / " + std::to_string(Cheevos::get()->size()) + ")";
|
||||
int pos = 2;
|
||||
TEXT->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, cheevos_surface_->getWidth() / 2, pos, CHEEVOS_LIST_CAPTION, 1, stringToColor("bright_green"));
|
||||
pos += TEXT->getCharacterSize();
|
||||
const Uint8 CHEEVO_LOCKED_COLOR = stringToColor("white");
|
||||
const Uint8 CHEEVO_UNLOCKED_COLOR = stringToColor("bright_green");
|
||||
constexpr int LINE_X1 = (CHEEVOS_TEXTURE_WIDTH / 7) * 3;
|
||||
constexpr int LINE_X2 = LINE_X1 + ((CHEEVOS_TEXTURE_WIDTH / 7) * 1);
|
||||
|
||||
for (const auto& cheevo : CHEEVOS_LIST) {
|
||||
const Uint8 CHEEVO_COLOR = cheevo.completed ? CHEEVO_UNLOCKED_COLOR : CHEEVO_LOCKED_COLOR;
|
||||
pos += CHEEVOS_PADDING;
|
||||
constexpr int HALF = CHEEVOS_PADDING / 2;
|
||||
cheevos_surface_->drawLine(LINE_X1, pos - HALF - 1, LINE_X2, pos - HALF - 1, CHEEVO_COLOR);
|
||||
TEXT->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, CHEEVOS_TEXTURE_WIDTH / 2, pos, cheevo.caption, 1, CHEEVO_COLOR);
|
||||
pos += TEXT->getCharacterSize() + 1;
|
||||
TEXT->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, CHEEVOS_TEXTURE_WIDTH / 2, pos, cheevo.description, 1, CHEEVO_COLOR);
|
||||
pos += TEXT->getCharacterSize();
|
||||
}
|
||||
|
||||
// Restablece el RenderSurface
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
|
||||
// Crea el sprite para el listado de logros (usa la zona del menu)
|
||||
cheevos_sprite_ = std::make_unique<Sprite>(cheevos_surface_, (GameCanvas::WIDTH - cheevos_surface_->getWidth()) / 2, MENU_ZONE_Y, cheevos_surface_->getWidth(), cheevos_surface_->getHeight());
|
||||
cheevos_surface_view_ = {.x = 0, .y = 0, .w = cheevos_surface_->getWidth(), .h = CHEEVOS_TEXTURE_VIEW_HEIGHT};
|
||||
cheevos_sprite_->setClip(cheevos_surface_view_);
|
||||
}
|
||||
|
||||
// Resetea el scroll de la lista de logros
|
||||
void Title::resetCheevosScroll() {
|
||||
cheevos_surface_view_.y = 0;
|
||||
cheevos_scroll_velocity_ = 0.0F;
|
||||
cheevos_sprite_->setClip(cheevos_surface_view_);
|
||||
}
|
||||
|
||||
// Dibuja el logo con el titulo del juego
|
||||
void Title::renderGameLogo() {
|
||||
game_logo_sprite_->render();
|
||||
}
|
||||
|
||||
// Dibuja el menu principal
|
||||
void Title::renderMainMenu() {
|
||||
// Si estamos en modo remap, mostrar la pantalla correspondiente
|
||||
if (is_remapping_keyboard_) {
|
||||
renderKeyboardRemap();
|
||||
return;
|
||||
}
|
||||
if (is_remapping_joystick_) {
|
||||
renderJoystickRemap();
|
||||
return;
|
||||
}
|
||||
|
||||
// Zona central del menu (debe coincidir con la textura de cheevos)
|
||||
constexpr int MENU_ZONE_Y = 73;
|
||||
constexpr int MENU_ZONE_HEIGHT = 102;
|
||||
|
||||
// Menú principal normal con 4 opciones centradas verticalmente en la zona
|
||||
const Uint8 COLOR = stringToColor("green");
|
||||
const int TEXT_SIZE = menu_text_->getCharacterSize();
|
||||
const int MENU_CENTER_Y = MENU_ZONE_Y + (MENU_ZONE_HEIGHT / 2);
|
||||
const int SPACING = 2 * TEXT_SIZE; // Espaciado entre opciones
|
||||
|
||||
// Calcula posiciones centradas verticalmente (4 items con espaciado)
|
||||
const int TOTAL_HEIGHT = 3 * SPACING; // 3 espacios entre 4 items
|
||||
const int START_Y = MENU_CENTER_Y - (TOTAL_HEIGHT / 2);
|
||||
|
||||
auto* loc = Locale::get();
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y, loc->get("title.menu.play"), 1, COLOR);
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y + SPACING, loc->get("title.menu.keyboard"), 1, COLOR);
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y + (2 * SPACING), loc->get("title.menu.joystick"), 1, COLOR);
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y + (3 * SPACING), loc->get("title.menu.projects"), 1, COLOR);
|
||||
}
|
||||
|
||||
// Dibuja el menu de logros
|
||||
void Title::renderCheevosMenu() {
|
||||
cheevos_sprite_->render();
|
||||
}
|
||||
|
||||
// Dibuja los elementos en la surface
|
||||
void Title::fillTitleSurface() {
|
||||
// Renderiza sobre la textura
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(title_surface_);
|
||||
|
||||
// Rellena la textura de color
|
||||
title_surface_->clear(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
switch (state_) {
|
||||
case State::MAIN_MENU:
|
||||
case State::FADE_MENU:
|
||||
renderGameLogo();
|
||||
renderMainMenu();
|
||||
renderMarquee();
|
||||
break;
|
||||
|
||||
case State::CHEEVOS_MENU:
|
||||
renderGameLogo();
|
||||
renderCheevosMenu();
|
||||
renderMarquee();
|
||||
break;
|
||||
|
||||
case State::SHOW_LOADING_SCREEN:
|
||||
case State::FADE_LOADING_SCREEN:
|
||||
loading_screen_sprite_->render();
|
||||
renderGameLogo();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Deja el renderizador como estaba
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
|
||||
// Maneja la captura de teclas para redefinir el teclado
|
||||
void Title::handleKeyboardRemap(const SDL_Event& event) {
|
||||
SDL_Scancode scancode = event.key.scancode;
|
||||
|
||||
// Valida la tecla
|
||||
if (!isKeyValid(scancode)) {
|
||||
remap_error_message_ = Locale::get()->get("title.keys.invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
// Verifica duplicados
|
||||
if (isKeyDuplicate(scancode, remap_step_)) {
|
||||
remap_error_message_ = Locale::get()->get("title.keys.already_used");
|
||||
return;
|
||||
}
|
||||
|
||||
// Tecla valida, guardar
|
||||
temp_keys_[remap_step_] = scancode;
|
||||
remap_error_message_.clear();
|
||||
remap_step_++;
|
||||
|
||||
// Si completamos los 3 pasos, mostrar resultado y esperar
|
||||
if (remap_step_ >= 3) {
|
||||
remap_completed_ = true;
|
||||
state_time_ = 0.0F; // Resetear el timer para el delay de 1 segundo
|
||||
}
|
||||
}
|
||||
|
||||
// Valida si una tecla es permitida
|
||||
auto Title::isKeyValid(SDL_Scancode scancode) -> bool {
|
||||
// Prohibir ESC (reservado para cancelar)
|
||||
if (scancode == SDL_SCANCODE_ESCAPE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prohibir teclas F1-F12 (reservadas para funciones del sistema)
|
||||
if (scancode >= SDL_SCANCODE_F1 && scancode <= SDL_SCANCODE_F12) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prohibir Enter/Return (reservado para confirmaciones)
|
||||
if (scancode == SDL_SCANCODE_RETURN || scancode == SDL_SCANCODE_RETURN2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Verifica si una tecla ya fue usada en pasos anteriores
|
||||
auto Title::isKeyDuplicate(SDL_Scancode scancode, int current_step) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (int i = 0; i < current_step; i++) {
|
||||
if (temp_keys_[i] == scancode) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Retorna el nombre de la accion para el paso actual
|
||||
auto Title::getActionName(int step) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
||||
switch (step) {
|
||||
case 0:
|
||||
return "LEFT";
|
||||
case 1:
|
||||
return "RIGHT";
|
||||
case 2:
|
||||
return "JUMP";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica y guarda las teclas redefinidas
|
||||
void Title::applyKeyboardRemap() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Guardar las nuevas teclas en Options::controls
|
||||
Options::keyboard_controls.key_left = temp_keys_[0];
|
||||
Options::keyboard_controls.key_right = temp_keys_[1];
|
||||
Options::keyboard_controls.key_jump = temp_keys_[2];
|
||||
|
||||
// Aplicar los bindings al sistema de Input
|
||||
Input::get()->applyKeyboardBindingsFromOptions();
|
||||
|
||||
// Guardar a archivo de configuracion
|
||||
Options::saveToFile();
|
||||
}
|
||||
|
||||
// Dibuja la pantalla de redefinir teclado
|
||||
void Title::renderKeyboardRemap() const {
|
||||
// Zona central del menu (debe coincidir con la textura de cheevos)
|
||||
constexpr int MENU_ZONE_Y = 73;
|
||||
constexpr int MENU_ZONE_HEIGHT = 102;
|
||||
|
||||
const Uint8 COLOR = stringToColor("green");
|
||||
const Uint8 ERROR_COLOR = stringToColor("red");
|
||||
const int TEXT_SIZE = menu_text_->getCharacterSize();
|
||||
const int MENU_CENTER_Y = MENU_ZONE_Y + (MENU_ZONE_HEIGHT / 2);
|
||||
|
||||
// Calcula posiciones centradas verticalmente
|
||||
// Layout: Mensaje principal, espacio, 3 teclas (LEFT/RIGHT/JUMP), espacio, mensaje de error
|
||||
const int LINE_SPACING = TEXT_SIZE;
|
||||
const int START_Y = MENU_CENTER_Y - (2 * TEXT_SIZE); // Centrado aproximado
|
||||
|
||||
// Mensaje principal: "PRESS KEY FOR [ACTION]" o "KEYS DEFINED" si completado
|
||||
auto* loc = Locale::get();
|
||||
if (remap_step_ >= 3) {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y, loc->get("title.keys.defined"), 1, COLOR);
|
||||
} else {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y, loc->get("title.keys.prompt" + std::to_string(remap_step_)), 1, COLOR);
|
||||
}
|
||||
|
||||
// Mostrar teclas ya capturadas (con espaciado de 2 líneas desde el mensaje principal)
|
||||
const int KEYS_START_Y = START_Y + (2 * LINE_SPACING);
|
||||
if (remap_step_ > 0) {
|
||||
const std::string LEFT_KEY = SDL_GetScancodeName(temp_keys_[0]);
|
||||
const std::string LEFT_MSG = loc->get("title.keys.label0") + LEFT_KEY; // NOLINT(readability-static-accessed-through-instance)
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, KEYS_START_Y, LEFT_MSG, 1, COLOR);
|
||||
}
|
||||
if (remap_step_ > 1) {
|
||||
const std::string RIGHT_KEY = SDL_GetScancodeName(temp_keys_[1]);
|
||||
const std::string RIGHT_MSG = loc->get("title.keys.label1") + RIGHT_KEY; // NOLINT(readability-static-accessed-through-instance)
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, KEYS_START_Y + LINE_SPACING, RIGHT_MSG, 1, COLOR);
|
||||
}
|
||||
if (remap_step_ >= 3) {
|
||||
const std::string JUMP_KEY = SDL_GetScancodeName(temp_keys_[2]);
|
||||
const std::string JUMP_MSG = loc->get("title.keys.label2") + JUMP_KEY; // NOLINT(readability-static-accessed-through-instance)
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, KEYS_START_Y + (2 * LINE_SPACING), JUMP_MSG, 1, COLOR);
|
||||
}
|
||||
|
||||
// Mensaje de error si existe (4 líneas después del inicio de las teclas)
|
||||
if (!remap_error_message_.empty()) {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, KEYS_START_Y + (4 * LINE_SPACING), remap_error_message_, 1, ERROR_COLOR);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la pantalla de redefinir joystick
|
||||
void Title::renderJoystickRemap() const {
|
||||
// Zona central del menu (debe coincidir con la textura de cheevos)
|
||||
constexpr int MENU_ZONE_Y = 73;
|
||||
constexpr int MENU_ZONE_HEIGHT = 102;
|
||||
|
||||
const Uint8 COLOR = stringToColor("green");
|
||||
const Uint8 ERROR_COLOR = stringToColor("red");
|
||||
const int TEXT_SIZE = menu_text_->getCharacterSize();
|
||||
const int MENU_CENTER_Y = MENU_ZONE_Y + (MENU_ZONE_HEIGHT / 2);
|
||||
|
||||
// Calcula posiciones centradas verticalmente
|
||||
// Layout: Mensaje principal, espacio, 3 botones (LEFT/RIGHT/JUMP), espacio, mensaje de error
|
||||
const int LINE_SPACING = TEXT_SIZE;
|
||||
const int START_Y = MENU_CENTER_Y - (2 * TEXT_SIZE); // Centrado aproximado
|
||||
|
||||
// Mensaje principal: "PRESS BUTTON FOR [ACTION]" o "BUTTONS DEFINED" si completado
|
||||
auto* loc = Locale::get();
|
||||
if (remap_step_ >= 3) {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y, loc->get("title.buttons.defined"), 1, COLOR);
|
||||
} else {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, START_Y, loc->get("title.buttons.prompt" + std::to_string(remap_step_)), 1, COLOR);
|
||||
}
|
||||
|
||||
// Mostrar botones ya capturados (con espaciado de 2 líneas desde el mensaje principal)
|
||||
const int BUTTONS_START_Y = START_Y + (2 * LINE_SPACING);
|
||||
if (remap_step_ > 0) {
|
||||
const std::string LEFT_BTN = getButtonName(temp_buttons_[0]);
|
||||
const std::string LEFT_MSG = loc->get("title.keys.label0") + LEFT_BTN; // NOLINT(readability-static-accessed-through-instance)
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, BUTTONS_START_Y, LEFT_MSG, 1, COLOR);
|
||||
}
|
||||
if (remap_step_ > 1) {
|
||||
const std::string RIGHT_BTN = getButtonName(temp_buttons_[1]);
|
||||
const std::string RIGHT_MSG = loc->get("title.keys.label1") + RIGHT_BTN; // NOLINT(readability-static-accessed-through-instance)
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, BUTTONS_START_Y + LINE_SPACING, RIGHT_MSG, 1, COLOR);
|
||||
}
|
||||
if (remap_step_ >= 3) {
|
||||
const std::string JUMP_BTN = getButtonName(temp_buttons_[2]);
|
||||
const std::string JUMP_MSG = loc->get("title.keys.label2") + JUMP_BTN; // NOLINT(readability-static-accessed-through-instance)
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, BUTTONS_START_Y + (2 * LINE_SPACING), JUMP_MSG, 1, COLOR);
|
||||
}
|
||||
|
||||
// Mensaje de error si existe (4 líneas después del inicio de los botones)
|
||||
if (!remap_error_message_.empty()) {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, PlayArea::CENTER_X, BUTTONS_START_Y + (4 * LINE_SPACING), remap_error_message_, 1, ERROR_COLOR);
|
||||
}
|
||||
}
|
||||
|
||||
// Maneja la captura de botones del gamepad para redefinir
|
||||
void Title::handleJoystickRemap(const SDL_Event& event) {
|
||||
int captured_button = -1;
|
||||
|
||||
// Capturar botones del gamepad
|
||||
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
|
||||
captured_button = static_cast<int>(event.gbutton.button);
|
||||
}
|
||||
// Capturar triggers y ejes analógicos
|
||||
else if (event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION) {
|
||||
// Si el cooldown está activo, ignorar eventos de ejes (evita múltiples capturas)
|
||||
if (axis_cooldown_ > 0.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
constexpr Sint16 TRIGGER_THRESHOLD = 20000;
|
||||
constexpr Sint16 AXIS_THRESHOLD = 20000;
|
||||
|
||||
// Capturar triggers como botones (usando valores especiales 100/101)
|
||||
if (event.gaxis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER && event.gaxis.value > TRIGGER_THRESHOLD) {
|
||||
captured_button = Input::TRIGGER_L2_AS_BUTTON; // 100
|
||||
axis_cooldown_ = 0.5F; // Cooldown de medio segundo
|
||||
} else if (event.gaxis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER && event.gaxis.value > TRIGGER_THRESHOLD) {
|
||||
captured_button = Input::TRIGGER_R2_AS_BUTTON; // 101
|
||||
axis_cooldown_ = 0.5F;
|
||||
}
|
||||
// Capturar ejes del stick analógico (usando valores especiales 200+)
|
||||
else if (event.gaxis.axis == SDL_GAMEPAD_AXIS_LEFTX) {
|
||||
if (event.gaxis.value < -AXIS_THRESHOLD) {
|
||||
captured_button = 200; // Left stick izquierda
|
||||
axis_cooldown_ = 0.5F;
|
||||
} else if (event.gaxis.value > AXIS_THRESHOLD) {
|
||||
captured_button = 201; // Left stick derecha
|
||||
axis_cooldown_ = 0.5F;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Si no se capturó ningún input válido, salir
|
||||
if (captured_button == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Verifica duplicados
|
||||
if (isButtonDuplicate(captured_button, remap_step_)) {
|
||||
remap_error_message_ = Locale::get()->get("title.buttons.already_used");
|
||||
return;
|
||||
}
|
||||
|
||||
// Botón válido, guardar
|
||||
temp_buttons_[remap_step_] = captured_button;
|
||||
remap_error_message_.clear();
|
||||
remap_step_++;
|
||||
|
||||
// Si completamos los 3 pasos, mostrar resultado y esperar
|
||||
if (remap_step_ >= 3) {
|
||||
remap_completed_ = true;
|
||||
state_time_ = 0.0F; // Resetear el timer para el delay
|
||||
}
|
||||
}
|
||||
|
||||
// Valida si un botón está duplicado
|
||||
auto Title::isButtonDuplicate(int button, int current_step) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
||||
for (int i = 0; i < current_step; ++i) {
|
||||
if (temp_buttons_[i] == button) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Aplica y guarda los botones del gamepad redefinidos
|
||||
void Title::applyJoystickRemap() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Guardar los nuevos botones en Options::gamepad_controls
|
||||
Options::gamepad_controls.button_left = temp_buttons_[0];
|
||||
Options::gamepad_controls.button_right = temp_buttons_[1];
|
||||
Options::gamepad_controls.button_jump = temp_buttons_[2];
|
||||
|
||||
// Aplicar los bindings al sistema de Input
|
||||
Input::get()->applyGamepadBindingsFromOptions();
|
||||
|
||||
// Guardar a archivo de configuracion
|
||||
Options::saveToFile();
|
||||
}
|
||||
|
||||
// Retorna el nombre amigable del botón del gamepad
|
||||
auto Title::getButtonName(int button) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Triggers especiales
|
||||
if (button == Input::TRIGGER_L2_AS_BUTTON) {
|
||||
return "L2";
|
||||
}
|
||||
if (button == Input::TRIGGER_R2_AS_BUTTON) {
|
||||
return "R2";
|
||||
}
|
||||
|
||||
// Ejes del stick analógico
|
||||
if (button == 200) {
|
||||
return "LEFT STICK LEFT";
|
||||
}
|
||||
if (button == 201) {
|
||||
return "LEFT STICK RIGHT";
|
||||
}
|
||||
|
||||
// Botones estándar SDL
|
||||
const auto GAMEPAD_BUTTON = static_cast<SDL_GamepadButton>(button);
|
||||
const char* button_name = SDL_GetGamepadStringForButton(GAMEPAD_BUTTON);
|
||||
return (button_name != nullptr) ? std::string(button_name) : "UNKNOWN";
|
||||
}
|
||||
134
source/game/scenes/title.hpp
Normal file
134
source/game/scenes/title.hpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array> // Para std::array
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "game/scene_manager.hpp" // Para SceneManager::Scene
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
class Sprite; // Forward declaration
|
||||
class Surface; // Forward declaration
|
||||
class Text; // Forward declaration
|
||||
|
||||
class Title {
|
||||
public:
|
||||
// --- Constructor y Destructor ---
|
||||
Title();
|
||||
~Title();
|
||||
|
||||
// --- Bucle principal ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Estructuras y enumeraciones ---
|
||||
struct Glyph {
|
||||
uint32_t codepoint{0}; // Codepoint Unicode del carácter
|
||||
SDL_FRect clip{}; // Clip rect pre-calculado en el bitmap de fuente
|
||||
float x{0.0F}; // Posición en el eje x (float para precisión con delta time)
|
||||
float width{0.0F}; // Ancho pre-calculado del carácter
|
||||
bool enabled{false}; // Solo se escriben y mueven si estan habilitadas
|
||||
};
|
||||
|
||||
enum class State {
|
||||
SHOW_LOADING_SCREEN,
|
||||
FADE_LOADING_SCREEN,
|
||||
MAIN_MENU,
|
||||
CHEEVOS_MENU,
|
||||
FADE_MENU,
|
||||
POST_FADE_MENU,
|
||||
};
|
||||
|
||||
// --- Constantes de tiempo (en segundos) ---
|
||||
static constexpr float SHOW_LOADING_DURATION = 5.0F; // Tiempo mostrando loading screen (antes 500 frames)
|
||||
static constexpr float FADE_STEP_INTERVAL = 0.05F; // Intervalo entre pasos de fade (antes cada 4 frames)
|
||||
static constexpr float POST_FADE_DELAY = 1.0F; // Delay después del fade (pantalla en negro)
|
||||
static constexpr float MAIN_MENU_IDLE_TIMEOUT = 20.0F; // Timeout para ir a créditos (antes 2200 frames)
|
||||
static constexpr float KEYBOARD_REMAP_DISPLAY_DELAY = 2.0F; // Tiempo mostrando teclas definidas antes de guardar
|
||||
static constexpr float MARQUEE_SPEED = 100.0F; // Velocidad de marquesina (pixels/segundo)
|
||||
static constexpr float CHEEVOS_SCROLL_MAX_SPEED = 180.0F; // Velocidad máxima de scroll de logros (pixels/segundo)
|
||||
static constexpr float CHEEVOS_SCROLL_ACCELERATION = 600.0F; // Aceleración del scroll (pixels/segundo²)
|
||||
static constexpr float CHEEVOS_SCROLL_DECELERATION = 800.0F; // Desaceleración del scroll (pixels/segundo²)
|
||||
|
||||
// --- Constantes de marquesina ---
|
||||
static constexpr float MARQUEE_START_X = 256.0F; // Posición inicial (ancho pantalla)
|
||||
static constexpr float MARQUEE_EXIT_X = -10.0F; // Cuando desaparece de pantalla
|
||||
static constexpr float MARQUEE_Y = 184.0F; // Posición Y
|
||||
static constexpr float MARQUEE_LETTER_SPACING = 1.0F; // Espaciado entre letras
|
||||
|
||||
// --- Métodos ---
|
||||
void update(); // Actualiza las variables
|
||||
void render(); // Dibuja en pantalla
|
||||
void handleEvents(); // Comprueba el manejador de eventos
|
||||
void handleMainMenuKeyPress(SDL_Keycode key); // Maneja las teclas del menu principal
|
||||
void handleInput(float delta_time); // Comprueba las entradas
|
||||
void updateState(float delta_time); // Actualiza el estado actual
|
||||
void transitionToState(State new_state); // Transiciona a un nuevo estado
|
||||
void updateShowLoadingScreen(float delta_time); // Actualiza SHOW_LOADING_SCREEN
|
||||
void updateFadeLoadingScreen(float delta_time); // Actualiza FADE_LOADING_SCREEN
|
||||
void updateMainMenu(float delta_time); // Actualiza MAIN_MENU
|
||||
void updateCheevosMenu(float delta_time); // Actualiza CHEEVOS_MENU
|
||||
void updateFadeMenu(float delta_time); // Actualiza FADE_MENU
|
||||
void updatePostFadeMenu(float delta_time); // Actualiza POST_FADE_MENU
|
||||
void initMarquee(); // Inicializa la marquesina
|
||||
void updateMarquee(float delta_time); // Actualiza la marquesina (time-based)
|
||||
void renderMarquee() const; // Dibuja la marquesina
|
||||
void renderGameLogo(); // Dibuja el logo con el titulo del juego
|
||||
void renderMainMenu(); // Dibuja el menu principal
|
||||
void renderCheevosMenu(); // Dibuja el menu de logros
|
||||
void renderKeyboardRemap() const; // Dibuja la pantalla de redefinir teclado
|
||||
void renderJoystickRemap() const; // Dibuja la pantalla de redefinir joystick
|
||||
void handleKeyboardRemap(const SDL_Event& event); // Maneja la captura de teclas
|
||||
void handleJoystickRemap(const SDL_Event& event); // Maneja la captura de botones del gamepad
|
||||
static auto isKeyValid(SDL_Scancode scancode) -> bool; // Valida si una tecla es permitida
|
||||
auto isKeyDuplicate(SDL_Scancode scancode, int current_step) -> bool; // Valida si una tecla esta duplicada
|
||||
auto isButtonDuplicate(int button, int current_step) -> bool; // Valida si un boton esta duplicado
|
||||
void applyKeyboardRemap(); // Aplica y guarda las teclas redefinidas
|
||||
void applyJoystickRemap(); // Aplica y guarda los botones del gamepad redefinidos
|
||||
static auto getActionName(int step) -> std::string; // Retorna el nombre de la accion (LEFT/RIGHT/JUMP)
|
||||
static auto getButtonName(int button) -> std::string; // Retorna el nombre amigable del boton del gamepad
|
||||
void createCheevosTexture(); // Crea y rellena la surface para mostrar los logros
|
||||
void resetCheevosScroll(); // Resetea el scroll de la lista de logros
|
||||
void fillTitleSurface(); // Dibuja los elementos en la surface
|
||||
|
||||
// --- Variables miembro ---
|
||||
// Objetos y punteros
|
||||
std::shared_ptr<Surface> game_logo_surface_; // Textura con los graficos
|
||||
std::unique_ptr<Sprite> game_logo_sprite_; // SSprite para manejar la surface
|
||||
std::shared_ptr<Surface> loading_screen_surface_; // Surface con los gráficos de la pantalla de carga
|
||||
std::unique_ptr<Sprite> loading_screen_sprite_; // SSprite con los gráficos de la pantalla de carga
|
||||
std::shared_ptr<Surface> cheevos_surface_; // Textura con la lista de logros
|
||||
std::unique_ptr<Sprite> cheevos_sprite_; // SSprite para manejar la surface con la lista de logros
|
||||
std::shared_ptr<Surface> title_surface_; // Surface donde se dibuja toda la clase
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para delta time
|
||||
std::shared_ptr<Text> marquee_text_; // Texto para marquesina
|
||||
std::shared_ptr<Text> menu_text_; // Texto para los menus
|
||||
|
||||
// Variables de estado de marquesina
|
||||
std::string long_text_; // Texto que aparece en la parte inferior del titulo
|
||||
std::vector<Glyph> letters_; // Vector con las letras de la marquesina
|
||||
int first_active_letter_{0}; // Primera letra activa (optimización)
|
||||
int last_active_letter_{0}; // Última letra activa (optimización)
|
||||
|
||||
// Variables de estado del menú de logros
|
||||
SDL_FRect cheevos_surface_view_; // Zona visible de la surface con el listado de logros
|
||||
float cheevos_scroll_velocity_{0.0F}; // Velocidad actual del scroll de logros (pixels/segundo)
|
||||
|
||||
// Variables de estado general
|
||||
State state_; // Estado en el que se encuentra el bucle principal
|
||||
float state_time_{0.0F}; // Tiempo acumulado en el estado actual
|
||||
float fade_accumulator_{0.0F}; // Acumulador para controlar el fade por tiempo
|
||||
SceneManager::Scene exit_scene_{SceneManager::Scene::GAME}; // Escena de destino al salir del título
|
||||
|
||||
// Variables para redefinir controles
|
||||
bool is_remapping_keyboard_{false}; // True si estamos redefiniendo teclado
|
||||
bool is_remapping_joystick_{false}; // True si estamos redefiniendo joystick
|
||||
int remap_step_{0}; // Paso actual en la redefinicion (0=LEFT, 1=RIGHT, 2=JUMP)
|
||||
std::array<SDL_Scancode, 3> temp_keys_; // Almacenamiento temporal de teclas capturadas
|
||||
std::array<int, 3> temp_buttons_; // Almacenamiento temporal de botones de gamepad capturados
|
||||
std::string remap_error_message_; // Mensaje de error si la tecla/boton es invalido
|
||||
float axis_cooldown_{0.0F}; // Cooldown para evitar múltiples capturas de ejes
|
||||
bool remap_completed_{false}; // True cuando se completa el remap (mostrar antes de guardar)
|
||||
};
|
||||
450
source/game/ui/console.cpp
Normal file
450
source/game/ui/console.cpp
Normal file
@@ -0,0 +1,450 @@
|
||||
#include "game/ui/console.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // Para ranges::transform
|
||||
#include <cctype> // Para toupper
|
||||
#include <sstream> // Para std::istringstream
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/sprite.hpp" // Para Sprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "game/options.hpp" // Para Options
|
||||
#include "game/ui/notifier.hpp" // Para Notifier
|
||||
|
||||
// ── Helpers de texto ──────────────────────────────────────────────────────────
|
||||
|
||||
// Convierte la entrada a uppercase y la divide en tokens por espacios
|
||||
static auto parseTokens(const std::string& input) -> std::vector<std::string> {
|
||||
std::vector<std::string> tokens;
|
||||
std::string token;
|
||||
for (unsigned char c : input) {
|
||||
if (c == ' ') {
|
||||
if (!token.empty()) {
|
||||
tokens.push_back(token);
|
||||
token.clear();
|
||||
}
|
||||
} else {
|
||||
token += static_cast<char>(std::toupper(c));
|
||||
}
|
||||
}
|
||||
if (!token.empty()) {
|
||||
tokens.push_back(token);
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
// Calcula la altura total de la consola para N líneas de mensaje (+ 1 línea de input)
|
||||
static auto calcTargetHeight(int num_msg_lines) -> float {
|
||||
constexpr int TEXT_SIZE = 6;
|
||||
constexpr int PADDING_IN_V = TEXT_SIZE / 2;
|
||||
return static_cast<float>((TEXT_SIZE * (num_msg_lines + 1)) + (PADDING_IN_V * 2));
|
||||
}
|
||||
|
||||
// Divide text en líneas respetando los \n existentes y haciendo word-wrap por ancho en píxeles
|
||||
auto Console::wrapText(const std::string& text) const -> std::vector<std::string> {
|
||||
constexpr int PADDING_IN_H = 6; // TEXT_SIZE; simétrico a ambos lados
|
||||
const int MAX_PX = static_cast<int>(Options::game.width) - (2 * PADDING_IN_H);
|
||||
|
||||
std::vector<std::string> result;
|
||||
std::istringstream segment_stream(text);
|
||||
std::string segment;
|
||||
|
||||
while (std::getline(segment_stream, segment)) {
|
||||
if (segment.empty()) {
|
||||
result.emplace_back();
|
||||
continue;
|
||||
}
|
||||
std::string current_line;
|
||||
std::istringstream word_stream(segment);
|
||||
std::string word;
|
||||
while (word_stream >> word) {
|
||||
const std::string TEST = current_line.empty() ? word : (current_line + ' ' + word);
|
||||
if (text_->length(TEST) <= MAX_PX) {
|
||||
current_line = TEST;
|
||||
} else {
|
||||
if (!current_line.empty()) { result.push_back(current_line); }
|
||||
current_line = word;
|
||||
}
|
||||
}
|
||||
if (!current_line.empty()) { result.push_back(current_line); }
|
||||
}
|
||||
|
||||
if (result.empty()) { result.emplace_back(); }
|
||||
return result;
|
||||
}
|
||||
|
||||
// ── Singleton ─────────────────────────────────────────────────────────────────
|
||||
|
||||
// [SINGLETON]
|
||||
Console* Console::console = nullptr;
|
||||
|
||||
// [SINGLETON]
|
||||
void Console::init(const std::string& font_name) {
|
||||
Console::console = new Console(font_name);
|
||||
}
|
||||
|
||||
// [SINGLETON]
|
||||
void Console::destroy() {
|
||||
delete Console::console;
|
||||
Console::console = nullptr;
|
||||
}
|
||||
|
||||
// [SINGLETON]
|
||||
auto Console::get() -> Console* {
|
||||
return Console::console;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Console::Console(const std::string& font_name)
|
||||
: text_(Resource::Cache::get()->getText(font_name)) {
|
||||
msg_lines_ = {std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION)};
|
||||
height_ = calcTargetHeight(static_cast<int>(msg_lines_.size()));
|
||||
target_height_ = height_;
|
||||
y_ = -height_;
|
||||
|
||||
// Cargar comandos desde YAML
|
||||
registry_.load("data/console/commands.yaml");
|
||||
|
||||
buildSurface();
|
||||
}
|
||||
|
||||
// Crea la Surface con el aspecto visual de la consola
|
||||
void Console::buildSurface() {
|
||||
const float WIDTH = Options::game.width;
|
||||
|
||||
surface_ = std::make_shared<Surface>(WIDTH, height_);
|
||||
|
||||
// Posición inicial (fuera de pantalla por arriba)
|
||||
SDL_FRect sprite_rect = {.x = 0, .y = y_, .w = WIDTH, .h = height_};
|
||||
sprite_ = std::make_shared<Sprite>(surface_, sprite_rect);
|
||||
|
||||
// Dibujo inicial del texto
|
||||
redrawText();
|
||||
}
|
||||
|
||||
// Redibuja el texto dinámico sobre la surface (fondo + borde + líneas)
|
||||
void Console::redrawText() {
|
||||
const float WIDTH = Options::game.width;
|
||||
constexpr int TEXT_SIZE = 6;
|
||||
constexpr int PADDING_IN_H = TEXT_SIZE;
|
||||
constexpr int PADDING_IN_V = TEXT_SIZE / 2;
|
||||
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface_);
|
||||
|
||||
// Fondo y borde
|
||||
surface_->clear(BG_COLOR);
|
||||
SDL_FRect rect = {.x = 0, .y = 0, .w = WIDTH, .h = height_};
|
||||
surface_->drawRectBorder(&rect, BORDER_COLOR);
|
||||
|
||||
// Líneas de mensaje con efecto typewriter (solo muestra los primeros typewriter_chars_)
|
||||
int y_pos = PADDING_IN_V;
|
||||
int remaining = typewriter_chars_;
|
||||
for (const auto& line : msg_lines_) {
|
||||
if (remaining <= 0) { break; }
|
||||
const int VISIBLE = std::min(remaining, static_cast<int>(line.size()));
|
||||
text_->writeColored(PADDING_IN_H, y_pos, line.substr(0, VISIBLE), MSG_COLOR);
|
||||
remaining -= VISIBLE;
|
||||
y_pos += TEXT_SIZE;
|
||||
}
|
||||
|
||||
// Línea de input (siempre la última)
|
||||
const bool SHOW_CURSOR = cursor_visible_ && (static_cast<int>(input_line_.size()) < MAX_LINE_CHARS);
|
||||
const std::string INPUT_STR = prompt_ + input_line_ + (SHOW_CURSOR ? "_" : "");
|
||||
text_->writeColored(PADDING_IN_H, y_pos, INPUT_STR, BORDER_COLOR);
|
||||
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
}
|
||||
|
||||
// Actualiza la animación de la consola
|
||||
void Console::update(float delta_time) { // NOLINT(readability-function-cognitive-complexity)
|
||||
if (status_ == Status::HIDDEN) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Parpadeo del cursor (solo cuando activa)
|
||||
if (status_ == Status::ACTIVE) {
|
||||
cursor_timer_ += delta_time;
|
||||
const float THRESHOLD = cursor_visible_ ? CURSOR_ON_TIME : CURSOR_OFF_TIME;
|
||||
if (cursor_timer_ >= THRESHOLD) {
|
||||
cursor_timer_ = 0.0F;
|
||||
cursor_visible_ = !cursor_visible_;
|
||||
}
|
||||
}
|
||||
|
||||
// Efecto typewriter: revelar letras una a una (solo cuando ACTIVE)
|
||||
if (status_ == Status::ACTIVE) {
|
||||
int total_chars = 0;
|
||||
for (const auto& line : msg_lines_) { total_chars += static_cast<int>(line.size()); }
|
||||
if (typewriter_chars_ < total_chars) {
|
||||
typewriter_timer_ += delta_time;
|
||||
while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < total_chars) {
|
||||
typewriter_timer_ -= TYPEWRITER_CHAR_DELAY;
|
||||
++typewriter_chars_;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Animación de altura (resize cuando msg_lines_ cambia); solo en ACTIVE
|
||||
if (status_ == Status::ACTIVE && height_ != target_height_) {
|
||||
const float PREV_HEIGHT = height_;
|
||||
if (height_ < target_height_) {
|
||||
height_ = std::min(height_ + (SLIDE_SPEED * delta_time), target_height_);
|
||||
} else {
|
||||
height_ = std::max(height_ - (SLIDE_SPEED * delta_time), target_height_);
|
||||
}
|
||||
// Actualizar el Notifier incrementalmente con el delta de altura
|
||||
if (Notifier::get() != nullptr) {
|
||||
const int DELTA_PX = static_cast<int>(height_) - static_cast<int>(PREV_HEIGHT);
|
||||
if (DELTA_PX > 0) {
|
||||
Notifier::get()->addYOffset(DELTA_PX);
|
||||
notifier_offset_applied_ += DELTA_PX;
|
||||
} else if (DELTA_PX < 0) {
|
||||
Notifier::get()->removeYOffset(-DELTA_PX);
|
||||
notifier_offset_applied_ += DELTA_PX;
|
||||
}
|
||||
}
|
||||
// Reconstruir la Surface al nuevo tamaño (pequeña: 256×~18-72px)
|
||||
const float WIDTH = Options::game.width;
|
||||
surface_ = std::make_shared<Surface>(WIDTH, height_);
|
||||
sprite_->setSurface(surface_);
|
||||
}
|
||||
|
||||
// Redibujar texto cada frame
|
||||
redrawText();
|
||||
|
||||
switch (status_) {
|
||||
case Status::RISING: {
|
||||
y_ += SLIDE_SPEED * delta_time;
|
||||
if (y_ >= 0.0F) {
|
||||
y_ = 0.0F;
|
||||
status_ = Status::ACTIVE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Status::VANISHING: {
|
||||
y_ -= SLIDE_SPEED * delta_time;
|
||||
if (y_ <= -height_) {
|
||||
y_ = -height_;
|
||||
status_ = Status::HIDDEN;
|
||||
// Resetear el mensaje una vez completamente oculta
|
||||
msg_lines_ = {std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION)};
|
||||
target_height_ = calcTargetHeight(static_cast<int>(msg_lines_.size()));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_FRect rect = {.x = 0, .y = y_, .w = Options::game.width, .h = height_};
|
||||
sprite_->setPosition(rect);
|
||||
sprite_->setClip({.x = 0.0F, .y = 0.0F, .w = Options::game.width, .h = height_});
|
||||
}
|
||||
|
||||
// Renderiza la consola
|
||||
void Console::render() {
|
||||
if (status_ == Status::HIDDEN) {
|
||||
return;
|
||||
}
|
||||
sprite_->render();
|
||||
}
|
||||
|
||||
// Activa o desactiva la consola
|
||||
void Console::toggle() {
|
||||
switch (status_) {
|
||||
case Status::HIDDEN:
|
||||
// Al abrir: la consola siempre empieza con 1 línea de mensaje (altura base)
|
||||
target_height_ = calcTargetHeight(static_cast<int>(msg_lines_.size()));
|
||||
height_ = target_height_;
|
||||
y_ = -height_;
|
||||
status_ = Status::RISING;
|
||||
input_line_.clear();
|
||||
cursor_timer_ = 0.0F;
|
||||
cursor_visible_ = true;
|
||||
// El mensaje inicial ("JDD Console v1.0") aparece completo, sin typewriter
|
||||
typewriter_chars_ = static_cast<int>(msg_lines_[0].size());
|
||||
typewriter_timer_ = 0.0F;
|
||||
SDL_StartTextInput(SDL_GetKeyboardFocus());
|
||||
if (Notifier::get() != nullptr) {
|
||||
const int OFFSET = static_cast<int>(height_);
|
||||
Notifier::get()->addYOffset(OFFSET);
|
||||
notifier_offset_applied_ = OFFSET;
|
||||
}
|
||||
if (on_toggle) { on_toggle(true); }
|
||||
break;
|
||||
case Status::ACTIVE:
|
||||
// Al cerrar: mantener el texto visible hasta que esté completamente oculta
|
||||
status_ = Status::VANISHING;
|
||||
target_height_ = height_; // No animar durante VANISHING
|
||||
history_index_ = -1;
|
||||
saved_input_.clear();
|
||||
SDL_StopTextInput(SDL_GetKeyboardFocus());
|
||||
if (Notifier::get() != nullptr) {
|
||||
Notifier::get()->removeYOffset(notifier_offset_applied_);
|
||||
notifier_offset_applied_ = 0;
|
||||
}
|
||||
if (on_toggle) { on_toggle(false); }
|
||||
break;
|
||||
default:
|
||||
// Durante RISING o VANISHING no se hace nada
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Procesa el evento SDL: entrada de texto, Backspace, Enter
|
||||
void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-function-cognitive-complexity)
|
||||
if (status_ != Status::ACTIVE) { return; }
|
||||
|
||||
if (event.type == SDL_EVENT_TEXT_INPUT) {
|
||||
// Filtrar caracteres de control (tab, newline, etc.)
|
||||
if (static_cast<unsigned char>(event.text.text[0]) < 32) { return; }
|
||||
if (static_cast<int>(input_line_.size()) < MAX_LINE_CHARS) {
|
||||
input_line_ += event.text.text;
|
||||
}
|
||||
tab_matches_.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||
switch (event.key.scancode) {
|
||||
case SDL_SCANCODE_BACKSPACE:
|
||||
tab_matches_.clear();
|
||||
if (!input_line_.empty()) { input_line_.pop_back(); }
|
||||
break;
|
||||
case SDL_SCANCODE_RETURN:
|
||||
case SDL_SCANCODE_KP_ENTER:
|
||||
processCommand();
|
||||
break;
|
||||
case SDL_SCANCODE_UP:
|
||||
// Navegar hacia atrás en el historial
|
||||
tab_matches_.clear();
|
||||
if (history_index_ < static_cast<int>(history_.size()) - 1) {
|
||||
if (history_index_ == -1) { saved_input_ = input_line_; }
|
||||
++history_index_;
|
||||
input_line_ = history_[static_cast<size_t>(history_index_)];
|
||||
}
|
||||
break;
|
||||
case SDL_SCANCODE_DOWN:
|
||||
// Navegar hacia el presente en el historial
|
||||
tab_matches_.clear();
|
||||
if (history_index_ >= 0) {
|
||||
--history_index_;
|
||||
input_line_ = (history_index_ == -1)
|
||||
? saved_input_
|
||||
: history_[static_cast<size_t>(history_index_)];
|
||||
}
|
||||
break;
|
||||
case SDL_SCANCODE_TAB: {
|
||||
if (tab_matches_.empty()) {
|
||||
// Calcular el input actual en mayúsculas
|
||||
std::string upper;
|
||||
for (unsigned char c : input_line_) { upper += static_cast<char>(std::toupper(c)); }
|
||||
|
||||
const size_t SPACE_POS = upper.rfind(' ');
|
||||
if (SPACE_POS == std::string::npos) {
|
||||
// Modo comando: ciclar keywords visibles que empiecen por el prefijo
|
||||
for (const auto& kw : registry_.getVisibleKeywords()) {
|
||||
if (upper.empty() || kw.starts_with(upper)) {
|
||||
tab_matches_.emplace_back(kw);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const std::string BASE_CMD = upper.substr(0, SPACE_POS);
|
||||
const std::string SUB_PREFIX = upper.substr(SPACE_POS + 1);
|
||||
const auto OPTS = registry_.getCompletions(BASE_CMD);
|
||||
for (const auto& arg : OPTS) {
|
||||
if (SUB_PREFIX.empty() || std::string_view{arg}.starts_with(SUB_PREFIX)) {
|
||||
tab_matches_.emplace_back(BASE_CMD + " " + arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
tab_index_ = -1;
|
||||
}
|
||||
if (tab_matches_.empty()) { break; }
|
||||
tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size());
|
||||
std::string result = tab_matches_[static_cast<size_t>(tab_index_)];
|
||||
for (char& c : result) { c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); }
|
||||
input_line_ = result;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ejecuta el comando introducido y reinicia la línea de input
|
||||
void Console::processCommand() {
|
||||
if (!input_line_.empty()) {
|
||||
// Añadir al historial (sin duplicados consecutivos)
|
||||
if (history_.empty() || history_.front() != input_line_) {
|
||||
history_.push_front(input_line_);
|
||||
if (static_cast<int>(history_.size()) > MAX_HISTORY_SIZE) {
|
||||
history_.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
const auto TOKENS = parseTokens(input_line_);
|
||||
if (!TOKENS.empty()) {
|
||||
const std::string& cmd = TOKENS[0];
|
||||
const std::vector<std::string> ARGS(TOKENS.begin() + 1, TOKENS.end());
|
||||
std::string result;
|
||||
bool instant = false;
|
||||
|
||||
const auto* def = registry_.findCommand(cmd);
|
||||
if (def != nullptr) {
|
||||
result = registry_.execute(cmd, ARGS);
|
||||
instant = def->instant;
|
||||
} else {
|
||||
std::string cmd_lower = cmd;
|
||||
std::ranges::transform(cmd_lower, cmd_lower.begin(), ::tolower);
|
||||
result = "Unknown: " + cmd_lower;
|
||||
}
|
||||
|
||||
// Word-wrap automático según el ancho disponible en píxeles
|
||||
msg_lines_ = wrapText(result);
|
||||
|
||||
// Actualizar la altura objetivo para animar el resize
|
||||
target_height_ = calcTargetHeight(static_cast<int>(msg_lines_.size()));
|
||||
|
||||
// Typewriter: instantáneo si el comando lo requiere, letra a letra si no
|
||||
if (instant) {
|
||||
int total = 0;
|
||||
for (const auto& l : msg_lines_) { total += static_cast<int>(l.size()); }
|
||||
typewriter_chars_ = total;
|
||||
} else {
|
||||
typewriter_chars_ = 0;
|
||||
}
|
||||
typewriter_timer_ = 0.0F;
|
||||
}
|
||||
}
|
||||
input_line_.clear();
|
||||
history_index_ = -1;
|
||||
saved_input_.clear();
|
||||
tab_matches_.clear();
|
||||
cursor_timer_ = 0.0F;
|
||||
cursor_visible_ = true;
|
||||
}
|
||||
|
||||
// Indica si la consola está activa (visible o en animación)
|
||||
auto Console::isActive() -> bool {
|
||||
return status_ != Status::HIDDEN;
|
||||
}
|
||||
|
||||
// Devuelve los píxeles visibles de la consola (sincronizado con la animación)
|
||||
auto Console::getVisibleHeight() -> int {
|
||||
if (status_ == Status::HIDDEN) { return 0; }
|
||||
return static_cast<int>(y_ + height_);
|
||||
}
|
||||
|
||||
// Scope de comandos
|
||||
void Console::setScope(const std::string& scope) { registry_.setScope(scope); }
|
||||
auto Console::getScope() const -> std::string { return registry_.getScope(); }
|
||||
117
source/game/ui/console.hpp
Normal file
117
source/game/ui/console.hpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <deque> // Para deque (historial)
|
||||
#include <functional> // Para function
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "game/ui/console_commands.hpp" // Para CommandRegistry
|
||||
|
||||
class Surface;
|
||||
class Sprite;
|
||||
class Text;
|
||||
|
||||
class Console {
|
||||
public:
|
||||
// Singleton
|
||||
static void init(const std::string& font_name);
|
||||
static void destroy();
|
||||
static auto get() -> Console*;
|
||||
|
||||
// Métodos principales
|
||||
void update(float delta_time);
|
||||
void render();
|
||||
void toggle();
|
||||
void handleEvent(const SDL_Event& event);
|
||||
|
||||
// Consultas
|
||||
auto isActive() -> bool; // true si RISING, ACTIVE o VANISHING
|
||||
auto getVisibleHeight() -> int; // Píxeles visibles actuales (0 = oculta, height_ = totalmente visible)
|
||||
[[nodiscard]] auto getText() const -> std::shared_ptr<Text> { return text_; }
|
||||
|
||||
// Prompt configurable (por defecto "> ")
|
||||
void setPrompt(const std::string& prompt) { prompt_ = prompt; }
|
||||
|
||||
// Scope de comandos (filtra help y tab completion)
|
||||
void setScope(const std::string& scope);
|
||||
[[nodiscard]] auto getScope() const -> std::string;
|
||||
|
||||
// Callback llamado al abrir (true) o cerrar (false) la consola
|
||||
std::function<void(bool)> on_toggle;
|
||||
|
||||
private:
|
||||
enum class Status {
|
||||
HIDDEN,
|
||||
RISING,
|
||||
ACTIVE,
|
||||
VANISHING,
|
||||
};
|
||||
|
||||
// Constantes visuales
|
||||
static constexpr Uint8 BG_COLOR = 0; // PaletteColor::BLACK
|
||||
static constexpr Uint8 BORDER_COLOR = 9; // PaletteColor::BRIGHT_GREEN
|
||||
static constexpr Uint8 MSG_COLOR = 8; // PaletteColor::GREEN
|
||||
static constexpr float SLIDE_SPEED = 180.0F;
|
||||
|
||||
// Constantes de consola
|
||||
static constexpr std::string_view CONSOLE_NAME = "JDD Console";
|
||||
static constexpr std::string_view CONSOLE_VERSION = "v2.2";
|
||||
static constexpr int MAX_LINE_CHARS = 32;
|
||||
static constexpr int MAX_HISTORY_SIZE = 20;
|
||||
static constexpr float CURSOR_ON_TIME = 0.5F;
|
||||
static constexpr float CURSOR_OFF_TIME = 0.3F;
|
||||
static constexpr float TYPEWRITER_CHAR_DELAY = 0.01F; // segundos entre letra y letra
|
||||
|
||||
// [SINGLETON]
|
||||
static Console* console;
|
||||
|
||||
// Constructor y destructor privados [SINGLETON]
|
||||
explicit Console(const std::string& font_name);
|
||||
~Console() = default;
|
||||
|
||||
// Métodos privados
|
||||
void buildSurface(); // Crea la Surface con el aspecto visual
|
||||
void redrawText(); // Redibuja el texto dinámico (msg + input + cursor)
|
||||
void processCommand(); // Procesa el comando introducido por el usuario
|
||||
[[nodiscard]] auto wrapText(const std::string& text) const -> std::vector<std::string>; // Word-wrap por ancho en píxeles
|
||||
|
||||
// Objetos de renderizado
|
||||
std::shared_ptr<Text> text_;
|
||||
std::shared_ptr<Surface> surface_;
|
||||
std::shared_ptr<Sprite> sprite_;
|
||||
|
||||
// Estado de la animación
|
||||
Status status_{Status::HIDDEN};
|
||||
float y_{0.0F}; // Posición Y actual (animada)
|
||||
float height_{0.0F}; // Altura del panel
|
||||
|
||||
// Estado de la entrada de texto
|
||||
std::vector<std::string> msg_lines_; // Líneas de mensaje (1 o más)
|
||||
std::string input_line_;
|
||||
std::string prompt_{"> "}; // Prompt configurable
|
||||
float cursor_timer_{0.0F};
|
||||
bool cursor_visible_{true};
|
||||
|
||||
// Efecto typewriter
|
||||
int typewriter_chars_{0}; // Caracteres de msg_lines_ actualmente visibles
|
||||
float typewriter_timer_{0.0F};
|
||||
|
||||
// Animación de altura dinámica
|
||||
float target_height_{0.0F}; // Altura objetivo (según número de líneas de mensaje)
|
||||
int notifier_offset_applied_{0}; // Acumulador del offset enviado al Notifier
|
||||
|
||||
// Historial de comandos (navegable con flechas arriba/abajo)
|
||||
std::deque<std::string> history_;
|
||||
int history_index_{-1}; // -1 = en la entrada actual (presente)
|
||||
std::string saved_input_; // guarda input_line_ al empezar a navegar
|
||||
|
||||
// Estado de autocompletado (TAB)
|
||||
std::vector<std::string> tab_matches_; // Comandos que coinciden con el prefijo actual
|
||||
int tab_index_{-1}; // Índice actual en tab_matches_
|
||||
|
||||
// Registro de comandos (metadatos YAML + handlers C++)
|
||||
CommandRegistry registry_;
|
||||
};
|
||||
1363
source/game/ui/console_commands.cpp
Normal file
1363
source/game/ui/console_commands.cpp
Normal file
File diff suppressed because it is too large
Load Diff
61
source/game/ui/console_commands.hpp
Normal file
61
source/game/ui/console_commands.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional> // Para function
|
||||
#include <string> // Para string
|
||||
#include <unordered_map> // Para unordered_map
|
||||
#include <vector> // Para vector
|
||||
|
||||
// Definición de un comando de consola (metadatos cargados desde YAML)
|
||||
struct CommandDef {
|
||||
std::string keyword;
|
||||
std::string handler_id;
|
||||
std::string category;
|
||||
std::string description;
|
||||
std::string usage;
|
||||
bool instant{false};
|
||||
bool hidden{false};
|
||||
bool debug_only{false};
|
||||
bool help_hidden{false};
|
||||
bool dynamic_completions{false};
|
||||
std::vector<std::string> scopes; // Ámbitos: "global", "game", "editor", "debug"
|
||||
std::unordered_map<std::string, std::vector<std::string>> completions;
|
||||
};
|
||||
|
||||
// Tipo de función handler para comandos
|
||||
using CommandHandler = std::function<std::string(const std::vector<std::string>& args)>;
|
||||
|
||||
// Proveedor de completions dinámicas: devuelve las opciones para TAB en UPPERCASE
|
||||
using DynamicCompletionProvider = std::function<std::vector<std::string>()>;
|
||||
|
||||
// Registro de comandos: une metadatos YAML con handlers C++
|
||||
class CommandRegistry {
|
||||
public:
|
||||
// Carga los metadatos de comandos desde un archivo YAML y registra los handlers
|
||||
void load(const std::string& yaml_path);
|
||||
|
||||
// Búsqueda y ejecución
|
||||
[[nodiscard]] auto findCommand(const std::string& keyword) const -> const CommandDef*;
|
||||
auto execute(const std::string& keyword, const std::vector<std::string>& args) const -> std::string;
|
||||
|
||||
// Generación de ayuda (auto-generada desde los metadatos)
|
||||
[[nodiscard]] auto generateTerminalHelp() const -> std::string;
|
||||
[[nodiscard]] auto generateConsoleHelp() const -> std::string;
|
||||
|
||||
// Scope activo (filtra comandos visibles en help y tab completion)
|
||||
void setScope(const std::string& scope) { active_scope_ = scope; }
|
||||
[[nodiscard]] auto getScope() const -> const std::string& { return active_scope_; }
|
||||
|
||||
// TAB completion
|
||||
[[nodiscard]] auto getCompletions(const std::string& path) const -> std::vector<std::string>;
|
||||
[[nodiscard]] auto getVisibleKeywords() const -> std::vector<std::string>;
|
||||
|
||||
private:
|
||||
std::vector<CommandDef> commands_;
|
||||
std::unordered_map<std::string, CommandHandler> handlers_;
|
||||
std::unordered_map<std::string, std::vector<std::string>> completions_map_;
|
||||
std::unordered_map<std::string, DynamicCompletionProvider> dynamic_providers_;
|
||||
std::string active_scope_; // Scope activo ("" = sin filtro, muestra todo)
|
||||
|
||||
void registerHandlers();
|
||||
[[nodiscard]] auto isCommandVisible(const CommandDef& cmd) const -> bool;
|
||||
};
|
||||
293
source/game/ui/notifier.cpp
Normal file
293
source/game/ui/notifier.cpp
Normal file
@@ -0,0 +1,293 @@
|
||||
#include "game/ui/notifier.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // Para remove_if
|
||||
#include <iterator> // Para prev
|
||||
#include <ranges> // Para reverse_view
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "core/audio/audio.hpp" // Para Audio
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/sprite/sprite.hpp" // Para SSprite
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/text.hpp" // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "game/options.hpp" // Para Options, options, NotificationPosition
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/utils.hpp" // Para PaletteColor
|
||||
|
||||
// [SINGLETON]
|
||||
Notifier* Notifier::notifier = nullptr;
|
||||
|
||||
// Definición de estilos predefinidos
|
||||
const Notifier::Style Notifier::Style::DEFAULT = {
|
||||
.bg_color = static_cast<Uint8>(PaletteColor::BLUE),
|
||||
.border_color = static_cast<Uint8>(PaletteColor::CYAN),
|
||||
.text_color = static_cast<Uint8>(PaletteColor::CYAN),
|
||||
.shape = Notifier::Shape::SQUARED,
|
||||
.text_align = Notifier::TextAlign::CENTER,
|
||||
.duration = 2.0F,
|
||||
.sound_file = "notify.wav",
|
||||
.play_sound = false};
|
||||
|
||||
const Notifier::Style Notifier::Style::CHEEVO = {
|
||||
.bg_color = static_cast<Uint8>(PaletteColor::MAGENTA),
|
||||
.border_color = static_cast<Uint8>(PaletteColor::BRIGHT_MAGENTA),
|
||||
.text_color = static_cast<Uint8>(PaletteColor::WHITE),
|
||||
.shape = Notifier::Shape::SQUARED,
|
||||
.text_align = Notifier::TextAlign::CENTER,
|
||||
.duration = 4.0F,
|
||||
.sound_file = "notify.wav",
|
||||
.play_sound = true};
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Notifier::init(const std::string& icon_file, const std::string& text) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
Notifier::notifier = new Notifier(icon_file, text);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Notifier::destroy() {
|
||||
delete Notifier::notifier;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
auto Notifier::get() -> Notifier* {
|
||||
return Notifier::notifier;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Notifier::Notifier(const std::string& icon_file, const std::string& text)
|
||||
: icon_surface_(!icon_file.empty() ? Resource::Cache::get()->getSurface(icon_file) : nullptr),
|
||||
text_(Resource::Cache::get()->getText(text)),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()),
|
||||
has_icons_(!icon_file.empty()) {}
|
||||
|
||||
// Dibuja las notificaciones por pantalla
|
||||
void Notifier::render() {
|
||||
for (auto& notification : std::ranges::reverse_view(notifications_)) {
|
||||
notification.sprite->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void Notifier::update(float delta_time) {
|
||||
for (auto& notification : notifications_) {
|
||||
// Si la notificación anterior está "saliendo", no hagas nada
|
||||
if (!notifications_.empty() && ¬ification != ¬ifications_.front()) {
|
||||
const auto& previous_notification = *(std::prev(¬ification));
|
||||
if (previous_notification.state == Status::RISING) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (notification.state) {
|
||||
case Status::RISING: {
|
||||
const float DISPLACEMENT = SLIDE_SPEED * delta_time;
|
||||
notification.rect.y += DISPLACEMENT;
|
||||
|
||||
if (notification.rect.y >= notification.y) {
|
||||
notification.rect.y = notification.y;
|
||||
notification.state = Status::STAY;
|
||||
notification.elapsed_time = 0.0F;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Status::STAY: {
|
||||
notification.elapsed_time += delta_time;
|
||||
if (notification.elapsed_time >= notification.display_duration) {
|
||||
notification.state = Status::VANISHING;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Status::VANISHING: {
|
||||
const float DISPLACEMENT = SLIDE_SPEED * delta_time;
|
||||
notification.rect.y -= DISPLACEMENT;
|
||||
|
||||
const float TARGET_Y = notification.y - notification.travel_dist;
|
||||
if (notification.rect.y <= TARGET_Y) {
|
||||
notification.rect.y = TARGET_Y;
|
||||
notification.state = Status::FINISHED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Status::FINISHED:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
notification.sprite->setPosition(notification.rect);
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
}
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
void Notifier::clearFinishedNotifications() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
auto result = std::ranges::remove_if(notifications_, [](const Notification& notification) -> bool {
|
||||
return notification.state == Status::FINISHED;
|
||||
});
|
||||
notifications_.erase(result.begin(), result.end());
|
||||
}
|
||||
|
||||
void Notifier::show(std::vector<std::string> texts, const Style& style, int icon, bool can_be_removed, const std::string& code) {
|
||||
// Si no hay texto, acaba
|
||||
if (texts.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Si las notificaciones no se apilan, elimina las anteriores
|
||||
if (!stack_) {
|
||||
clearNotifications();
|
||||
}
|
||||
|
||||
// Elimina las cadenas vacías
|
||||
auto result = std::ranges::remove_if(texts, [](const std::string& s) -> bool { return s.empty(); });
|
||||
texts.erase(result.begin(), result.end());
|
||||
|
||||
// Encuentra la cadena más larga
|
||||
std::string longest;
|
||||
for (const auto& text : texts) {
|
||||
if (text.length() > longest.length()) {
|
||||
longest = text;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa variables
|
||||
const int TEXT_SIZE = 6;
|
||||
const auto PADDING_IN_H = TEXT_SIZE;
|
||||
const auto PADDING_IN_V = TEXT_SIZE / 2;
|
||||
const int ICON_SPACE = icon >= 0 ? ICON_SIZE + PADDING_IN_H : 0;
|
||||
const TextAlign TEXT_IS = ICON_SPACE > 0 ? TextAlign::LEFT : style.text_align;
|
||||
const float WIDTH = Options::game.width - (PADDING_OUT * 2);
|
||||
const float HEIGHT = (TEXT_SIZE * texts.size()) + (PADDING_IN_V * 2);
|
||||
const auto SHAPE = style.shape;
|
||||
|
||||
// Posición horizontal
|
||||
float desp_h = ((Options::game.width / 2) - (WIDTH / 2));
|
||||
;
|
||||
|
||||
// Posición vertical
|
||||
const int DESP_V = y_offset_;
|
||||
|
||||
// Offset
|
||||
const auto TRAVEL_DIST = HEIGHT + PADDING_OUT;
|
||||
const int TRAVEL_MOD = 1;
|
||||
const int OFFSET = !notifications_.empty() ? notifications_.back().y + (TRAVEL_MOD * notifications_.back().travel_dist) : DESP_V;
|
||||
|
||||
// Crea la notificacion
|
||||
Notification n;
|
||||
|
||||
// Inicializa variables
|
||||
n.code = code;
|
||||
n.can_be_removed = can_be_removed;
|
||||
n.y = OFFSET;
|
||||
n.travel_dist = TRAVEL_DIST;
|
||||
n.texts = texts;
|
||||
n.shape = SHAPE;
|
||||
n.display_duration = style.duration;
|
||||
const float Y_POS = OFFSET + -TRAVEL_DIST;
|
||||
n.rect = {.x = desp_h, .y = Y_POS, .w = WIDTH, .h = HEIGHT};
|
||||
|
||||
// Crea la textura
|
||||
n.surface = std::make_shared<Surface>(WIDTH, HEIGHT);
|
||||
|
||||
// Prepara para dibujar en la textura
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(n.surface);
|
||||
|
||||
// Dibuja el fondo de la notificación
|
||||
SDL_FRect rect;
|
||||
if (SHAPE == Shape::ROUNDED) {
|
||||
rect = {.x = 4, .y = 0, .w = WIDTH - (4 * 2), .h = HEIGHT};
|
||||
n.surface->fillRect(&rect, style.bg_color);
|
||||
|
||||
rect = {.x = 4 / 2, .y = 1, .w = WIDTH - 4, .h = HEIGHT - 2};
|
||||
n.surface->fillRect(&rect, style.bg_color);
|
||||
|
||||
rect = {.x = 1, .y = 4 / 2, .w = WIDTH - 2, .h = HEIGHT - 4};
|
||||
n.surface->fillRect(&rect, style.bg_color);
|
||||
|
||||
rect = {.x = 0, .y = 4, .w = WIDTH, .h = HEIGHT - (4 * 2)};
|
||||
n.surface->fillRect(&rect, style.bg_color);
|
||||
}
|
||||
|
||||
else if (SHAPE == Shape::SQUARED) {
|
||||
n.surface->clear(style.bg_color);
|
||||
SDL_FRect squared_rect = {.x = 0, .y = 0, .w = n.surface->getWidth(), .h = n.surface->getHeight()};
|
||||
n.surface->drawRectBorder(&squared_rect, style.border_color);
|
||||
}
|
||||
|
||||
// Dibuja el icono de la notificación
|
||||
if (has_icons_ && icon >= 0 && texts.size() >= 2) {
|
||||
auto sp = std::make_unique<Sprite>(icon_surface_, SDL_FRect{.x = 0, .y = 0, .w = ICON_SIZE, .h = ICON_SIZE});
|
||||
sp->setPosition({.x = PADDING_IN_H, .y = PADDING_IN_V, .w = ICON_SIZE, .h = ICON_SIZE});
|
||||
sp->setClip(SDL_FRect{.x = ICON_SIZE * (icon % 10), .y = ICON_SIZE * (icon / 10), .w = ICON_SIZE, .h = ICON_SIZE});
|
||||
sp->render();
|
||||
}
|
||||
|
||||
// Escribe el texto de la notificación
|
||||
const auto COLOR = style.text_color;
|
||||
int iterator = 0;
|
||||
for (const auto& text : texts) {
|
||||
switch (TEXT_IS) {
|
||||
case TextAlign::LEFT:
|
||||
text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + (iterator * (TEXT_SIZE + 1)), text, COLOR);
|
||||
break;
|
||||
case TextAlign::CENTER:
|
||||
text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, WIDTH / 2, PADDING_IN_V + (iterator * (TEXT_SIZE + 1)), text, 1, COLOR);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
++iterator;
|
||||
}
|
||||
|
||||
// Deja de dibujar en la textura
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
|
||||
// Crea el sprite de la notificación
|
||||
n.sprite = std::make_shared<Sprite>(n.surface, n.rect);
|
||||
|
||||
// Añade la notificación a la lista
|
||||
notifications_.emplace_back(n);
|
||||
|
||||
// Reproduce el sonido de la notificación
|
||||
if (style.play_sound && !style.sound_file.empty()) {
|
||||
Audio::get()->playSound(style.sound_file, Audio::Group::INTERFACE);
|
||||
}
|
||||
}
|
||||
|
||||
// Indica si hay notificaciones activas
|
||||
auto Notifier::isActive() -> bool { return !notifications_.empty(); }
|
||||
|
||||
// Finaliza y elimnina todas las notificaciones activas
|
||||
void Notifier::clearNotifications() {
|
||||
for (auto& notification : notifications_) {
|
||||
if (notification.can_be_removed) {
|
||||
notification.state = Status::FINISHED;
|
||||
}
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
}
|
||||
|
||||
// Ajusta el offset vertical base
|
||||
void Notifier::addYOffset(int px) { y_offset_ += px; }
|
||||
void Notifier::removeYOffset(int px) { y_offset_ -= px; }
|
||||
|
||||
// Obtiene los códigos de las notificaciones
|
||||
auto Notifier::getCodes() -> std::vector<std::string> {
|
||||
std::vector<std::string> codes;
|
||||
codes.reserve(notifications_.size());
|
||||
for (const auto& notification : notifications_) {
|
||||
codes.emplace_back(notification.code);
|
||||
}
|
||||
return codes;
|
||||
}
|
||||
115
source/game/ui/notifier.hpp
Normal file
115
source/game/ui/notifier.hpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
class Sprite; // lines 8-8
|
||||
class Surface; // lines 10-10
|
||||
class Text; // lines 9-9
|
||||
class DeltaTimer; // lines 11-11
|
||||
|
||||
class Notifier {
|
||||
public:
|
||||
// Justificado para las notificaciones
|
||||
enum class TextAlign {
|
||||
LEFT,
|
||||
CENTER,
|
||||
};
|
||||
|
||||
// Forma de las notificaciones
|
||||
enum class Shape {
|
||||
ROUNDED,
|
||||
SQUARED,
|
||||
};
|
||||
|
||||
// Estilo de notificación
|
||||
struct Style {
|
||||
Uint8 bg_color; // Color de fondo
|
||||
Uint8 border_color; // Color del borde
|
||||
Uint8 text_color; // Color del texto
|
||||
Shape shape; // Forma (ROUNDED/SQUARED)
|
||||
TextAlign text_align; // Alineación del texto
|
||||
float duration; // Duración en segundos
|
||||
std::string sound_file; // Archivo de sonido (vacío = sin sonido)
|
||||
bool play_sound; // Si reproduce sonido
|
||||
|
||||
// Estilos predefinidos
|
||||
static const Style DEFAULT;
|
||||
static const Style CHEEVO;
|
||||
};
|
||||
|
||||
// Gestión singleton
|
||||
static void init(const std::string& icon_file, const std::string& text); // Inicialización
|
||||
static void destroy(); // Destrucción
|
||||
static auto get() -> Notifier*; // Acceso al singleton
|
||||
|
||||
// Métodos principales
|
||||
void render(); // Renderizado
|
||||
void update(float delta_time); // Actualización lógica
|
||||
void show(
|
||||
std::vector<std::string> texts,
|
||||
const Style& style = Style::DEFAULT,
|
||||
int icon = -1,
|
||||
bool can_be_removed = true,
|
||||
const std::string& code = std::string()); // Mostrar notificación
|
||||
|
||||
// Consultas
|
||||
auto isActive() -> bool; // Indica si hay notificaciones activas
|
||||
auto getCodes() -> std::vector<std::string>; // Obtiene códigos de notificaciones
|
||||
|
||||
// Offset vertical (para evitar solapamiento con Console y renderInfo)
|
||||
void addYOffset(int px); // Suma píxeles al offset base
|
||||
void removeYOffset(int px); // Resta píxeles al offset base
|
||||
|
||||
private:
|
||||
// Tipos anidados
|
||||
enum class Status {
|
||||
RISING,
|
||||
STAY,
|
||||
VANISHING,
|
||||
FINISHED,
|
||||
};
|
||||
|
||||
struct Notification {
|
||||
std::shared_ptr<Surface> surface{nullptr};
|
||||
std::shared_ptr<Sprite> sprite{nullptr};
|
||||
std::vector<std::string> texts;
|
||||
Status state{Status::RISING};
|
||||
Shape shape{Shape::SQUARED};
|
||||
SDL_FRect rect{.x = 0.0F, .y = 0.0F, .w = 0.0F, .h = 0.0F};
|
||||
int y{0};
|
||||
int travel_dist{0};
|
||||
std::string code;
|
||||
bool can_be_removed{true};
|
||||
int height{0};
|
||||
float elapsed_time{0.0F};
|
||||
float display_duration{0.0F};
|
||||
};
|
||||
|
||||
// Constantes
|
||||
static constexpr float ICON_SIZE = 16.0F;
|
||||
static constexpr float PADDING_OUT = 0.0F;
|
||||
static constexpr float SLIDE_SPEED = 120.0F; // Pixels per second for slide animations
|
||||
|
||||
// [SINGLETON] Objeto notifier
|
||||
static Notifier* notifier;
|
||||
|
||||
// Métodos privados
|
||||
void clearFinishedNotifications(); // Elimina las notificaciones finalizadas
|
||||
void clearNotifications(); // Finaliza y elimina todas las notificaciones activas
|
||||
|
||||
// Constructor y destructor privados [SINGLETON]
|
||||
Notifier(const std::string& icon_file, const std::string& text);
|
||||
~Notifier() = default;
|
||||
|
||||
// Variables miembro
|
||||
std::shared_ptr<Surface> icon_surface_; // Textura para los iconos
|
||||
std::shared_ptr<Text> text_; // Objeto para dibujar texto
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer for frame-independent animations
|
||||
std::vector<Notification> notifications_; // Lista de notificaciones activas
|
||||
bool stack_{false}; // Indica si las notificaciones se apilan
|
||||
bool has_icons_{false}; // Indica si el notificador tiene textura para iconos
|
||||
int y_offset_{0}; // Offset vertical base (ajustado por Console y renderInfo)
|
||||
};
|
||||
Reference in New Issue
Block a user