Modificada la estructura on es guarden els datos de la demo

This commit is contained in:
2024-10-20 20:43:03 +02:00
parent 3bf61fc758
commit b263e0c4be
10 changed files with 183 additions and 208 deletions

View File

@@ -55,11 +55,11 @@ Game::Game(int player_id, int current_stage, bool demo)
difficulty_ = options.game.difficulty;
// Crea los objetos
Scoreboard::init(renderer_);
Scoreboard::init();
scoreboard_ = Scoreboard::get();
fade_ = std::make_unique<Fade>();
background_ = std::make_unique<Background>(renderer_);
background_ = std::make_unique<Background>();
explosions_ = std::make_unique<Explosions>();
balloon_formations_ = std::make_unique<BalloonFormations>();
@@ -69,10 +69,10 @@ Game::Game(int player_id, int current_stage, bool demo)
// Inicializa los vectores con los datos para la demo
if (demo_.enabled)
{ // Aleatoriza la asignación del fichero
const auto index1 = rand() % 2;
const auto index2 = (index1 + 1) % 2;
loadDemoFile(asset_->get("demo1.bin"), &this->demo_.data_file[index1]);
loadDemoFile(asset_->get("demo2.bin"), &this->demo_.data_file[index2]);
const std::string demo1 = rand() % 2 == 0 ? "demo1.bin" : "demo2.bin";
const std::string demo2 = (demo1 == "demo1.bin") ? "demo2.bin" : "demo1.bin";
demo_.data.emplace_back(loadDemoDataFromFile(asset_->get(demo1)));
demo_.data.emplace_back(loadDemoDataFromFile(asset_->get(demo2)));
}
background_->setPos(param.game.play_area.rect);
@@ -98,7 +98,7 @@ Game::~Game()
manager->saveToFile(asset_->get("score.bin"));
}
#ifdef RECORDING
saveDemoFile(asset->get("demo1.bin"));
saveDemoFile(Asset::get()->get("demo1.bin"));
#endif
// Elimina todos los objetos contenidos en vectores
@@ -115,9 +115,6 @@ Game::~Game()
// Inicializa las variables necesarias para la sección 'Game'
void Game::init(int player_id)
{
ticks_ = 0;
ticks_speed_ = 15;
// Elimina qualquier jugador que hubiese antes de crear los nuevos
players_.clear();
@@ -192,6 +189,7 @@ void Game::init(int player_id)
scoreboard_->setMode(SCOREBOARD_CENTER_PANEL, ScoreboardMode::STAGE_INFO);
// Resto de variables
ticks_ = 0;
hi_score_.score = options.game.hi_score_table[0].score;
hi_score_.name = options.game.hi_score_table[0].name;
paused_ = false;
@@ -203,8 +201,8 @@ void Game::init(int player_id)
menace_current_ = 0;
menace_threshold_ = 0;
hi_score_achieved_ = false;
stage_bitmap_counter_ = STAGE_COUNTER;
game_over_counter_ = GAME_OVER_COUNTER;
stage_bitmap_counter_ = STAGE_COUNTER_;
game_over_counter_ = GAME_OVER_COUNTER_;
time_stopped_ = false;
time_stopped_counter_ = 0;
counter_ = 0;
@@ -214,13 +212,13 @@ void Game::init(int player_id)
helper_.need_coffee = false;
helper_.need_coffee_machine = false;
helper_.need_power_ball = false;
helper_.counter = HELP_COUNTER;
helper_.item_disk_odds = ITEM_POINTS_1_DISK_ODDS;
helper_.item_gavina_odds = ITEM_POINTS_2_GAVINA_ODDS;
helper_.item_pacmar_odds = ITEM_POINTS_3_PACMAR_ODDS;
helper_.item_clock_odds = ITEM_CLOCK_ODDS;
helper_.item_coffee_odds = ITEM_COFFEE_ODDS;
helper_.item_coffee_machine_odds = ITEM_COFFEE_MACHINE_ODDS;
helper_.counter = HELP_COUNTER_;
helper_.item_disk_odds = ITEM_POINTS_1_DISK_ODDS_;
helper_.item_gavina_odds = ITEM_POINTS_2_GAVINA_ODDS_;
helper_.item_pacmar_odds = ITEM_POINTS_3_PACMAR_ODDS_;
helper_.item_clock_odds = ITEM_CLOCK_ODDS_;
helper_.item_coffee_odds = ITEM_COFFEE_ODDS_;
helper_.item_coffee_machine_odds = ITEM_COFFEE_MACHINE_ODDS_;
power_ball_enabled_ = false;
power_ball_counter_ = 0;
coffee_machine_enabled_ = false;
@@ -282,7 +280,7 @@ void Game::init(int player_id)
// Modo grabar demo
#ifdef RECORDING
demo.recording = true;
demo_.recording = true;
#else
demo_.recording = false;
#endif
@@ -422,10 +420,11 @@ void Game::unloadMedia()
}
// Carga el fichero de datos para la demo
bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTAL_DEMO_DATA])
DemoData Game::loadDemoDataFromFile(const std::string &file_path)
{
DemoData dd;
// Indicador de éxito en la carga
auto success = true;
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
auto file = SDL_RWFromFile(file_path.c_str(), "r+b");
if (!file)
@@ -443,14 +442,8 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
// Inicializas los datos y los guarda en el fichero
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
{
DemoKeys dk;
dk.left = 0;
dk.right = 0;
dk.no_input = 0;
dk.fire = 0;
dk.fire_left = 0;
dk.fire_right = 0;
(*data_file)[i] = dk;
DemoKeys dk = DemoKeys();
dd.push_back(dk);
SDL_RWwrite(file, &dk, sizeof(DemoKeys), 1);
}
@@ -460,7 +453,6 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
else
{ // Si no puede crear el fichero
std::cout << "Error: Unable to create file " << file_name.c_str() << std::endl;
success = false;
}
}
// El fichero existe
@@ -472,16 +464,16 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
{
DemoKeys tmp;
SDL_RWread(file, &tmp, sizeof(DemoKeys), 1);
(*data_file)[i] = tmp;
DemoKeys dk = DemoKeys();
SDL_RWread(file, &dk, sizeof(DemoKeys), 1);
dd.push_back(dk);
}
// Cierra el fichero
SDL_RWclose(file);
}
return success;
return dd;
}
#ifdef RECORDING
@@ -497,7 +489,7 @@ bool Game::saveDemoFile(const std::string &file_path)
// Guarda los datos
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
{
SDL_RWwrite(file, &demo.dataFile[0][i], sizeof(DemoKeys), 1);
SDL_RWwrite(file, &demo_.data[0][i], sizeof(DemoKeys), 1);
}
std::cout << "Writing file " << file_name.c_str() << std::endl;
@@ -669,7 +661,7 @@ void Game::updateStage()
}
// Incrementa el contador del bitmap que aparece mostrando el cambio de fase
if (stage_bitmap_counter_ < STAGE_COUNTER)
if (stage_bitmap_counter_ < STAGE_COUNTER_)
{
stage_bitmap_counter_++;
}
@@ -1195,8 +1187,6 @@ void Game::renderItems()
// Devuelve un item al azar y luego segun sus probabilidades
ItemType Game::dropItem()
{
return ItemType::COFFEE_MACHINE;
const auto lucky_number = rand() % 100;
const auto item = rand() % 6;
@@ -1233,7 +1223,7 @@ ItemType Game::dropItem()
case 4:
if (lucky_number < helper_.item_coffee_odds)
{
helper_.item_coffee_odds = ITEM_COFFEE_ODDS;
helper_.item_coffee_odds = ITEM_COFFEE_ODDS_;
return ItemType::COFFEE;
}
else
@@ -1248,7 +1238,7 @@ ItemType Game::dropItem()
case 5:
if (lucky_number < helper_.item_coffee_machine_odds)
{
helper_.item_coffee_machine_odds = ITEM_COFFEE_MACHINE_ODDS;
helper_.item_coffee_machine_odds = ITEM_COFFEE_MACHINE_ODDS_;
if (!coffee_machine_enabled_ && helper_.need_coffee_machine)
{
return ItemType::COFFEE_MACHINE;
@@ -1447,7 +1437,7 @@ void Game::updateTimeStoppedCounter()
if (time_stopped_counter_ > 0)
{
time_stopped_counter_--;
stopAllBalloons(TIME_STOPPED_COUNTER);
stopAllBalloons(TIME_STOPPED_COUNTER_);
}
else
{
@@ -1469,7 +1459,7 @@ void Game::updateBalloonDeployCounter()
void Game::update()
{
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
if (SDL_GetTicks() - ticks_ > ticks_speed_)
if (SDL_GetTicks() - ticks_ > TICKS_SPEED_)
{
// Actualiza el contador de ticks
ticks_ = SDL_GetTicks();
@@ -1505,9 +1495,9 @@ void Game::update()
checkInput();
// Incrementa el contador de la demo
if (demo.counter < TOTAL_DEMO_DATA)
if (demo_.counter < TOTAL_DEMO_DATA)
{
demo.counter++;
demo_.counter++;
}
// Si se ha llenado el vector con datos, sale del programa
@@ -1695,7 +1685,7 @@ void Game::updateMenace()
void Game::renderMessages()
{
// GetReady
if (counter_ < STAGE_COUNTER && !demo_.enabled)
if (counter_ < STAGE_COUNTER_ && !demo_.enabled)
{
text_nokia2_big_->write((int)get_ready_bitmap_path_[counter_], param.game.play_area.center_y - 8, lang::getText(75), -2);
}
@@ -1725,7 +1715,7 @@ void Game::renderMessages()
}
// STAGE NUMBER
if (stage_bitmap_counter_ < STAGE_COUNTER)
if (stage_bitmap_counter_ < STAGE_COUNTER_)
{
const auto stage_number = balloon_formations_->getStage(current_stage_).number;
std::string text;
@@ -1755,9 +1745,9 @@ void Game::renderMessages()
// Habilita el efecto del item de detener el tiempo
void Game::enableTimeStopItem()
{
stopAllBalloons(TIME_STOPPED_COUNTER);
stopAllBalloons(TIME_STOPPED_COUNTER_);
setTimeStopped(true);
incTimeStoppedCounter(TIME_STOPPED_COUNTER);
incTimeStoppedCounter(TIME_STOPPED_COUNTER_);
if (JA_GetMusicState() == JA_MUSIC_PLAYING && !demo_.enabled)
{
JA_PauseMusic();
@@ -1828,8 +1818,8 @@ void Game::initPaths()
}
// Letrero de STAGE #
constexpr auto first_part = STAGE_COUNTER / 4; // 50
constexpr auto second_part = first_part * 3; // 150
constexpr auto first_part = STAGE_COUNTER_ / 4; // 50
constexpr auto second_part = first_part * 3; // 150
const auto center_point = param.game.play_area.center_y - (BLOCK * 2);
const auto distance = (param.game.play_area.rect.h) - (param.game.play_area.center_y - 16);
@@ -1843,7 +1833,7 @@ void Game::initPaths()
stage_bitmap_path_[i] = (int)center_point;
}
for (int i = second_part; i < STAGE_COUNTER; ++i)
for (int i = second_part; i < STAGE_COUNTER_; ++i)
{
stage_bitmap_path_[i] = (sin[(int)(((i - 149) * 1.8f) + 90)] * (center_point + 17) - 17);
}
@@ -1870,7 +1860,7 @@ void Game::initPaths()
get_ready_bitmap_path_[i] = (int)finish1;
}
for (int i = second_part; i < STAGE_COUNTER; ++i)
for (int i = second_part; i < STAGE_COUNTER_; ++i)
{
get_ready_bitmap_path_[i] = sin[(int)((i - second_part) * 1.8f)];
get_ready_bitmap_path_[i] *= distance2;
@@ -1886,7 +1876,7 @@ void Game::updateGameCompleted()
game_completed_counter_++;
}
if (game_completed_counter_ == GAME_COMPLETED_END)
if (game_completed_counter_ == GAME_COMPLETED_END_)
{
section::name = section::Name::TITLE;
section::options = section::Options::TITLE_1;
@@ -2034,20 +2024,6 @@ void Game::checkEvents()
break;
}
// Ralentiza mucho la lógica
case SDLK_4:
{
ticks_speed_ *= 10;
break;
}
// Acelera mucho la lógica
case SDLK_5:
{
ticks_speed_ /= 10;
break;
}
default:
break;
}
@@ -2247,7 +2223,7 @@ void Game::handleDemoMode()
// Incluye movimientos (izquierda, derecha, sin movimiento) y disparos automáticos.
void Game::handleDemoPlayerInput(const std::shared_ptr<Player> &player, int index)
{
const auto &demoData = demo_.data_file[index][demo_.counter];
const auto &demoData = demo_.data[index][demo_.counter];
if (demoData.left == 1)
player->setInput(InputType::LEFT);
@@ -2307,21 +2283,21 @@ void Game::handleNormalPlayerInput(const std::shared_ptr<Player> &player)
{
player->setInput(InputType::LEFT);
#ifdef RECORDING
demo.keys.left = 1;
demo_.keys.left = 1;
#endif
}
else if (input_->checkInput(InputType::RIGHT, INPUT_ALLOW_REPEAT, options.controller[controllerIndex].device_type, options.controller[controllerIndex].index))
{
player->setInput(InputType::RIGHT);
#ifdef RECORDING
demo.keys.right = 1;
demo_.keys.right = 1;
#endif
}
else
{
player->setInput(InputType::NONE);
#ifdef RECORDING
demo.keys.no_input = 1;
demo_.keys.no_input = 1;
#endif
}
@@ -2335,21 +2311,21 @@ void Game::handleFireInputs(const std::shared_ptr<Player> &player, bool autofire
{
handleFireInput(player, BulletType::UP);
#ifdef RECORDING
demo.keys.fire = 1;
demo_.keys.fire = 1;
#endif
}
else if (input_->checkInput(InputType::FIRE_LEFT, autofire, options.controller[controllerIndex].device_type, options.controller[controllerIndex].index))
{
handleFireInput(player, BulletType::LEFT);
#ifdef RECORDING
demo.keys.fire_left = 1;
demo_.keys.fire_left = 1;
#endif
}
else if (input_->checkInput(InputType::FIRE_RIGHT, autofire, options.controller[controllerIndex].device_type, options.controller[controllerIndex].index))
{
handleFireInput(player, BulletType::RIGHT);
#ifdef RECORDING
demo.keys.fire_right = 1;
demo_.keys.fire_right = 1;
#endif
}
}