repensada la forma d'asignar fitxers de demo als jugadors

refets els fitxers de demo i afegit un tercer fitxer
This commit is contained in:
2025-09-29 14:00:10 +02:00
parent c16fc1bae5
commit c98cb0d29f
5 changed files with 110 additions and 84 deletions

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,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

@@ -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)
@@ -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

View File

@@ -316,6 +316,11 @@ 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& {
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);
}
@@ -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));
}
}
@@ -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));
}
}

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
@@ -1292,13 +1293,10 @@ 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_) {
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;
}
}
@@ -1530,18 +1528,31 @@ void Game::initDemo(Player::Id player_id) {
setState(State::PLAYING);
#ifndef RECORDING
// Solo en modo reproducción: 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));
// 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<int, NUM_STAGES> STAGES = {0, 3, 5};
stage_manager_->jumpToStage(STAGES.at(STAGE));
// Activa o no al otro jugador
if (rand() % 3 != 0) {