#include "game.h" #include // for SDL_BLENDMODE_BLEND #include // for SDL_GetError #include // for SDL_PIXELFORMAT_RGBA8888 #include // for SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_... #include // for SDL_GetTicks #include // for basic_ostream, operator<<, cout, endl #include // for vector #include "asset.h" // for Asset #include "cheevos.h" // for Cheevos #include "debug.h" // for Debug #include "defines.h" // for BLOCK, PLAY_AREA_HEIGHT, GAMECANVAS_... #include "global_events.h" // for check #include "global_inputs.h" // for check #include "input.h" // for Input, InputAction, REPEAT_FALSE #include "item_tracker.h" // for ItemTracker #include "jail_audio.h" // for JA_PauseMusic, JA_PlaySound, JA_Resu... #include "notifier.h" // for Notifier, NotificationText #include "options.h" // for Options, options, Cheat, OptionsVideo #include "resource.h" // for ResourceRoom, Resource #include "room.h" // for Room, RoomData #include "room_tracker.h" // for RoomTracker #include "scoreboard.h" // for ScoreboardData, Scoreboard #include "screen.h" // for Screen #include "stats.h" // for Stats #include "text.h" // for Text, TEXT_CENTER, TEXT_COLOR #include "utils.h" // for Color, stringToColor, colorAreEqual // Constructor Game::Game(GameMode mode) : board_(std::make_shared(0, 9, 0, true, 0, SDL_GetTicks(), options.cheats.jail_is_open == Cheat::CheatState::ENABLED)), scoreboard_(std::make_shared(board_)), room_tracker_(std::make_shared()), stats_(std::make_shared(Asset::get()->get("stats.csv"), Asset::get()->get("stats_buffer.csv"))), mode_(mode), #ifdef DEBUG current_room_("03.room"), spawn_point_(PlayerSpawn(25 * BLOCK, 13 * BLOCK, 0, 0, 0, PlayerState::STANDING, SDL_FLIP_HORIZONTAL)) #else current_room_("03.room"), spawn_point_(PlayerSpawn(25 * BLOCK, 13 * BLOCK, 0, 0, 0, PlayerState::STANDING, SDL_FLIP_HORIZONTAL)) #endif { #ifdef DEBUG Debug::get()->setEnabled(false); #endif // Crea objetos e inicializa variables ItemTracker::init(); DEMO_init(); room_ = std::make_shared(current_room_, board_); initPlayer(spawn_point_, room_); initStats(); total_items_ = getTotalItems(); createRoomNameTexture(); changeRoom(current_room_); Cheevos::get()->enable(!options.cheats.enabled()); // Deshabilita los logros si hay trucos activados Cheevos::get()->clearUnobtainableState(); options.section.section = (mode_ == GameMode::GAME) ? Section::GAME : Section::DEMO; options.section.subsection = Subsection::NONE; } Game::~Game() { ItemTracker::destroy(); } // Comprueba los eventos de la cola void Game::checkEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { globalEvents::check(event); #ifdef DEBUG checkDebugEvents(event); #endif } } // Comprueba el teclado void Game::checkInput() { if (Input::get()->checkInput(InputAction::TOGGLE_MUSIC, REPEAT_FALSE)) { board_->music = !board_->music; board_->music ? JA_ResumeMusic() : JA_PauseMusic(); Notifier::get()->show({"MUSIC " + std::string(board_->music ? "ENABLED" : "DISABLED")}, NotificationText::CENTER); } else if (Input::get()->checkInput(InputAction::PAUSE, REPEAT_FALSE)) { togglePause(); Notifier::get()->show({std::string(paused_ ? "GAME PAUSED" : "GAME RUNNING")}, NotificationText::CENTER); } globalInputs::check(); } // Bucle para el juego void Game::run() { JA_PlayMusic(Resource::get()->getMusic("game.ogg")); if (!board_->music) { JA_PauseMusic(); } while (options.section.section == Section::GAME || options.section.section == Section::DEMO) { update(); checkEvents(); render(); } JA_StopMusic(); } // Actualiza el juego, las variables, comprueba la entrada, etc. void Game::update() { // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego if (SDL_GetTicks() - ticks_ > GAME_SPEED) { // Actualiza el contador de ticks ticks_ = SDL_GetTicks(); // Comprueba el teclado checkInput(); #ifdef DEBUG Debug::get()->clear(); #endif // Actualiza los objetos room_->update(); if (mode_ == GameMode::GAME) { player_->update(); checkPlayerOnBorder(); checkPlayerAndItems(); checkPlayerAndEnemies(); checkIfPlayerIsAlive(); checkGameOver(); checkEndGame(); checkRestoringJail(); checkSomeCheevos(); } DEMO_checkRoomChange(); scoreboard_->update(); updateBlackScreen(); Screen::get()->update(); #ifdef DEBUG updateDebugInfo(); #endif } } // Pinta los objetos en pantalla void Game::render() { // Prepara para dibujar el frame Screen::get()->start(); // Dibuja los elementos del juego en orden room_->renderMap(); room_->renderEnemies(); room_->renderItems(); if (mode_ == GameMode::GAME) { player_->render(); } renderRoomName(); scoreboard_->render(); renderBlackScreen(); #ifdef DEBUG // Debug info renderDebugInfo(); #endif // Actualiza la pantalla Screen::get()->render(); } #ifdef DEBUG // Pasa la información de debug void Game::updateDebugInfo() { Debug::get()->add("X = " + std::to_string(static_cast(player_->x_)) + ", Y = " + std::to_string(static_cast(player_->y_))); Debug::get()->add("VX = " + std::to_string(player_->vx_).substr(0, 4) + ", VY = " + std::to_string(player_->vy_).substr(0, 4)); Debug::get()->add("STATE = " + std::to_string(static_cast(player_->state_))); } // Pone la información de debug en pantalla void Game::renderDebugInfo() { /* if (!Debug::get()->getEnabled()) { return; } // Borra el marcador SDL_Rect rect = {0, 18 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT}; SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 255); SDL_RenderFillRect(Screen::get()->getRenderer(), &rect); // Pinta la rejilla SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 255, 255, 255, 32); for (int i = 0; i < PLAY_AREA_BOTTOM; i += 8) { // Lineas horizontales SDL_RenderDrawLine(Screen::get()->getRenderer(), 0, i, PLAY_AREA_RIGHT, i); } for (int i = 0; i < PLAY_AREA_RIGHT; i += 8) { // Lineas verticales SDL_RenderDrawLine(Screen::get()->getRenderer(), i, 0, i, PLAY_AREA_BOTTOM - 1); } // Pinta el texto Debug::get()->setPos({1, 18 * 8}); Debug::get()->render(); */ } // Comprueba los eventos void Game::checkDebugEvents(const SDL_Event &event) { if (event.type == SDL_KEYDOWN && event.key.repeat == 0) { switch (event.key.keysym.scancode) { case SDL_SCANCODE_G: Debug::get()->toggleEnabled(); options.cheats.invincible = static_cast(Debug::get()->getEnabled()); board_->music = !Debug::get()->getEnabled(); board_->music ? JA_ResumeMusic() : JA_PauseMusic(); break; case SDL_SCANCODE_R: Resource::get()->reload(); break; case SDL_SCANCODE_W: changeRoom(room_->getRoom(BORDER_TOP)); break; case SDL_SCANCODE_A: changeRoom(room_->getRoom(BORDER_LEFT)); break; case SDL_SCANCODE_S: changeRoom(room_->getRoom(BORDER_BOTTOM)); break; case SDL_SCANCODE_D: changeRoom(room_->getRoom(BORDER_RIGHT)); break; case SDL_SCANCODE_F6: Notifier::get()->show({"ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS"}, NotificationText::LEFT, 2, false, "F6"); break; case SDL_SCANCODE_F7: Notifier::get()->show({"ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS"}, NotificationText::LEFT, 3, false, "F7"); break; case SDL_SCANCODE_F8: Notifier::get()->show({"JAILDESIGNER", "IS LOGGED IN"}, NotificationText::LEFT, 4, false); break; case SDL_SCANCODE_F9: Notifier::get()->show({"JAILDESIGNER", "IS LOGGED IN"}, NotificationText::LEFT, 5, false); break; default: break; } } } #endif // Escribe el nombre de la pantalla void Game::renderRoomName() { // Dibuja la textura con el nombre de la habitación room_name_surface_->render(nullptr, &room_name_rect_); } // Cambia de habitación bool Game::changeRoom(const std::string &room_path) { // En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada if (room_path == "0") { return false; } // Verifica que exista el fichero que se va a cargar if (Asset::get()->get(room_path) != "") { // Crea un objeto habitación nuevo a partir del fichero room_ = std::make_shared(room_path, board_); // Pone el nombre de la habitación en la textura fillRoomNameTexture(); // Pone el color del marcador en función del color del borde de la habitación setScoreBoardColor(); if (room_tracker_->addRoom(room_path)) { // Incrementa el contador de habitaciones visitadas board_->rooms++; options.stats.rooms = board_->rooms; // Actualiza las estadisticas stats_->addVisit(room_->getName()); } // Pasa la nueva habitación al jugador player_->setRoom(room_); // Cambia la habitación actual current_room_ = room_path; return true; } return false; } // Comprueba si el jugador esta en el borde de la pantalla void Game::checkPlayerOnBorder() { if (player_->getOnBorder()) { const std::string roomName = room_->getRoom(player_->getBorder()); if (changeRoom(roomName)) { player_->switchBorders(); spawn_point_ = player_->getSpawnParams(); } } } // Comprueba las colisiones del jugador con los enemigos bool Game::checkPlayerAndEnemies() { const bool death = room_->enemyCollision(player_->getCollider()); if (death) { killPlayer(); } return death; } // Comprueba las colisiones del jugador con los objetos void Game::checkPlayerAndItems() { room_->itemCollision(player_->getCollider()); } // Comprueba si el jugador esta vivo void Game::checkIfPlayerIsAlive() { if (!player_->isAlive()) { killPlayer(); } } // Comprueba si ha terminado la partida void Game::checkGameOver() { if (board_->lives < 0 && black_screen_counter_ > 17) { options.section.section = Section::GAME_OVER; } } // Mata al jugador void Game::killPlayer() { if (options.cheats.invincible == Cheat::CheatState::ENABLED) { return; } // Resta una vida al jugador if (options.cheats.infinite_lives == Cheat::CheatState::DISABLED) { --board_->lives; } // Actualiza las estadisticas stats_->addDeath(room_->getName()); // Invalida el logro de pasarse el juego sin morir Cheevos::get()->setUnobtainable(11); // Sonido JA_PlaySound(Resource::get()->getSound("death.wav")); // Pone la pantalla en negro un tiempo setBlackScreen(); // Crea la nueva habitación y el nuevo jugador room_ = std::make_shared(current_room_, board_); initPlayer(spawn_point_, room_); // Pone los objetos en pausa mientras esta la habitación en negro room_->setPaused(true); player_->setPaused(true); } // Establece la pantalla en negro void Game::setBlackScreen() { black_screen_ = true; } // Actualiza las variables relativas a la pantalla en negro void Game::updateBlackScreen() { if (black_screen_) { black_screen_counter_++; if (black_screen_counter_ > 20) { black_screen_ = false; black_screen_counter_ = 0; player_->setPaused(false); room_->setPaused(false); Screen::get()->setBorderColor(room_->getBorderColor()); } } } // Dibuja la pantalla negra void Game::renderBlackScreen() { if (black_screen_) { Screen::get()->clear(); Screen::get()->setBorderColor(stringToColor("black")); } } // Pone el color del marcador en función del color del borde de la habitación void Game::setScoreBoardColor() { // Obtiene el color del borde const Uint8 colorBorder = room_->getBorderColor(); const bool isBlack = colorBorder == stringToColor("black"); const bool isBrightBlack = colorBorder == stringToColor("bright_black"); // Si el color del borde es negro o negro brillante cambia el texto del marcador a blanco board_->color = isBlack || isBrightBlack ? stringToColor("white") : colorBorder; } // Comprueba si ha finalizado el juego bool Game::checkEndGame() { const bool isOnTheRoom = room_->getName() == "THE JAIL"; // Estar en la habitación que toca const bool haveTheItems = board_->items >= int(total_items_ * 0.9f) || options.cheats.jail_is_open == Cheat::CheatState::ENABLED; // Con mas del 90% de los items recogidos const bool isOnTheDoor = player_->getRect().x <= 128; // Y en la ubicación que toca (En la puerta) if (haveTheItems) { board_->jail_is_open = true; } if (haveTheItems && isOnTheRoom && isOnTheDoor) { // Comprueba los logros de completar el juego checkEndGameCheevos(); options.section.section = Section::ENDING; return true; } return false; } // Obtiene la cantidad total de items que hay en el mapeado del juego int Game::getTotalItems() { int items = 0; auto rooms = Resource::get()->getRooms(); for (const auto &room : rooms) { items += room.room->items.size(); } return items; } // Pone el juego en pausa void Game::togglePause() { paused_ = !paused_; player_->setPaused(paused_); room_->setPaused(paused_); scoreboard_->setPaused(paused_); } // Da vidas al jugador cuando está en la Jail void Game::checkRestoringJail() { if (room_->getName() != "THE JAIL" || board_->lives == 9) { return; } static int counter = 0; if (!paused_) { counter++; } // Incrementa el numero de vidas if (counter == 100) { counter = 0; board_->lives++; JA_PlaySound(Resource::get()->getSound("death.wav")); // Invalida el logro de completar el juego sin entrar a la jail const bool haveTheItems = board_->items >= int(total_items_ * 0.9f); if (!haveTheItems) { Cheevos::get()->setUnobtainable(9); } } } // Inicializa el diccionario de las estadísticas void Game::initStats() { auto rooms = Resource::get()->getRooms(); for (const auto &room : rooms) { stats_->addDictionary(room.room->number, room.room->name); } stats_->init(); } // Crea la textura con el nombre de la habitación void Game::fillRoomNameTexture() { // Pone la textura como destino de renderizado auto previuos_renderer = Screen::get()->getRendererSurface(); Screen::get()->setRendererSurface(room_name_surface_); // Rellena la textura de color room_name_surface_->clear(stringToColor("white")); // Escribe el texto en la textura auto text = Resource::get()->getText("smb2"); text->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, text->getCharacterSize() / 2, room_->getName(), 1, room_->getBGColor()); // Deja el renderizador por defecto Screen::get()->setRendererSurface(previuos_renderer); } // Comprueba algunos logros void Game::checkSomeCheevos() { auto cheevos = Cheevos::get(); // Logros sobre la cantidad de items if (board_->items == total_items_) { cheevos->unlock(4); cheevos->unlock(3); cheevos->unlock(2); cheevos->unlock(1); } else if (board_->items >= total_items_ * 0.75f) { cheevos->unlock(3); cheevos->unlock(2); cheevos->unlock(1); } else if (board_->items >= total_items_ * 0.5f) { cheevos->unlock(2); cheevos->unlock(1); } else if (board_->items >= total_items_ * 0.25f) { cheevos->unlock(1); } // Logros sobre las habitaciones visitadas if (board_->rooms >= 60) { cheevos->unlock(7); cheevos->unlock(6); cheevos->unlock(5); } else if (board_->rooms >= 40) { cheevos->unlock(6); cheevos->unlock(5); } else if (board_->rooms >= 20) { cheevos->unlock(5); } } // Comprueba los logros de completar el juego void Game::checkEndGameCheevos() { auto cheevos = Cheevos::get(); // "Complete the game" cheevos->unlock(8); // "Complete the game without entering the jail" cheevos->unlock(9); // "Complete the game with all items" if (board_->items == total_items_) { cheevos->unlock(10); } // "Complete the game without dying" cheevos->unlock(11); // "Complete the game in under 30 minutes" if (scoreboard_->getMinutes() < 30) { cheevos->unlock(12); } } // Inicializa al jugador void Game::initPlayer(const PlayerSpawn &spawn_point, std::shared_ptr room) { std::string player_texture = options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.gif" : "player.gif"; std::string player_animations = options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.ani" : "player.ani"; const PlayerData player(spawn_point, player_texture, player_animations, room); player_ = std::make_shared(player); } // Crea la textura para poner el nombre de la habitación void Game::createRoomNameTexture() { auto text = Resource::get()->getText("smb2"); room_name_surface_ = std::make_shared(options.game.width, text->getCharacterSize() * 2); // Establece el destino de la textura room_name_rect_ = {0, PLAY_AREA_HEIGHT, options.game.width, text->getCharacterSize() * 2}; } // DEMO MODE: Inicializa las variables para el modo demo void Game::DEMO_init() { if (mode_ == GameMode::DEMO) { demo_ = DemoData(0, 400, 0, {"04.room", "54.room", "20.room", "09.room", "05.room", "11.room", "31.room", "44.room"}); current_room_ = demo_.rooms.front(); } } // DEMO MODE: Comprueba si se ha de cambiar de habitación void Game::DEMO_checkRoomChange() { if (mode_ == GameMode::DEMO) { demo_.counter++; if (demo_.counter == demo_.room_time) { demo_.counter = 0; demo_.room_index++; if (demo_.room_index == (int)demo_.rooms.size()) { options.section.section = Section::LOGO; options.section.subsection = Subsection::LOGO_TO_TITLE; } else { changeRoom(demo_.rooms[demo_.room_index]); } } } }