Precàrrega dels fitxers amb dades per al mode demostració
This commit is contained in:
@@ -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 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)));
|
||||
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));
|
||||
}
|
||||
|
||||
background_->setPos(param.game.play_area.rect);
|
||||
@@ -98,7 +98,7 @@ Game::~Game()
|
||||
manager->saveToFile(asset_->get("score.bin"));
|
||||
}
|
||||
#ifdef RECORDING
|
||||
saveDemoFile(Asset::get()->get("demo1.bin"));
|
||||
saveDemoFile(Asset::get()->get("demo1.bin"), demo_.data.at(0));
|
||||
#endif
|
||||
|
||||
// Elimina todos los objetos contenidos en vectores
|
||||
@@ -419,93 +419,6 @@ void Game::unloadMedia()
|
||||
item_animations_.clear();
|
||||
}
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
DemoData Game::loadDemoDataFromFile(const std::string &file_path)
|
||||
{
|
||||
DemoData dd;
|
||||
|
||||
// Indicador de éxito en la carga
|
||||
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)
|
||||
{ // El fichero no existe
|
||||
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
|
||||
|
||||
// Creamos el fichero para escritura
|
||||
file = SDL_RWFromFile(file_path.c_str(), "w+b");
|
||||
|
||||
// Si ha creado el fichero
|
||||
if (file)
|
||||
{
|
||||
std::cout << "New file (" << file_name.c_str() << ") created!" << std::endl;
|
||||
|
||||
// Inicializas los datos y los guarda en el fichero
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
||||
{
|
||||
DemoKeys dk = DemoKeys();
|
||||
dd.push_back(dk);
|
||||
SDL_RWwrite(file, &dk, sizeof(DemoKeys), 1);
|
||||
}
|
||||
|
||||
// Cerramos el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{ // Si no puede crear el fichero
|
||||
std::cout << "Error: Unable to create file " << file_name.c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
// El fichero existe
|
||||
else
|
||||
{
|
||||
// Mensaje de proceder a la carga de los datos
|
||||
std::cout << "Reading file: " << file_name.c_str() << std::endl;
|
||||
|
||||
// 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_RWread(file, &dk, sizeof(DemoKeys), 1);
|
||||
dd.push_back(dk);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
return dd;
|
||||
}
|
||||
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool Game::saveDemoFile(const std::string &file_path)
|
||||
{
|
||||
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(), "w+b");
|
||||
if (file)
|
||||
{
|
||||
// Guarda los datos
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
||||
{
|
||||
SDL_RWwrite(file, &demo_.data[0][i], sizeof(DemoKeys), 1);
|
||||
}
|
||||
|
||||
std::cout << "Writing file " << file_name.c_str() << std::endl;
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Error: Unable to save " << file_name.c_str() << " file! " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
#endif // RECORDING
|
||||
|
||||
// Crea una formación de enemigos
|
||||
void Game::deployBalloonFormation()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user