Compare commits

14 Commits

Author SHA1 Message Date
4e083a8cdb item.cpp: afegida rotació 2025-09-30 14:16:25 +02:00
cbe4315701 moving_sprite.cpp: afegit umbral a stopRotate() 2025-09-30 14:11:58 +02:00
49d561b583 moving_sprite.cpp: afegida funcio per a escalar la velocitat de rotacio 2025-09-30 13:59:13 +02:00
6e56a6fd79 moving_sprite.cpp: afegits nous metodes per controlar la rotació 2025-09-30 13:47:48 +02:00
267d9647e0 moving_sprite.cpp: la variable rotate.speed ja no es gastava 2025-09-30 13:38:06 +02:00
13b3702d00 eliminat param.game.item_size 2025-09-30 13:23:50 +02:00
4500845dcd muguda la logica de demo de utils.cpp a demo.cpp 2025-09-30 12:58:32 +02:00
a4abc02f88 nou: modificat el valor de velocitat en la creació dels globos verds. i tornat a deixar com estava 2025-09-30 12:42:38 +02:00
a0fb6934b0 corregit: en el mode demo no calculava correctament el estat del fondo 2025-09-30 09:56:32 +02:00
19645445b2 corregit: els fills dels globos verds eixien taronja 2025-09-30 08:47:49 +02:00
efe8628a3c corregit: el log de CREATING PLAYER TEXTURES en resource.cpp 2025-09-29 14:22:44 +02:00
c98cb0d29f repensada la forma d'asignar fitxers de demo als jugadors
refets els fitxers de demo i afegit un tercer fitxer
2025-09-29 14:00:10 +02:00
c16fc1bae5 corregit: el mode demo ja funciona correctament 2025-09-29 12:47:13 +02:00
fa0af1179a corregit: no trobava version.h 2025-09-29 07:54:46 +02:00
33 changed files with 672 additions and 511 deletions

View File

@@ -95,6 +95,7 @@ set(APP_SOURCES
# --- Otros ---
source/color.cpp
source/demo.cpp
source/define_buttons.cpp
source/difficulty.cpp
source/input_types.cpp
@@ -124,7 +125,8 @@ add_executable(${PROJECT_NAME} ${APP_SOURCES} ${EXTERNAL_SOURCES})
# --- 3. DIRECTORIOS DE INCLUSIÓN ---
target_include_directories(${PROJECT_NAME} PUBLIC
"${CMAKE_SOURCE_DIR}/source"
"${CMAKE_SOURCE_DIR}/source/external"
"${CMAKE_SOURCE_DIR}/source/external"
"${CMAKE_BINARY_DIR}"
)
# Enlazar la librería SDL3
@@ -139,6 +141,9 @@ target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-Os -ffunctio
# Definir _DEBUG en modo Debug
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:_DEBUG>)
# Descomentar la siguiente línea para activar el modo grabación de demos
# target_compile_definitions(${PROJECT_NAME} PRIVATE RECORDING)
# Configuración específica para cada plataforma
if(WIN32)

View File

@@ -20,6 +20,7 @@ DATA|${PREFIX}/config/stages.txt
# Archivos con los datos de la demo
DEMODATA|${PREFIX}/data/demo/demo1.bin
DEMODATA|${PREFIX}/data/demo/demo2.bin
DEMODATA|${PREFIX}/data/demo/demo3.bin
# Música
MUSIC|${PREFIX}/data/music/congratulations.ogg

View File

@@ -2,7 +2,6 @@
# Formato: PARAMETRO VALOR
# --- GAME ---
game.item_size 20 # Tamaño de los items del juego (en píxeles)
game.item_text_outline_color E0E0E0F0 # Color del outline del texto de los items (RGBA hex)
game.width 320 # Ancho de la resolución nativa del juego (en píxeles)
game.height 240 # Alto de la resolución nativa del juego (en píxeles)

View File

@@ -4,31 +4,31 @@
# Los pools no necesitan estar ordenados ni ser consecutivos
# Pool para la fase 1
POOL: 0 FORMATIONS: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
POOL: 0 FORMATIONS: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# Pool para la fase 2
POOL: 1 FORMATIONS: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
POOL: 1 FORMATIONS: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
# Pool para la fase 3
POOL: 2 FORMATIONS: 0, 1, 2, 3, 4, 55, 56, 57, 58, 59
POOL: 2 FORMATIONS: 0, 1, 2, 3, 4, 55, 56, 57, 58, 59
# Pool para la fase 4
POOL: 3 FORMATIONS: 50, 51, 52, 53, 54, 5, 6, 7, 8, 9
POOL: 3 FORMATIONS: 50, 51, 52, 53, 54, 5, 6, 7, 8, 9
# Pool para la fase 5
POOL: 4 FORMATIONS: 60, 61, 62, 63, 64, 65, 66, 67, 68, 69
POOL: 4 FORMATIONS: 60, 61, 62, 63, 64, 65, 66, 67, 68, 69
# Pool para la fase 6
POOL: 5 FORMATIONS: 10, 61, 12, 63, 14, 65, 16, 67, 18, 69
POOL: 5 FORMATIONS: 10, 61, 12, 63, 14, 65, 16, 67, 18, 69
# Pool para la fase 7
POOL: 6 FORMATIONS: 60, 11, 62, 13, 64, 15, 66, 17, 68, 19
POOL: 6 FORMATIONS: 60, 11, 62, 13, 64, 15, 66, 17, 68, 19
# Pool para la fase 8
POOL: 7 FORMATIONS: 20, 21, 22, 23, 24, 65, 66, 67, 68, 69
POOL: 7 FORMATIONS: 20, 21, 22, 23, 24, 65, 66, 67, 68, 69
# Pool para la fase 9
POOL: 8 FORMATIONS: 70, 71, 72, 73, 74, 15, 16, 17, 18, 19
POOL: 8 FORMATIONS: 70, 71, 72, 73, 74, 15, 16, 17, 18, 19
# Pool para la fase 10
POOL: 9 FORMATIONS: 20, 21, 22, 23, 24, 70, 71, 72, 73, 74

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

BIN
data/demo/demo3.bin Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -2,6 +2,7 @@
#include <SDL3/SDL.h> // Para SDL_LogCategory, SDL_LogInfo, SDL_LogError, SDL_LogWarn
#include <algorithm> // Para std::sort
#include <cstddef> // Para size_t
#include <exception> // Para exception
#include <filesystem> // Para std::filesystem
@@ -299,6 +300,9 @@ auto Asset::getListByType(Type type) const -> std::vector<std::string> {
}
}
// Ordenar alfabéticamente para garantizar orden consistente
std::sort(list.begin(), list.end());
return list;
}

View File

@@ -122,6 +122,8 @@ class Balloon {
// --- Setters ---
void setVelY(float vel_y) { vy_ = vel_y; }
void setVelX(float vel_x) { vx_ = vel_x; }
void alterVelX(float percent) {vx_ *= percent; }
void setGameTempo(float tempo) { game_tempo_ = tempo; }
void setInvulnerable(bool value) { invulnerable_ = value; }
void setBouncingSound(bool value) { sound_.bouncing_enabled = value; }

View File

@@ -16,7 +16,7 @@
#include "utils.h"
// Constructor
BalloonManager::BalloonManager(IStageInfo *stage_info)
BalloonManager::BalloonManager(IStageInfo* stage_info)
: explosions_(std::make_unique<Explosions>()),
balloon_formations_(std::make_unique<BalloonFormations>()),
stage_info_(stage_info) { init(); }
@@ -64,7 +64,7 @@ void BalloonManager::init() {
// Actualiza (time-based)
void BalloonManager::update(float deltaTime) {
for (const auto &balloon : balloons_) {
for (const auto& balloon : balloons_) {
balloon->update(deltaTime);
}
updateBalloonDeployCounter(deltaTime);
@@ -73,7 +73,7 @@ void BalloonManager::update(float deltaTime) {
// Renderiza los objetos
void BalloonManager::render() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->render();
}
explosions_->render();
@@ -158,7 +158,7 @@ void BalloonManager::deployFormation(int formation_id, float y) {
// Vacia del vector de globos los globos que ya no sirven
void BalloonManager::freeBalloons() {
auto result = std::ranges::remove_if(balloons_, [](const auto &balloon) { return !balloon->isEnabled(); });
auto result = std::ranges::remove_if(balloons_, [](const auto& balloon) { return !balloon->isEnabled(); });
balloons_.erase(result.begin(), balloons_.end());
}
@@ -173,7 +173,7 @@ auto BalloonManager::canPowerBallBeCreated() -> bool { return (!power_ball_enabl
// Calcula el poder actual de los globos en pantalla
auto BalloonManager::calculateScreenPower() -> int {
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon) { return sum + (balloon->isEnabled() ? balloon->getPower() : 0); });
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto& balloon) { return sum + (balloon->isEnabled() ? balloon->getPower() : 0); });
}
// Crea un globo nuevo en el vector de globos
@@ -194,7 +194,7 @@ auto BalloonManager::createBalloon(Balloon::Config config) -> std::shared_ptr<Ba
}
// Crea un globo a partir de otro globo
void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon> &balloon, const std::string &direction) {
void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon>& balloon, const std::string& direction) {
if (can_deploy_balloons_) {
// Calcula parametros
const int PARENT_HEIGHT = balloon->getHeight();
@@ -208,6 +208,7 @@ void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon> &balloon,
Balloon::Config config = {
.x = std::clamp(X - (CHILD_WIDTH / 2), MIN_X, MAX_X),
.y = balloon->getPosY() + ((PARENT_HEIGHT - CHILD_HEIGHT) / 2),
.type = balloon->getType(),
.size = static_cast<Balloon::Size>(static_cast<int>(balloon->getSize()) - 1),
.vel_x = direction == "LEFT" ? Balloon::VELX_NEGATIVE : Balloon::VELX_POSITIVE,
.game_tempo = balloon_speed_,
@@ -216,9 +217,22 @@ void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon> &balloon,
// Crea el globo
auto b = createBalloon(config);
// Establece parametros (deltaTime en segundos - velocidades en pixels/segundo)
constexpr float VEL_Y_BALLOON_PER_S = -150.0F; // -2.50 pixels/frame convertido a pixels/segundo (-2.50 * 60 = -150)
b->setVelY(b->getType() == Balloon::Type::BALLOON ? VEL_Y_BALLOON_PER_S : Balloon::VELX_NEGATIVE * 2.0F);
// Establece parametros
constexpr float VEL_Y_BALLOON_PER_S = -150.0F;
switch (b->getType()) {
case Balloon::Type::BALLOON: {
b->setVelY(VEL_Y_BALLOON_PER_S);
break;
}
case Balloon::Type::FLOATER: {
const float MODIFIER = (rand() % 2 == 0) ? 1.0F : 1.0F;
b->setVelY(Balloon::VELX_NEGATIVE * 2.0F * MODIFIER);
(rand() % 2 == 0) ? b->alterVelX(1.0F) : b->alterVelX(1.0F);
break;
}
default:
break;
}
// Herencia de estados
if (balloon->isStopped()) { b->stop(); }
@@ -266,13 +280,13 @@ void BalloonManager::createPowerBall() {
// Establece la velocidad de los globos
void BalloonManager::setBalloonSpeed(float speed) {
balloon_speed_ = speed;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setGameTempo(speed);
}
}
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
auto BalloonManager::popBalloon(const std::shared_ptr<Balloon> &balloon) -> int {
auto BalloonManager::popBalloon(const std::shared_ptr<Balloon>& balloon) -> int {
stage_info_->addPower(1);
int score = 0;
@@ -297,7 +311,7 @@ auto BalloonManager::popBalloon(const std::shared_ptr<Balloon> &balloon) -> int
}
// Explosiona un globo. Lo destruye = no crea otros globos
auto BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon) -> int {
auto BalloonManager::destroyBalloon(std::shared_ptr<Balloon>& balloon) -> int {
int score = 0;
// Calcula la puntuación y el poder que generaria el globo en caso de romperlo a él y a sus hijos
@@ -332,7 +346,7 @@ auto BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon) -> int {
// Destruye todos los globos
auto BalloonManager::destroyAllBalloons() -> int {
int score = 0;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
score += destroyBalloon(balloon);
}
@@ -345,7 +359,7 @@ auto BalloonManager::destroyAllBalloons() -> int {
// Detiene todos los globos
void BalloonManager::stopAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
if (!balloon->isBeingCreated()) {
balloon->stop();
}
@@ -354,7 +368,7 @@ void BalloonManager::stopAllBalloons() {
// Pone en marcha todos los globos
void BalloonManager::startAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
if (!balloon->isBeingCreated()) {
balloon->start();
}
@@ -363,7 +377,7 @@ void BalloonManager::startAllBalloons() {
// Cambia el color de todos los globos
void BalloonManager::reverseColorsToAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
if (balloon->isStopped()) {
balloon->useReverseColor();
}
@@ -372,7 +386,7 @@ void BalloonManager::reverseColorsToAllBalloons() {
// Cambia el color de todos los globos
void BalloonManager::normalColorsToAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->useNormalColor();
}
}
@@ -384,13 +398,13 @@ void BalloonManager::createTwoBigBalloons() {
// Obtiene el nivel de ameza actual generado por los globos
auto BalloonManager::getMenace() -> int {
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon) { return sum + (balloon->isEnabled() ? balloon->getMenace() : 0); });
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto& balloon) { return sum + (balloon->isEnabled() ? balloon->getMenace() : 0); });
}
// Establece el sonido de los globos
void BalloonManager::setSounds(bool value) {
sound_enabled_ = value;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setSound(value);
}
}
@@ -398,14 +412,14 @@ void BalloonManager::setSounds(bool value) {
// Activa o desactiva los sonidos de rebote los globos
void BalloonManager::setBouncingSounds(bool value) {
bouncing_sound_enabled_ = value;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setBouncingSound(value);
}
}
// Activa o desactiva los sonidos de los globos al explotar
void BalloonManager::setPoppingSounds(bool value) {
poping_sound_enabled_ = value;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setPoppingSound(value);
}
}

View File

@@ -15,7 +15,6 @@ namespace GameDefaults {
namespace Game {
constexpr float WIDTH = 320.0F;
constexpr float HEIGHT = 256.0F;
constexpr float ITEM_SIZE = 20.0F;
constexpr int NAME_ENTRY_IDLE_TIME = 10;
constexpr int NAME_ENTRY_TOTAL_TIME = 60;
constexpr bool HIT_STOP = false;

70
source/demo.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "demo.h"
#include <SDL3/SDL.h> // Para SDL_IOStream, SDL_IOFromConstMem, SDL_IOFromFile, SDL_ReadIO, SDL_WriteIO, SDL_CloseIO
#include <stdexcept> // Para runtime_error
#include "resource_helper.h" // Para ResourceHelper
#include "utils.h" // Para printWithDots, getFileName
// Carga el fichero de datos para la demo
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
DemoData dd;
SDL_IOStream *file = nullptr;
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
file = SDL_IOFromConstMem(resource_data.data(), resource_data.size());
} else {
// Fallback a filesystem directo
file = SDL_IOFromFile(file_path.c_str(), "r+b");
}
if (file == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
DemoKeys dk = DemoKeys();
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
dd.push_back(dk);
}
// Cierra el fichero
SDL_CloseIO(file);
return dd;
}
#ifdef RECORDING
// Guarda el fichero de datos para la demo
bool saveDemoFile(const std::string &file_path, const DemoData &dd) {
auto success = true;
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
if (file) {
// Guarda los datos
for (const auto &data : dd) {
if (SDL_WriteIO(file, &data, sizeof(DemoKeys)) != sizeof(DemoKeys)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str());
success = false;
break;
}
}
if (success) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str());
}
// Cierra el fichero
SDL_CloseIO(file);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
success = false;
}
return success;
}
#endif // RECORDING

