primer commit
This commit is contained in:
91
source/game/defaults.hpp
Normal file
91
source/game/defaults.hpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen::Filter
|
||||
#include "utils/defines.hpp" // Para GameCanvas
|
||||
|
||||
// Forward declarations from Options namespace
|
||||
namespace Options {
|
||||
// enum class ControlScheme;
|
||||
enum class NotificationPosition;
|
||||
} // namespace Options
|
||||
|
||||
namespace Defaults {
|
||||
|
||||
// --- CANVAS ---
|
||||
// Dimensiones del canvas del juego (usa GameCanvas como fuente única)
|
||||
namespace Canvas {
|
||||
constexpr int WIDTH = GameCanvas::WIDTH; // Ancho del canvas del juego (320)
|
||||
constexpr int HEIGHT = GameCanvas::HEIGHT; // Alto del canvas del juego (240)
|
||||
} // namespace Canvas
|
||||
|
||||
// --- WINDOW ---
|
||||
namespace Window {
|
||||
constexpr int ZOOM = 2; // Zoom de la ventana por defecto
|
||||
} // namespace Window
|
||||
|
||||
// --- VIDEO ---
|
||||
namespace 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 SHADERS = false; // Shaders desactivados 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
|
||||
} // namespace Video
|
||||
|
||||
// --- BORDER ---
|
||||
namespace 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 defecto
|
||||
} // namespace Border
|
||||
|
||||
// --- AUDIO ---
|
||||
namespace Audio {
|
||||
constexpr float VOLUME = 1.0F; // Volumen por defecto
|
||||
constexpr bool ENABLED = true; // Audio por defecto
|
||||
} // namespace Audio
|
||||
|
||||
// --- MUSIC ---
|
||||
namespace Music {
|
||||
constexpr float VOLUME = 0.8F; // Volumen por defecto de la musica
|
||||
constexpr bool ENABLED = true; // Musica habilitada por defecto
|
||||
constexpr const char* TITLE_TRACK = "574070_KUVO_Farewell_to_school.ogg"; // Musica de la escena title
|
||||
constexpr const char* GAME_TRACK = "574071_EA_DTV.ogg"; // Musica de la escena game
|
||||
constexpr int FADE_DURATION_MS = 1000; // Duracion del fade out en milisegundos
|
||||
} // namespace Music
|
||||
|
||||
// --- SOUND ---
|
||||
namespace Sound {
|
||||
constexpr float VOLUME = 1.0F; // Volumen por defecto de los efectos de sonido
|
||||
constexpr bool ENABLED = true; // Sonido habilitado por defecto
|
||||
constexpr const char* JUMP = "jump.wav"; // Sonido de salto
|
||||
constexpr const char* HIT = "hit.wav"; // Sonido de golpe/daño
|
||||
constexpr const char* LAND = "land.wav"; // Sonido de aterrizaje
|
||||
constexpr const char* ITEM = "item.wav"; // Sonido de recoger item
|
||||
constexpr const char* NOTIFY = "notify.wav"; // Sonido de notificación
|
||||
} // namespace Sound
|
||||
|
||||
// --- CHEATS ---
|
||||
namespace 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
|
||||
constexpr bool ALTERNATE_SKIN = false; // Skin alternativa desactivada por defecto
|
||||
} // namespace Cheat
|
||||
|
||||
// --- CONTROLS ---
|
||||
namespace 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 Controls
|
||||
|
||||
} // namespace Defaults
|
||||
96
source/game/entities/enemy.cpp
Normal file
96
source/game/entities/enemy.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "game/entities/enemy.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cstdlib> // Para rand
|
||||
|
||||
#include "core/rendering/surface_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<SurfaceAnimatedSprite>(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));
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
||||
void Enemy::checkPath() {
|
||||
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_;
|
||||
}
|
||||
51
source/game/entities/enemy.hpp
Normal file
51
source/game/entities/enemy.hpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
class SurfaceAnimatedSprite; // 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
|
||||
|
||||
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<SurfaceAnimatedSprite> 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
|
||||
};
|
||||
56
source/game/entities/item.cpp
Normal file
56
source/game/entities/item.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "game/entities/item.hpp"
|
||||
|
||||
#include "core/rendering/surface_sprite.hpp" // Para SSprite
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
|
||||
// Constructor
|
||||
Item::Item(const Data& item)
|
||||
: sprite_(std::make_shared<SurfaceSprite>(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 {
|
||||
// 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 {
|
||||
const SDL_FPoint P = {sprite_->getX(), sprite_->getY()};
|
||||
return P;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
44
source/game/entities/item.hpp
Normal file
44
source/game/entities/item.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
class SurfaceSprite;
|
||||
|
||||
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
|
||||
|
||||
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<SurfaceSprite> 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
|
||||
};
|
||||
559
source/game/entities/player.cpp
Normal file
559
source/game/entities/player.cpp
Normal file
@@ -0,0 +1,559 @@
|
||||
// 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/surface_animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "game/defaults.hpp" // Para Defaults::Sound
|
||||
#include "game/gameplay/room.hpp" // Para Room, TileType
|
||||
#include "game/options.hpp" // Para Cheat, Options, options
|
||||
#include "utils/color.hpp" // Para Color
|
||||
#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();
|
||||
|
||||
previous_state_ = state_;
|
||||
}
|
||||
|
||||
// Pinta el jugador en pantalla
|
||||
void Player::render() {
|
||||
sprite_->render();
|
||||
#ifdef _DEBUG
|
||||
if (Debug::get()->isEnabled()) {
|
||||
Screen::get()->getRendererSurface()->putPixel(under_right_foot_.x, under_right_foot_.y, Color::index(Color::Cpc::GREEN));
|
||||
Screen::get()->getRendererSurface()->putPixel(under_left_foot_.x, under_left_foot_.y, Color::index(Color::Cpc::GREEN));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Player::update(float delta_time) {
|
||||
if (!is_paused_) {
|
||||
handleInput();
|
||||
updateState(delta_time);
|
||||
move(delta_time);
|
||||
animate(delta_time);
|
||||
border_ = handleBorders();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas y modifica variables
|
||||
void Player::handleInput() {
|
||||
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_AIR:
|
||||
moveOnAir(delta_time);
|
||||
break;
|
||||
}
|
||||
syncSpriteAndCollider(); // Actualiza la posición del sprite y las colisiones
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->add(std::string("X : " + std::to_string(static_cast<int>(x_))));
|
||||
Debug::get()->add(std::string("Y : " + std::to_string(static_cast<int>(y_))));
|
||||
Debug::get()->add(std::string("LGP: " + std::to_string(last_grounded_position_)));
|
||||
switch (state_) {
|
||||
case State::ON_GROUND:
|
||||
Debug::get()->add(std::string("ON_GROUND"));
|
||||
break;
|
||||
case State::ON_AIR:
|
||||
Debug::get()->add(std::string("ON_AIR"));
|
||||
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) {
|
||||
// Pasar a ON_AIR sin impulso de salto (caída)
|
||||
previous_state_ = state_;
|
||||
state_ = State::ON_AIR;
|
||||
last_grounded_position_ = static_cast<int>(y_);
|
||||
vy_ = 0.0F; // Sin impulso inicial, la gravedad lo acelerará
|
||||
}
|
||||
}
|
||||
|
||||
void Player::transitionToState(State state) {
|
||||
previous_state_ = state_;
|
||||
state_ = state;
|
||||
|
||||
switch (state) {
|
||||
case State::ON_GROUND:
|
||||
vy_ = 0;
|
||||
break;
|
||||
case State::ON_AIR:
|
||||
if (previous_state_ == State::ON_GROUND) {
|
||||
vy_ = JUMP_VELOCITY; // Impulso de salto
|
||||
last_grounded_position_ = y_;
|
||||
Audio::get()->playSound(Defaults::Sound::JUMP, Audio::Group::GAME);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::updateState(float delta_time) {
|
||||
switch (state_) {
|
||||
case State::ON_GROUND:
|
||||
updateOnGround(delta_time);
|
||||
break;
|
||||
case State::ON_AIR:
|
||||
updateOnAir(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::ON_AIR); }
|
||||
}
|
||||
|
||||
// Actualización lógica del estado ON_AIR
|
||||
void Player::updateOnAir(float delta_time) {
|
||||
(void)delta_time; // No usado, pero se mantiene por consistencia de interfaz
|
||||
auto_movement_ = false; // Desactiva el movimiento automático mientras está en el aire
|
||||
}
|
||||
|
||||
// Movimiento físico del estado ON_GROUND
|
||||
void Player::moveOnGround(float delta_time) {
|
||||
// Determina cuál debe ser la velocidad a partir de automovement o de wanna_go_
|
||||
updateVelocity(delta_time);
|
||||
|
||||
if (vx_ == 0.0F) { return; }
|
||||
|
||||
// Movimiento horizontal y colision con muros
|
||||
applyHorizontalMovement(delta_time);
|
||||
}
|
||||
|
||||
// Movimiento físico del estado ON_AIR
|
||||
void Player::moveOnAir(float delta_time) {
|
||||
// Movimiento horizontal (el jugador puede moverse en el aire)
|
||||
updateVelocity(delta_time);
|
||||
applyHorizontalMovement(delta_time);
|
||||
|
||||
// Aplicar gravedad
|
||||
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);
|
||||
const int POS = room_->checkBottomSurfaces(PROJECTION);
|
||||
|
||||
if (POS == Collision::NONE) {
|
||||
y_ += DISPLACEMENT_Y;
|
||||
} else {
|
||||
// Colisión con el techo: detener ascenso
|
||||
y_ = POS + 1;
|
||||
vy_ = 0.0F;
|
||||
}
|
||||
}
|
||||
// Movimiento vertical hacia abajo
|
||||
else if (vy_ > 0.0F) {
|
||||
const SDL_FRect PROJECTION = getProjection(Direction::DOWN, DISPLACEMENT_Y);
|
||||
handleLandingFromAir(DISPLACEMENT_Y, 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) {
|
||||
return Room::Border::BOTTOM;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
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 se aplica siempre que el jugador está en el aire
|
||||
if (state_ == State::ON_AIR) {
|
||||
vy_ += GRAVITY_FORCE * delta_time;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece la animación del jugador
|
||||
void Player::animate(float delta_time) {
|
||||
if (vx_ != 0) {
|
||||
sprite_->update(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
// 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_);
|
||||
|
||||
return on_top_surface || on_conveyor_belt;
|
||||
}
|
||||
|
||||
// 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 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) {
|
||||
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 cheats)
|
||||
void Player::setColor(Uint8 color) {
|
||||
if (color != 0) {
|
||||
color_ = color;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
|
||||
color_ = Color::index(Color::Cpc::CYAN);
|
||||
} else if (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED) {
|
||||
color_ = Color::index(Color::Cpc::YELLOW);
|
||||
} else {
|
||||
color_ = Color::index(Color::Cpc::WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
// 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};
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Inicializa el sprite del jugador
|
||||
void Player::initSprite(const std::string& animations_path) {
|
||||
const auto& animation_data = Resource::Cache::get()->getAnimationData(animations_path);
|
||||
sprite_ = std::make_unique<SurfaceAnimatedSprite>(animation_data);
|
||||
sprite_->setWidth(WIDTH);
|
||||
sprite_->setHeight(HEIGHT);
|
||||
sprite_->setCurrentAnimation("walk");
|
||||
}
|
||||
|
||||
// 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_);
|
||||
}
|
||||
|
||||
// Calcula la velocidad en x con sistema caminar/correr y momentum
|
||||
void Player::updateVelocity(float delta_time) {
|
||||
if (auto_movement_) {
|
||||
// La cinta transportadora tiene el control (velocidad fija)
|
||||
vx_ = RUN_VELOCITY * room_->getConveyorBeltDirection();
|
||||
sprite_->setFlip(vx_ < 0.0F ? Flip::LEFT : Flip::RIGHT);
|
||||
movement_time_ = 0.0F;
|
||||
} else {
|
||||
// El jugador tiene el control
|
||||
switch (wanna_go_) {
|
||||
case Direction::LEFT:
|
||||
movement_time_ += delta_time;
|
||||
if (movement_time_ < TIME_TO_RUN) {
|
||||
// Caminando: velocidad fija inmediata
|
||||
vx_ = -WALK_VELOCITY;
|
||||
} else {
|
||||
// Corriendo: acelerar hacia RUN_VELOCITY
|
||||
vx_ -= RUN_ACCELERATION * delta_time;
|
||||
vx_ = std::max(vx_, -RUN_VELOCITY);
|
||||
}
|
||||
sprite_->setFlip(Flip::LEFT);
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
movement_time_ += delta_time;
|
||||
if (movement_time_ < TIME_TO_RUN) {
|
||||
// Caminando: velocidad fija inmediata
|
||||
vx_ = WALK_VELOCITY;
|
||||
} else {
|
||||
// Corriendo: acelerar hacia RUN_VELOCITY
|
||||
vx_ += RUN_ACCELERATION * delta_time;
|
||||
vx_ = std::min(vx_, RUN_VELOCITY);
|
||||
}
|
||||
sprite_->setFlip(Flip::RIGHT);
|
||||
break;
|
||||
case Direction::NONE:
|
||||
movement_time_ = 0.0F;
|
||||
// Desacelerar gradualmente (momentum)
|
||||
if (vx_ > 0.0F) {
|
||||
vx_ -= HORIZONTAL_DECELERATION * delta_time;
|
||||
vx_ = std::max(vx_, 0.0F);
|
||||
} else if (vx_ < 0.0F) {
|
||||
vx_ += HORIZONTAL_DECELERATION * delta_time;
|
||||
vx_ = std::min(vx_, 0.0F);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
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
|
||||
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);
|
||||
Audio::get()->playSound(Defaults::Sound::LAND, Audio::Group::GAME);
|
||||
return true;
|
||||
}
|
||||
|
||||
// No hay colisión
|
||||
y_ += displacement;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de proyeccion
|
||||
auto Player::getProjection(Direction direction, float displacement) -> SDL_FRect {
|
||||
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};
|
||||
|
||||
case Direction::RIGHT:
|
||||
return {
|
||||
.x = x_ + WIDTH,
|
||||
.y = y_,
|
||||
.w = std::ceil(displacement), // Para evitar que tenga una anchura de 0 pixels
|
||||
.h = HEIGHT};
|
||||
|
||||
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() {
|
||||
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
|
||||
is_alive_ = true; // No puede morir
|
||||
} else {
|
||||
is_alive_ = false; // Muere
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
174
source/game/entities/player.hpp
Normal file
174
source/game/entities/player.hpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#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/surface_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
|
||||
|
||||
class Player {
|
||||
public:
|
||||
// --- Enums y Structs ---
|
||||
enum class State {
|
||||
ON_GROUND, // En suelo plano o conveyor belt
|
||||
ON_AIR, // En el aire (saltando o cayendo)
|
||||
};
|
||||
|
||||
enum class Direction {
|
||||
LEFT,
|
||||
RIGHT,
|
||||
UP,
|
||||
DOWN,
|
||||
NONE
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
// --- 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_, y_, WIDTH, 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 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
|
||||
|
||||
#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 de tamaño ---
|
||||
static constexpr int WIDTH = 8; // Ancho del jugador en pixels
|
||||
static constexpr int HEIGHT = 16; // Alto del jugador en pixels
|
||||
|
||||
// --- Constantes de movimiento horizontal ---
|
||||
static constexpr float WALK_VELOCITY = 50.0F; // Velocidad al caminar (inmediata) en pixels/segundo
|
||||
static constexpr float RUN_VELOCITY = 80.0F; // Velocidad al correr en pixels/segundo
|
||||
static constexpr float TIME_TO_RUN = 0.8F; // Segundos caminando antes de empezar a correr
|
||||
static constexpr float RUN_ACCELERATION = 150.0F; // Aceleración de caminar a correr en pixels/segundo²
|
||||
static constexpr float HORIZONTAL_DECELERATION = 250.0F; // Desaceleración al soltar (momentum) en pixels/segundo²
|
||||
|
||||
// --- Constantes de salto ---
|
||||
static constexpr float JUMP_VELOCITY = -160.0F; // Impulso de salto en pixels/segundo (más fuerte, menos floaty)
|
||||
static constexpr float GRAVITY_FORCE = 280.0F; // Gravedad en pixels/segundo² (más alta, menos floaty)
|
||||
|
||||
// --- Objetos y punteros ---
|
||||
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
|
||||
std::unique_ptr<SurfaceAnimatedSprite> 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
|
||||
float movement_time_ = 0.0F; // Tiempo que lleva moviéndose en la misma dirección (para transición caminar→correr)
|
||||
|
||||
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_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior izquierda del jugador
|
||||
SDL_FPoint under_right_foot_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior derecha del 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 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 ---
|
||||
Uint8 color_ = 0; // Color del jugador
|
||||
|
||||
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 updateOnAir(float delta_time); // Actualización lógica estado ON_AIR
|
||||
|
||||
// --- Métodos de movimiento por estado ---
|
||||
void moveOnGround(float delta_time); // Movimiento físico estado ON_GROUND
|
||||
void moveOnAir(float delta_time); // Movimiento físico estado ON_AIR
|
||||
|
||||
// --- Funciones de inicialización ---
|
||||
void initSprite(const std::string& animations_path); // Inicializa el sprite del jugador
|
||||
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
|
||||
|
||||
// --- 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
|
||||
|
||||
// --- 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
|
||||
auto handleKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
|
||||
void updateVelocity(float delta_time); // Calcula la velocidad en x con aceleración
|
||||
void markAsDead(); // Marca al jugador como muerto
|
||||
};
|
||||
333
source/game/gameplay/collision_map.cpp
Normal file
333
source/game/gameplay/collision_map.cpp
Normal file
@@ -0,0 +1,333 @@
|
||||
#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> collision_map, int conveyor_belt_direction)
|
||||
: tile_map_(std::move(collision_map)),
|
||||
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();
|
||||
setAutoSurfaces();
|
||||
}
|
||||
|
||||
// Devuelve el tipo de tile que hay en ese pixel
|
||||
auto CollisionMap::getTile(SDL_FPoint point) const -> Tile {
|
||||
const int POS = ((point.y / TILE_SIZE) * MAP_WIDTH) + (point.x / TILE_SIZE);
|
||||
return getTile(POS);
|
||||
}
|
||||
|
||||
// Devuelve el tipo de tile que hay en ese indice
|
||||
// Mapeo directo desde collisionmap: 1=WALL, 2=PASSABLE, 3=ANIMATED, 6=KILL
|
||||
auto CollisionMap::getTile(int index) const -> Tile {
|
||||
const bool ON_RANGE = (index > -1) && (index < (int)tile_map_.size());
|
||||
|
||||
if (ON_RANGE) {
|
||||
switch (tile_map_[index]) {
|
||||
case 1:
|
||||
return Tile::WALL;
|
||||
case 2:
|
||||
return Tile::PASSABLE;
|
||||
case 3:
|
||||
return Tile::ANIMATED;
|
||||
case 6:
|
||||
return Tile::KILL;
|
||||
default:
|
||||
return Tile::EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
return Tile::EMPTY;
|
||||
}
|
||||
|
||||
// === Queries de colisión ===
|
||||
|
||||
// Comprueba las colisiones con paredes derechas
|
||||
auto CollisionMap::checkRightSurfaces(const SDL_FRect& rect) -> int {
|
||||
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 {
|
||||
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 {
|
||||
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) {
|
||||
return checkCollision(s, p);
|
||||
});
|
||||
}
|
||||
|
||||
// Comprueba las colisiones con suelos
|
||||
auto CollisionMap::checkBottomSurfaces(const SDL_FRect& rect) -> int {
|
||||
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 {
|
||||
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) {
|
||||
return checkCollision(s, p);
|
||||
});
|
||||
}
|
||||
|
||||
// === Helpers para recopilar tiles ===
|
||||
|
||||
// Helper: recopila tiles inferiores (muros sin muro debajo)
|
||||
auto CollisionMap::collectBottomTiles() -> std::vector<int> {
|
||||
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> {
|
||||
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> {
|
||||
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) {
|
||||
if (tiles.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (i < (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 < (int)tiles.size()) {
|
||||
while (tiles[i] == tiles[i - 1] + 1) {
|
||||
last_one = i;
|
||||
i++;
|
||||
if (i >= (int)tiles.size()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
line.x2 = ((tiles[last_one] % MAP_WIDTH) * TILE_SIZE) + TILE_SIZE - 1;
|
||||
lines.push_back(line);
|
||||
|
||||
// Salta separadores
|
||||
if (i < (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() {
|
||||
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() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula las superficies automaticas (conveyor belts)
|
||||
void CollisionMap::setAutoSurfaces() {
|
||||
std::vector<int> tile = collectAnimatedTiles();
|
||||
buildHorizontalLines(tile, conveyor_belt_floors_, false);
|
||||
}
|
||||
104
source/game/gameplay/collision_map.hpp
Normal file
104
source/game/gameplay/collision_map.hpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "utils/defines.hpp" // Para Tile::SIZE, PlayArea
|
||||
#include "utils/utils.hpp" // Para LineHorizontal, LineVertical
|
||||
|
||||
/**
|
||||
* @brief Mapa de colisiones de una habitación
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - Almacenar la geometría de colisión (superficies, 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,
|
||||
KILL,
|
||||
ANIMATED
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor
|
||||
* @param collision_map Vector con índices de colisión (1=WALL, 2=PASSABLE, etc.)
|
||||
* @param conveyor_belt_direction Dirección de las cintas transportadoras (-1, 0, +1)
|
||||
*/
|
||||
CollisionMap(std::vector<int> collision_map, 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
|
||||
|
||||
// --- Métodos estáticos ---
|
||||
static auto getTileSize() -> int { return TILE_SIZE; } // Tamaño del tile en pixels
|
||||
|
||||
// --- 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 getConveyorBeltFloors() const -> const std::vector<LineHorizontal>& { return conveyor_belt_floors_; }
|
||||
|
||||
private:
|
||||
// --- Constantes (usar defines de PlayArea) ---
|
||||
static constexpr int TILE_SIZE = ::Tile::SIZE; // Tamaño del tile en pixels
|
||||
static constexpr int MAP_WIDTH = PlayArea::TILE_COLS; // Ancho del mapa en tiles
|
||||
static constexpr int MAP_HEIGHT = PlayArea::TILE_ROWS; // Alto del mapa en tiles
|
||||
|
||||
// --- Datos de la habitación ---
|
||||
std::vector<int> tile_map_; // Índices de colisión de la habitación
|
||||
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<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 setAutoSurfaces(); // Calcula conveyor belts
|
||||
};
|
||||
49
source/game/gameplay/enemy_manager.cpp
Normal file
49
source/game/gameplay/enemy_manager.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#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) {
|
||||
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() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
});
|
||||
}
|
||||
45
source/game/gameplay/enemy_manager.hpp
Normal file
45
source/game/gameplay/enemy_manager.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <vector> // Para vector
|
||||
|
||||
class Enemy;
|
||||
|
||||
/**
|
||||
* @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
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<Enemy>> enemies_; // Colección de enemigos
|
||||
};
|
||||
68
source/game/gameplay/item_manager.cpp
Normal file
68
source/game/gameplay/item_manager.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#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) {
|
||||
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 {
|
||||
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
|
||||
data_->items++;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
69
source/game/gameplay/item_manager.hpp
Normal file
69
source/game/gameplay/item_manager.hpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "scoreboard.hpp" // Para Scoreboard::Data
|
||||
|
||||
class Item;
|
||||
|
||||
/**
|
||||
* @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
|
||||
|
||||
// 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) {
|
||||
// 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 {
|
||||
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 {
|
||||
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
|
||||
};
|
||||
232
source/game/gameplay/room.cpp
Normal file
232
source/game/gameplay/room.cpp
Normal file
@@ -0,0 +1,232 @@
|
||||
#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/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 collision_data_, conveyor_belt_direction_)
|
||||
collision_map_ = std::make_unique<CollisionMap>(collision_data_, 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 para renderizado (viene embebido en el YAML)
|
||||
collision_data_ = room.collision_map; // Collisionmap para colisiones (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() {
|
||||
if (data_->jail_is_open && name_ == "THE JAIL") {
|
||||
// Elimina el último enemigo (Bry debe ser el último enemigo definido en el fichero)
|
||||
if (!enemy_manager_->isEmpty()) {
|
||||
enemy_manager_->removeLastEnemy();
|
||||
}
|
||||
|
||||
// Abre las puertas (tanto en tilemap para renderizado como en collisionmap para colisiones)
|
||||
constexpr int TILE_A = 16 + (13 * MAP_WIDTH);
|
||||
constexpr int TILE_B = 16 + (14 * MAP_WIDTH);
|
||||
if (TILE_A < tile_map_.size()) {
|
||||
tile_map_[TILE_A] = -1; // Renderizado: vacío
|
||||
collision_data_[TILE_A] = -1; // Colisiones: vacío
|
||||
}
|
||||
if (TILE_B < tile_map_.size()) {
|
||||
tile_map_[TILE_B] = -1; // Renderizado: vacío
|
||||
collision_data_[TILE_B] = -1; // Colisiones: vacío
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
}
|
||||
#endif
|
||||
|
||||
// Actualiza las variables y objetos de la habitación
|
||||
void Room::update(float delta_time) {
|
||||
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 {
|
||||
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 {
|
||||
// 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 {
|
||||
// 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);
|
||||
}
|
||||
|
||||
// === 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);
|
||||
}
|
||||
|
||||
// Carga una habitación desde un archivo YAML (delegado a RoomLoader)
|
||||
auto Room::loadYAML(const std::string& file_path, bool verbose) -> Data {
|
||||
return RoomLoader::loadYAML(file_path, verbose);
|
||||
}
|
||||
128
source/game/gameplay/room.hpp
Normal file
128
source/game/gameplay/room.hpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#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/defines.hpp" // Para Tile::SIZE, PlayArea
|
||||
#include "utils/utils.hpp" // Para LineHorizontal, LineVertical
|
||||
class SurfaceSprite; // 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,
|
||||
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<int> collision_map; // Índice de colisiones de 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(); // Definido en .cpp para poder usar unique_ptr con forward declarations
|
||||
|
||||
// --- Funciones ---
|
||||
[[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)
|
||||
#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
|
||||
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
|
||||
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 (usar defines de PlayArea)
|
||||
static constexpr int TILE_SIZE = ::Tile::SIZE; // Ancho del tile en pixels
|
||||
static constexpr int MAP_WIDTH = PlayArea::TILE_COLS; // Ancho del mapa en tiles
|
||||
static constexpr int MAP_HEIGHT = PlayArea::TILE_ROWS; // 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)
|
||||
std::vector<int> collision_data_; // Indice de colisiones de 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
|
||||
};
|
||||
388
source/game/gameplay/room_loader.cpp
Normal file
388
source/game/gameplay/room_loader.cpp
Normal file
@@ -0,0 +1,388 @@
|
||||
#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 "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 {
|
||||
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 {
|
||||
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> {
|
||||
std::vector<int> tilemap_flat;
|
||||
tilemap_flat.reserve(PlayArea::TILE_COUNT); // TILE_ROWS × TILE_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) {
|
||||
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
|
||||
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) {
|
||||
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(PlayArea::TILE_ROWS);
|
||||
|
||||
for (const auto& row_node : tilemap_node) {
|
||||
std::vector<int> row;
|
||||
row.reserve(PlayArea::TILE_COLS);
|
||||
|
||||
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 el collisionmap de la habitación
|
||||
void RoomLoader::parseCollisionmap(const fkyaml::node& yaml, Room::Data& room, const std::string& file_name, bool verbose) {
|
||||
if (!yaml.contains("collisionmap")) {
|
||||
// Si no hay collisionmap, es opcional - no es error
|
||||
if (verbose) {
|
||||
std::cout << "No collisionmap found in " << file_name << " (optional)\n";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& collisionmap_node = yaml["collisionmap"];
|
||||
|
||||
// Read 2D array
|
||||
std::vector<std::vector<int>> collisionmap_2d;
|
||||
collisionmap_2d.reserve(PlayArea::TILE_ROWS);
|
||||
|
||||
for (const auto& row_node : collisionmap_node) {
|
||||
std::vector<int> row;
|
||||
row.reserve(PlayArea::TILE_COLS);
|
||||
|
||||
for (const auto& tile_node : row_node) {
|
||||
row.push_back(tile_node.get_value<int>());
|
||||
}
|
||||
|
||||
collisionmap_2d.push_back(row);
|
||||
}
|
||||
|
||||
// Convert to 1D flat array (reutilizamos flattenTilemap)
|
||||
room.collision_map = flattenTilemap(collisionmap_2d);
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Loaded collisionmap: " << room.collision_map.size() << " tiles\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Parsea los límites de movimiento de un enemigo
|
||||
void RoomLoader::parseEnemyBoundaries(const fkyaml::node& bounds_node, Enemy::Data& enemy) {
|
||||
// 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 {
|
||||
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) {
|
||||
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 {
|
||||
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) {
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
// Carga un archivo de room en formato YAML
|
||||
auto RoomLoader::loadYAML(const std::string& file_path, bool verbose) -> Room::Data {
|
||||
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);
|
||||
parseCollisionmap(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;
|
||||
}
|
||||
142
source/game/gameplay/room_loader.hpp
Normal file
142
source/game/gameplay/room_loader.hpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#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 24x40 tiles (convertido a vector 1D de 960 elementos)
|
||||
* - enemies: lista de enemigos (opcional)
|
||||
* - items: lista de items (opcional)
|
||||
*/
|
||||
static auto loadYAML(const std::string& file_path, bool verbose = false) -> Room::Data;
|
||||
|
||||
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 (24 rows × 40 cols)
|
||||
* @return Vector 1D flat con 960 elementos
|
||||
*/
|
||||
static auto flattenTilemap(const std::vector<std::vector<int>>& tilemap_2d) -> std::vector<int>;
|
||||
|
||||
/**
|
||||
* @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 el collisionmap 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 parseCollisionmap(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 {
|
||||
return std::ranges::any_of(rooms_, [&name](const auto& l) { return l == name; });
|
||||
}
|
||||
|
||||
// Añade la habitación a la lista
|
||||
auto RoomTracker::addRoom(const std::string& name) -> bool {
|
||||
// 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;
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
170
source/game/gameplay/scoreboard.cpp
Normal file
170
source/game/gameplay/scoreboard.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
#include "game/gameplay/scoreboard.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
|
||||
#include "core/rendering/text.hpp" // Para Text
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#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))) {
|
||||
constexpr float SURFACE_WIDTH = ScoreboardArea::WIDTH;
|
||||
constexpr float SURFACE_HEIGHT = ScoreboardArea::HEIGHT;
|
||||
|
||||
// Reserva memoria para los objetos
|
||||
const auto& player_animation_data = Resource::Cache::get()->getAnimationData(Options::cheats.alternate_skin == Options::Cheat::State::ENABLED ? "player2.yaml" : "player.yaml");
|
||||
player_sprite_ = std::make_shared<SurfaceAnimatedSprite>(player_animation_data);
|
||||
player_sprite_->setCurrentAnimation("walk_menu");
|
||||
|
||||
surface_ = std::make_shared<Surface>(SURFACE_WIDTH, SURFACE_HEIGHT);
|
||||
surface_dest_ = {.x = ScoreboardArea::X, .y = ScoreboardArea::Y, .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;
|
||||
|
||||
// Actualizar sprite con delta time
|
||||
player_sprite_->update(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 {
|
||||
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;
|
||||
}
|
||||
|
||||
// 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"));
|
||||
|
||||
// Anclas
|
||||
constexpr int LINE1 = Tile::SIZE;
|
||||
constexpr int LINE2 = 3 * Tile::SIZE;
|
||||
|
||||
// Dibuja las vidas
|
||||
// Calcular desplazamiento basado en tiempo
|
||||
const int DESP = static_cast<int>(time_accumulator_ / SPRITE_WALK_CYCLE_DURATION) % 8;
|
||||
const int FRAME = DESP % SPRITE_WALK_FRAMES;
|
||||
player_sprite_->setCurrentAnimationFrame(FRAME);
|
||||
player_sprite_->setPosY(LINE2);
|
||||
for (int i = 0; i < data_->lives; ++i) {
|
||||
player_sprite_->setPosX(8 + (16 * i) + DESP);
|
||||
const int INDEX = i % color_.size();
|
||||
player_sprite_->render(4, color_.at(INDEX));
|
||||
}
|
||||
|
||||
// Muestra si suena la música
|
||||
if (data_->music) {
|
||||
const Uint8 C = data_->color;
|
||||
SDL_FRect clip = {0, 8, 8, 8};
|
||||
item_surface_->renderWithColorReplace(20 * Tile::SIZE, LINE2, 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(Tile::SIZE, LINE1, "Items collected ", data_->color);
|
||||
text->writeColored(17 * Tile::SIZE, LINE1, ITEMS_TEXT, items_color_);
|
||||
text->writeColored(20 * Tile::SIZE, LINE1, " Time ", data_->color);
|
||||
text->writeColored(26 * Tile::SIZE, LINE1, 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(22 * Tile::SIZE, LINE2, "Rooms", stringToColor("white"));
|
||||
text->writeColored(28 * Tile::SIZE, LINE2, ROOMS_TEXT, stringToColor("white"));
|
||||
|
||||
// Deja el renderizador como estaba
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
68
source/game/gameplay/scoreboard.hpp
Normal file
68
source/game/gameplay/scoreboard.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string, basic_string
|
||||
#include <utility>
|
||||
#include <vector> // Para vector
|
||||
class SurfaceAnimatedSprite; // 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
|
||||
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)
|
||||
static constexpr int SPRITE_WALK_FRAMES = 4; // Número de frames de animación
|
||||
|
||||
// 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<SurfaceAnimatedSprite> 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
|
||||
};
|
||||
188
source/game/gameplay/tilemap_renderer.cpp
Normal file
188
source/game/gameplay/tilemap_renderer.cpp
Normal file
@@ -0,0 +1,188 @@
|
||||
#include "tilemap_renderer.hpp"
|
||||
|
||||
#include "core/rendering/screen.hpp"
|
||||
#include "core/rendering/surface.hpp"
|
||||
#include "core/rendering/surface_sprite.hpp"
|
||||
#ifdef _DEBUG
|
||||
#include "core/system/debug.hpp"
|
||||
#endif
|
||||
#include "game/gameplay/collision_map.hpp"
|
||||
#include "utils/color.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 = {0, 0, PlayArea::WIDTH, 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, Color::index(Color::Cpc::BLUE));
|
||||
}
|
||||
|
||||
// TopSurfaces
|
||||
for (auto l : collision_map->getTopFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, Color::index(Color::Cpc::RED));
|
||||
}
|
||||
|
||||
// LeftSurfaces
|
||||
for (auto l : collision_map->getLeftWalls()) {
|
||||
surface->drawLine(l.x, l.y1, l.x, l.y2, Color::index(Color::Cpc::GREEN));
|
||||
}
|
||||
|
||||
// RightSurfaces
|
||||
for (auto l : collision_map->getRightWalls()) {
|
||||
surface->drawLine(l.x, l.y1, l.x, l.y2, Color::index(Color::Cpc::MAGENTA));
|
||||
}
|
||||
|
||||
// AutoSurfaces (Conveyor Belts)
|
||||
for (auto l : collision_map->getConveyorBeltFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, Color::index(Color::Cpc::WHITE));
|
||||
}
|
||||
}
|
||||
|
||||
// Redibuja el tilemap (para actualizar modo debug)
|
||||
void TilemapRenderer::redrawMap(const CollisionMap* collision_map) {
|
||||
fillMapTexture(collision_map);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Pinta el mapa estático y debug lines
|
||||
void TilemapRenderer::fillMapTexture(const CollisionMap* collision_map) {
|
||||
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 = {0, 0, TILE_SIZE, 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 (se dibujan en renderAnimatedTiles)
|
||||
const int INDEX = (y * MAP_WIDTH) + x;
|
||||
const bool IS_ANIMATED = collision_map->getTile(INDEX) == CollisionMap::Tile::ANIMATED;
|
||||
const bool IS_VISIBLE = tile_map_[INDEX] > -1;
|
||||
|
||||
if (IS_VISIBLE && !IS_ANIMATED) {
|
||||
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) {
|
||||
// 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<SurfaceSprite>(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() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
118
source/game/gameplay/tilemap_renderer.hpp
Normal file
118
source/game/gameplay/tilemap_renderer.hpp
Normal file
@@ -0,0 +1,118 @@
|
||||
#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 SurfaceSprite;
|
||||
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);
|
||||
#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<SurfaceSprite> 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
|
||||
};
|
||||
610
source/game/options.cpp
Normal file
610
source/game/options.cpp
Normal file
@@ -0,0 +1,610 @@
|
||||
#include "game/options.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <fstream> // Para ifstream, ofstream
|
||||
#include <iostream> // Para cout, cerr
|
||||
#include <string> // Para string
|
||||
#include <unordered_map> // Para unordered_map
|
||||
|
||||
#include "core/input/input_types.hpp" // Para BUTTON_TO_STRING, STRING_TO_BUTTON
|
||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||
#include "game/defaults.hpp" // Para GameDefaults::VERSION
|
||||
|
||||
namespace Options {
|
||||
|
||||
// --- Funciones helper de conversión ---
|
||||
|
||||
// Mapa de nombres de filtro
|
||||
const std::unordered_map<Screen::Filter, std::string> FILTER_TO_STRING = {
|
||||
{Screen::Filter::NEAREST, "nearest"},
|
||||
{Screen::Filter::LINEAR, "linear"}};
|
||||
|
||||
const std::unordered_map<std::string, Screen::Filter> STRING_TO_FILTER = {
|
||||
{"nearest", Screen::Filter::NEAREST},
|
||||
{"linear", Screen::Filter::LINEAR}};
|
||||
|
||||
// Mapa de scancodes comunes (los más usados para controles de juego)
|
||||
const std::unordered_map<SDL_Scancode, std::string> SCANCODE_TO_STRING = {
|
||||
// Flechas
|
||||
{SDL_SCANCODE_LEFT, "LEFT"},
|
||||
{SDL_SCANCODE_RIGHT, "RIGHT"},
|
||||
{SDL_SCANCODE_UP, "UP"},
|
||||
{SDL_SCANCODE_DOWN, "DOWN"},
|
||||
// Letras
|
||||
{SDL_SCANCODE_A, "A"},
|
||||
{SDL_SCANCODE_B, "B"},
|
||||
{SDL_SCANCODE_C, "C"},
|
||||
{SDL_SCANCODE_D, "D"},
|
||||
{SDL_SCANCODE_E, "E"},
|
||||
{SDL_SCANCODE_F, "F"},
|
||||
{SDL_SCANCODE_G, "G"},
|
||||
{SDL_SCANCODE_H, "H"},
|
||||
{SDL_SCANCODE_I, "I"},
|
||||
{SDL_SCANCODE_J, "J"},
|
||||
{SDL_SCANCODE_K, "K"},
|
||||
{SDL_SCANCODE_L, "L"},
|
||||
{SDL_SCANCODE_M, "M"},
|
||||
{SDL_SCANCODE_N, "N"},
|
||||
{SDL_SCANCODE_O, "O"},
|
||||
{SDL_SCANCODE_P, "P"},
|
||||
{SDL_SCANCODE_Q, "Q"},
|
||||
{SDL_SCANCODE_R, "R"},
|
||||
{SDL_SCANCODE_S, "S"},
|
||||
{SDL_SCANCODE_T, "T"},
|
||||
{SDL_SCANCODE_U, "U"},
|
||||
{SDL_SCANCODE_V, "V"},
|
||||
{SDL_SCANCODE_W, "W"},
|
||||
{SDL_SCANCODE_X, "X"},
|
||||
{SDL_SCANCODE_Y, "Y"},
|
||||
{SDL_SCANCODE_Z, "Z"},
|
||||
// Números
|
||||
{SDL_SCANCODE_0, "0"},
|
||||
{SDL_SCANCODE_1, "1"},
|
||||
{SDL_SCANCODE_2, "2"},
|
||||
{SDL_SCANCODE_3, "3"},
|
||||
{SDL_SCANCODE_4, "4"},
|
||||
{SDL_SCANCODE_5, "5"},
|
||||
{SDL_SCANCODE_6, "6"},
|
||||
{SDL_SCANCODE_7, "7"},
|
||||
{SDL_SCANCODE_8, "8"},
|
||||
{SDL_SCANCODE_9, "9"},
|
||||
// Teclas especiales
|
||||
{SDL_SCANCODE_SPACE, "SPACE"},
|
||||
{SDL_SCANCODE_RETURN, "RETURN"},
|
||||
{SDL_SCANCODE_ESCAPE, "ESCAPE"},
|
||||
{SDL_SCANCODE_TAB, "TAB"},
|
||||
{SDL_SCANCODE_BACKSPACE, "BACKSPACE"},
|
||||
{SDL_SCANCODE_LSHIFT, "LSHIFT"},
|
||||
{SDL_SCANCODE_RSHIFT, "RSHIFT"},
|
||||
{SDL_SCANCODE_LCTRL, "LCTRL"},
|
||||
{SDL_SCANCODE_RCTRL, "RCTRL"},
|
||||
{SDL_SCANCODE_LALT, "LALT"},
|
||||
{SDL_SCANCODE_RALT, "RALT"}};
|
||||
|
||||
const std::unordered_map<std::string, SDL_Scancode> STRING_TO_SCANCODE = {
|
||||
// Flechas
|
||||
{"LEFT", SDL_SCANCODE_LEFT},
|
||||
{"RIGHT", SDL_SCANCODE_RIGHT},
|
||||
{"UP", SDL_SCANCODE_UP},
|
||||
{"DOWN", SDL_SCANCODE_DOWN},
|
||||
// Letras
|
||||
{"A", SDL_SCANCODE_A},
|
||||
{"B", SDL_SCANCODE_B},
|
||||
{"C", SDL_SCANCODE_C},
|
||||
{"D", SDL_SCANCODE_D},
|
||||
{"E", SDL_SCANCODE_E},
|
||||
{"F", SDL_SCANCODE_F},
|
||||
{"G", SDL_SCANCODE_G},
|
||||
{"H", SDL_SCANCODE_H},
|
||||
{"I", SDL_SCANCODE_I},
|
||||
{"J", SDL_SCANCODE_J},
|
||||
{"K", SDL_SCANCODE_K},
|
||||
{"L", SDL_SCANCODE_L},
|
||||
{"M", SDL_SCANCODE_M},
|
||||
{"N", SDL_SCANCODE_N},
|
||||
{"O", SDL_SCANCODE_O},
|
||||
{"P", SDL_SCANCODE_P},
|
||||
{"Q", SDL_SCANCODE_Q},
|
||||
{"R", SDL_SCANCODE_R},
|
||||
{"S", SDL_SCANCODE_S},
|
||||
{"T", SDL_SCANCODE_T},
|
||||
{"U", SDL_SCANCODE_U},
|
||||
{"V", SDL_SCANCODE_V},
|
||||
{"W", SDL_SCANCODE_W},
|
||||
{"X", SDL_SCANCODE_X},
|
||||
{"Y", SDL_SCANCODE_Y},
|
||||
{"Z", SDL_SCANCODE_Z},
|
||||
// Números
|
||||
{"0", SDL_SCANCODE_0},
|
||||
{"1", SDL_SCANCODE_1},
|
||||
{"2", SDL_SCANCODE_2},
|
||||
{"3", SDL_SCANCODE_3},
|
||||
{"4", SDL_SCANCODE_4},
|
||||
{"5", SDL_SCANCODE_5},
|
||||
{"6", SDL_SCANCODE_6},
|
||||
{"7", SDL_SCANCODE_7},
|
||||
{"8", SDL_SCANCODE_8},
|
||||
{"9", SDL_SCANCODE_9},
|
||||
// Teclas especiales
|
||||
{"SPACE", SDL_SCANCODE_SPACE},
|
||||
{"RETURN", SDL_SCANCODE_RETURN},
|
||||
{"ESCAPE", SDL_SCANCODE_ESCAPE},
|
||||
{"TAB", SDL_SCANCODE_TAB},
|
||||
{"BACKSPACE", SDL_SCANCODE_BACKSPACE},
|
||||
{"LSHIFT", SDL_SCANCODE_LSHIFT},
|
||||
{"RSHIFT", SDL_SCANCODE_RSHIFT},
|
||||
{"LCTRL", SDL_SCANCODE_LCTRL},
|
||||
{"RCTRL", SDL_SCANCODE_RCTRL},
|
||||
{"LALT", SDL_SCANCODE_LALT},
|
||||
{"RALT", SDL_SCANCODE_RALT}};
|
||||
|
||||
// Mapa extendido de botones de gamepad (incluye ejes como botones)
|
||||
const std::unordered_map<int, std::string> GAMEPAD_BUTTON_TO_STRING = {
|
||||
{SDL_GAMEPAD_BUTTON_WEST, "WEST"},
|
||||
{SDL_GAMEPAD_BUTTON_NORTH, "NORTH"},
|
||||
{SDL_GAMEPAD_BUTTON_EAST, "EAST"},
|
||||
{SDL_GAMEPAD_BUTTON_SOUTH, "SOUTH"},
|
||||
{SDL_GAMEPAD_BUTTON_START, "START"},
|
||||
{SDL_GAMEPAD_BUTTON_BACK, "BACK"},
|
||||
{SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, "LEFT_SHOULDER"},
|
||||
{SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, "RIGHT_SHOULDER"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_UP, "DPAD_UP"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_DOWN, "DPAD_DOWN"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_LEFT, "DPAD_LEFT"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_RIGHT, "DPAD_RIGHT"},
|
||||
{100, "L2_AS_BUTTON"},
|
||||
{101, "R2_AS_BUTTON"},
|
||||
{200, "LEFT_STICK_LEFT"},
|
||||
{201, "LEFT_STICK_RIGHT"}};
|
||||
|
||||
const std::unordered_map<std::string, int> STRING_TO_GAMEPAD_BUTTON = {
|
||||
{"WEST", SDL_GAMEPAD_BUTTON_WEST},
|
||||
{"NORTH", SDL_GAMEPAD_BUTTON_NORTH},
|
||||
{"EAST", SDL_GAMEPAD_BUTTON_EAST},
|
||||
{"SOUTH", SDL_GAMEPAD_BUTTON_SOUTH},
|
||||
{"START", SDL_GAMEPAD_BUTTON_START},
|
||||
{"BACK", SDL_GAMEPAD_BUTTON_BACK},
|
||||
{"LEFT_SHOULDER", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER},
|
||||
{"RIGHT_SHOULDER", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER},
|
||||
{"DPAD_UP", SDL_GAMEPAD_BUTTON_DPAD_UP},
|
||||
{"DPAD_DOWN", SDL_GAMEPAD_BUTTON_DPAD_DOWN},
|
||||
{"DPAD_LEFT", SDL_GAMEPAD_BUTTON_DPAD_LEFT},
|
||||
{"DPAD_RIGHT", SDL_GAMEPAD_BUTTON_DPAD_RIGHT},
|
||||
{"L2_AS_BUTTON", 100},
|
||||
{"R2_AS_BUTTON", 101},
|
||||
{"LEFT_STICK_LEFT", 200},
|
||||
{"LEFT_STICK_RIGHT", 201}};
|
||||
|
||||
// Lista de paletas válidas
|
||||
const std::vector<std::string> VALID_PALETTES = {
|
||||
"black-and-white",
|
||||
"green-phosphor",
|
||||
"island-joy-16",
|
||||
"lost-century",
|
||||
"na16",
|
||||
"orange-screen",
|
||||
"pico-8",
|
||||
"ruzx-spectrum",
|
||||
"ruzx-spectrum-revision-2",
|
||||
"steam-lords",
|
||||
"sweetie-16",
|
||||
"sweetie-16_bona",
|
||||
"zx-spectrum",
|
||||
"zx-spectrum-adjusted",
|
||||
"zxarne-5-2"};
|
||||
|
||||
// Funciones helper de conversión
|
||||
auto filterToString(Screen::Filter filter) -> std::string {
|
||||
auto it = FILTER_TO_STRING.find(filter);
|
||||
if (it != FILTER_TO_STRING.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return "nearest";
|
||||
}
|
||||
|
||||
auto stringToFilter(const std::string& str) -> Screen::Filter {
|
||||
auto it = STRING_TO_FILTER.find(str);
|
||||
if (it != STRING_TO_FILTER.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return Defaults::Video::FILTER;
|
||||
}
|
||||
|
||||
auto scancodeToString(SDL_Scancode scancode) -> std::string {
|
||||
auto it = SCANCODE_TO_STRING.find(scancode);
|
||||
if (it != SCANCODE_TO_STRING.end()) {
|
||||
return it->second;
|
||||
}
|
||||
// Fallback: devolver el código numérico como string
|
||||
return std::to_string(static_cast<int>(scancode));
|
||||
}
|
||||
|
||||
auto stringToScancode(const std::string& str, SDL_Scancode default_value) -> SDL_Scancode {
|
||||
auto it = STRING_TO_SCANCODE.find(str);
|
||||
if (it != STRING_TO_SCANCODE.end()) {
|
||||
return it->second;
|
||||
}
|
||||
// Intentar parsear como número (compatibilidad hacia atrás)
|
||||
try {
|
||||
int val = std::stoi(str);
|
||||
return static_cast<SDL_Scancode>(val);
|
||||
} catch (...) {
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
auto gamepadButtonToString(int button) -> std::string {
|
||||
auto it = GAMEPAD_BUTTON_TO_STRING.find(button);
|
||||
if (it != GAMEPAD_BUTTON_TO_STRING.end()) {
|
||||
return it->second;
|
||||
}
|
||||
// Fallback: devolver el código numérico como string
|
||||
return std::to_string(button);
|
||||
}
|
||||
|
||||
auto stringToGamepadButton(const std::string& str, int default_value) -> int {
|
||||
auto it = STRING_TO_GAMEPAD_BUTTON.find(str);
|
||||
if (it != STRING_TO_GAMEPAD_BUTTON.end()) {
|
||||
return it->second;
|
||||
}
|
||||
// Intentar parsear como número (compatibilidad hacia atrás)
|
||||
try {
|
||||
return std::stoi(str);
|
||||
} catch (...) {
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
auto isValidPalette(const std::string& palette) -> bool {
|
||||
return std::ranges::any_of(VALID_PALETTES, [&palette](const auto& valid) { return valid == palette; });
|
||||
}
|
||||
|
||||
// --- Funciones helper para loadFromFile() ---
|
||||
|
||||
// Carga configuración de ventana desde YAML
|
||||
void loadWindowConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("window")) {
|
||||
const auto& win = yaml["window"];
|
||||
if (win.contains("zoom")) {
|
||||
try {
|
||||
int val = win["zoom"].get_value<int>();
|
||||
window.zoom = (val > 0) ? val : Defaults::Window::ZOOM;
|
||||
} catch (...) {
|
||||
window.zoom = Defaults::Window::ZOOM;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carga configuración de borde desde YAML
|
||||
void loadBorderConfigFromYaml(const fkyaml::node& border) {
|
||||
if (border.contains("enabled")) {
|
||||
try {
|
||||
video.border.enabled = border["enabled"].get_value<bool>();
|
||||
} catch (...) {
|
||||
video.border.enabled = Defaults::Border::ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
if (border.contains("width")) {
|
||||
try {
|
||||
auto val = border["width"].get_value<float>();
|
||||
video.border.width = (val > 0) ? val : Defaults::Border::WIDTH;
|
||||
} catch (...) {
|
||||
video.border.width = Defaults::Border::WIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
if (border.contains("height")) {
|
||||
try {
|
||||
auto val = border["height"].get_value<float>();
|
||||
video.border.height = (val > 0) ? val : Defaults::Border::HEIGHT;
|
||||
} catch (...) {
|
||||
video.border.height = Defaults::Border::HEIGHT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carga los campos básicos de configuración de video
|
||||
void loadBasicVideoFieldsFromYaml(const fkyaml::node& vid) {
|
||||
// fullscreen (antes era "mode")
|
||||
if (vid.contains("fullscreen")) {
|
||||
try {
|
||||
video.fullscreen = vid["fullscreen"].get_value<bool>();
|
||||
} catch (...) {
|
||||
video.fullscreen = Defaults::Video::FULLSCREEN;
|
||||
}
|
||||
}
|
||||
|
||||
// filter (ahora es string)
|
||||
if (vid.contains("filter")) {
|
||||
try {
|
||||
auto filter_str = vid["filter"].get_value<std::string>();
|
||||
video.filter = stringToFilter(filter_str);
|
||||
} catch (...) {
|
||||
video.filter = Defaults::Video::FILTER;
|
||||
}
|
||||
}
|
||||
|
||||
if (vid.contains("shaders")) {
|
||||
try {
|
||||
video.shaders = vid["shaders"].get_value<bool>();
|
||||
} catch (...) {
|
||||
video.shaders = Defaults::Video::SHADERS;
|
||||
}
|
||||
}
|
||||
|
||||
if (vid.contains("vertical_sync")) {
|
||||
try {
|
||||
video.vertical_sync = vid["vertical_sync"].get_value<bool>();
|
||||
} catch (...) {
|
||||
video.vertical_sync = Defaults::Video::VERTICAL_SYNC;
|
||||
}
|
||||
}
|
||||
|
||||
if (vid.contains("integer_scale")) {
|
||||
try {
|
||||
video.integer_scale = vid["integer_scale"].get_value<bool>();
|
||||
} catch (...) {
|
||||
video.integer_scale = Defaults::Video::INTEGER_SCALE;
|
||||
}
|
||||
}
|
||||
|
||||
if (vid.contains("keep_aspect")) {
|
||||
try {
|
||||
video.keep_aspect = vid["keep_aspect"].get_value<bool>();
|
||||
} catch (...) {
|
||||
video.keep_aspect = Defaults::Video::KEEP_ASPECT;
|
||||
}
|
||||
}
|
||||
|
||||
if (vid.contains("palette")) {
|
||||
try {
|
||||
auto palette_str = vid["palette"].get_value<std::string>();
|
||||
if (isValidPalette(palette_str)) {
|
||||
video.palette = palette_str;
|
||||
} else {
|
||||
video.palette = Defaults::Video::PALETTE_NAME;
|
||||
}
|
||||
} catch (...) {
|
||||
video.palette = Defaults::Video::PALETTE_NAME;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carga configuración de video desde YAML
|
||||
void loadVideoConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("video")) {
|
||||
const auto& vid = yaml["video"];
|
||||
loadBasicVideoFieldsFromYaml(vid);
|
||||
|
||||
// Lee border
|
||||
if (vid.contains("border")) {
|
||||
loadBorderConfigFromYaml(vid["border"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carga controles de teclado desde YAML
|
||||
void loadKeyboardControlsFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("keyboard_controls")) {
|
||||
const auto& ctrl = yaml["keyboard_controls"];
|
||||
|
||||
if (ctrl.contains("key_left")) {
|
||||
try {
|
||||
auto key_str = ctrl["key_left"].get_value<std::string>();
|
||||
keyboard_controls.key_left = stringToScancode(key_str, Defaults::Controls::KEY_LEFT);
|
||||
} catch (...) {
|
||||
keyboard_controls.key_left = Defaults::Controls::KEY_LEFT;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctrl.contains("key_right")) {
|
||||
try {
|
||||
auto key_str = ctrl["key_right"].get_value<std::string>();
|
||||
keyboard_controls.key_right = stringToScancode(key_str, Defaults::Controls::KEY_RIGHT);
|
||||
} catch (...) {
|
||||
keyboard_controls.key_right = Defaults::Controls::KEY_RIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctrl.contains("key_jump")) {
|
||||
try {
|
||||
auto key_str = ctrl["key_jump"].get_value<std::string>();
|
||||
keyboard_controls.key_jump = stringToScancode(key_str, Defaults::Controls::KEY_JUMP);
|
||||
} catch (...) {
|
||||
keyboard_controls.key_jump = Defaults::Controls::KEY_JUMP;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carga controles de gamepad desde YAML
|
||||
void loadGamepadControlsFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("gamepad_controls")) {
|
||||
const auto& gp = yaml["gamepad_controls"];
|
||||
|
||||
if (gp.contains("button_left")) {
|
||||
try {
|
||||
auto button_str = gp["button_left"].get_value<std::string>();
|
||||
gamepad_controls.button_left = stringToGamepadButton(button_str, Defaults::Controls::GAMEPAD_BUTTON_LEFT);
|
||||
} catch (...) {
|
||||
gamepad_controls.button_left = Defaults::Controls::GAMEPAD_BUTTON_LEFT;
|
||||
}
|
||||
}
|
||||
|
||||
if (gp.contains("button_right")) {
|
||||
try {
|
||||
auto button_str = gp["button_right"].get_value<std::string>();
|
||||
gamepad_controls.button_right = stringToGamepadButton(button_str, Defaults::Controls::GAMEPAD_BUTTON_RIGHT);
|
||||
} catch (...) {
|
||||
gamepad_controls.button_right = Defaults::Controls::GAMEPAD_BUTTON_RIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
if (gp.contains("button_jump")) {
|
||||
try {
|
||||
auto button_str = gp["button_jump"].get_value<std::string>();
|
||||
gamepad_controls.button_jump = stringToGamepadButton(button_str, Defaults::Controls::GAMEPAD_BUTTON_JUMP);
|
||||
} catch (...) {
|
||||
gamepad_controls.button_jump = Defaults::Controls::GAMEPAD_BUTTON_JUMP;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Crea e inicializa las opciones del programa
|
||||
void init() {
|
||||
#ifdef _DEBUG
|
||||
console = true;
|
||||
#else
|
||||
console = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Establece la ruta del fichero de configuración
|
||||
void setConfigFile(const std::string& path) {
|
||||
config_file_path = path;
|
||||
}
|
||||
|
||||
// Carga las opciones desde el fichero configurado
|
||||
auto loadFromFile() -> bool {
|
||||
// Versión esperada del fichero
|
||||
const std::string CONFIG_VERSION = std::string(Project::VERSION);
|
||||
version = "";
|
||||
|
||||
// Intenta abrir y leer el fichero
|
||||
std::ifstream file(config_file_path);
|
||||
if (!file.good()) {
|
||||
if (console) {
|
||||
std::cout << "Config file not found, creating default: " << config_file_path << '\n';
|
||||
}
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Lee todo el contenido del fichero
|
||||
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
try {
|
||||
if (console) {
|
||||
std::cout << "Reading config file: " << config_file_path << '\n';
|
||||
}
|
||||
|
||||
// Parsea el YAML
|
||||
auto yaml = fkyaml::node::deserialize(content);
|
||||
|
||||
// Lee la versión
|
||||
if (yaml.contains("version")) {
|
||||
version = yaml["version"].get_value<std::string>();
|
||||
}
|
||||
|
||||
// Si la versión no coincide, crea un fichero nuevo con valores por defecto
|
||||
if (CONFIG_VERSION != version) {
|
||||
if (console) {
|
||||
std::cout << "Config version mismatch (expected: " << CONFIG_VERSION << ", got: " << version << "), creating new config\n";
|
||||
}
|
||||
init();
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Carga las diferentes secciones de configuración usando funciones helper
|
||||
loadWindowConfigFromYaml(yaml);
|
||||
loadVideoConfigFromYaml(yaml);
|
||||
loadKeyboardControlsFromYaml(yaml);
|
||||
loadGamepadControlsFromYaml(yaml);
|
||||
|
||||
if (console) {
|
||||
std::cout << "Config file loaded successfully\n\n";
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (const fkyaml::exception& e) {
|
||||
if (console) {
|
||||
std::cerr << "Error parsing YAML config: " << e.what() << '\n';
|
||||
std::cerr << "Creating new config with defaults\n";
|
||||
}
|
||||
init();
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Guarda las opciones al fichero configurado
|
||||
auto saveToFile() -> bool {
|
||||
// Abre el fichero para escritura
|
||||
std::ofstream file(config_file_path);
|
||||
if (!file.is_open()) {
|
||||
if (console) {
|
||||
std::cerr << "Error: Unable to open file " << config_file_path << " for writing\n";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (console) {
|
||||
std::cout << "Writing config file: " << config_file_path << '\n';
|
||||
}
|
||||
|
||||
// Escribe el fichero manualmente para controlar el orden y los comentarios
|
||||
file << "# JailDoctor's Dilemma - Configuration File\n";
|
||||
file << "# \n";
|
||||
file << "# This file is automatically generated and managed by the game.\n";
|
||||
file << "# Manual edits are preserved if valid.\n";
|
||||
file << "\n";
|
||||
|
||||
// VERSION
|
||||
file << "# VERSION \n";
|
||||
file << "version: \"" << Project::VERSION << "\"\n";
|
||||
file << "\n";
|
||||
|
||||
// WINDOW
|
||||
file << "# WINDOW\n";
|
||||
file << "window:\n";
|
||||
file << " zoom: " << window.zoom << "\n";
|
||||
file << "\n";
|
||||
|
||||
// VIDEO
|
||||
file << "# VIDEO \n";
|
||||
file << "video:\n";
|
||||
file << " fullscreen: " << (video.fullscreen ? "true" : "false") << "\n";
|
||||
file << " filter: " << filterToString(video.filter) << " # filter: nearest (pixel perfect) | linear (smooth)\n";
|
||||
file << " shaders: " << (video.shaders ? "true" : "false") << "\n";
|
||||
file << " vertical_sync: " << (video.vertical_sync ? "true" : "false") << "\n";
|
||||
file << " integer_scale: " << (video.integer_scale ? "true" : "false") << "\n";
|
||||
file << " keep_aspect: " << (video.keep_aspect ? "true" : "false") << "\n";
|
||||
file << " palette: " << video.palette << "\n";
|
||||
file << " border:\n";
|
||||
file << " enabled: " << (video.border.enabled ? "true" : "false") << "\n";
|
||||
file << " width: " << video.border.width << "\n";
|
||||
file << " height: " << video.border.height << "\n";
|
||||
file << "\n";
|
||||
|
||||
// KEYBOARD CONTROLS
|
||||
file << "# KEYBOARD CONTROLS\n";
|
||||
file << "keyboard_controls:\n";
|
||||
file << " key_left: " << scancodeToString(keyboard_controls.key_left) << "\n";
|
||||
file << " key_right: " << scancodeToString(keyboard_controls.key_right) << "\n";
|
||||
file << " key_jump: " << scancodeToString(keyboard_controls.key_jump) << "\n";
|
||||
file << "\n";
|
||||
|
||||
// GAMEPAD CONTROLS
|
||||
file << "# GAMEPAD CONTROLS\n";
|
||||
file << "gamepad_controls:\n";
|
||||
file << " button_left: " << gamepadButtonToString(gamepad_controls.button_left) << "\n";
|
||||
file << " button_right: " << gamepadButtonToString(gamepad_controls.button_right) << "\n";
|
||||
file << " button_jump: " << gamepadButtonToString(gamepad_controls.button_jump) << "\n";
|
||||
|
||||
file.close();
|
||||
|
||||
if (console) {
|
||||
std::cout << "Config file saved successfully\n\n";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Options
|
||||
123
source/game/options.hpp
Normal file
123
source/game/options.hpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include <string> // Para string, basic_string
|
||||
#include <utility>
|
||||
|
||||
#include "core/rendering/screen.hpp" // Para Screen::Filter
|
||||
#include "game/defaults.hpp"
|
||||
#include "project.h" // Para Project::NAME, VERSION, COPYRIGHT
|
||||
#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
|
||||
State alternate_skin{Defaults::Cheat::ALTERNATE_SKIN ? State::ENABLED : State::DISABLED}; // Indica si se usa una skin diferente para el jugador
|
||||
|
||||
// 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 con opciones de la ventana
|
||||
struct Window {
|
||||
std::string caption{std::format("{} v{} ({})", Project::LONG_NAME, Project::VERSION, Project::COPYRIGHT)}; // 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 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 shaders{Defaults::Video::SHADERS}; // Indica si se van a usar shaders 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
|
||||
Border border{}; // Borde de la pantalla
|
||||
std::string palette{Defaults::Video::PALETTE_NAME}; // Paleta de colores a usar en el juego
|
||||
std::string info; // Información sobre el modo de vídeo
|
||||
};
|
||||
|
||||
// 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
|
||||
};
|
||||
|
||||
// --- Variables globales ---
|
||||
inline std::string version{}; // Versión del fichero de configuración. Sirve para saber si las opciones son compatibles
|
||||
inline bool console{false}; // Indica si ha de mostrar información por la consola de texto
|
||||
inline Cheat cheats{}; // Contiene trucos y ventajas para el juego
|
||||
inline Game game{}; // Opciones de juego
|
||||
inline Video video{}; // Opciones de video
|
||||
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
|
||||
|
||||
// Ruta completa del fichero de configuración (establecida mediante setConfigFile)
|
||||
inline std::string config_file_path{};
|
||||
|
||||
// --- Funciones públicas ---
|
||||
void init(); // Crea e inicializa las opciones del programa
|
||||
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
|
||||
|
||||
} // namespace Options
|
||||
34
source/game/scene_manager.hpp
Normal file
34
source/game/scene_manager.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#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
|
||||
TITLE, // Pantalla de título/menú principal
|
||||
GAME, // Juego principal
|
||||
QUIT // Salir del programa
|
||||
};
|
||||
|
||||
// --- Opciones para transiciones entre escenas ---
|
||||
enum class Options {
|
||||
NONE, // Sin opciones especiales
|
||||
};
|
||||
|
||||
// --- Variables de estado globales ---
|
||||
#ifdef _DEBUG
|
||||
inline Scene current = Scene::GAME; // Escena actual
|
||||
inline Options options = Options::NONE; // Opciones de la escena actual
|
||||
#else
|
||||
inline Scene current = Scene::LOGO; // Escena actual
|
||||
inline Options options = Options::NONE; // Opciones de la escena actual
|
||||
#endif
|
||||
|
||||
} // namespace SceneManager
|
||||
778
source/game/scenes/game.cpp
Normal file
778
source/game/scenes/game.cpp
Normal file
@@ -0,0 +1,778 @@
|
||||
#include "game/scenes/game.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cmath> // Para std::sqrt, std::min
|
||||
#include <utility>
|
||||
#include <vector> // Para vector
|
||||
|
||||
#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
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#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 ResourceRoom, Resource
|
||||
#include "core/resources/resource_list.hpp" // Para Asset
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "game/gameplay/item_tracker.hpp" // Para ItemTracker
|
||||
#include "game/gameplay/room.hpp" // Para Room, RoomData
|
||||
#include "game/gameplay/room_tracker.hpp" // Para RoomTracker
|
||||
#include "game/gameplay/scoreboard.hpp" // Para Scoreboard::Data, Scoreboard
|
||||
#include "game/defaults.hpp" // Para Defaults::Music
|
||||
#include "game/options.hpp" // Para Options, options, Cheat, SectionState
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "game/ui/notifier.hpp" // Para Notifier, NotificationText, CHEEVO_NO...
|
||||
#include "utils/defines.hpp" // Para Tile::SIZE, PlayArea::HEIGHT, RoomBorder::BOTTOM
|
||||
#include "utils/color.hpp" // Para Color
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
#ifdef _DEBUG
|
||||
#include "core/system/debug.hpp" // Para Debug
|
||||
#endif
|
||||
|
||||
// Constructor
|
||||
Game::Game(Mode mode)
|
||||
: scoreboard_data_(std::make_shared<Scoreboard::Data>(0, 9, 0, true, 0, SDL_GetTicks(), Options::cheats.jail_is_open == Options::Cheat::State::ENABLED)),
|
||||
scoreboard_(std::make_shared<Scoreboard>(scoreboard_data_)),
|
||||
room_tracker_(std::make_shared<RoomTracker>()),
|
||||
mode_(mode),
|
||||
#ifdef _DEBUG
|
||||
current_room_("03.yaml"),
|
||||
spawn_data_(Player::SpawnData(25 * Tile::SIZE, 21 * Tile::SIZE, 0, 0, 0, Player::State::ON_GROUND, Flip::LEFT))
|
||||
#else
|
||||
current_room_("03.yaml"),
|
||||
spawn_data_(Player::SpawnData(25 * Tile::SIZE, 13 * Tile::SIZE, 0, 0, 0, Player::State::ON_GROUND, Flip::LEFT))
|
||||
#endif
|
||||
{
|
||||
// Crea objetos e inicializa variables
|
||||
ItemTracker::init();
|
||||
demoInit();
|
||||
room_ = std::make_shared<Room>(current_room_, scoreboard_data_);
|
||||
initPlayer(spawn_data_, room_);
|
||||
total_items_ = getTotalItems();
|
||||
|
||||
createRoomNameTexture();
|
||||
game_backbuffer_surface_ = std::make_shared<Surface>(Options::game.width, Options::game.height);
|
||||
changeRoom(current_room_);
|
||||
|
||||
SceneManager::current = SceneManager::Scene::GAME;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
ItemTracker::destroy();
|
||||
}
|
||||
|
||||
// Comprueba los eventos de la cola
|
||||
void Game::handleEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
GlobalEvents::handle(event);
|
||||
#ifdef _DEBUG
|
||||
handleDebugEvents(event);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba el teclado
|
||||
void Game::handleInput() {
|
||||
Input::get()->update();
|
||||
|
||||
// Inputs globales siempre funcionan
|
||||
if (Input::get()->checkAction(InputAction::TOGGLE_MUSIC, Input::DO_NOT_ALLOW_REPEAT)) {
|
||||
scoreboard_data_->music = !scoreboard_data_->music;
|
||||
scoreboard_data_->music ? Audio::get()->resumeMusic() : Audio::get()->pauseMusic();
|
||||
Notifier::get()->show({"MUSIC " + std::string(scoreboard_data_->music ? "ENABLED" : "DISABLED")});
|
||||
}
|
||||
|
||||
// Durante fade/postfade, solo procesar inputs globales
|
||||
if (state_ != State::PLAYING) {
|
||||
GlobalInputs::handle();
|
||||
return;
|
||||
}
|
||||
|
||||
// Input de pausa solo en estado PLAYING
|
||||
if (Input::get()->checkAction(InputAction::PAUSE, Input::DO_NOT_ALLOW_REPEAT)) {
|
||||
togglePause();
|
||||
Notifier::get()->show({std::string(paused_ ? "GAME PAUSED" : "GAME RUNNING")});
|
||||
}
|
||||
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// Bucle para el juego
|
||||
void Game::run() {
|
||||
keepMusicPlaying();
|
||||
if (!scoreboard_data_->music && mode_ == Mode::GAME) {
|
||||
Audio::get()->pauseMusic();
|
||||
}
|
||||
|
||||
while (SceneManager::current == SceneManager::Scene::GAME) {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
if (mode_ == Mode::GAME) {
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||
void Game::update() {
|
||||
const float DELTA_TIME = delta_timer_.tick();
|
||||
|
||||
handleEvents(); // Comprueba los eventos
|
||||
handleInput(); // Comprueba las entradas
|
||||
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->clear();
|
||||
#endif
|
||||
|
||||
// Dispatch por estado
|
||||
switch (state_) {
|
||||
case State::PLAYING:
|
||||
updatePlaying(DELTA_TIME);
|
||||
break;
|
||||
case State::BLACK_SCREEN:
|
||||
updateBlackScreen(DELTA_TIME);
|
||||
break;
|
||||
case State::GAME_OVER:
|
||||
updateGameOver(DELTA_TIME);
|
||||
break;
|
||||
case State::FADE_TO_ENDING:
|
||||
updateFadeToEnding(DELTA_TIME);
|
||||
break;
|
||||
case State::POST_FADE_ENDING:
|
||||
updatePostFadeEnding(DELTA_TIME);
|
||||
break;
|
||||
}
|
||||
|
||||
Audio::update(); // Actualiza el objeto Audio
|
||||
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
|
||||
|
||||
#ifdef _DEBUG
|
||||
updateDebugInfo();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Actualiza el juego en estado PLAYING
|
||||
void Game::updatePlaying(float delta_time) {
|
||||
// Actualiza los objetos
|
||||
room_->update(delta_time);
|
||||
switch (mode_) {
|
||||
case Mode::GAME:
|
||||
#ifdef _DEBUG
|
||||
// Maneja el arrastre del jugador con el ratón (debug)
|
||||
handleDebugMouseDrag(delta_time);
|
||||
|
||||
// Si estamos arrastrando, no ejecutar la física normal del jugador
|
||||
if (!debug_dragging_player_) {
|
||||
player_->update(delta_time);
|
||||
}
|
||||
#else
|
||||
player_->update(delta_time);
|
||||
#endif
|
||||
checkPlayerIsOnBorder();
|
||||
checkPlayerAndItems();
|
||||
checkPlayerAndEnemies();
|
||||
checkIfPlayerIsAlive();
|
||||
checkEndGame();
|
||||
checkRestoringJail(delta_time);
|
||||
break;
|
||||
|
||||
case Mode::DEMO:
|
||||
demoCheckRoomChange(delta_time);
|
||||
break;
|
||||
}
|
||||
scoreboard_->update(delta_time);
|
||||
keepMusicPlaying();
|
||||
}
|
||||
|
||||
// Actualiza el juego en estado BLACK_SCREEN
|
||||
void Game::updateBlackScreen(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
|
||||
// Si se acabaron las vidas Y pasó el threshold → GAME_OVER
|
||||
if (scoreboard_data_->lives < 0 && state_time_ > GAME_OVER_THRESHOLD) {
|
||||
transitionToState(State::GAME_OVER);
|
||||
return;
|
||||
}
|
||||
|
||||
// Si pasó la duración completa → volver a PLAYING
|
||||
if (state_time_ > BLACK_SCREEN_DURATION) {
|
||||
// Despausar al salir
|
||||
player_->setPaused(false);
|
||||
room_->setPaused(false);
|
||||
Screen::get()->setBorderColor(room_->getBorderColor());
|
||||
transitionToState(State::PLAYING);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el juego en estado GAME_OVER
|
||||
void Game::updateGameOver(float delta_time) {
|
||||
// Pequeño delay antes de cambiar escena
|
||||
state_time_ += delta_time;
|
||||
if (state_time_ > 0.1F) { // 100ms de delay mínimo
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el juego en estado FADE_TO_ENDING
|
||||
void Game::updateFadeToEnding(float delta_time) {
|
||||
// Actualiza room, enemies, items (todo sigue funcionando)
|
||||
room_->update(delta_time);
|
||||
|
||||
// NO actualizar player (congelar movimiento)
|
||||
// player_->update(delta_time); -- COMENTADO INTENCIONALMENTE
|
||||
|
||||
// Actualiza scoreboard
|
||||
scoreboard_->update(delta_time);
|
||||
keepMusicPlaying();
|
||||
|
||||
// Aplica el fade progresivo al BACKBUFFER (no al renderer de pantalla)
|
||||
fade_accumulator_ += delta_time;
|
||||
if (fade_accumulator_ >= FADE_STEP_INTERVAL) {
|
||||
fade_accumulator_ = 0.0F;
|
||||
if (game_backbuffer_surface_->fadeSubPalette()) {
|
||||
// Fade completado, transicionar a POST_FADE
|
||||
transitionToState(State::POST_FADE_ENDING);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el juego en estado POST_FADE_ENDING
|
||||
void Game::updatePostFadeEnding(float delta_time) {
|
||||
// Pantalla negra estática, acumular tiempo
|
||||
state_time_ += delta_time;
|
||||
|
||||
// Después del delay, cambiar a la escena de ending
|
||||
if (state_time_ >= POST_FADE_DELAY) {
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
}
|
||||
}
|
||||
|
||||
// Cambia al estado especificado y resetea los timers
|
||||
void Game::transitionToState(State new_state) {
|
||||
// Lógica de ENTRADA según el nuevo estado
|
||||
if (new_state == State::BLACK_SCREEN) {
|
||||
// Respawn room y player
|
||||
room_ = std::make_shared<Room>(current_room_, scoreboard_data_);
|
||||
initPlayer(spawn_data_, room_);
|
||||
// Pausar ambos
|
||||
room_->setPaused(true);
|
||||
player_->setPaused(true);
|
||||
}
|
||||
|
||||
state_ = new_state;
|
||||
state_time_ = 0.0F;
|
||||
fade_accumulator_ = 0.0F;
|
||||
}
|
||||
|
||||
// Pinta los objetos en pantalla
|
||||
void Game::render() {
|
||||
// Dispatch por estado
|
||||
switch (state_) {
|
||||
case State::PLAYING:
|
||||
renderPlaying();
|
||||
break;
|
||||
case State::BLACK_SCREEN:
|
||||
renderBlackScreen();
|
||||
break;
|
||||
case State::GAME_OVER:
|
||||
renderGameOver();
|
||||
break;
|
||||
case State::FADE_TO_ENDING:
|
||||
renderFadeToEnding();
|
||||
break;
|
||||
case State::POST_FADE_ENDING:
|
||||
renderPostFadeEnding();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Renderiza el juego en estado PLAYING (directo a pantalla)
|
||||
void Game::renderPlaying() {
|
||||
// Prepara para dibujar el frame
|
||||
Screen::get()->start();
|
||||
|
||||
// Dibuja los elementos del juego en orden
|
||||
room_->renderMap();
|
||||
room_->renderEnemies();
|
||||
room_->renderItems();
|
||||
if (mode_ == Mode::GAME) {
|
||||
player_->render();
|
||||
}
|
||||
renderRoomName();
|
||||
#ifdef _DEBUG
|
||||
if (!Debug::get()->isEnabled()) {
|
||||
scoreboard_->render();
|
||||
}
|
||||
// Debug info
|
||||
renderDebugInfo();
|
||||
#else
|
||||
scoreboard_->render();
|
||||
#endif
|
||||
|
||||
// Actualiza la pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Renderiza el juego en estado BLACK_SCREEN (pantalla negra)
|
||||
void Game::renderBlackScreen() {
|
||||
Screen::get()->start();
|
||||
auto const COLOR = Color::index(Color::Cpc::BLACK);
|
||||
Screen::get()->clearSurface(COLOR);
|
||||
Screen::get()->setBorderColor(COLOR);
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Renderiza el juego en estado GAME_OVER (pantalla negra)
|
||||
void Game::renderGameOver() {
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(Color::index(Color::Cpc::BLACK));
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Renderiza el juego en estado FADE_TO_ENDING (via backbuffer)
|
||||
void Game::renderFadeToEnding() {
|
||||
// 1. Guardar renderer actual
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
|
||||
// 2. Cambiar target a backbuffer
|
||||
Screen::get()->setRendererSurface(game_backbuffer_surface_);
|
||||
|
||||
// 3. Renderizar todo a backbuffer
|
||||
game_backbuffer_surface_->clear(Color::index(Color::Cpc::BLACK));
|
||||
room_->renderMap();
|
||||
room_->renderEnemies();
|
||||
room_->renderItems();
|
||||
player_->render(); // Player congelado pero visible
|
||||
renderRoomName();
|
||||
scoreboard_->render();
|
||||
|
||||
// 4. Restaurar renderer original
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
|
||||
// 5. Preparar pantalla y volcar backbuffer (fade YA aplicado en update)
|
||||
Screen::get()->start();
|
||||
game_backbuffer_surface_->render();
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Renderiza el juego en estado POST_FADE_ENDING (pantalla negra)
|
||||
void Game::renderPostFadeEnding() {
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(Color::index(Color::Cpc::BLACK));
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Pasa la información de debug
|
||||
void Game::updateDebugInfo() {
|
||||
// Debug::get()->add("X = " + std::to_string(static_cast<int>(player_->x_)) + ", Y = " + std::to_string(static_cast<int>(player_->y_)));
|
||||
// Debug::get()->add("VX = " + std::to_string(player_->vx_).substr(0, 4) + ", VY = " + std::to_string(player_->vy_).substr(0, 4));
|
||||
// Debug::get()->add("STATE = " + std::to_string(static_cast<int>(player_->state_)));
|
||||
}
|
||||
|
||||
// Pone la información de debug en pantalla
|
||||
void Game::renderDebugInfo() {
|
||||
if (!Debug::get()->isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto surface = Screen::get()->getRendererSurface();
|
||||
|
||||
// Borra el área del scoreboard
|
||||
SDL_FRect rect = {ScoreboardArea::X, ScoreboardArea::Y, ScoreboardArea::WIDTH, ScoreboardArea::HEIGHT};
|
||||
surface->fillRect(&rect, Color::index(Color::Cpc::BLACK));
|
||||
|
||||
// Pinta la rejilla
|
||||
/*for (int i = 0; i < PlayArea::BOTTOM; i += 8)
|
||||
{
|
||||
// Lineas horizontales
|
||||
surface->drawLine(0, i, PlayArea::RIGHT, i, static_cast<Uint8>(PaletteColor::BRIGHT_BLACK));
|
||||
}
|
||||
for (int i = 0; i < PlayArea::RIGHT; i += 8)
|
||||
{
|
||||
// Lineas verticales
|
||||
surface->drawLine(i, 0, i, PlayArea::BOTTOM - 1, static_cast<Uint8>(PaletteColor::BRIGHT_BLACK));
|
||||
}*/
|
||||
|
||||
// Pinta el texto de debug en el área del scoreboard
|
||||
Debug::get()->setPos({ScoreboardArea::X, ScoreboardArea::Y});
|
||||
Debug::get()->render();
|
||||
}
|
||||
|
||||
// Comprueba los eventos
|
||||
void Game::handleDebugEvents(const SDL_Event& event) {
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) {
|
||||
switch (event.key.key) {
|
||||
case SDLK_F12:
|
||||
Debug::get()->toggleEnabled();
|
||||
Notifier::get()->show({"DEBUG " + std::string(Debug::get()->isEnabled() ? "ENABLED" : "DISABLED")});
|
||||
room_->redrawMap(); // Redibuja el tilemap para mostrar/ocultar líneas de colisión
|
||||
Options::cheats.invincible = static_cast<Options::Cheat::State>(Debug::get()->isEnabled());
|
||||
player_->setColor();
|
||||
scoreboard_data_->music = !Debug::get()->isEnabled();
|
||||
scoreboard_data_->music ? Audio::get()->resumeMusic() : Audio::get()->pauseMusic();
|
||||
break;
|
||||
|
||||
case SDLK_R:
|
||||
Resource::Cache::get()->reload();
|
||||
break;
|
||||
|
||||
case SDLK_W:
|
||||
changeRoom(room_->getRoom(Room::Border::TOP));
|
||||
break;
|
||||
|
||||
case SDLK_A:
|
||||
changeRoom(room_->getRoom(Room::Border::LEFT));
|
||||
break;
|
||||
|
||||
case SDLK_S:
|
||||
changeRoom(room_->getRoom(Room::Border::BOTTOM));
|
||||
break;
|
||||
|
||||
case SDLK_D:
|
||||
changeRoom(room_->getRoom(Room::Border::RIGHT));
|
||||
break;
|
||||
|
||||
case SDLK_1:
|
||||
Options::cheats.infinite_lives = Options::cheats.infinite_lives == Options::Cheat::State::ENABLED ? Options::Cheat::State::DISABLED : Options::Cheat::State::ENABLED;
|
||||
Notifier::get()->show({std::string("INFINITE LIVES ") + (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED ? "ENABLED" : "DISABLED")}, Notifier::Style::DEFAULT, -1, true);
|
||||
player_->setColor();
|
||||
break;
|
||||
|
||||
case SDLK_2:
|
||||
Options::cheats.invincible = Options::cheats.invincible == Options::Cheat::State::ENABLED ? Options::Cheat::State::DISABLED : Options::Cheat::State::ENABLED;
|
||||
Notifier::get()->show({std::string("INVINCIBLE ") + (Options::cheats.invincible == Options::Cheat::State::ENABLED ? "ENABLED" : "DISABLED")}, Notifier::Style::DEFAULT, -1, true);
|
||||
player_->setColor();
|
||||
break;
|
||||
|
||||
case SDLK_3:
|
||||
Options::cheats.jail_is_open = Options::cheats.jail_is_open == Options::Cheat::State::ENABLED ? Options::Cheat::State::DISABLED : Options::Cheat::State::ENABLED;
|
||||
Notifier::get()->show({std::string("JAIL IS OPEN ") + (Options::cheats.jail_is_open == Options::Cheat::State::ENABLED ? "ENABLED" : "DISABLED")}, Notifier::Style::DEFAULT, -1, true);
|
||||
break;
|
||||
|
||||
case SDLK_0:
|
||||
Screen::get()->toggleDebugInfo();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Maneja el arrastre del jugador con el ratón (debug)
|
||||
void Game::handleDebugMouseDrag(float delta_time) {
|
||||
// Solo funciona si Debug está habilitado
|
||||
if (!Debug::get()->isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Obtener estado del ratón (coordenadas de ventana física)
|
||||
float mouse_x = 0.0F;
|
||||
float mouse_y = 0.0F;
|
||||
SDL_MouseButtonFlags buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
|
||||
// Convertir coordenadas de ventana a coordenadas lógicas del renderer
|
||||
float render_x = 0.0F;
|
||||
float render_y = 0.0F;
|
||||
SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);
|
||||
|
||||
// Restar el offset del borde para obtener coordenadas del área de juego
|
||||
SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
|
||||
float game_x = render_x - dst_rect.x;
|
||||
float game_y = render_y - dst_rect.y;
|
||||
|
||||
// Verificar si los botones están presionados
|
||||
bool left_button_pressed = (buttons & SDL_BUTTON_LMASK) != 0;
|
||||
bool right_button_pressed = (buttons & SDL_BUTTON_RMASK) != 0;
|
||||
|
||||
// Botón derecho: teleport instantáneo a la posición del cursor
|
||||
if (right_button_pressed && !debug_dragging_player_) {
|
||||
player_->setDebugPosition(game_x, game_y);
|
||||
player_->finalizeDebugTeleport();
|
||||
} else if (left_button_pressed) {
|
||||
// Obtener posición actual del jugador
|
||||
SDL_FRect player_rect = player_->getRect();
|
||||
float player_x = player_rect.x;
|
||||
float player_y = player_rect.y;
|
||||
|
||||
// Calcular distancia al objetivo
|
||||
float dx = game_x - player_x;
|
||||
float dy = game_y - player_y;
|
||||
float distance = std::sqrt(dx * dx + dy * dy);
|
||||
|
||||
// Constantes de velocidad con ease-in (aceleración progresiva)
|
||||
constexpr float DRAG_SPEED_MIN = 30.0F; // Velocidad inicial (pixels/segundo)
|
||||
constexpr float DRAG_SPEED_MAX = 600.0F; // Velocidad máxima (pixels/segundo)
|
||||
constexpr float DRAG_ACCELERATION = 600.0F; // Aceleración (pixels/segundo²)
|
||||
|
||||
// Incrementar velocidad con el tiempo (ease-in)
|
||||
if (!debug_dragging_player_) {
|
||||
debug_drag_speed_ = DRAG_SPEED_MIN; // Iniciar con velocidad mínima
|
||||
}
|
||||
debug_drag_speed_ = std::min(DRAG_SPEED_MAX, debug_drag_speed_ + DRAG_ACCELERATION * delta_time);
|
||||
|
||||
if (distance > 1.0F) {
|
||||
// Calcular el movimiento con la velocidad actual
|
||||
float move_factor = std::min(1.0F, debug_drag_speed_ * delta_time / distance);
|
||||
float new_x = player_x + dx * move_factor;
|
||||
float new_y = player_y + dy * move_factor;
|
||||
|
||||
// Mover el jugador hacia la posición del cursor
|
||||
player_->setDebugPosition(new_x, new_y);
|
||||
}
|
||||
|
||||
debug_dragging_player_ = true;
|
||||
Debug::get()->add(std::string("X : " + std::to_string(static_cast<int>(player_rect.x))));
|
||||
Debug::get()->add(std::string("Y : " + std::to_string(static_cast<int>(player_rect.y))));
|
||||
} else if (debug_dragging_player_) {
|
||||
// Botón soltado después de arrastrar: finalizar teleport
|
||||
player_->finalizeDebugTeleport();
|
||||
debug_dragging_player_ = false;
|
||||
debug_drag_speed_ = 0.0F; // Reset para el próximo arrastre
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Escribe el nombre de la pantalla
|
||||
void Game::renderRoomName() {
|
||||
// Dibuja la textura con el nombre de la habitación
|
||||
room_name_surface_->render(nullptr, &room_name_rect_);
|
||||
}
|
||||
|
||||
// Cambia de habitación
|
||||
auto Game::changeRoom(const std::string& room_path) -> bool {
|
||||
// En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada
|
||||
if (room_path == "0") {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verifica que exista el fichero que se va a cargar
|
||||
if (!Resource::List::get()->get(room_path).empty()) {
|
||||
// Crea un objeto habitación nuevo a partir del fichero
|
||||
room_ = std::make_shared<Room>(room_path, scoreboard_data_);
|
||||
|
||||
// Pone el nombre de la habitación en la textura
|
||||
fillRoomNameTexture();
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
setScoreBoardColor();
|
||||
|
||||
if (room_tracker_->addRoom(room_path)) {
|
||||
// Incrementa el contador de habitaciones visitadas
|
||||
scoreboard_data_->rooms++;
|
||||
}
|
||||
|
||||
// Pasa la nueva habitación al jugador
|
||||
player_->setRoom(room_);
|
||||
|
||||
// Cambia la habitación actual
|
||||
current_room_ = room_path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta en el borde de la pantalla
|
||||
void Game::checkPlayerIsOnBorder() {
|
||||
if (player_->isOnBorder()) {
|
||||
const auto BORDER = player_->getBorder();
|
||||
const auto ROOM_NAME = room_->getRoom(BORDER);
|
||||
|
||||
// Si puede cambiar de habitación, cambia
|
||||
if (changeRoom(ROOM_NAME)) {
|
||||
player_->switchBorders();
|
||||
spawn_data_ = player_->getSpawnParams();
|
||||
return;
|
||||
}
|
||||
|
||||
// Si ha llegado al fondo y no hay habitación, muere
|
||||
if (BORDER == Room::Border::BOTTOM) {
|
||||
killPlayer();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las colisiones del jugador con los enemigos
|
||||
auto Game::checkPlayerAndEnemies() -> bool {
|
||||
const bool DEATH = room_->enemyCollision(player_->getCollider());
|
||||
if (DEATH) {
|
||||
killPlayer();
|
||||
}
|
||||
return DEATH;
|
||||
}
|
||||
|
||||
// Comprueba las colisiones del jugador con los objetos
|
||||
void Game::checkPlayerAndItems() {
|
||||
room_->itemCollision(player_->getCollider());
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta vivo
|
||||
void Game::checkIfPlayerIsAlive() {
|
||||
if (!player_->isAlive()) {
|
||||
killPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
// Mata al jugador
|
||||
void Game::killPlayer() {
|
||||
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Resta una vida al jugador
|
||||
if (Options::cheats.infinite_lives == Options::Cheat::State::DISABLED) {
|
||||
--scoreboard_data_->lives;
|
||||
}
|
||||
|
||||
// Sonido
|
||||
Audio::get()->playSound(Defaults::Sound::HIT, Audio::Group::GAME);
|
||||
|
||||
// Transicionar al estado BLACK_SCREEN (el respawn ocurre en transitionToState)
|
||||
transitionToState(State::BLACK_SCREEN);
|
||||
}
|
||||
|
||||
// Pone el color del marcador en función del color del borde de la habitación
|
||||
void Game::setScoreBoardColor() {
|
||||
// Obtiene el color del borde
|
||||
const Uint8 BORDER_COLOR = room_->getBorderColor();
|
||||
|
||||
const bool IS_BLACK = BORDER_COLOR == Color::index(Color::Cpc::BLACK);
|
||||
|
||||
// Si el color del borde es negro cambia el texto del marcador a blanco
|
||||
scoreboard_data_->color = IS_BLACK ? Color::index(Color::Cpc::WHITE) : BORDER_COLOR;
|
||||
}
|
||||
|
||||
// Comprueba si ha finalizado el juego
|
||||
auto Game::checkEndGame() -> bool {
|
||||
const bool IS_ON_THE_ROOM = room_->getName() == "THE JAIL"; // Estar en la habitación que toca
|
||||
const bool HAVE_THE_ITEMS = scoreboard_data_->items >= int(total_items_ * 0.9F) || Options::cheats.jail_is_open == Options::Cheat::State::ENABLED; // Con mas del 90% de los items recogidos
|
||||
const bool IS_ON_THE_DOOR = player_->getRect().x <= 128; // Y en la ubicación que toca (En la puerta)
|
||||
|
||||
scoreboard_data_->jail_is_open = HAVE_THE_ITEMS;
|
||||
|
||||
if (HAVE_THE_ITEMS && IS_ON_THE_ROOM && IS_ON_THE_DOOR) {
|
||||
// Iniciar transición de fade en vez de cambio inmediato de escena
|
||||
transitionToState(State::FADE_TO_ENDING);
|
||||
Audio::get()->fadeOutMusic(Defaults::Music::FADE_DURATION_MS); // Fade out de música
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Obtiene la cantidad total de items que hay en el mapeado del juego
|
||||
auto Game::getTotalItems() -> int {
|
||||
int items = 0;
|
||||
auto rooms = Resource::Cache::get()->getRooms();
|
||||
|
||||
for (const auto& room : rooms) {
|
||||
items += room.room->items.size();
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
// Pone el juego en pausa
|
||||
void Game::togglePause() {
|
||||
paused_ = !paused_;
|
||||
|
||||
player_->setPaused(paused_);
|
||||
room_->setPaused(paused_);
|
||||
scoreboard_->setPaused(paused_);
|
||||
}
|
||||
|
||||
// Da vidas al jugador cuando está en la Jail
|
||||
void Game::checkRestoringJail(float delta_time) {
|
||||
if (room_->getName() != "THE JAIL" || scoreboard_data_->lives == 9) {
|
||||
jail_restore_time_ = 0.0F; // Reset timer cuando no está en la Jail
|
||||
return;
|
||||
}
|
||||
|
||||
if (!paused_) {
|
||||
jail_restore_time_ += delta_time;
|
||||
}
|
||||
|
||||
// Incrementa el numero de vidas
|
||||
if (jail_restore_time_ >= JAIL_RESTORE_INTERVAL) {
|
||||
jail_restore_time_ -= JAIL_RESTORE_INTERVAL; // Mantiene el excedente para precisión
|
||||
scoreboard_data_->lives++;
|
||||
Audio::get()->playSound(Defaults::Sound::HIT, Audio::Group::GAME);
|
||||
}
|
||||
}
|
||||
|
||||
// Crea la textura con el nombre de la habitación
|
||||
void Game::fillRoomNameTexture() {
|
||||
// Pone la textura como destino de renderizado
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(room_name_surface_);
|
||||
|
||||
// Rellena la textura de color
|
||||
room_name_surface_->clear(stringToColor("white"));
|
||||
|
||||
// Escribe el texto en la textura
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
text->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, text->getCharacterSize() / 2, room_->getName(), 1, room_->getBGColor());
|
||||
|
||||
// Deja el renderizador por defecto
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
}
|
||||
|
||||
// Inicializa al jugador
|
||||
void Game::initPlayer(const Player::SpawnData& spawn_point, std::shared_ptr<Room> room) {
|
||||
std::string player_animations = Options::cheats.alternate_skin == Options::Cheat::State::ENABLED ? "player2.yaml" : "player.yaml";
|
||||
const Player::Data PLAYER{.spawn_data = spawn_point, .animations_path = player_animations, .room = std::move(room)};
|
||||
player_ = std::make_shared<Player>(PLAYER);
|
||||
}
|
||||
|
||||
// Crea la textura para poner el nombre de la habitación
|
||||
void Game::createRoomNameTexture() {
|
||||
auto text = Resource::Cache::get()->getText("smb2");
|
||||
room_name_surface_ = std::make_shared<Surface>(Options::game.width, text->getCharacterSize() * 2);
|
||||
|
||||
// Establece el destino de la textura
|
||||
room_name_rect_ = {.x = 0.0F, .y = PlayArea::HEIGHT, .w = Options::game.width, .h = text->getCharacterSize() * 2.0F};
|
||||
}
|
||||
|
||||
// Hace sonar la música
|
||||
void Game::keepMusicPlaying() {
|
||||
const std::string MUSIC_PATH = mode_ == Mode::GAME ? Defaults::Music::GAME_TRACK : Defaults::Music::TITLE_TRACK;
|
||||
|
||||
// Si la música no está sonando
|
||||
if (Audio::get()->getMusicState() == Audio::MusicState::STOPPED) {
|
||||
Audio::get()->playMusic(MUSIC_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
// DEMO MODE: Inicializa las variables para el modo demo
|
||||
void Game::demoInit() {
|
||||
if (mode_ == Mode::DEMO) {
|
||||
demo_ = DemoData(0.0F, 0, {"04.yaml", "54.yaml", "20.yaml", "09.yaml", "05.yaml", "11.yaml", "31.yaml", "44.yaml"});
|
||||
current_room_ = demo_.rooms.front();
|
||||
}
|
||||
}
|
||||
|
||||
// DEMO MODE: Comprueba si se ha de cambiar de habitación
|
||||
void Game::demoCheckRoomChange(float delta_time) {
|
||||
if (mode_ == Mode::DEMO) {
|
||||
demo_.time_accumulator += delta_time;
|
||||
if (demo_.time_accumulator >= DEMO_ROOM_DURATION) {
|
||||
demo_.time_accumulator = 0.0F;
|
||||
demo_.room_index++;
|
||||
if (demo_.room_index == (int)demo_.rooms.size()) {
|
||||
SceneManager::current = SceneManager::Scene::LOGO;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
} else {
|
||||
changeRoom(demo_.rooms[demo_.room_index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
132
source/game/scenes/game.hpp
Normal file
132
source/game/scenes/game.hpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#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)
|
||||
void renderBlackScreen(); // Renderiza el juego en estado BLACK_SCREEN (pantalla negra)
|
||||
void renderGameOver(); // Renderiza el juego en estado GAME_OVER (pantalla negra)
|
||||
void renderFadeToEnding(); // Renderiza el juego en estado FADE_TO_ENDING (via backbuffer)
|
||||
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
|
||||
void updateDebugInfo(); // Pone la información de debug en pantalla
|
||||
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)
|
||||
#endif
|
||||
};
|
||||
296
source/game/scenes/logo.cpp
Normal file
296
source/game/scenes/logo.cpp
Normal file
@@ -0,0 +1,296 @@
|
||||
#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/surface.hpp" // Para Surface
|
||||
#include "core/rendering/surface_sprite.hpp" // Para SSprite
|
||||
#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/defines.hpp" // Para GameCanvas
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
#include "utils/easing_functions.hpp" // Para funciones de suavizado
|
||||
#include "utils/color.hpp" // Para Color
|
||||
|
||||
// Constructor
|
||||
Logo::Logo()
|
||||
: jailgames_surface_(Resource::Cache::get()->getSurface("jailgames.gif")),
|
||||
since_1998_surface_(Resource::Cache::get()->getSurface("since_1998.gif")),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()) {
|
||||
// Calcula posiciones dinámicas basadas en el tamaño del canvas y las texturas
|
||||
jailgames_dest_x_ = GameCanvas::CENTER_X - (jailgames_surface_->getWidth() / 2);
|
||||
base_y_ = GameCanvas::CENTER_Y - (jailgames_surface_->getHeight() / 2);
|
||||
|
||||
// Crea el sprite "Since 1998" centrado horizontalmente, debajo del logo JAILGAMES
|
||||
const int SINCE_1998_X = GameCanvas::CENTER_X - (since_1998_surface_->getWidth() / 2);
|
||||
const int SINCE_1998_Y = base_y_ + jailgames_surface_->getHeight() + 5;
|
||||
since_1998_sprite_ = std::make_shared<SurfaceSprite>(
|
||||
since_1998_surface_,
|
||||
SINCE_1998_X,
|
||||
SINCE_1998_Y,
|
||||
since_1998_surface_->getWidth(),
|
||||
since_1998_surface_->getHeight());
|
||||
|
||||
// Configura variables
|
||||
since_1998_sprite_->setClip(0, 0, since_1998_surface_->getWidth(), since_1998_surface_->getHeight());
|
||||
since_1998_color_ = Color::index(Color::Cpc::BLACK);
|
||||
jailgames_color_ = Color::index(Color::Cpc::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) { return Easing::backOut(t); }, // Overshoot retro
|
||||
[](float t) { 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(Color::index(Color::Cpc::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 {
|
||||
// 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::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() {
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(Color::index(Color::Cpc::BLACK));
|
||||
|
||||
// Dibuja los objetos
|
||||
for (const auto& sprite : jailgames_sprite_) {
|
||||
sprite->render(27, jailgames_color_);
|
||||
}
|
||||
since_1998_sprite_->render(27, 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::NONE:
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
break;
|
||||
|
||||
default:
|
||||
// Ninguna acción por defecto
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa el vector de colores
|
||||
void Logo::initColors() {
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<Uint8> COLORS = {
|
||||
Color::index(Color::Cpc::BLACK),
|
||||
Color::index(Color::Cpc::BLUE),
|
||||
Color::index(Color::Cpc::RED),
|
||||
Color::index(Color::Cpc::MAGENTA),
|
||||
Color::index(Color::Cpc::GREEN),
|
||||
Color::index(Color::Cpc::CYAN),
|
||||
Color::index(Color::Cpc::YELLOW),
|
||||
Color::index(Color::Cpc::BRIGHT_WHITE)};
|
||||
for (const auto& color : COLORS) {
|
||||
color_.push_back(color);
|
||||
}
|
||||
}
|
||||
|
||||
// Crea los sprites de cada linea
|
||||
void Logo::initSprites() {
|
||||
const int TEXTURE_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<SurfaceSprite>(jailgames_surface_, 0, i, TEXTURE_WIDTH, 1));
|
||||
jailgames_sprite_.back()->setClip(0, i, TEXTURE_WIDTH, 1);
|
||||
|
||||
// Calcular posición inicial (alternando entre derecha e izquierda, fuera del canvas)
|
||||
constexpr int LINE_OFFSET = 6;
|
||||
const int INITIAL_X = (i % 2 == 0)
|
||||
? (GameCanvas::WIDTH + (i * LINE_OFFSET))
|
||||
: (-TEXTURE_WIDTH - (i * LINE_OFFSET));
|
||||
jailgames_initial_x_.push_back(INITIAL_X);
|
||||
|
||||
jailgames_sprite_.at(i)->setX(INITIAL_X);
|
||||
jailgames_sprite_.at(i)->setY(base_y_ + i);
|
||||
}
|
||||
}
|
||||
81
source/game/scenes/logo.hpp
Normal file
81
source/game/scenes/logo.hpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#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 SurfaceSprite; // 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
|
||||
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
|
||||
|
||||
// --- Constantes de animación ---
|
||||
static constexpr float JAILGAMES_SLIDE_DURATION = 0.8F; // Duración de la animación de slide-in (segundos)
|
||||
|
||||
// --- 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<SurfaceSprite>> 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<SurfaceSprite> since_1998_sprite_; // SSprite para manejar la textura2
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para delta time
|
||||
|
||||
// Posiciones calculadas dinámicamente (centrado independiente del tamaño del canvas)
|
||||
int jailgames_dest_x_{0}; // Posición X de destino para JAILGAMES (centrado horizontal)
|
||||
int base_y_{0}; // Posición Y base para los sprites (centrado vertical)
|
||||
|
||||
// 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
|
||||
};
|
||||
612
source/game/scenes/title.cpp
Normal file
612
source/game/scenes/title.cpp
Normal file
@@ -0,0 +1,612 @@
|
||||
#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/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/rendering/surface_sprite.hpp" // Para SSprite
|
||||
#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/defaults.hpp" // Para Defaults::Music
|
||||
#include "game/options.hpp" // Para Options, options, SectionState, Section
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "utils/defines.hpp" // Para GameCanvas::CENTER_X, GameCanvas::WIDTH
|
||||
#include "utils/color.hpp" // Para Color
|
||||
#include "utils/utils.hpp" // Para stringToColor
|
||||
|
||||
// Constructor
|
||||
Title::Title()
|
||||
: game_logo_surface_(Resource::Cache::get()->getSurface("title_logo.gif")),
|
||||
game_logo_sprite_(std::make_unique<SurfaceSprite>(
|
||||
game_logo_surface_,
|
||||
(GameCanvas::WIDTH - game_logo_surface_->getWidth()) / 2, // Centrado horizontal dinámico
|
||||
static_cast<int>(GameCanvas::HEIGHT * 0.05F), // Posición Y proporcional (~5% desde arriba)
|
||||
game_logo_surface_->getWidth(),
|
||||
game_logo_surface_->getHeight())),
|
||||
title_surface_(std::make_shared<Surface>(Options::game.width, Options::game.height)),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()),
|
||||
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
|
||||
state_ = State::MAIN_MENU;
|
||||
|
||||
// Establece SceneManager
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
|
||||
// Acciones iniciales
|
||||
Screen::get()->setBorderColor(Color::index(Color::Cpc::BLACK)); // Cambia el color del borde
|
||||
Audio::get()->playMusic(Defaults::Music::TITLE_TRACK); // Inicia la musica
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// 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(Defaults::Music::FADE_DURATION_MS);
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
// 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::MAIN_MENU:
|
||||
updateMainMenu(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 MAIN_MENU
|
||||
void Title::updateMainMenu(float 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, vuelve al logo
|
||||
if (state_time_ >= MAIN_MENU_IDLE_TIMEOUT) {
|
||||
exit_scene_ = SceneManager::Scene::LOGO;
|
||||
transitionToState(State::FADE_MENU);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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(Color::index(Color::Cpc::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();
|
||||
}
|
||||
}
|
||||
|
||||
// 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 dinámica del menú (proporcional al canvas)
|
||||
// El logo ocupa la parte superior, el menú se centra en el espacio restante
|
||||
const int LOGO_BOTTOM = static_cast<int>(GameCanvas::HEIGHT * 0.25F); // Espacio reservado para logo
|
||||
const int MENU_ZONE_HEIGHT = GameCanvas::HEIGHT - LOGO_BOTTOM; // Espacio disponible para menú
|
||||
|
||||
// Menú principal normal con 3 opciones centradas verticalmente en la zona
|
||||
const Uint8 COLOR = stringToColor("green");
|
||||
const int TEXT_SIZE = menu_text_->getCharacterSize();
|
||||
const int MENU_CENTER_Y = LOGO_BOTTOM + (MENU_ZONE_HEIGHT / 2);
|
||||
const int SPACING = 2 * TEXT_SIZE; // Espaciado entre opciones
|
||||
|
||||
// Calcula posiciones centradas verticalmente (3 items con espaciado)
|
||||
const int TOTAL_HEIGHT = 2 * SPACING; // 2 espacios entre 3 items
|
||||
const int START_Y = MENU_CENTER_Y - (TOTAL_HEIGHT / 2);
|
||||
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, "1. PLAY", 1, COLOR);
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y + SPACING, "2. REDEFINE KEYBOARD", 1, COLOR);
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y + (2 * SPACING), "3. REDEFINE JOYSTICK", 1, COLOR);
|
||||
}
|
||||
|
||||
// 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(Color::index(Color::Cpc::BLACK));
|
||||
|
||||
switch (state_) {
|
||||
case State::MAIN_MENU:
|
||||
case State::FADE_MENU:
|
||||
renderGameLogo();
|
||||
renderMainMenu();
|
||||
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_ = "INVALID KEY! TRY ANOTHER";
|
||||
return;
|
||||
}
|
||||
|
||||
// Verifica duplicados
|
||||
if (isKeyDuplicate(scancode, remap_step_)) {
|
||||
remap_error_message_ = "KEY ALREADY USED! TRY ANOTHER";
|
||||
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 {
|
||||
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 {
|
||||
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() {
|
||||
// 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() {
|
||||
// Zona dinámica del menú (proporcional al canvas)
|
||||
const int LOGO_BOTTOM = static_cast<int>(GameCanvas::HEIGHT * 0.25F);
|
||||
const int MENU_ZONE_HEIGHT = GameCanvas::HEIGHT - LOGO_BOTTOM;
|
||||
|
||||
const Uint8 COLOR = stringToColor("green");
|
||||
const Uint8 ERROR_COLOR = stringToColor("red");
|
||||
const int TEXT_SIZE = menu_text_->getCharacterSize();
|
||||
const int MENU_CENTER_Y = LOGO_BOTTOM + (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
|
||||
if (remap_step_ >= 3) {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, "KEYS DEFINED", 1, COLOR);
|
||||
} else {
|
||||
const std::string ACTION = getActionName(remap_step_);
|
||||
const std::string MESSAGE = "PRESS KEY FOR " + ACTION;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, MESSAGE, 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 = "LEFT: " + LEFT_KEY;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::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 = "RIGHT: " + RIGHT_KEY;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::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 = "JUMP: " + JUMP_KEY;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::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, GameCanvas::CENTER_X, KEYS_START_Y + (4 * LINE_SPACING), remap_error_message_, 1, ERROR_COLOR);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja la pantalla de redefinir joystick
|
||||
void Title::renderJoystickRemap() {
|
||||
// Zona dinámica del menú (proporcional al canvas)
|
||||
const int LOGO_BOTTOM = static_cast<int>(GameCanvas::HEIGHT * 0.25F);
|
||||
const int MENU_ZONE_HEIGHT = GameCanvas::HEIGHT - LOGO_BOTTOM;
|
||||
|
||||
const Uint8 COLOR = stringToColor("green");
|
||||
const Uint8 ERROR_COLOR = stringToColor("red");
|
||||
const int TEXT_SIZE = menu_text_->getCharacterSize();
|
||||
const int MENU_CENTER_Y = LOGO_BOTTOM + (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
|
||||
if (remap_step_ >= 3) {
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, "BUTTONS DEFINED", 1, COLOR);
|
||||
} else {
|
||||
const std::string ACTION = getActionName(remap_step_);
|
||||
const std::string MESSAGE = "PRESS BUTTON FOR " + ACTION;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::CENTER_X, START_Y, MESSAGE, 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 = "LEFT: " + LEFT_BTN;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::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 = "RIGHT: " + RIGHT_BTN;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::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 = "JUMP: " + JUMP_BTN;
|
||||
menu_text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, GameCanvas::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, GameCanvas::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_ = "BUTTON ALREADY USED! TRY ANOTHER";
|
||||
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 {
|
||||
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() {
|
||||
// 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 {
|
||||
// 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";
|
||||
}
|
||||
87
source/game/scenes/title.hpp
Normal file
87
source/game/scenes/title.hpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array> // Para std::array
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
|
||||
#include "game/scene_manager.hpp" // Para SceneManager::Scene
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
class SurfaceSprite; // Forward declaration
|
||||
class Surface; // Forward declaration
|
||||
class Text; // Forward declaration
|
||||
|
||||
class Title {
|
||||
public:
|
||||
// --- Constructor y Destructor ---
|
||||
Title();
|
||||
~Title() = default;
|
||||
|
||||
// --- Bucle principal ---
|
||||
void run();
|
||||
|
||||
private:
|
||||
// --- Enumeraciones ---
|
||||
enum class State {
|
||||
MAIN_MENU,
|
||||
FADE_MENU,
|
||||
POST_FADE_MENU,
|
||||
};
|
||||
|
||||
// --- Constantes de tiempo (en segundos) ---
|
||||
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
|
||||
|
||||
// --- 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 updateMainMenu(float delta_time); // Actualiza MAIN_MENU
|
||||
void updateFadeMenu(float delta_time); // Actualiza FADE_MENU
|
||||
void updatePostFadeMenu(float delta_time); // Actualiza POST_FADE_MENU
|
||||
void renderGameLogo(); // Dibuja el logo con el titulo del juego
|
||||
void renderMainMenu(); // Dibuja el menu principal
|
||||
void renderKeyboardRemap(); // Dibuja la pantalla de redefinir teclado
|
||||
void renderJoystickRemap(); // 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 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<SurfaceSprite> game_logo_sprite_; // SSprite para manejar la surface
|
||||
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> menu_text_; // Texto para los menus
|
||||
|
||||
// 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)
|
||||
};
|
||||
289
source/game/ui/notifier.cpp
Normal file
289
source/game/ui/notifier.cpp
Normal file
@@ -0,0 +1,289 @@
|
||||
#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/surface.hpp" // Para Surface
|
||||
#include "core/rendering/surface_sprite.hpp" // Para SSprite
|
||||
#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/color.hpp" // Para Color
|
||||
|
||||
// [SINGLETON]
|
||||
Notifier* Notifier::notifier = nullptr;
|
||||
|
||||
// Definición de estilos predefinidos
|
||||
const Notifier::Style Notifier::Style::DEFAULT = {
|
||||
.bg_color = Color::index(Color::Cpc::BLUE),
|
||||
.border_color = Color::index(Color::Cpc::CYAN),
|
||||
.text_color = Color::index(Color::Cpc::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 = Color::index(Color::Cpc::MAGENTA),
|
||||
.border_color = Color::index(Color::Cpc::BRIGHT_MAGENTA),
|
||||
.text_color = Color::index(Color::Cpc::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) {
|
||||
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() {
|
||||
auto result = std::ranges::remove_if(notifications_, [](const Notification& notification) {
|
||||
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) { 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 = PADDING_OUT;
|
||||
|
||||
// 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 = {0, 0, n.surface->getWidth(), 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<SurfaceSprite>(icon_surface_, (SDL_FRect){0, 0, ICON_SIZE, ICON_SIZE});
|
||||
sp->setPosition({PADDING_IN_H, PADDING_IN_V, ICON_SIZE, ICON_SIZE});
|
||||
sp->setClip((SDL_FRect){ICON_SIZE * (icon % 10), ICON_SIZE * (icon / 10), ICON_SIZE, 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<SurfaceSprite>(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();
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
110
source/game/ui/notifier.hpp
Normal file
110
source/game/ui/notifier.hpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string, basic_string
|
||||
#include <vector> // Para vector
|
||||
class SurfaceSprite; // 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
|
||||
|
||||
private:
|
||||
// Tipos anidados
|
||||
enum class Status {
|
||||
RISING,
|
||||
STAY,
|
||||
VANISHING,
|
||||
FINISHED,
|
||||
};
|
||||
|
||||
struct Notification {
|
||||
std::shared_ptr<Surface> surface{nullptr};
|
||||
std::shared_ptr<SurfaceSprite> sprite{nullptr};
|
||||
std::vector<std::string> texts;
|
||||
Status state{Status::RISING};
|
||||
Shape shape{Shape::SQUARED};
|
||||
SDL_FRect rect{0.0F, 0.0F, 0.0F, 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
|
||||
};
|
||||
Reference in New Issue
Block a user