54
source/demo.h Normal file
View File

@@ -0,0 +1,54 @@
#pragma once
#include <SDL3/SDL.h> // Para Uint8
#include <string> // Para string
#include <vector> // Para vector
// --- Constantes ---
constexpr int TOTAL_DEMO_DATA = 2000;
// --- Estructuras ---
struct DemoKeys {
Uint8 left;
Uint8 right;
Uint8 no_input;
Uint8 fire;
Uint8 fire_left;
Uint8 fire_right;
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
: left(l),
right(r),
no_input(ni),
fire(f),
fire_left(fl),
fire_right(fr) {}
};
// --- Tipos ---
using DemoData = std::vector<DemoKeys>;
struct Demo {
bool enabled = false; // Indica si está activo el modo demo
bool recording = false; // Indica si está activado el modo para grabar la demo
float elapsed_s = 0.0F; // Segundos transcurridos de demo
int index = 0; // Contador para el modo demo
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
Demo() = default;
Demo(bool e, bool r, int c, const DemoKeys& k, const std::vector<DemoData>& d)
: enabled(e),
recording(r),
index(c),
keys(k),
data(d) {}
};
// --- Funciones ---
auto loadDemoDataFromFile(const std::string& file_path) -> DemoData;
#ifdef RECORDING
bool saveDemoFile(const std::string& file_path, const DemoData& dd);
#endif

View File

@@ -42,7 +42,7 @@ Director::Director(int argc, std::span<char *> argv) {
Section::name = Section::Name::GAME;
Section::options = Section::Options::GAME_PLAY_1P;
#elif _DEBUG
Section::name = Section::Name::GAME;
Section::name = Section::Name::TITLE;
Section::options = Section::Options::GAME_PLAY_1P;
#else // NORMAL GAME
Section::name = Section::Name::LOGO;

View File

@@ -9,7 +9,7 @@
class Texture; // lines 6-6
Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::shared_ptr<Texture> &texture, const std::vector<std::string> &animation)
Item::Item(ItemType type, float x, float y, SDL_FRect& play_area, const std::shared_ptr<Texture>& texture, const std::vector<std::string>& animation)
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
play_area_(play_area),
type_(type) {
@@ -26,8 +26,6 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
break;
}
default: {
width_ = param.game.item_size;
height_ = param.game.item_size;
pos_x_ = x;
pos_y_ = y;
// 6 velocidades: 3 negativas (-1.0, -0.66, -0.33) y 3 positivas (0.33, 0.66, 1.0)
@@ -35,13 +33,17 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
if (direction < 3) {
// Velocidades negativas: -1.0, -0.66, -0.33
vel_x_ = -ITEM_VEL_X_BASE + (direction * ITEM_VEL_X_STEP);
rotate_speed_ = -720.0F;
} else {
// Velocidades positivas: 0.33, 0.66, 1.0
vel_x_ = ITEM_VEL_X_STEP + ((direction - 3) * ITEM_VEL_X_STEP);
rotate_speed_ = 720.0F;
}
vel_y_ = ITEM_VEL_Y;
accel_y_ = ITEM_ACCEL_Y;
collider_.r = width_ / 2;
sprite_->startRotate();
sprite_->setRotateAmount(rotate_speed_);
break;
}
}
@@ -103,7 +105,8 @@ void Item::move(float deltaTime) {
// Si toca el borde lateral
if (pos_x_ == MIN_X || pos_x_ == MAX_X) {
vel_x_ = -vel_x_; // Invierte la velocidad horizontal
vel_x_ = -vel_x_; // Invierte la velocidad horizontal
sprite_->scaleRotateAmount(-1.0F); // Invierte la rotación
}
// Si colisiona por arriba, rebota (excepto la máquina de café)
@@ -117,8 +120,9 @@ void Item::move(float deltaTime) {
// Si colisiona con la parte inferior
if (pos_y_ > play_area_.h - height_) {
// Corrige la posición
pos_y_ = play_area_.h - height_;
pos_y_ = play_area_.h - height_; // Corrige la posición
sprite_->scaleRotateAmount(0.5F); // Reduce la rotación
sprite_->stopRotate(300.0F); // Detiene la rotacion
switch (type_) {
case ItemType::COFFEE_MACHINE:

View File

@@ -27,8 +27,10 @@ enum class ItemType : int {
class Item {
public:
// --- Constantes ---
static constexpr int COFFEE_MACHINE_WIDTH = 30; // Anchura de la máquina de café
static constexpr int COFFEE_MACHINE_HEIGHT = 39; // Altura de la máquina de café
static constexpr float WIDTH = 20.0F; // Anchura del item
static constexpr float HEIGHT = 20.0F; // ALtura del item
static constexpr int COFFEE_MACHINE_WIDTH = 30; // Anchura de la máquina de café
static constexpr int COFFEE_MACHINE_HEIGHT = 39; // Altura de la máquina de café
static constexpr float LIFETIME_DURATION_S = 10.0f; // Duración de vida del ítem en segundos
// Velocidades base (pixels/segundo) - Coffee Machine
@@ -37,10 +39,10 @@ class Item {
static constexpr float COFFEE_MACHINE_ACCEL_Y = 360.0F; // Aceleración Y de máquina de café (0.1*60²fps = 360 pixels/segundo²)
// Velocidades base (pixels/segundo) - Items normales
static constexpr float ITEM_VEL_X_BASE = 60.0F; // Velocidad X base para items (1.0F*60fps)
static constexpr float ITEM_VEL_X_STEP = 20.0F; // Incremento de velocidad X (0.33F*60fps)
static constexpr float ITEM_VEL_Y = -240.0F; // Velocidad Y inicial de items (-4.0F*60fps)
static constexpr float ITEM_ACCEL_Y = 720.0F; // Aceleración Y de items (0.2*60²fps = 720 pixels/segundo²)
static constexpr float ITEM_VEL_X_BASE = 60.0F; // Velocidad X base para items (1.0F*60fps)
static constexpr float ITEM_VEL_X_STEP = 20.0F; // Incremento de velocidad X (0.33F*60fps)
static constexpr float ITEM_VEL_Y = -240.0F; // Velocidad Y inicial de items (-4.0F*60fps)
static constexpr float ITEM_ACCEL_Y = 720.0F; // Aceleración Y de items (0.2*60²fps = 720 pixels/segundo²)
// Constantes de física de rebote
static constexpr float BOUNCE_VEL_THRESHOLD = 60.0F; // Umbral de velocidad para parar (1.0F*60fps)
@@ -49,14 +51,14 @@ class Item {
static constexpr float HORIZONTAL_DAMPING = 0.75F; // Factor de amortiguación horizontal
// --- Constructor y destructor ---
Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::shared_ptr<Texture> &texture, const std::vector<std::string> &animation); // Constructor principal
Item(ItemType type, float x, float y, SDL_FRect& play_area, const std::shared_ptr<Texture>& texture, const std::vector<std::string>& animation); // Constructor principal
~Item() = default; // Destructor
// --- Métodos principales ---
void alignTo(int x); // Centra el objeto en la posición X indicada
void render(); // Renderiza el objeto en pantalla
void disable(); // Desactiva el objeto
void update(float deltaTime); // Actualiza la posición, animación y contadores (time-based)
void alignTo(int x); // Centra el objeto en la posición X indicada
void render(); // Renderiza el objeto en pantalla
void disable(); // Desactiva el objeto
void update(float deltaTime); // Actualiza la posición, animación y contadores (time-based)
// --- Getters ---
[[nodiscard]] auto getPosX() const -> float { return pos_x_; } // Obtiene la posición X
@@ -66,7 +68,7 @@ class Item {
[[nodiscard]] auto getType() const -> ItemType { return type_; } // Obtiene el tipo
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; } // Verifica si está habilitado
[[nodiscard]] auto isOnFloor() const -> bool { return floor_collision_; } // Verifica si está en el suelo
auto getCollider() -> Circle & { return collider_; } // Obtiene el colisionador
auto getCollider() -> Circle& { return collider_; } // Obtiene el colisionador
private:
// --- Objetos y punteros ---
@@ -76,15 +78,16 @@ class Item {
SDL_FRect play_area_; // Rectángulo con la zona de juego
Circle collider_; // Círculo de colisión del objeto
ItemType type_; // Tipo de objeto
float pos_x_; // Posición X del objeto
float pos_y_; // Posición Y del objeto
float vel_x_; // Velocidad en el eje X
float vel_y_; // Velocidad en el eje Y
float pos_x_ = 0.0F; // Posición X del objeto
float pos_y_ = 0.0F; // Posición Y del objeto
float vel_x_ = 0.0F; // Velocidad en el eje X
float vel_y_ = 0.0F; // Velocidad en el eje Y
float accel_x_ = 0.0F; // Aceleración en el eje X
float accel_y_; // Aceleración en el eje Y
int width_; // Ancho del objeto
int height_; // Alto del objeto
float lifetime_timer_ = 0.0f; // Acumulador de tiempo de vida del ítem (segundos)
float accel_y_ = 0.0F; // Aceleración en el eje Y
float width_ = WIDTH; // Ancho del objeto
float height_ = HEIGHT; // Alto del objeto
float rotate_speed_ = 0.0F; // Velocidad de rotacion
float lifetime_timer_ = 0.0f; // Acumulador de tiempo de vida del ítem (segundos)
bool floor_collision_ = false; // Indica si el objeto colisiona con el suelo
bool enabled_ = true; // Indica si el objeto está habilitado
@@ -92,6 +95,6 @@ class Item {
void shiftColliders(); // Alinea el círculo de colisión con la posición del objeto
void shiftSprite(); // Coloca el sprite en la posición del objeto
void move(float deltaTime); // Actualiza la posición y estados del objeto (time-based)
void updateTimeToLive(float deltaTime); // Actualiza el contador de tiempo de vida (time-based)
void updateTimeToLive(float deltaTime); // Actualiza el contador de tiempo de vida (time-based)
static auto getCoffeeMachineSpawn(int player_x, int item_width, int area_width, int margin = 2) -> int; // Calcula la zona de aparición de la máquina de café
};

View File

@@ -1,5 +1,6 @@
#include "moving_sprite.h"
#include <cmath> // Para std::abs
#include <utility>
#include "texture.h" // Para Texture
@@ -90,6 +91,21 @@ void MovingSprite::setRotate(bool enable) {
rotate_.enabled = enable;
}
// Habilita la rotación y establece el centro en el centro del sprite
void MovingSprite::startRotate() {
rotate_.enabled = true;
rotate_.center.x = pos_.w / 2.0F;
rotate_.center.y = pos_.h / 2.0F;
}
// Detiene la rotación y resetea el ángulo a cero
void MovingSprite::stopRotate(float threshold) {
if (threshold == 0.0F || std::abs(rotate_.amount) <= threshold) {
rotate_.enabled = false;
rotate_.angle = 0.0;
}
}
// Establece la posición y_ el tamaño del objeto
void MovingSprite::setPos(SDL_FRect rect) {
x_ = rect.x;

View File

@@ -15,7 +15,6 @@ class MovingSprite : public Sprite {
// --- Estructuras ---
struct Rotate {
bool enabled{false}; // Indica si ha de rotar
int speed{1}; // Velocidad de giro
double angle{0.0}; // Ángulo para dibujarlo
float amount{0.0F}; // Cantidad de grados a girar en cada iteración
SDL_FPoint center{.x = 0.0F, .y = 0.0F}; // Centro de rotación
@@ -28,10 +27,10 @@ class MovingSprite : public Sprite {
~MovingSprite() override = default;
// --- Métodos principales ---
virtual void update(float deltaTime); // Actualiza las variables internas del objeto (time-based)
void clear() override; // Reinicia todas las variables a cero
void stop(); // Elimina el movimiento del sprite
void render() override; // Muestra el sprite por pantalla
virtual void update(float deltaTime); // Actualiza las variables internas del objeto (time-based)
void clear() override; // Reinicia todas las variables a cero
void stop(); // Elimina el movimiento del sprite
void render() override; // Muestra el sprite por pantalla
// --- Configuración ---
void setPos(SDL_FRect rect); // Establece la posición y el tamaño del objeto
@@ -47,8 +46,10 @@ class MovingSprite : public Sprite {
void setAngle(double value) { rotate_.angle = value; } // Establece el ángulo
void setRotatingCenter(SDL_FPoint point) { rotate_.center = point; } // Establece el centro de rotación
void setRotate(bool enable); // Activa o desactiva el efecto de rotación
void setRotateSpeed(int value) { rotate_.speed = std::max(1, value); } // Establece la velocidad de rotación
void setRotateAmount(double value) { rotate_.amount = value; } // Establece la cantidad de rotación
void startRotate(); // Habilita la rotación con centro automático
void stopRotate(float threshold = 0.0F); // Detiene la rotación y resetea ángulo
void setRotateAmount(double value) { rotate_.amount = value; } // Establece la velocidad de rotación
void scaleRotateAmount(float value) { rotate_.amount *= value; } // Modifica la velocidad de rotacion
void switchRotate() { rotate_.amount *= -1; } // Cambia el sentido de la rotación
void setFlip(SDL_FlipMode flip) { flip_ = flip; } // Establece el flip
void flip() { flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL; } // Cambia el flip

View File

@@ -87,7 +87,6 @@ auto setParams(const std::string& var, const std::string& value) -> bool {
static const std::unordered_map<std::string, std::function<void(const std::string&)>> INT_PARAMS = {
{"game.width", [](const std::string& v) { param.game.width = std::stoi(v); }},
{"game.height", [](const std::string& v) { param.game.height = std::stoi(v); }},
{"game.item_size", [](const std::string& v) { param.game.item_size = std::stoi(v); }},
{"game.play_area.rect.x", [](const std::string& v) { param.game.play_area.rect.x = std::stoi(v); }},
{"game.play_area.rect.y", [](const std::string& v) { param.game.play_area.rect.y = std::stoi(v); }},
{"game.play_area.rect.w", [](const std::string& v) { param.game.play_area.rect.w = std::stoi(v); }},

View File

@@ -14,7 +14,6 @@
struct ParamGame {
float width = GameDefaults::Game::WIDTH;
float height = GameDefaults::Game::HEIGHT;
float item_size = GameDefaults::Game::ITEM_SIZE;
Zone play_area{}; // Se inicializa en el constructor de Param
Zone game_area{}; // Se inicializa en el constructor de Param
int name_entry_idle_time = GameDefaults::Game::NAME_ENTRY_IDLE_TIME;

View File

@@ -137,15 +137,15 @@ class Player {
void decScoreMultiplier(); // Decrementa el multiplicador
// --- Estados de juego ---
void setPlayingState(State state); // Cambia el estado de juego
void setInvulnerable(bool value); // Establece el valor del estado de invulnerabilidad
void setPowerUp(); // Activa el modo PowerUp
void updatePowerUp(float deltaTime); // Actualiza el valor de PowerUp
void giveExtraHit(); // Concede un toque extra al jugador
void removeExtraHit(); // Quita el toque extra al jugador
void decContinueCounter(); // Decrementa el contador de continuar
void setWalkingState(State state) { walking_state_ = state; } // Establece el estado de caminar
void startFiringSystem(int cooldown_frames); // Inicia el sistema de disparo
void setPlayingState(State state); // Cambia el estado de juego
void setInvulnerable(bool value); // Establece el valor del estado de invulnerabilidad
void setPowerUp(); // Activa el modo PowerUp
void updatePowerUp(float deltaTime); // Actualiza el valor de PowerUp
void giveExtraHit(); // Concede un toque extra al jugador
void removeExtraHit(); // Quita el toque extra al jugador
void decContinueCounter(); // Decrementa el contador de continuar
void setWalkingState(State state) { walking_state_ = state; } // Establece el estado de caminar
void startFiringSystem(int cooldown_frames); // Inicia el sistema de disparo
void setScoreBoardPanel(Scoreboard::Id panel) { scoreboard_panel_ = panel; } // Establece el panel del marcador
void addCredit();
void passShowingName();
@@ -196,11 +196,11 @@ class Player {
[[nodiscard]] auto getCoffees() const -> int { return coffees_; }
[[nodiscard]] auto getPowerUpCounter() const -> int { return power_up_counter_; }
[[nodiscard]] auto getInvulnerableCounter() const -> int { return invulnerable_counter_; }
[[nodiscard]] auto getBulletColor() const -> Bullet::Color; // Devuelve el color actual de bala según el estado
auto getNextBulletColor() -> Bullet::Color; // Devuelve el color para la próxima bala (alterna si está en modo toggle)
void setBulletColors(Bullet::Color normal, Bullet::Color powered); // Establece los colores de bala para este jugador
[[nodiscard]] auto getBulletColor() const -> Bullet::Color; // Devuelve el color actual de bala según el estado
auto getNextBulletColor() -> Bullet::Color; // Devuelve el color para la próxima bala (alterna si está en modo toggle)
void setBulletColors(Bullet::Color normal, Bullet::Color powered); // Establece los colores de bala para este jugador
[[nodiscard]] auto getBulletSoundFile() const -> std::string { return bullet_sound_file_; } // Devuelve el archivo de sonido de bala
void setBulletSoundFile(const std::string& filename); // Establece el archivo de sonido de bala para este jugador
void setBulletSoundFile(const std::string& filename); // Establece el archivo de sonido de bala para este jugador
// Contadores y timers
[[nodiscard]] auto getContinueCounter() const -> int { return continue_counter_; }
@@ -217,6 +217,10 @@ class Player {
[[nodiscard]] auto getUsesKeyboard() const -> bool { return uses_keyboard_; }
[[nodiscard]] auto getController() const -> int { return controller_index_; }
// Demo file management
[[nodiscard]] auto getDemoFile() const -> size_t { return demo_file_; }
void setDemoFile(size_t demo_file) { demo_file_ = demo_file; }
private:
// --- Constantes de física y movimiento ---
static constexpr float BASE_SPEED = 90.0f; // Velocidad base del jugador (pixels/segundo)
@@ -249,17 +253,17 @@ class Player {
IStageInfo* stage_info_; // Informacion de la pantalla actual
// --- Variables de estado ---
SDL_FRect play_area_; // Rectángulo con la zona de juego
Circle collider_ = Circle(0, 0, 9); // Círculo de colisión del jugador
std::string name_; // Nombre del jugador
std::string last_enter_name_; // Último nombre introducido en la tabla de puntuaciones
Scoreboard::Id scoreboard_panel_ = Scoreboard::Id::LEFT; // Panel del marcador asociado al jugador
Id id_; // Identificador para el jugador
State walking_state_ = State::WALKING_STOP; // Estado del jugador al moverse
State firing_state_ = State::FIRING_NONE; // Estado del jugador al disparar
State playing_state_ = State::WAITING; // Estado del jugador en el juego
SDL_FRect play_area_; // Rectángulo con la zona de juego
Circle collider_ = Circle(0, 0, 9); // Círculo de colisión del jugador
std::string name_; // Nombre del jugador
std::string last_enter_name_; // Último nombre introducido en la tabla de puntuaciones
Scoreboard::Id scoreboard_panel_ = Scoreboard::Id::LEFT; // Panel del marcador asociado al jugador
Id id_; // Identificador para el jugador
State walking_state_ = State::WALKING_STOP; // Estado del jugador al moverse
State firing_state_ = State::FIRING_NONE; // Estado del jugador al disparar
State playing_state_ = State::WAITING; // Estado del jugador en el juego
BulletColorPair bullet_colors_ = {Bullet::Color::YELLOW, Bullet::Color::GREEN}; // Par de colores de balas para este jugador
std::string bullet_sound_file_ = "bullet1p.wav"; // Archivo de sonido de bala para este jugador
std::string bullet_sound_file_ = "bullet1p.wav"; // Archivo de sonido de bala para este jugador
float pos_x_ = 0.0F; // Posición en el eje X
float default_pos_x_; // Posición inicial para el jugador
@@ -304,6 +308,7 @@ class Player {
int power_up_x_offset_ = 0; // Desplazamiento del sprite de PowerUp respecto al sprite del jugador
int continue_counter_ = 10; // Contador para poder continuar
int controller_index_ = 0; // Índice del array de mandos que utilizará para moverse
size_t demo_file_ = 0; // Indice del fichero de datos para el modo demo
float name_entry_idle_time_accumulator_ = 0.0f; // Tiempo idle acumulado para poner nombre (milisegundos)
float name_entry_total_time_accumulator_ = 0.0f; // Tiempo total acumulado poniendo nombre (milisegundos)
int step_counter_ = 0; // Cuenta los pasos para los estados en los que camina automáticamente
@@ -313,16 +318,16 @@ class Player {
bool invulnerable_ = true; // Indica si el jugador es invulnerable
bool extra_hit_ = false; // Indica si el jugador tiene un toque extra
bool power_up_ = false; // Indica si el jugador tiene activo el modo PowerUp
bool power_sprite_visible_ = false; // Indica si el sprite de power-up debe ser visible
bool in_power_up_ending_phase_ = false; // Indica si está en la fase final del power-up (alternando colores)
bool bullet_color_toggle_ = false; // Para alternar entre verde y amarillo en fase final
bool power_sprite_visible_ = false; // Indica si el sprite de power-up debe ser visible
bool in_power_up_ending_phase_ = false; // Indica si está en la fase final del power-up (alternando colores)
bool bullet_color_toggle_ = false; // Para alternar entre verde y amarillo en fase final
bool demo_ = false; // Para que el jugador sepa si está en el modo demostración
bool game_completed_ = false; // Indica si ha completado el juego
bool uses_keyboard_ = false; // Indica si usa el teclado como dispositivo de control
// --- Métodos internos ---
void shiftColliders(); // Actualiza el círculo de colisión a la posición del jugador
void shiftSprite(); // Recoloca el sprite
void shiftColliders(); // Actualiza el círculo de colisión a la posición del jugador
void shiftSprite(); // Recoloca el sprite
// --- Setters internos ---
void setController(int index) { controller_index_ = index; }
@@ -333,11 +338,11 @@ class Player {
void setScoreMultiplier(float value) { score_multiplier_ = value; }
// --- Actualizadores de estado (time-based) ---
void updateInvulnerable(float deltaTime); // Monitoriza el estado de invulnerabilidad
void updateContinueCounter(float deltaTime); // Actualiza el contador de continue
void updateEnterNameCounter(float deltaTime); // Actualiza el contador de entrar nombre
void updateShowingName(float deltaTime); // Actualiza el estado SHOWING_NAME
void decNameEntryCounter(); // Decrementa el contador de entrar nombre
void updateInvulnerable(float deltaTime); // Monitoriza el estado de invulnerabilidad
void updateContinueCounter(float deltaTime); // Actualiza el contador de continue
void updateEnterNameCounter(float deltaTime); // Actualiza el contador de entrar nombre
void updateShowingName(float deltaTime); // Actualiza el estado SHOWING_NAME
void decNameEntryCounter(); // Decrementa el contador de entrar nombre
// --- Utilidades generales ---
void updateScoreboard(); // Actualiza el panel del marcador
@@ -347,41 +352,41 @@ class Player {
void addScoreToScoreBoard() const; // Añade una puntuación a la tabla de records
// --- Sistema de disparo (nuevo - dos líneas) ---
void updateFireSystem(float deltaTime); // Método principal del nuevo sistema de disparo
void updateFunctionalLine(float deltaTime); // Actualiza la línea funcional (CanFire)
void updateVisualLine(float deltaTime); // Actualiza la línea visual (Animaciones)
void updateFiringStateFromVisual(); // Sincroniza firing_state_ con visual_fire_state_
void transitionToRecoilingNew(); // Transición AIMING → RECOILING
void transitionToThreatPose(); // Transición RECOILING → THREAT_POSE
void transitionToNormalNew(); // Transición THREAT_POSE → NORMAL
void updateFireSystem(float deltaTime); // Método principal del nuevo sistema de disparo
void updateFunctionalLine(float deltaTime); // Actualiza la línea funcional (CanFire)
void updateVisualLine(float deltaTime); // Actualiza la línea visual (Animaciones)
void updateFiringStateFromVisual(); // Sincroniza firing_state_ con visual_fire_state_
void transitionToRecoilingNew(); // Transición AIMING → RECOILING
void transitionToThreatPose(); // Transición RECOILING → THREAT_POSE
void transitionToNormalNew(); // Transición THREAT_POSE → NORMAL
// --- Manejadores de movimiento ---
void handlePlayingMovement(float deltaTime); // Gestiona el movimiento durante el juego
void handleRecoverMovement(); // Comprueba si ha acabado la animación de recuperación
void updateStepCounter(float deltaTime); // Incrementa o ajusta el contador de pasos
void setInputBasedOnPlayerId(); // Asocia las entradas de control según el jugador
void handlePlayingMovement(float deltaTime); // Gestiona el movimiento durante el juego
void handleRecoverMovement(); // Comprueba si ha acabado la animación de recuperación
void updateStepCounter(float deltaTime); // Incrementa o ajusta el contador de pasos
void setInputBasedOnPlayerId(); // Asocia las entradas de control según el jugador
// --- Manejadores de estados especiales ---
void handleRollingMovement(); // Actualiza la lógica de movimiento de "rodar"
void handleRollingBoundaryCollision(); // Detecta colisiones con límites durante rodamiento
void handleRollingGroundCollision(); // Gestiona interacción con el suelo durante rodamiento
void handleRollingStop(); // Detiene el movimiento del objeto rodante
void handleRollingBounce(); // Aplica lógica de rebote durante rodamiento
void handleContinueTimeOut(); // Gestiona tiempo de espera en pantalla "Continuar"
void handleRollingMovement(); // Actualiza la lógica de movimiento de "rodar"
void handleRollingBoundaryCollision(); // Detecta colisiones con límites durante rodamiento
void handleRollingGroundCollision(); // Gestiona interacción con el suelo durante rodamiento
void handleRollingStop(); // Detiene el movimiento del objeto rodante
void handleRollingBounce(); // Aplica lógica de rebote durante rodamiento
void handleContinueTimeOut(); // Gestiona tiempo de espera en pantalla "Continuar"
// --- Manejadores de transiciones de pantalla ---
void handleTitleAnimation(float deltaTime); // Ejecuta animación del título
void handleLeavingScreen(float deltaTime); // Lógica para salir de pantalla
void handleEnteringScreen(float deltaTime); // Lógica para entrar en pantalla
void handlePlayer1Entering(float deltaTime); // Entrada del Jugador 1
void handlePlayer2Entering(float deltaTime); // Entrada del Jugador 2
void handleTitleAnimation(float deltaTime); // Ejecuta animación del título
void handleLeavingScreen(float deltaTime); // Lógica para salir de pantalla
void handleEnteringScreen(float deltaTime); // Lógica para entrar en pantalla
void handlePlayer1Entering(float deltaTime); // Entrada del Jugador 1
void handlePlayer2Entering(float deltaTime); // Entrada del Jugador 2
// --- Manejadores de pantallas especiales ---
void handleCreditsMovement(float deltaTime); // Movimiento en pantalla de créditos
void handleCreditsRightMovement(); // Movimiento hacia la derecha en créditos
void handleCreditsLeftMovement(); // Movimiento hacia la izquierda en créditos
void handleWaitingMovement(float deltaTime); // Animación del jugador saludando
void updateWalkingStateForCredits(); // Actualiza estado de caminata en créditos
void handleCreditsMovement(float deltaTime); // Movimiento en pantalla de créditos
void handleCreditsRightMovement(); // Movimiento hacia la derecha en créditos
void handleCreditsLeftMovement(); // Movimiento hacia la izquierda en créditos
void handleWaitingMovement(float deltaTime); // Animación del jugador saludando
void updateWalkingStateForCredits(); // Actualiza estado de caminata en créditos
// --- Utilidades de animación ---
[[nodiscard]] auto computeAnimation() const -> std::pair<std::string, SDL_FlipMode>; // Calcula animación de movimiento y disparo

View File

@@ -10,22 +10,22 @@
#include <stdexcept> // Para runtime_error
#include <utility> // Para move
#include "asset.h" // Para Asset
#include "color.h" // Para Color
#include "version.h" // Para Version::APP_NAME y Version::GIT_HASH
#include "asset.h" // Para Asset
#include "color.h" // Para Color
#include "external/jail_audio.h" // Para JA_LoadMusic, JA_LoadSound, JA_DeleteMusic, JA_DeleteSound
#include "lang.h" // Para getText
#include "param.h" // Para Param, param, ParamResource, ParamGame
#include "resource_helper.h" // Para ResourceHelper
#include "screen.h" // Para Screen
#include "text.h" // Para Text
#include "lang.h" // Para getText
#include "param.h" // Para Param, param, ParamResource, ParamGame
#include "resource_helper.h" // Para ResourceHelper
#include "screen.h" // Para Screen
#include "text.h" // Para Text
#include "version.h" // Para Version::APP_NAME y Version::GIT_HASH
struct JA_Music_t; // lines 11-11
struct JA_Sound_t; // lines 12-12
// Helper para cargar archivos de audio desde pack o filesystem
namespace {
std::string createTempAudioFile(const std::string &file_path, std::vector<std::string> &temp_files_tracker) {
std::string createTempAudioFile(const std::string& file_path, std::vector<std::string>& temp_files_tracker) {
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
// Crear archivo temporal
@@ -41,7 +41,7 @@ std::string createTempAudioFile(const std::string &file_path, std::vector<std::s
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot create temp file %s", temp_path.c_str());
return file_path;
}
temp_file.write(reinterpret_cast<const char *>(resource_data.data()), resource_data.size());
temp_file.write(reinterpret_cast<const char*>(resource_data.data()), resource_data.size());
temp_file.close();
// Agregar a la lista de archivos temporales para limpieza posterior
@@ -56,7 +56,7 @@ std::string createTempAudioFile(const std::string &file_path, std::vector<std::s
// Declaraciones de funciones que necesitas implementar en otros archivos
// Singleton
Resource *Resource::instance = nullptr;
Resource* Resource::instance = nullptr;
// Inicializa la instancia única del singleton con modo de carga
void Resource::init(LoadingMode mode) {
@@ -70,7 +70,7 @@ void Resource::destroy() {
}
// Obtiene la instancia
auto Resource::get() -> Resource * { return Resource::instance; }
auto Resource::get() -> Resource* { return Resource::instance; }
// Constructor con modo de carga
Resource::Resource(LoadingMode mode)
@@ -111,10 +111,10 @@ void Resource::loadTextFilesQuiet() {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES (quiet load)");
auto list = Asset::get()->getListByType(Asset::Type::FONT);
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
// Buscar en nuestra lista y cargar directamente
auto it = std::ranges::find_if(text_files_, [&name](const auto &t) { return t.name == name; });
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) { return t.name == name; });
if (it != text_files_.end()) {
it->text_file = Text::loadFile(l);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Text file loaded: %s", name.c_str());
@@ -140,12 +140,12 @@ void Resource::loadEssentialTextures() {
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
auto name = getFileName(file);
// Solo cargar texturas esenciales
if (std::ranges::find(ESSENTIAL_TEXTURES, name) != ESSENTIAL_TEXTURES.end()) {
// Buscar en nuestra lista y cargar
auto it = std::ranges::find_if(textures_, [&name](const auto &t) { return t.name == name; });
auto it = std::ranges::find_if(textures_, [&name](const auto& t) { return t.name == name; });
if (it != textures_.end()) {
it->texture = std::make_shared<Texture>(Screen::get()->getRenderer(), file);
}
@@ -160,35 +160,35 @@ void Resource::initResourceLists() {
// Inicializa lista de sonidos
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
sounds_.clear();
for (const auto &file : sound_list) {
for (const auto& file : sound_list) {
sounds_.emplace_back(getFileName(file));
}
// Inicializa lista de músicas
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
musics_.clear();
for (const auto &file : music_list) {
for (const auto& file : music_list) {
musics_.emplace_back(getFileName(file));
}
// Inicializa lista de texturas
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
textures_.clear();
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
textures_.emplace_back(getFileName(file));
}
// Inicializa lista de ficheros de texto
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
text_files_.clear();
for (const auto &file : text_file_list) {
for (const auto& file : text_file_list) {
text_files_.emplace_back(getFileName(file));
}
// Inicializa lista de animaciones
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
animations_.clear();
for (const auto &file : animation_list) {
for (const auto& file : animation_list) {
animations_.emplace_back(getFileName(file));
}
@@ -211,7 +211,7 @@ void Resource::initResourceLists() {
"smb2_grad"};
texts_.clear();
for (const auto &text_name : TEXT_OBJECTS) {
for (const auto& text_name : TEXT_OBJECTS) {
texts_.emplace_back(text_name); // Constructor con nullptr por defecto
}
@@ -219,8 +219,8 @@ void Resource::initResourceLists() {
}
// Obtiene el sonido a partir de un nombre (con carga perezosa)
auto Resource::getSound(const std::string &name) -> JA_Sound_t * {
auto it = std::ranges::find_if(sounds_, [&name](const auto &s) { return s.name == name; });
auto Resource::getSound(const std::string& name) -> JA_Sound_t* {
auto it = std::ranges::find_if(sounds_, [&name](const auto& s) { return s.name == name; });
if (it != sounds_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -235,8 +235,8 @@ auto Resource::getSound(const std::string &name) -> JA_Sound_t * {
}
// Obtiene la música a partir de un nombre (con carga perezosa)
auto Resource::getMusic(const std::string &name) -> JA_Music_t * {
auto it = std::ranges::find_if(musics_, [&name](const auto &m) { return m.name == name; });
auto Resource::getMusic(const std::string& name) -> JA_Music_t* {
auto it = std::ranges::find_if(musics_, [&name](const auto& m) { return m.name == name; });
if (it != musics_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -251,8 +251,8 @@ auto Resource::getMusic(const std::string &name) -> JA_Music_t * {
}
// Obtiene la textura a partir de un nombre (con carga perezosa)
auto Resource::getTexture(const std::string &name) -> std::shared_ptr<Texture> {
auto it = std::ranges::find_if(textures_, [&name](const auto &t) { return t.name == name; });
auto Resource::getTexture(const std::string& name) -> std::shared_ptr<Texture> {
auto it = std::ranges::find_if(textures_, [&name](const auto& t) { return t.name == name; });
if (it != textures_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -267,8 +267,8 @@ auto Resource::getTexture(const std::string &name) -> std::shared_ptr<Texture> {
}
// Obtiene el fichero de texto a partir de un nombre (con carga perezosa)
auto Resource::getTextFile(const std::string &name) -> std::shared_ptr<Text::File> {
auto it = std::ranges::find_if(text_files_, [&name](const auto &t) { return t.name == name; });
auto Resource::getTextFile(const std::string& name) -> std::shared_ptr<Text::File> {
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) { return t.name == name; });
if (it != text_files_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -283,8 +283,8 @@ auto Resource::getTextFile(const std::string &name) -> std::shared_ptr<Text::Fil
}
// Obtiene el objeto de texto a partir de un nombre (con carga perezosa)
auto Resource::getText(const std::string &name) -> std::shared_ptr<Text> {
auto it = std::ranges::find_if(texts_, [&name](const auto &t) { return t.name == name; });
auto Resource::getText(const std::string& name) -> std::shared_ptr<Text> {
auto it = std::ranges::find_if(texts_, [&name](const auto& t) { return t.name == name; });
if (it != texts_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -299,8 +299,8 @@ auto Resource::getText(const std::string &name) -> std::shared_ptr<Text> {
}
// Obtiene la animación a partir de un nombre (con carga perezosa)
auto Resource::getAnimation(const std::string &name) -> AnimationsFileBuffer & {
auto it = std::ranges::find_if(animations_, [&name](const auto &a) { return a.name == name; });
auto Resource::getAnimation(const std::string& name) -> AnimationsFileBuffer& {
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
if (it != animations_.end()) {
// Si está en modo lazy y no se ha cargado aún (vector vacío), lo carga ahora
@@ -315,16 +315,21 @@ auto Resource::getAnimation(const std::string &name) -> AnimationsFileBuffer & {
}
// Obtiene el fichero con los datos para el modo demostración a partir de un índice
auto Resource::getDemoData(int index) -> DemoData & {
auto Resource::getDemoData(int index) -> DemoData& {
if (index < 0 || index >= static_cast<int>(demos_.size())) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index %d out of range for demo data (size: %d)", index, static_cast<int>(demos_.size()));
static DemoData empty_demo;
return empty_demo;
}
return demos_.at(index);
}
// --- Métodos de carga perezosa ---
auto Resource::loadSoundLazy(const std::string &name) -> JA_Sound_t * {
auto Resource::loadSoundLazy(const std::string& name) -> JA_Sound_t* {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading sound lazily: %s", name.c_str());
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
for (const auto &file : sound_list) {
for (const auto& file : sound_list) {
if (getFileName(file) == name) {
std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_);
return JA_LoadSound(audio_path.c_str());
@@ -333,10 +338,10 @@ auto Resource::loadSoundLazy(const std::string &name) -> JA_Sound_t * {
return nullptr;
}
auto Resource::loadMusicLazy(const std::string &name) -> JA_Music_t * {
auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading music lazily: %s", name.c_str());
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
for (const auto &file : music_list) {
for (const auto& file : music_list) {
if (getFileName(file) == name) {
std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_);
return JA_LoadMusic(audio_path.c_str());
@@ -345,10 +350,10 @@ auto Resource::loadMusicLazy(const std::string &name) -> JA_Music_t * {
return nullptr;
}
auto Resource::loadTextureLazy(const std::string &name) -> std::shared_ptr<Texture> {
auto Resource::loadTextureLazy(const std::string& name) -> std::shared_ptr<Texture> {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading texture lazily: %s", name.c_str());
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
if (getFileName(file) == name) {
return std::make_shared<Texture>(Screen::get()->getRenderer(), file);
}
@@ -356,10 +361,10 @@ auto Resource::loadTextureLazy(const std::string &name) -> std::shared_ptr<Textu
return nullptr;
}
auto Resource::loadTextFileLazy(const std::string &name) -> std::shared_ptr<Text::File> {
auto Resource::loadTextFileLazy(const std::string& name) -> std::shared_ptr<Text::File> {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text file lazily: %s", name.c_str());
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
for (const auto &file : text_file_list) {
for (const auto& file : text_file_list) {
if (getFileName(file) == name) {
return Text::loadFile(file);
}
@@ -367,7 +372,7 @@ auto Resource::loadTextFileLazy(const std::string &name) -> std::shared_ptr<Text
return nullptr;
}
auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr<Text> {
auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text object lazily: %s", name.c_str());
// Mapeo de objetos de texto a sus recursos
@@ -391,7 +396,7 @@ auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr<Text> {
{.key = "smb2", .texture_file = "smb2.png", .text_file = "smb2.txt"},
{.key = "smb2_grad", .texture_file = "smb2_grad.png", .text_file = "smb2.txt"}};
for (const auto &mapping : TEXT_MAPPINGS) {
for (const auto& mapping : TEXT_MAPPINGS) {
if (mapping.key == name) {
// Cargar las dependencias automáticamente
auto texture = getTexture(mapping.texture_file); // Esto cargará la textura si no está cargada
@@ -406,10 +411,10 @@ auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr<Text> {
return nullptr;
}
auto Resource::loadAnimationLazy(const std::string &name) -> AnimationsFileBuffer {
auto Resource::loadAnimationLazy(const std::string& name) -> AnimationsFileBuffer {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading animation lazily: %s", name.c_str());
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
for (const auto &file : animation_list) {
for (const auto& file : animation_list) {
if (getFileName(file) == name) {
return loadAnimationsFromFile(file);
}
@@ -436,13 +441,13 @@ void Resource::load() {
initProgressBar();
// Muerstra la ventana y desactiva el sincronismo vertical
auto *screen = Screen::get();
auto* screen = Screen::get();
auto vsync = Screen::getVSync();
screen->setVSync(false);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES");
loadSounds(); // Carga sonidos
loadMusics(); // Carga músicas
loadSounds(); // Carga sonidos
loadMusics(); // Carga músicas
loadTextures(); // Carga texturas
loadTextFiles(); // Carga ficheros de texto
loadAnimations(); // Carga animaciones
@@ -472,7 +477,7 @@ void Resource::loadSounds() {
auto list = Asset::get()->getListByType(Asset::Type::SOUND);
sounds_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
@@ -487,7 +492,7 @@ void Resource::loadMusics() {
auto list = Asset::get()->getListByType(Asset::Type::MUSIC);
musics_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
@@ -502,7 +507,7 @@ void Resource::loadTextures() {
auto list = Asset::get()->getListByType(Asset::Type::BITMAP);
textures_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
textures_.emplace_back(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l));
@@ -515,7 +520,7 @@ void Resource::loadTextFiles() {
auto list = Asset::get()->getListByType(Asset::Type::FONT);
text_files_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
text_files_.emplace_back(name, Text::loadFile(l));
@@ -528,7 +533,7 @@ void Resource::loadAnimations() {
auto list = Asset::get()->getListByType(Asset::Type::ANIMATION);
animations_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
animations_.emplace_back(name, loadAnimationsFromFile(l));
@@ -538,12 +543,13 @@ void Resource::loadAnimations() {
// Carga los datos para el modo demostración
void Resource::loadDemoData() {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES");
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
demos_.clear();
constexpr std::array<const char *, 2> DEMO_FILES = {"demo1.bin", "demo2.bin"};
for (const auto &file : DEMO_FILES) {
updateLoadingProgress(file);
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
demos_.emplace_back(loadDemoDataFromFile(l));
}
}
@@ -564,12 +570,12 @@ void Resource::createPlayerTextures() {
// Bucle principal
for (size_t player_idx = 0; player_idx < players.size(); ++player_idx) {
const auto &player = players[player_idx]; // Obtenemos el jugador actual
const auto& player = players[player_idx]; // Obtenemos el jugador actual
// Encontrar el archivo original de la textura
std::string texture_file_path;
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
if (getFileName(file) == player.base_texture) {
texture_file_path = file;
break;
@@ -650,7 +656,7 @@ void Resource::createTextTextures() {
{"game_text_1000000_points", Lang::getText("[GAME_TEXT] 8")}};
auto text1 = getText("04b_25_enhanced");
for (const auto &s : strings1) {
for (const auto& s : strings1) {
textures_.emplace_back(s.name, text1->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
printWithDots("Texture : ", s.name, "[ DONE ]");
}
@@ -665,7 +671,7 @@ void Resource::createTextTextures() {
{"game_text_game_over", "Game Over"}};
auto text2 = getText("04b_25_2x_enhanced");
for (const auto &s : strings2) {
for (const auto& s : strings2) {
textures_.emplace_back(s.name, text2->writeDXToTexture(Text::STROKE, s.text, -4, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
printWithDots("Texture : ", s.name, "[ DONE ]");
}
@@ -705,7 +711,7 @@ void Resource::createText() {
{"smb2", "smb2.png", "smb2.txt"},
{"smb2_grad", "smb2_grad.png", "smb2.txt"}};
for (const auto &resource : resources) {
for (const auto& resource : resources) {
if (!resource.white_texture_file.empty()) {
// Crear texto con textura blanca
texts_.emplace_back(resource.key, std::make_shared<Text>(getTexture(resource.texture_file), getTexture(resource.white_texture_file), getTextFile(resource.text_file)));
@@ -719,7 +725,7 @@ void Resource::createText() {
// Vacía el vector de sonidos y libera la memoria asociada
void Resource::clearSounds() {
for (auto &sound : sounds_) {
for (auto& sound : sounds_) {
if (sound.sound != nullptr) {
JA_DeleteSound(sound.sound);
sound.sound = nullptr;
@@ -730,7 +736,7 @@ void Resource::clearSounds() {
// Vacía el vector de músicas y libera la memoria asociada
void Resource::clearMusics() {
for (auto &music : musics_) {
for (auto& music : musics_) {
if (music.music != nullptr) {
JA_DeleteMusic(music.music);
music.music = nullptr;
@@ -750,7 +756,7 @@ void Resource::calculateTotalResources() {
Asset::Type::DEMODATA};
size_t total = 0;
for (const auto &asset_type : ASSET_TYPES) {
for (const auto& asset_type : ASSET_TYPES) {
auto list = Asset::get()->getListByType(asset_type);
total += list.size();
}
@@ -761,8 +767,8 @@ void Resource::calculateTotalResources() {
// Muestra el progreso de carga en pantalla (barra y texto)
void Resource::renderProgress() {
// Obtiene la pantalla y el renderer
auto *screen = Screen::get();
auto *renderer = screen->getRenderer();
auto* screen = Screen::get();
auto* renderer = screen->getRenderer();
// Actualiza la lógica principal de la pantalla (input, etc.)
screen->coreUpdate();
@@ -836,12 +842,11 @@ void Resource::checkEvents() {
// Carga los datos para el modo demostración (sin mostrar progreso)
void Resource::loadDemoDataQuiet() {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES (quiet load)");
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
demos_.clear();
constexpr std::array<const char *, 2> DEMO_FILES = {"demo1.bin", "demo2.bin"};
for (const auto &file : DEMO_FILES) {
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Demo file loaded: %s", file);
for (const auto& l : list) {
demos_.emplace_back(loadDemoDataFromFile(l));
}
}
@@ -872,13 +877,13 @@ void Resource::updateProgressBar() {
// Limpia archivos temporales de audio
void Resource::cleanupTempAudioFiles() {
for (const auto &temp_path : temp_audio_files_) {
for (const auto& temp_path : temp_audio_files_) {
try {
if (std::filesystem::exists(temp_path)) {
std::filesystem::remove(temp_path);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Removed temp audio file: %s", temp_path.c_str());
}
} catch (const std::exception &e) {
} catch (const std::exception& e) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to remove temp audio file %s: %s", temp_path.c_str(), e.what());
}
}

View File

@@ -11,7 +11,7 @@
#include "animated_sprite.h" // Para AnimationsFileBuffer
#include "text.h" // Para Text, TextFile
#include "texture.h" // Para Texture
#include "utils.h" // Para DemoData
#include "demo.h" // Para DemoData
struct JA_Music_t;
struct JA_Sound_t;

View File

@@ -217,6 +217,10 @@ void Screen::renderInfo() {
// FPS
const std::string FPS_TEXT = std::to_string(fps_.last_value) + " FPS";
debug_info_.text->writeDX(Text::COLOR | Text::STROKE, param.game.width - debug_info_.text->length(FPS_TEXT) - 2, 1 + debug_info_.text->getCharacterSize(), FPS_TEXT, 1, param.debug.color, 1, param.debug.color.DARKEN(150));
#ifdef RECORDING
// RECORDING
debug_info_.text->writeDX(Text::COLOR | Text::STROKE, param.game.width - debug_info_.text->length("RECORDING"), 2*(1 + debug_info_.text->getCharacterSize()), "RECORDING", 1, param.debug.color, 1, param.debug.color.DARKEN(150));
#endif
}
}
#endif

View File

@@ -2,12 +2,13 @@
#include <SDL3/SDL.h> // Para SDL_GetTicks, SDL_SetRenderTarget, SDL_CreateTexture, SDL_Delay, SDL_DestroyTexture, SDL_EventType, SDL_GetRenderTarget, SDL_PollEvent, SDL_RenderTexture, SDL_SetTextureBlendMode, SDL_BLENDMODE_BLEND, SDL_Event, SDL_PixelFormat, SDL_Point, SDL_TextureAccess
#include <algorithm> // Para find, clamp, find_if, min
#include <algorithm> // Para find, clamp, find_if, min, std::shuffle, std::iota
#include <array> // Para array
#include <cstdlib> // Para rand, size_t
#include <functional> // Para function
#include <iterator> // Para size
#include <memory> // Para shared_ptr, unique_ptr, __shared_ptr_access, allocator, make_unique, operator==, make_shared
#include <random> // std::random_device, std::default_random_engine
#include <utility> // Para move
#include "asset.h" // Para Asset
@@ -48,7 +49,7 @@
#endif
// Constructor
Game::Game(Player::Id player_id, int current_stage, bool demo)
Game::Game(Player::Id player_id, int current_stage, bool demo_enabled)
: renderer_(Screen::get()->getRenderer()),
screen_(Screen::get()),
input_(Input::get()),
@@ -62,7 +63,7 @@ Game::Game(Player::Id player_id, int current_stage, bool demo)
tabe_(std::make_unique<Tabe>()),
hit_(Hit(Resource::get()->getTexture("hit.png"))) {
// Pasa variables
demo_.enabled = demo;
demo_.enabled = demo_enabled;
// Otras variables
Section::name = Section::Name::GAME;
@@ -83,7 +84,9 @@ Game::Game(Player::Id player_id, int current_stage, bool demo)
fade_in_->setPostDuration(0);
fade_in_->setType(Fade::Type::RANDOM_SQUARE2);
fade_in_->setMode(Fade::Mode::IN);
#ifndef RECORDING
fade_in_->activate();
#endif
fade_out_->setColor(param.fade.color);
fade_out_->setPostDuration(param.fade.post_duration_ms);
@@ -109,6 +112,9 @@ Game::Game(Player::Id player_id, int current_stage, bool demo)
pause_manager_->setServiceMenuPause(is_active);
}
});
#ifdef RECORDING
setState(State::PLAYING);
#endif
}
Game::~Game() {
@@ -202,7 +208,7 @@ void Game::updateHiScore() {
hi_score_.name = Options::settings.hi_score_table.front().name;
// Si la puntuación actual es mayor que la máxima puntuación
for (const auto &player : players_) {
for (const auto& player : players_) {
if (player->getScore() > hi_score_.score) {
// Actualiza la máxima puntuación
hi_score_.score = player->getScore();
@@ -220,7 +226,7 @@ void Game::updateHiScore() {
// Actualiza las variables del jugador
void Game::updatePlayers(float deltaTime) {
for (auto &player : players_) {
for (auto& player : players_) {
player->update(deltaTime);
if (player->isPlaying()) {
@@ -256,7 +262,7 @@ void Game::updatePlayers(float deltaTime) {
// Dibuja a los jugadores
void Game::renderPlayers() {
for (auto &player : players_) {
for (auto& player : players_) {
player->render();
}
}
@@ -395,31 +401,33 @@ void Game::checkState() {
// Destruye todos los items
void Game::destroyAllItems() {
for (auto &item : items_) {
for (auto& item : items_) {
item->disable();
}
}
// Comprueba la colisión entre el jugador y los globos activos
auto Game::checkPlayerBalloonCollision(std::shared_ptr<Player> &player) -> std::shared_ptr<Balloon> {
for (auto &balloon : balloon_manager_->getBalloons()) {
auto Game::checkPlayerBalloonCollision(std::shared_ptr<Player>& player) -> std::shared_ptr<Balloon> {
#ifndef RECORDING
for (auto& balloon : balloon_manager_->getBalloons()) {
if (!balloon->isInvulnerable() && !balloon->isPowerBall()) {
if (checkCollision(player->getCollider(), balloon->getCollider())) {
return balloon; // Devuelve el globo con el que se ha producido la colisión
}
}
}
#endif
return nullptr; // No se ha producido ninguna colisión
}
// Comprueba la colisión entre el jugador y los items
void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player) {
void Game::checkPlayerItemCollision(std::shared_ptr<Player>& player) {
if (!player->isPlaying()) {
return;
}
for (auto &item : items_) {
for (auto& item : items_) {
if (item->isEnabled()) {
if (checkCollision(player->getCollider(), item->getCollider())) {
switch (item->getType()) {
@@ -492,7 +500,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player) {
// Comprueba y procesa la colisión de las balas
void Game::checkBulletCollision() {
for (auto &bullet : bullets_) {
for (auto& bullet : bullets_) {
if (!bullet->isEnabled()) {
continue;
}
@@ -508,7 +516,7 @@ void Game::checkBulletCollision() {
}
// Maneja la colisión entre bala y Tabe
auto Game::checkBulletTabeCollision(const std::shared_ptr<Bullet> &bullet) -> bool {
auto Game::checkBulletTabeCollision(const std::shared_ptr<Bullet>& bullet) -> bool {
if (!tabe_->isEnabled()) {
return false;
}
@@ -540,8 +548,8 @@ void Game::handleTabeHitEffects() {
}
// Maneja la colisión entre bala y globos
auto Game::checkBulletBalloonCollision(const std::shared_ptr<Bullet> &bullet) -> bool {
for (auto &balloon : balloon_manager_->getBalloons()) {
auto Game::checkBulletBalloonCollision(const std::shared_ptr<Bullet>& bullet) -> bool {
for (auto& balloon : balloon_manager_->getBalloons()) {
if (!balloon->isEnabled() || balloon->isInvulnerable()) {
continue;
}
@@ -557,7 +565,7 @@ auto Game::checkBulletBalloonCollision(const std::shared_ptr<Bullet> &bullet) ->
}
// Procesa el impacto en un globo
void Game::processBalloonHit(const std::shared_ptr<Bullet> &bullet, const std::shared_ptr<Balloon> &balloon) {
void Game::processBalloonHit(const std::shared_ptr<Bullet>& bullet, const std::shared_ptr<Balloon>& balloon) {
auto player = getPlayer(static_cast<Player::Id>(bullet->getOwner()));
handleItemDrop(balloon, player);
@@ -567,7 +575,7 @@ void Game::processBalloonHit(const std::shared_ptr<Bullet> &bullet, const std::s
}
// Maneja la caída de items cuando se destruye un globo
void Game::handleItemDrop(const std::shared_ptr<Balloon> &balloon, const std::shared_ptr<Player> &player) {
void Game::handleItemDrop(const std::shared_ptr<Balloon>& balloon, const std::shared_ptr<Player>& player) {
const auto DROPPED_ITEM = dropItem();
if (DROPPED_ITEM == ItemType::NONE || demo_.recording) {
return;
@@ -583,7 +591,7 @@ void Game::handleItemDrop(const std::shared_ptr<Balloon> &balloon, const std::sh
}
// Maneja la destrucción del globo y puntuación
void Game::handleBalloonDestruction(std::shared_ptr<Balloon> balloon, const std::shared_ptr<Player> &player) {
void Game::handleBalloonDestruction(std::shared_ptr<Balloon> balloon, const std::shared_ptr<Player>& player) {
if (player->isPlaying()) {
auto const SCORE = balloon_manager_->popBalloon(std::move(balloon)) * player->getScoreMultiplier() * difficulty_score_multiplier_;
player->addScore(SCORE, Options::settings.hi_score_table.back().score);
@@ -595,7 +603,7 @@ void Game::handleBalloonDestruction(std::shared_ptr<Balloon> balloon, const std:
// Mueve las balas activas
void Game::updateBullets(float deltaTime) {
for (auto &bullet : bullets_) {
for (auto& bullet : bullets_) {
if (bullet->update(deltaTime) == Bullet::MoveStatus::OUT) {
getPlayer(static_cast<Player::Id>(bullet->getOwner()))->decScoreMultiplier();
}
@@ -604,7 +612,7 @@ void Game::updateBullets(float deltaTime) {
// Pinta las balas activas
void Game::renderBullets() {
for (auto &bullet : bullets_) {
for (auto& bullet : bullets_) {
bullet->render();
}
}
@@ -627,7 +635,7 @@ void Game::freeBullets() {
// Actualiza los items
void Game::updateItems(float deltaTime) {
for (auto &item : items_) {
for (auto& item : items_) {
if (item->isEnabled()) {
item->update(deltaTime);
if (item->isOnFloor()) {
@@ -640,7 +648,7 @@ void Game::updateItems(float deltaTime) {
// Pinta los items activos
void Game::renderItems() {
for (auto &item : items_) {
for (auto& item : items_) {
item->render();
}
}
@@ -717,7 +725,7 @@ void Game::freeItems() {
}
// Crea un objeto PathSprite
void Game::createItemText(int x, const std::shared_ptr<Texture> &texture) {
void Game::createItemText(int x, const std::shared_ptr<Texture>& texture) {
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
const auto W = texture->getWidth();
@@ -740,11 +748,11 @@ void Game::createItemText(int x, const std::shared_ptr<Texture> &texture) {
}
// Crea un objeto PathSprite
void Game::createMessage(const std::vector<Path> &paths, const std::shared_ptr<Texture> &texture) {
void Game::createMessage(const std::vector<Path>& paths, const std::shared_ptr<Texture>& texture) {
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
// Inicializa
for (const auto &path : paths) {
for (const auto& path : paths) {
path_sprites_.back()->addPath(path, true);
}
path_sprites_.back()->enable();
@@ -778,8 +786,8 @@ void Game::throwCoffee(int x, int y) {
smart_sprites_.back()->setPosX(x - 8);
smart_sprites_.back()->setPosY(y - 8);
smart_sprites_.back()->setWidth(param.game.item_size);
smart_sprites_.back()->setHeight(param.game.item_size);
smart_sprites_.back()->setWidth(Item::WIDTH);
smart_sprites_.back()->setHeight(Item::HEIGHT);
smart_sprites_.back()->setVelX((-1.0F + ((rand() % 5) * 0.5F)) * 60.0f); // Convertir a pixels/segundo
smart_sprites_.back()->setVelY(-4.0F * 60.0f); // Convertir a pixels/segundo
smart_sprites_.back()->setAccelX(0.0F);
@@ -788,43 +796,42 @@ void Game::throwCoffee(int x, int y) {
smart_sprites_.back()->setDestY(param.game.height + 1);
smart_sprites_.back()->setEnabled(true);
smart_sprites_.back()->setFinishedDelay(0.0F);
smart_sprites_.back()->setSpriteClip(0, param.game.item_size, param.game.item_size, param.game.item_size);
smart_sprites_.back()->setRotatingCenter({param.game.item_size / 2, param.game.item_size / 2});
smart_sprites_.back()->setSpriteClip(0, Item::HEIGHT, Item::WIDTH, Item::HEIGHT);
smart_sprites_.back()->setRotatingCenter({Item::WIDTH / 2, Item::HEIGHT / 2});
smart_sprites_.back()->setRotate(true);
smart_sprites_.back()->setRotateSpeed(10);
smart_sprites_.back()->setRotateAmount(90.0);
}
// Actualiza los SmartSprites
void Game::updateSmartSprites(float deltaTime) {
for (auto &sprite : smart_sprites_) {
for (auto& sprite : smart_sprites_) {
sprite->update(deltaTime);
}
}
// Pinta los SmartSprites activos
void Game::renderSmartSprites() {
for (auto &sprite : smart_sprites_) {
for (auto& sprite : smart_sprites_) {
sprite->render();
}
}
// Actualiza los PathSprites
void Game::updatePathSprites(float deltaTime) {
for (auto &sprite : path_sprites_) {
for (auto& sprite : path_sprites_) {
sprite->update(deltaTime);
}
}
// Pinta los PathSprites activos
void Game::renderPathSprites() {
for (auto &sprite : path_sprites_) {
for (auto& sprite : path_sprites_) {
sprite->render();
}
}
// Acciones a realizar cuando el jugador colisiona con un globo
void Game::handlePlayerCollision(std::shared_ptr<Player> &player, std::shared_ptr<Balloon> &balloon) {
void Game::handlePlayerCollision(std::shared_ptr<Player>& player, std::shared_ptr<Balloon>& balloon) {
if (!player->isPlaying() || player->isInvulnerable()) {
return; // Si no está jugando o tiene inmunidad, no hace nada
}
@@ -909,9 +916,9 @@ void Game::update(float deltaTime) {
screen_->update(deltaTime); // Actualiza el objeto screen
Audio::update(); // Actualiza el objeto audio
updateDemo();
updateDemo(deltaTime);
#ifdef RECORDING
updateRecording();
updateRecording(deltaTime);
#endif
updateGameStates(deltaTime);
fillCanvas();
@@ -965,7 +972,7 @@ void Game::updateBackground(float deltaTime) {
// Dibuja los elementos de la zona de juego en su textura
void Game::fillCanvas() {
// Dibuja el contenido de la zona de juego en su textura
auto *temp = SDL_GetRenderTarget(renderer_);
auto* temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, canvas_);
// Dibuja los objetos
@@ -1012,10 +1019,8 @@ void Game::run() {
last_time_ = SDL_GetTicks();
while (Section::name == Section::Name::GAME) {
#ifndef RECORDING
checkInput();
#endif
const float delta_time = calculateDeltaTime();
checkInput();
update(delta_time);
handleEvents(); // Tiene que ir antes del render
render();
@@ -1026,7 +1031,7 @@ void Game::run() {
void Game::initPaths() {
// Recorrido para el texto de "Get Ready!" (0,1)
{
const auto &texture = Resource::get()->getTexture("game_text_get_ready");
const auto& texture = Resource::get()->getTexture("game_text_get_ready");
const auto W = texture->getWidth();
const int X0 = -W;
const int X1 = param.game.play_area.center_x - (W / 2);
@@ -1038,7 +1043,7 @@ void Game::initPaths() {
// Recorrido para el texto de "Last Stage!" o de "X stages left" (2,3)
{
const auto &texture = Resource::get()->getTexture("game_text_last_stage");
const auto& texture = Resource::get()->getTexture("game_text_last_stage");
const auto H = texture->getHeight();
const int Y0 = param.game.play_area.rect.h - H;
const int Y1 = param.game.play_area.center_y - (H / 2);
@@ -1050,7 +1055,7 @@ void Game::initPaths() {
// Recorrido para el texto de "Congratulations!!" (4,5)
{
const auto &texture = Resource::get()->getTexture("game_text_congratulations");
const auto& texture = Resource::get()->getTexture("game_text_congratulations");
const auto W = texture->getWidth();
const auto H = texture->getHeight();
const int X0 = -W;
@@ -1063,7 +1068,7 @@ void Game::initPaths() {
// Recorrido para el texto de "1.000.000 points!" (6,7)
{
const auto &texture = Resource::get()->getTexture("game_text_1000000_points");
const auto& texture = Resource::get()->getTexture("game_text_1000000_points");
const auto W = texture->getWidth();
const auto H = texture->getHeight();
const int X0 = param.game.play_area.rect.w;
@@ -1076,7 +1081,7 @@ void Game::initPaths() {
// Recorrido para el texto de "New Record!" (8,9)
{
const auto &texture = Resource::get()->getTexture("game_text_new_record");
const auto& texture = Resource::get()->getTexture("game_text_new_record");
const auto W = texture->getWidth();
const auto H = texture->getHeight();
const int X0 = -W;
@@ -1089,7 +1094,7 @@ void Game::initPaths() {
// Recorrido para el texto de "Game Over" (10,11)
{
const auto &texture = Resource::get()->getTexture("game_text_game_over");
const auto& texture = Resource::get()->getTexture("game_text_game_over");
const auto H = texture->getHeight();
const int Y0 = param.game.play_area.rect.h - H;
const int Y1 = param.game.play_area.center_y - (H / 2);
@@ -1106,7 +1111,7 @@ void Game::updateHelper() {
if (menace_ > 15) {
helper_.need_coffee = true;
helper_.need_coffee_machine = true;
for (const auto &player : players_) {
for (const auto& player : players_) {
if (player->isPlaying()) {
helper_.need_coffee &= (player->getCoffees() == 0);
helper_.need_coffee_machine &= (!player->isPowerUp());
@@ -1120,7 +1125,7 @@ void Game::updateHelper() {
// Comprueba si todos los jugadores han terminado de jugar
auto Game::allPlayersAreWaitingOrGameOver() -> bool {
auto success = true;
for (const auto &player : players_) {
for (const auto& player : players_) {
success &= player->isWaiting() || player->isGameOver();
}
@@ -1130,7 +1135,7 @@ auto Game::allPlayersAreWaitingOrGameOver() -> bool {
// Comprueba si todos los jugadores han terminado de jugar
auto Game::allPlayersAreGameOver() -> bool {
auto success = true;
for (const auto &player : players_) {
for (const auto& player : players_) {
success &= player->isGameOver();
}
@@ -1140,7 +1145,7 @@ auto Game::allPlayersAreGameOver() -> bool {
// Comprueba si todos los jugadores han terminado de jugar
auto Game::allPlayersAreNotPlaying() -> bool {
auto success = true;
for (const auto &player : players_) {
for (const auto& player : players_) {
success &= !player->isPlaying();
}
@@ -1173,7 +1178,7 @@ void Game::handleEvents() {
// Actualiza el marcador
void Game::updateScoreboard() {
for (const auto &player : players_) {
for (const auto& player : players_) {
scoreboard_->setScore(player->getScoreBoardPanel(), player->getScore());
scoreboard_->setMult(player->getScoreBoardPanel(), player->getScoreMultiplier());
}
@@ -1208,7 +1213,7 @@ void Game::checkPlayersStatusPlaying() {
// Comprueba si todos los jugadores estan esperando
if (allPlayersAreWaitingOrGameOver()) {
// Entonces los pone en estado de Game Over
for (auto &player : players_) {
for (auto& player : players_) {
player->setPlayingState(Player::State::GAME_OVER);
}
}
@@ -1220,7 +1225,7 @@ void Game::checkPlayersStatusPlaying() {
// Obtiene un jugador a partir de su "id"
auto Game::getPlayer(Player::Id id) -> std::shared_ptr<Player> {
auto it = std::find_if(players_.begin(), players_.end(), [id](const auto &player) { return player->getId() == id; });
auto it = std::find_if(players_.begin(), players_.end(), [id](const auto& player) { return player->getId() == id; });
if (it != players_.end()) {
return *it;
@@ -1262,7 +1267,7 @@ void Game::checkInput() {
// Verifica si alguno de los controladores ha solicitado una pausa y actualiza el estado de pausa del juego.
void Game::checkPauseInput() {
// Comprueba los mandos
for (const auto &gamepad : input_->getGamepads()) {
for (const auto& gamepad : input_->getGamepads()) {
if (input_->checkAction(Input::Action::PAUSE, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
pause_manager_->togglePlayerPause();
return;
@@ -1287,19 +1292,16 @@ void Game::demoHandlePassInput() {
// Gestiona las entradas de los jugadores en el modo demo, incluyendo movimientos y disparos automáticos.
void Game::demoHandleInput() {
int index = 0;
for (const auto &player : players_) {
for (const auto& player : players_) {
if (player->isPlaying()) {
// Maneja el input específico del jugador en modo demo.
demoHandlePlayerInput(player, index);
demoHandlePlayerInput(player, player->getDemoFile()); // Maneja el input específico del jugador en modo demo.
}
++index;
}
}
// Procesa las entradas para un jugador específico durante el modo demo.
void Game::demoHandlePlayerInput(const std::shared_ptr<Player> &player, int index) {
const auto &demo_data = demo_.data.at(index).at(demo_.counter);
void Game::demoHandlePlayerInput(const std::shared_ptr<Player>& player, int index) {
const auto& demo_data = demo_.data.at(index).at(demo_.index);
if (demo_data.left == 1) {
player->setInput(Input::Action::LEFT);
@@ -1319,7 +1321,7 @@ void Game::demoHandlePlayerInput(const std::shared_ptr<Player> &player, int inde
}
// Maneja el disparo de un jugador, incluyendo la creación de balas y la gestión del tiempo de espera entre disparos.
void Game::handleFireInput(const std::shared_ptr<Player> &player, Bullet::Type type) {
void Game::handleFireInput(const std::shared_ptr<Player>& player, Bullet::Type type) {
if (player->canFire()) {
SDL_Point bullet = {0, 0};
switch (type) {
@@ -1364,7 +1366,7 @@ void Game::handleFireInput(const std::shared_ptr<Player> &player, Bullet::Type t
// Gestiona las entradas de todos los jugadores en el modo normal (fuera del modo demo)
void Game::handlePlayersInput() {
for (const auto &player : players_) {
for (const auto& player : players_) {
if (player->isPlaying()) {
handleNormalPlayerInput(player); // Maneja el input de los jugadores en modo normal
} else if (player->isContinue()) {
@@ -1378,7 +1380,7 @@ void Game::handlePlayersInput() {
}
// Maneja las entradas de movimiento y disparo para un jugador en modo normal.
void Game::handleNormalPlayerInput(const std::shared_ptr<Player> &player) {
void Game::handleNormalPlayerInput(const std::shared_ptr<Player>& player) {
if (input_->checkAction(Input::Action::LEFT, Input::ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
player->setInput(Input::Action::LEFT);
#ifdef RECORDING
@@ -1401,7 +1403,7 @@ void Game::handleNormalPlayerInput(const std::shared_ptr<Player> &player) {
}
// Procesa las entradas de disparo del jugador, permitiendo disparos automáticos si está habilitado.
void Game::handleFireInputs(const std::shared_ptr<Player> &player, bool autofire) {
void Game::handleFireInputs(const std::shared_ptr<Player>& player, bool autofire) {
if (!player) {
return;
}
@@ -1431,7 +1433,7 @@ void Game::handleFireInputs(const std::shared_ptr<Player> &player, bool autofire
}
// Maneja la continuación del jugador cuando no está jugando, permitiendo que continúe si se pulsa el botón de inicio.
void Game::handlePlayerContinueInput(const std::shared_ptr<Player> &player) {
void Game::handlePlayerContinueInput(const std::shared_ptr<Player>& player) {
if (input_->checkAction(Input::Action::START, Input::DO_NOT_ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
player->setPlayingState(Player::State::RECOVER);
player->addCredit();
@@ -1450,7 +1452,7 @@ void Game::handlePlayerContinueInput(const std::shared_ptr<Player> &player) {
}
// Maneja la continuación del jugador cuando no está jugando, permitiendo que continúe si se pulsa el botón de inicio.
void Game::handlePlayerWaitingInput(const std::shared_ptr<Player> &player) {
void Game::handlePlayerWaitingInput(const std::shared_ptr<Player>& player) {
if (input_->checkAction(Input::Action::START, Input::DO_NOT_ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
player->setPlayingState(Player::State::ENTERING_SCREEN);
player->addCredit();
@@ -1459,7 +1461,7 @@ void Game::handlePlayerWaitingInput(const std::shared_ptr<Player> &player) {
}
// Procesa las entradas para la introducción del nombre del jugador.
void Game::handleNameInput(const std::shared_ptr<Player> &player) {
void Game::handleNameInput(const std::shared_ptr<Player>& player) {
if (input_->checkAction(Input::Action::FIRE_LEFT, Input::DO_NOT_ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
if (player->isShowingName()) {
player->passShowingName();
@@ -1514,21 +1516,43 @@ void Game::handleNameInput(const std::shared_ptr<Player> &player) {
// Inicializa las variables para el modo DEMO
void Game::initDemo(Player::Id player_id) {
#ifdef RECORDING
// En modo grabación, inicializar vector vacío para almacenar teclas
demo_.data.emplace_back(); // Vector vacío para grabación
demo_.data.at(0).reserve(TOTAL_DEMO_DATA); // Reservar espacio para 2000 elementos
#endif
if (demo_.enabled) {
// Cambia el estado del juego
setState(State::PLAYING);
// Aleatoriza la asignación del fichero con los datos del modo demostracion
const auto DEMO1 = rand() % 2;
const auto DEMO2 = (DEMO1 == 0) ? 1 : 0;
demo_.data.emplace_back(Resource::get()->getDemoData(DEMO1));
demo_.data.emplace_back(Resource::get()->getDemoData(DEMO2));
#ifndef RECORDING
// En modo juego: cargar todas las demos y asignar una diferente a cada jugador
auto const NUM_DEMOS = Asset::get()->getListByType(Asset::Type::DEMODATA).size();
for (size_t num_demo = 0; num_demo < NUM_DEMOS; ++num_demo) {
demo_.data.emplace_back(Resource::get()->getDemoData(num_demo));
}
// Crear índices mezclados para asignación aleatoria
std::vector<size_t> demo_indices(NUM_DEMOS);
std::iota(demo_indices.begin(), demo_indices.end(), 0);
std::random_device rd;
std::default_random_engine rng(rd());
std::shuffle(demo_indices.begin(), demo_indices.end(), rng);
// Asignar demos a jugadores (round-robin si hay más jugadores que demos)
for (size_t i = 0; i < players_.size(); ++i) {
size_t demo_index = demo_indices[i % NUM_DEMOS];
players_.at(i)->setDemoFile(demo_index);
}
#endif
// Selecciona una pantalla al azar
constexpr auto NUM_DEMOS = 3;
const auto DEMO = rand() % NUM_DEMOS;
constexpr std::array<int, NUM_DEMOS> STAGES = {0, 3, 5};
stage_manager_->jumpToStage(STAGES.at(DEMO));
constexpr auto NUM_STAGES = 3;
const auto STAGE = rand() % NUM_STAGES;
constexpr std::array<float, NUM_STAGES> STAGES = {0.005F, 0.32F, 0.53F};
stage_manager_->setTotalPower(stage_manager_->getTotalPowerNeededToCompleteGame() * STAGES.at(STAGE));
background_->setProgress(stage_manager_->getTotalPower());
// Activa o no al otro jugador
if (rand() % 3 != 0) {
@@ -1538,7 +1562,7 @@ void Game::initDemo(Player::Id player_id) {
}
// Asigna cafes a los jugadores
for (auto &player : players_) {
for (auto& player : players_) {
if (player->isPlaying()) {
for (int i = 0; i < rand() % 3; ++i) {
player->giveExtraHit();
@@ -1562,14 +1586,14 @@ void Game::initDemo(Player::Id player_id) {
#else
demo_.recording = false;
#endif
demo_.counter = 0;
demo_.index = 0;
}
// Inicializa el marcador
void Game::initScoreboard() {
scoreboard_->setPos(param.scoreboard.rect);
scoreboard_->setMode(Scoreboard::Id::CENTER, Scoreboard::Mode::STAGE_INFO);
for (const auto &player : players_) {
for (const auto& player : players_) {
scoreboard_->setName(player->getScoreBoardPanel(), player->getName());
if (player->isWaiting()) {
scoreboard_->setMode(player->getScoreBoardPanel(), Scoreboard::Mode::WAITING);
@@ -1617,7 +1641,11 @@ void Game::initPlayers(Player::Id player_id) {
// Crea al jugador uno y lo pone en modo espera
Player::Config config_player1{
.id = Player::Id::PLAYER1,
#ifdef RECORDING
.x = param.game.play_area.center_x - (Player::WIDTH / 2),
#else
.x = param.game.play_area.first_quarter_x - (Player::WIDTH / 2),
#endif
.y = Y,
.demo = demo_.enabled,
.play_area = &param.game.play_area.rect,
@@ -1634,7 +1662,11 @@ void Game::initPlayers(Player::Id player_id) {
player1->setName(Lang::getText("[SCOREBOARD] 1"));
player1->setGamepad(Options::gamepad_manager.getGamepad(Player::Id::PLAYER1).instance);
player1->setUsesKeyboard(Player::Id::PLAYER1 == Options::keyboard.player_id);
#ifdef RECORDING
player1->setPlayingState(Player::State::PLAYING);
#else
player1->setPlayingState((player_id == Player::Id::BOTH_PLAYERS || player_id == Player::Id::PLAYER1) ? STATE : Player::State::WAITING);
#endif
// Crea al jugador dos y lo pone en modo espera
Player::Config config_player2{
@@ -1663,14 +1695,14 @@ void Game::initPlayers(Player::Id player_id) {
players_.push_back(std::move(player1));
// Registra los jugadores en Options
for (const auto &player : players_) {
for (const auto& player : players_) {
Options::keyboard.addPlayer(player);
Options::gamepad_manager.addPlayer(player);
}
}
// Hace sonar la música
void Game::playMusic(const std::string &music_file, int loop) {
void Game::playMusic(const std::string& music_file, int loop) {
Audio::get()->playMusic(music_file, loop);
}
@@ -1692,7 +1724,7 @@ void Game::stopMusic() const {
}
// Actualiza las variables durante el modo demo
void Game::updateDemo() {
void Game::updateDemo(float deltaTime) {
if (demo_.enabled) {
balloon_manager_->setCreationTimeEnabled(balloon_manager_->getNumBalloons() != 0);
@@ -1700,16 +1732,12 @@ void Game::updateDemo() {
fade_in_->update();
fade_out_->update();
// Incrementa el contador de la demo cada 1/60 segundos (16.67ms)
static float demo_frame_timer = 0.0f;
demo_frame_timer += calculateDeltaTime();
if (demo_frame_timer >= 0.01667f && demo_.counter < TOTAL_DEMO_DATA) {
demo_.counter++;
demo_frame_timer -= 0.01667f; // Mantener precisión acumulada
}
// Actualiza el contador de tiempo y el índice
demo_.elapsed_s += deltaTime;
demo_.index = static_cast<int>(demo_.elapsed_s * 60.0F);
// Activa el fundido antes de acabar con los datos de la demo
if (demo_.counter == TOTAL_DEMO_DATA - 200) {
if (demo_.index == TOTAL_DEMO_DATA - 200) {
fade_out_->setType(Fade::Type::RANDOM_SQUARE2);
fade_out_->setPostDuration(param.fade.post_duration_ms);
fade_out_->activate();
@@ -1725,22 +1753,28 @@ void Game::updateDemo() {
#ifdef RECORDING
// Actualiza las variables durante el modo de grabación
void Game::updateRecording() {
// Solo mira y guarda el input en cada update
checkInput();
void Game::updateRecording(float deltaTime) {
// Actualiza el contador de tiempo y el índice
demo_.elapsed_s += deltaTime;
demo_.index = static_cast<int>(demo_.elapsed_s * 60.0F);
// Incrementa el contador de la demo cada 1/60 segundos (16.67ms)
static float recording_frame_timer = 0.0f;
recording_frame_timer += calculateDeltaTime();
if (recording_frame_timer >= 0.01667f && demo_.counter < TOTAL_DEMO_DATA) {
demo_.counter++;
recording_frame_timer -= 0.01667f; // Mantener precisión acumulada
if (demo_.index >= TOTAL_DEMO_DATA) {
Section::name = Section::Name::QUIT;
return;
}
// Si se ha llenado el vector con datos, sale del programa
else {
section::name = section::Name::QUIT;
return;
// Almacenar las teclas del frame actual en el vector de grabación
if (demo_.index < TOTAL_DEMO_DATA && demo_.data.size() > 0) {
// Asegurar que el vector tenga el tamaño suficiente
if (demo_.data.at(0).size() <= static_cast<size_t>(demo_.index)) {
demo_.data.at(0).resize(demo_.index + 1);
}
// Almacenar las teclas del frame actual
demo_.data.at(0).at(demo_.index) = demo_.keys;
// Resetear las teclas para el siguiente frame
demo_.keys = DemoKeys();
}
}
#endif
@@ -1763,7 +1797,7 @@ void Game::updateGameStateEnteringPlayer(float deltaTime) {
updatePlayers(deltaTime);
updateScoreboard();
updateBackground(deltaTime);
for (const auto &player : players_) {
for (const auto& player : players_) {
if (player->isPlaying()) {
setState(State::SHOWING_GET_READY_MESSAGE);
createMessage({paths_.at(0), paths_.at(1)}, Resource::get()->getTexture("game_text_get_ready"));
@@ -1833,7 +1867,7 @@ void Game::updateMenace() {
return;
}
const auto &stage = current_stage.value();
const auto& stage = current_stage.value();
const double FRACTION = stage_manager_->getCurrentStageProgressFraction();
const int DIFFERENCE = stage.getMaxMenace() - stage.getMinMenace();
@@ -1886,19 +1920,19 @@ void Game::setState(State state) {
game_completed_flags_.reset(); // Resetea flags de juego completado
break;
case State::GAME_OVER:
game_over_flags_.reset(); // Resetea flags de game over
game_over_flags_.reset(); // Resetea flags de game over
break;
default:
break;
}
}
void Game::playSound(const std::string &name) const {
void Game::playSound(const std::string& name) const {
if (demo_.enabled) {
return;
}
static auto *audio_ = Audio::get();
static auto* audio_ = Audio::get();
audio_->playSound(name);
}
@@ -1906,7 +1940,7 @@ void Game::playSound(const std::string &name) const {
void Game::sortPlayersByZOrder() {
// Procesar jugadores que van al fondo (se dibujan primero)
if (!players_to_put_at_back_.empty()) {
for (auto &player : players_to_put_at_back_) {
for (auto& player : players_to_put_at_back_) {
auto it = std::find(players_.begin(), players_.end(), player);
if (it != players_.end() && it != players_.begin()) {
std::shared_ptr<Player> dying_player = *it;
@@ -1919,7 +1953,7 @@ void Game::sortPlayersByZOrder() {
// Procesar jugadores que van al frente (se dibujan últimos)
if (!players_to_put_at_front_.empty()) {
for (auto &player : players_to_put_at_front_) {
for (auto& player : players_to_put_at_front_) {
auto it = std::find(players_.begin(), players_.end(), player);
if (it != players_.end() && it != players_.end() - 1) {
std::shared_ptr<Player> front_player = *it;
@@ -1932,12 +1966,12 @@ void Game::sortPlayersByZOrder() {
}
// Mueve el jugador para pintarlo al fondo de la lista de jugadores
void Game::sendPlayerToTheBack(const std::shared_ptr<Player> &player) {
void Game::sendPlayerToTheBack(const std::shared_ptr<Player>& player) {
players_to_put_at_back_.push_back(player);
}
// Mueve el jugador para pintarlo el primero de la lista de jugadores
void Game::sendPlayerToTheFront(const std::shared_ptr<Player> &player) {
void Game::sendPlayerToTheFront(const std::shared_ptr<Player>& player) {
players_to_put_at_front_.push_back(player);
}
@@ -1956,7 +1990,7 @@ void Game::handleGameCompletedEvents() {
createMessage({paths_.at(4), paths_.at(5)}, Resource::get()->getTexture("game_text_congratulations"));
createMessage({paths_.at(6), paths_.at(7)}, Resource::get()->getTexture("game_text_1000000_points"));
for (auto &player : players_) {
for (auto& player : players_) {
if (player->isPlaying()) {
player->addScore(1000000, Options::settings.hi_score_table.back().score);
player->setPlayingState(Player::State::CELEBRATING);
@@ -1972,7 +2006,7 @@ void Game::handleGameCompletedEvents() {
// Fin de celebraciones
if (!game_completed_flags_.end_celebrations_triggered && game_completed_timer_ >= END_CELEBRATIONS_S) {
for (auto &player : players_) {
for (auto& player : players_) {
if (player->isCelebrating()) {
player->setPlayingState(player->qualifiesForHighScore() ? Player::State::ENTERING_NAME_GAME_COMPLETED : Player::State::LEAVING_SCREEN);
}
@@ -2009,7 +2043,7 @@ void Game::handleGameOverEvents() {
#ifdef _DEBUG
// Comprueba los eventos en el modo DEBUG
void Game::handleDebugEvents(const SDL_Event &event) {
void Game::handleDebugEvents(const SDL_Event& event) {
static int formation_id_ = 0;
if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) {
switch (event.key.key) {
@@ -2052,7 +2086,7 @@ void Game::handleDebugEvents(const SDL_Event &event) {
break;
}
case SDLK_8: {
for (const auto &player : players_) {
for (const auto& player : players_) {
if (player->isPlaying()) {
createItem(ItemType::COFFEE_MACHINE, player->getPosX(), param.game.game_area.rect.y - Item::COFFEE_MACHINE_HEIGHT);
break;

View File

@@ -6,7 +6,7 @@
#include <string> // Para string
#include <vector> // Para vector
#include "bullet.h" // Para Bullet
#include "bullet.h" // Para Bullet
#include "hit.h" // Para Hit
#include "item.h" // Para Item, ItemType
#include "manage_hiscore_table.h" // Para HiScoreEntry
@@ -15,7 +15,8 @@
#include "player.h" // Para Player
#include "smart_sprite.h" // Para SmartSprite
#include "stage.h" // Para StageManager
#include "utils.h" // Para Demo
#include "demo.h" // Para Demo
#include "utils.h" // Para otras utilidades
class Background;
class Balloon;
@@ -56,8 +57,8 @@ class Game {
static constexpr bool DEMO_ON = true; // Modo demo activado
// --- Constructor y destructor ---
Game(Player::Id player_id, int current_stage, bool demo); // Constructor principal
~Game(); // Destructor
Game(Player::Id player_id, int current_stage, bool demo_enabled); // Constructor principal
~Game(); // Destructor
// --- Bucle principal ---
void run(); // Ejecuta el bucle principal del juego
@@ -77,9 +78,9 @@ class Game {
static constexpr float HELP_COUNTER_S = 16.667f; // Contador de ayuda (1000 frames a 60fps → segundos)
static constexpr float GAME_COMPLETED_START_FADE_S = 8.333f; // Inicio del fade al completar (500 frames → segundos)
static constexpr float GAME_COMPLETED_END_S = 11.667f; // Fin del juego completado (700 frames → segundos)
static constexpr float GAME_OVER_DURATION_S = 8.5f;
static constexpr float TIME_STOPPED_DURATION_S = 6.0f;
static constexpr float DEMO_FADE_PRE_DURATION_S = 0.5f;
static constexpr float GAME_OVER_DURATION_S = 8.5f;
static constexpr float TIME_STOPPED_DURATION_S = 6.0f;
static constexpr float DEMO_FADE_PRE_DURATION_S = 0.5f;
static constexpr int ITEM_POINTS_1_DISK_ODDS = 10;
static constexpr int ITEM_POINTS_2_GAVINA_ODDS = 6;
static constexpr int ITEM_POINTS_3_PACMAR_ODDS = 3;
@@ -167,35 +168,35 @@ class Game {
// Estructuras para gestionar flags de eventos basados en tiempo
struct GameOverFlags {
bool music_fade_triggered = false;
bool message_triggered = false;
bool fade_out_triggered = false;
bool music_fade_triggered = false;
bool message_triggered = false;
bool fade_out_triggered = false;
void reset() {
music_fade_triggered = false;
message_triggered = false;
fade_out_triggered = false;
}
void reset() {
music_fade_triggered = false;
message_triggered = false;
fade_out_triggered = false;
}
} game_over_flags_;
struct GameCompletedFlags {
bool start_celebrations_triggered = false;
bool end_celebrations_triggered = false;
bool start_celebrations_triggered = false;
bool end_celebrations_triggered = false;
void reset() {
start_celebrations_triggered = false;
end_celebrations_triggered = false;
}
void reset() {
start_celebrations_triggered = false;
end_celebrations_triggered = false;
}
} game_completed_flags_;
struct TimeStoppedFlags {
bool color_flash_sound_played = false;
bool warning_phase_started = false;
bool color_flash_sound_played = false;
bool warning_phase_started = false;
void reset() {
color_flash_sound_played = false;
warning_phase_started = false;
}
void reset() {
color_flash_sound_played = false;
warning_phase_started = false;
}
} time_stopped_flags_;
#ifdef _DEBUG
@@ -259,11 +260,11 @@ class Game {
void demoHandlePlayerInput(const std::shared_ptr<Player>& player, int index); // Procesa entrada de jugador en demo
// --- Sistema de balas y proyectiles ---
void updateBullets(float deltaTime); // Actualiza posición y estado de todas las balas (time-based)
void renderBullets(); // Renderiza todas las balas activas
void updateBullets(float deltaTime); // Actualiza posición y estado de todas las balas (time-based)
void renderBullets(); // Renderiza todas las balas activas
void createBullet(int x, int y, Bullet::Type kind, Bullet::Color color, int owner); // Crea una nueva bala
void checkBulletCollision(); // Verifica colisiones de todas las balas
void freeBullets(); // Libera memoria del vector de balas
void checkBulletCollision(); // Verifica colisiones de todas las balas
void freeBullets(); // Libera memoria del vector de balas
// --- Colisiones específicas de balas ---
auto checkBulletTabeCollision(const std::shared_ptr<Bullet>& bullet) -> bool; // Detecta colisión bala-Tabe
@@ -325,7 +326,7 @@ class Game {
// --- Modo demostración ---
void initDemo(Player::Id player_id); // Inicializa variables para el modo demostración
void updateDemo(); // Actualiza lógica específica del modo demo
void updateDemo(float deltaTime); // Actualiza lógica específica del modo demo
// --- Recursos y renderizado ---
void setResources(); // Asigna texturas y animaciones a los objetos
@@ -346,7 +347,7 @@ class Game {
// SISTEMA DE GRABACIÓN (CONDICIONAL)
#ifdef RECORDING
void updateRecording(); // Actualiza variables durante modo de grabación
void updateRecording(float deltaTime); // Actualiza variables durante modo de grabación
#endif
// --- Depuración (solo en modo DEBUG) ---

View File

@@ -14,6 +14,7 @@
#include "global_events.h" // Para check
#include "global_inputs.h" // Para check
#include "input.h" // Para Input
#include "item.h" // Para Item
#include "lang.h" // Para getText
#include "param.h" // Para Param, param, ParamGame, ParamFade, Param...
#include "resource.h" // Para Resource
@@ -78,43 +79,43 @@ void Instructions::iniSprites() {
// Inicializa los sprites
for (int i = 0; i < (int)item_textures_.size(); ++i) {
auto sprite = std::make_unique<Sprite>(item_textures_[i], 0, 0, param.game.item_size, param.game.item_size);
sprite->setPosition((SDL_FPoint){sprite_pos_.x, sprite_pos_.y + ((param.game.item_size + item_space_) * i)});
auto sprite = std::make_unique<Sprite>(item_textures_[i], 0, 0, Item::WIDTH, Item::HEIGHT);
sprite->setPosition((SDL_FPoint){sprite_pos_.x, sprite_pos_.y + ((Item::HEIGHT + item_space_) * i)});
sprites_.push_back(std::move(sprite));
}
}
// Actualiza los sprites
void Instructions::updateSprites() {
SDL_FRect src_rect = {0, 0, param.game.item_size, param.game.item_size};
SDL_FRect src_rect = {0, 0, Item::WIDTH, Item::HEIGHT};
// Disquito (desplazamiento 12/60 = 0.2s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[0]->setSpriteClip(src_rect);
// Gavina (desplazamiento 9/60 = 0.15s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[1]->setSpriteClip(src_rect);
// Pacmar (desplazamiento 6/60 = 0.1s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[2]->setSpriteClip(src_rect);
// Time Stopper (desplazamiento 3/60 = 0.05s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[3]->setSpriteClip(src_rect);
// Coffee (sin desplazamiento)
src_rect.y = param.game.item_size * (static_cast<int>(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[4]->setSpriteClip(src_rect);
}
// Rellena la textura de texto
void Instructions::fillTexture() {
const int X_OFFSET = param.game.item_size + 8;
const int X_OFFSET = Item::WIDTH + 8;
// Modifica el renderizador para pintar en la textura
auto *temp = SDL_GetRenderTarget(renderer_);
auto* temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, texture_);
// Limpia la textura
@@ -130,7 +131,7 @@ void Instructions::fillTexture() {
constexpr int SPACE_POST_HEADER = 20;
constexpr int SPACE_PRE_HEADER = 28;
const int SPACE_BETWEEN_LINES = text_->getCharacterSize() * 1.5F;
const int SPACE_BETWEEN_ITEM_LINES = param.game.item_size + item_space_;
const int SPACE_BETWEEN_ITEM_LINES = Item::HEIGHT + item_space_;
const int SPACE_NEW_PARAGRAPH = SPACE_BETWEEN_LINES * 0.5F;
const int SIZE = (NUM_LINES * SPACE_BETWEEN_LINES) + (NUM_ITEM_LINES * SPACE_BETWEEN_ITEM_LINES) + (NUM_POST_HEADERS * SPACE_POST_HEADER) + (NUM_PRE_HEADERS * SPACE_PRE_HEADER) + (SPACE_NEW_PARAGRAPH);
@@ -144,7 +145,7 @@ void Instructions::fillTexture() {
Lang::getText("[INSTRUCTIONS] 09"),
Lang::getText("[INSTRUCTIONS] 10"),
Lang::getText("[INSTRUCTIONS] 11")};
for (const auto &desc : ITEM_DESCRIPTIONS) {
for (const auto& desc : ITEM_DESCRIPTIONS) {
const int L = text_->length(desc);
length = L > length ? L : length;
}
@@ -178,13 +179,13 @@ void Instructions::fillTexture() {
// Da valor a la variable
sprite_pos_.x = ANCHOR_ITEM;
sprite_pos_.y = ANCHOR3 - ((param.game.item_size - text_->getCharacterSize()) / 2);
sprite_pos_.y = ANCHOR3 - ((Item::HEIGHT - text_->getCharacterSize()) / 2);
}
// Rellena el backbuffer
void Instructions::fillBackbuffer() {
// Modifica el renderizador para pintar en la textura
auto *temp = SDL_GetRenderTarget(renderer_);
auto* temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, backbuffer_);
// Limpia la textura
@@ -195,7 +196,7 @@ void Instructions::fillBackbuffer() {
SDL_RenderTexture(renderer_, texture_, nullptr, nullptr);
// Dibuja los sprites
for (auto &sprite : sprites_) {
for (auto& sprite : sprites_) {
sprite->render();
}
@@ -207,7 +208,7 @@ void Instructions::fillBackbuffer() {
void Instructions::update(float delta_time) {
elapsed_time_ += delta_time; // Incrementa el tiempo transcurrido
static auto *const SCREEN = Screen::get();
static auto* const SCREEN = Screen::get();
SCREEN->update(delta_time); // Actualiza el objeto screen
Audio::update(); // Actualiza el objeto audio
@@ -220,7 +221,7 @@ void Instructions::update(float delta_time) {
// Pinta en pantalla
void Instructions::render() {
static auto *const SCREEN = Screen::get();
static auto* const SCREEN = Screen::get();
SCREEN->start(); // Prepara para empezar a dibujar en la textura de juego
SCREEN->clean(); // Limpia la pantalla
@@ -287,11 +288,11 @@ auto Instructions::initializeLines(int height) -> std::vector<Line> {
}
// Método para mover las líneas con suavizado
auto Instructions::moveLines(std::vector<Line> &lines, int width, float duration, Uint32 start_delay) -> bool {
auto Instructions::moveLines(std::vector<Line>& lines, int width, float duration, Uint32 start_delay) -> bool {
Uint32 current_time = SDL_GetTicks();
bool all_lines_off_screen = true;
for (auto &line : lines) {
for (auto& line : lines) {
// Establecer start_time en el primer cuadro de animación
if (line.start_time == 0) {
line.start_time = current_time + line.y * start_delay;
@@ -316,8 +317,8 @@ auto Instructions::moveLines(std::vector<Line> &lines, int width, float duration
}
// Método para renderizar las líneas
void Instructions::renderLines(SDL_Renderer *renderer, SDL_Texture *texture, const std::vector<Line> &lines) {
for (const auto &line : lines) {
void Instructions::renderLines(SDL_Renderer* renderer, SDL_Texture* texture, const std::vector<Line>& lines) {
for (const auto& line : lines) {
SDL_FRect src_rect = {0, static_cast<float>(line.y), 320, 1};
SDL_FRect dst_rect = {static_cast<float>(line.x), static_cast<float>(line.y), 320, 1};
SDL_RenderTexture(renderer, texture, &src_rect, &dst_rect);

View File

@@ -151,6 +151,51 @@ auto StageManager::jumpToStage(size_t target_stage_index) -> bool {
return true;
}
auto StageManager::setTotalPower(int target_total_power) -> bool {
if (target_total_power < 0) {
return false;
}
int total_power_needed = getTotalPowerNeededToCompleteGame();
if (target_total_power > total_power_needed) {
return false;
}
// Calcular en qué fase debería estar y cuánto poder de esa fase
int accumulated_power = 0;
size_t target_stage_index = 0;
int target_current_power = 0;
for (size_t i = 0; i < stages_.size(); ++i) {
int stage_power = stages_[i].getPowerToComplete();
if (accumulated_power + stage_power > target_total_power) {
// El objetivo está dentro de esta fase
target_stage_index = i;
target_current_power = target_total_power - accumulated_power;
break;
}
accumulated_power += stage_power;
if (accumulated_power == target_total_power) {
// El objetivo coincide exactamente con el final de esta fase
// Mover a la siguiente fase (si existe) con power 0
target_stage_index = (i + 1 < stages_.size()) ? i + 1 : i;
target_current_power = (i + 1 < stages_.size()) ? 0 : stage_power;
break;
}
}
// Actualizar estado
current_stage_index_ = target_stage_index;
current_power_ = target_current_power;
total_power_ = target_total_power;
updateStageStatuses();
return true;
}
auto StageManager::subtractPower(int amount) -> bool {
if (amount <= 0 || current_power_ < amount) {
return false;

View File

@@ -67,6 +67,7 @@ class StageManager : public IStageInfo {
// --- Navegación ---
auto jumpToStage(size_t target_stage_index) -> bool; // Salta a una fase específica
auto setTotalPower(int target_total_power) -> bool; // Establece el poder total y ajusta fase/progreso
// --- Consultas de estado ---
[[nodiscard]] auto getCurrentStage() const -> std::optional<StageData>; // Obtiene la fase actual

View File

@@ -38,7 +38,7 @@ Texture::Texture(SDL_Renderer *renderer, std::string path)
surface_ = loadSurface(path_);
// Añade la propia paleta del fichero a la lista
addPaletteFromGifFile(path_);
addPaletteFromGifFile(path_, true); // Usar modo silencioso
// Crea la textura, establece el BlendMode y copia la surface a la textura
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
@@ -301,7 +301,7 @@ void Texture::setPaletteColor(int palette, int index, Uint32 color) {
}
// Carga una paleta desde un fichero
auto Texture::loadPaletteFromFile(const std::string &file_path) -> Palette {
auto Texture::loadPaletteFromFile(const std::string &file_path, bool quiet) -> Palette {
Palette palette;
std::vector<Uint8> buffer;
@@ -329,7 +329,9 @@ auto Texture::loadPaletteFromFile(const std::string &file_path) -> Palette {
}
}
printWithDots("Palette : ", getFileName(file_path), "[ LOADED ]");
if (!quiet) {
printWithDots("Palette : ", getFileName(file_path), "[ LOADED ]");
}
// Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
GIF::Gif gif;
@@ -349,14 +351,14 @@ auto Texture::loadPaletteFromFile(const std::string &file_path) -> Palette {
}
// Añade una paleta a la lista
void Texture::addPaletteFromGifFile(const std::string &path) {
palettes_.emplace_back(loadPaletteFromFile(path));
void Texture::addPaletteFromGifFile(const std::string &path, bool quiet) {
palettes_.emplace_back(loadPaletteFromFile(path, quiet));
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
}
// Añade una paleta a la lista
void Texture::addPaletteFromPalFile(const std::string &path) {
palettes_.emplace_back(readPalFile(path));
palettes_.emplace_back(readPalFile(path, true)); // Usar modo silencioso
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
}
@@ -372,7 +374,7 @@ void Texture::setPalette(size_t palette) {
auto Texture::getRenderer() -> SDL_Renderer * { return renderer_; }
// Carga una paleta desde un archivo .pal
auto Texture::readPalFile(const std::string &file_path) -> Palette {
auto Texture::readPalFile(const std::string &file_path, bool quiet) -> Palette {
Palette palette{};
palette.fill(0); // Inicializar todo con 0 (transparente por defecto)

View File

@@ -49,7 +49,7 @@ class Texture {
void setAlpha(Uint8 alpha); // Establece el alpha para la modulación
// --- Paletas ---
void addPaletteFromGifFile(const std::string &path); // Añade una paleta a la lista
void addPaletteFromGifFile(const std::string &path, bool quiet = false); // Añade una paleta a la lista
void addPaletteFromPalFile(const std::string &path); // Añade una paleta a la lista
void setPaletteColor(int palette, int index, Uint32 color); // Establece un color de la paleta
void setPalette(size_t palette); // Cambia la paleta de la textura
@@ -76,8 +76,8 @@ class Texture {
// --- Métodos internos ---
auto loadSurface(const std::string &file_path) -> std::shared_ptr<Surface>; // Crea una surface desde un fichero .gif
void flipSurface(); // Vuelca la surface en la textura
static auto loadPaletteFromFile(const std::string &file_path) -> Palette; // Carga una paleta desde un fichero
static auto loadPaletteFromFile(const std::string &file_path, bool quiet = false) -> Palette; // Carga una paleta desde un fichero
void unloadTexture(); // Libera la memoria de la textura
void unloadSurface(); // Libera la surface actual
static auto readPalFile(const std::string &file_path) -> Palette; // Carga una paleta desde un archivo .pal
static auto readPalFile(const std::string &file_path, bool quiet = false) -> Palette; // Carga una paleta desde un archivo .pal
};

View File

@@ -320,68 +320,6 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", formatted_text.c_str());
}
// Carga el fichero de datos para la demo
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
DemoData dd;
SDL_IOStream *file = nullptr;
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
file = SDL_IOFromConstMem(resource_data.data(), resource_data.size());
} else {
// Fallback a filesystem directo
file = SDL_IOFromFile(file_path.c_str(), "r+b");
}
if (file == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
DemoKeys dk = DemoKeys();
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
dd.push_back(dk);
}
// Cierra el fichero
SDL_CloseIO(file);
return dd;
}
#ifdef RECORDING
// Guarda el fichero de datos para la demo
bool saveDemoFile(const std::string &file_path, const DemoData &dd) {
auto success = true;
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
if (file) {
// Guarda los datos
for (const auto &data : dd) {
if (SDL_RWwrite(file, &data, sizeof(DemoKeys), 1) != 1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str());
success = false;
break;
}
}
if (success) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str());
}
// Cierra el fichero
SDL_CloseIO(file);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
success = false;
}
return success;
}
#endif // RECORDING
// Obtiene el nombre de un fichero a partir de una ruta completa
auto getFileName(const std::string &path) -> std::string {

View File

@@ -10,7 +10,6 @@
// --- Constantes ---
constexpr int BLOCK = 8;
constexpr int TOTAL_DEMO_DATA = 2000;
// --- Estructuras ---
struct Overrides {
@@ -32,44 +31,6 @@ struct Circle {
r(radius) {}
};
struct DemoKeys {
Uint8 left;
Uint8 right;
Uint8 no_input;
Uint8 fire;
Uint8 fire_left;
Uint8 fire_right;
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
: left(l),
right(r),
no_input(ni),
fire(f),
fire_left(fl),
fire_right(fr) {}
};
// --- Tipos ---
using DemoData = std::vector<DemoKeys>;
struct Demo {
bool enabled; // Indica si está activo el modo demo
bool recording; // Indica si está activado el modo para grabar la demo
int counter; // Contador para el modo demo
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
Demo()
: enabled(false),
recording(false),
counter(0) {}
Demo(bool e, bool r, int c, const DemoKeys &k, const std::vector<DemoData> &d)
: enabled(e),
recording(r),
counter(c),
keys(k),
data(d) {}
};
struct Zone {
SDL_FRect rect; // Rectangulo que define la zona
@@ -88,21 +49,21 @@ extern Overrides overrides; // Configuración global de overrides
// Colisiones y geometría
auto distanceSquared(int x1, int y1, int x2, int y2) -> double;
auto getCollisionPoint(const Circle &a, const Circle &b) -> SDL_FPoint;
auto checkCollision(const Circle &a, const Circle &b) -> bool;
auto checkCollision(const Circle &a, const SDL_FRect &b) -> bool;
auto checkCollision(const SDL_FRect &a, const SDL_FRect &b) -> bool;
auto checkCollision(const SDL_FPoint &p, const SDL_FRect &r) -> bool;
auto getCollisionPoint(const Circle& a, const Circle& b) -> SDL_FPoint;
auto checkCollision(const Circle& a, const Circle& b) -> bool;
auto checkCollision(const Circle& a, const SDL_FRect& b) -> bool;
auto checkCollision(const SDL_FRect& a, const SDL_FRect& b) -> bool;
auto checkCollision(const SDL_FPoint& p, const SDL_FRect& r) -> bool;
// Conversión y manipulación de cadenas
auto stringToBool(const std::string &str) -> bool;
auto stringToBool(const std::string& str) -> bool;
auto boolToString(bool value) -> std::string;
auto boolToOnOff(bool value) -> std::string;
auto toLower(const std::string &str) -> std::string;
auto trim(const std::string &str) -> std::string;
auto toLower(const std::string& str) -> std::string;
auto trim(const std::string& str) -> std::string;
// Dibujo
void drawCircle(SDL_Renderer *renderer, int32_t center_x, int32_t center_y, int32_t radius);
void drawCircle(SDL_Renderer* renderer, int32_t center_x, int32_t center_y, int32_t radius);
// Funciones de suavizado (easing)
auto easeOutQuint(double time) -> double;
@@ -123,17 +84,11 @@ auto easeOutCubic(double time) -> double;
auto easeInCubic(double time) -> double;
// Utilidades varias
auto stringInVector(const std::vector<std::string> &vec, const std::string &str) -> bool; // Comprueba si un vector contiene una cadena
void printWithDots(const std::string &text1, const std::string &text2, const std::string &text3); // Imprime una línea con puntos
auto truncateWithEllipsis(const std::string &input, size_t length) -> std::string; // Trunca un string y le añade puntos suspensivos
auto stringInVector(const std::vector<std::string>& vec, const std::string& str) -> bool; // Comprueba si un vector contiene una cadena
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3); // Imprime una línea con puntos
auto truncateWithEllipsis(const std::string& input, size_t length) -> std::string; // Trunca un string y le añade puntos suspensivos
// Demo
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData;
#ifdef RECORDING
bool saveDemoFile(const std::string &file_path, const DemoData &dd);
#endif
// Ficheros y rutas
auto getFileName(const std::string &path) -> std::string; // Obtiene el nombre de un fichero a partir de una ruta
auto getPath(const std::string &full_path) -> std::string; // Obtiene la ruta eliminando el nombre del fichero
auto getFileName(const std::string& path) -> std::string; // Obtiene el nombre de un fichero a partir de una ruta
auto getPath(const std::string& full_path) -> std::string; // Obtiene la ruta eliminando el nombre del fichero