#include "const.h" #include "map.h" // Constructor Map::Map(SDL_Renderer *renderer, std::string file, Asset *asset) { this->asset = asset; this->renderer = renderer; texture_tile = new LTexture(); texture_actor = new LTexture(); texture_bg = new LTexture(); loadTextureFromFile(texture_tile, asset->get("tiles_volcano.png"), renderer); loadTextureFromFile(texture_actor, asset->get("actors.png"), renderer); loadTextureFromFile(texture_bg, asset->get("bg_surface.png"), renderer); sprite_tile = new AnimatedSprite(texture_tile, renderer); sprite_actor = new AnimatedSprite(texture_actor, renderer); const SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; background = new Sprite(rect, texture_bg, renderer); music = JA_LoadMusic(asset->get("music_surface.ogg").c_str()); src_rect = {0, 0, 0, 0}; dst_rect = {0, 0, 0, 0}; w = 0; h = 0; room = 0; loadFromFile(file); } // Destructor Map::~Map() { texture_tile->unload(); delete texture_tile; texture_tile = nullptr; texture_actor->unload(); delete texture_actor; texture_actor = nullptr; texture_bg->unload(); delete texture_bg; texture_bg = nullptr; delete sprite_tile; sprite_tile = nullptr; delete sprite_actor; sprite_actor = nullptr; delete background; background = nullptr; delete[] tile; delete[] actor; JA_DeleteMusic(music); } // Carga el mapa a partir de un fichero void Map::loadFromFile(std::string path) { std::string filename = path.substr(path.find_last_of("\\/") + 1); SDL_RWops *file = SDL_RWFromFile(path.c_str(), "r+b"); Uint8 *w; Uint8 *h; if (file == NULL) { printf("Warning: Unable to open %s file\n", filename.c_str()); } else { printf("Reading file %s\n", filename.c_str()); SDL_RWread(file, &w, sizeof(Uint8), 1); SDL_RWread(file, &h, sizeof(Uint8), 1); long size = (*w) * (*h); tile = new Uint8[size]; actor = new Uint8[size]; for (long i = 0; i < size; i++) SDL_RWread(file, &tile[i], sizeof(Uint8), 1); for (long i = 0; i < size; i++) SDL_RWread(file, &actor[i], sizeof(Uint8), 1); SDL_RWclose(file); } } // Actualiza todas las variables void Map::update() { background->setPosX(0); background->setPosY(0); } // Dibuja el objeto void Map::render() { background->render(); } // Obtiene el valor del tile de la habitación actual Uint8 Map::getTile(int x, int y) { const long room_x = (room % 12) * ROOM_WIDTH_IN_TILES; const long room_y = (room / 12) * ROOM_HEIGHT_IN_TILES; return tile[(room_x + x) + (room_y + y) * w]; } // Obtiene el tipo de tile segun su ubicación en la textura Uint8 Map::readMapTile(Uint8 x, Uint8 y) { const Uint8 tile = getTile(x, y); if (tile >= 0 && tile <= 63) { return TILE_BACKGROUND; } else if (tile >= 64 && tile <= 143) { return TILE_PLATFORM; } else if (tile >= 144 && tile <= 175) { return TILE_TRAVESABLE_PLATFORM; } else if (tile >= 176 && tile <= 207) { return TILE_KILLING_PLATFORM; } else if (tile >= 208 && tile <= 255) { return TILE_ACTOR; } else { return 0; } } // Obtiene el valor del actor en esa ubicación Uint8 Map::getActor(Uint8 x, Uint8 y) { long room_x = (room % 12) * ROOM_WIDTH_IN_TILES; long room_y = (room / 12) * ROOM_HEIGHT_IN_TILES; return actor[(room_x + x) + (room_y + y) * w]; } // Establece el valor del actor en esa ubicación void Map::setActor(Uint8 x, Uint8 y, Uint8 valor) { long room_x = (room % 12) * ROOM_WIDTH_IN_TILES; long room_y = (room / 12) * ROOM_HEIGHT_IN_TILES; actor[(room_x + x) + (room_y + y) * w] = valor; } // Carga las texturas del mapa en función de la zona void Map::setMapGFX(Uint8 zone) { switch (zone) { case ZONE_SURFACE: loadTextureFromFile(texture_tile, asset->get("tiles_surface.png").c_str(), renderer); loadTextureFromFile(texture_bg, asset->get("bg_surface.png").c_str(), renderer); break; case ZONE_VOLCANO: loadTextureFromFile(texture_tile, asset->get("tiles_volcano.png").c_str(), renderer); loadTextureFromFile(texture_bg, asset->get("bg_surface.png").c_str(), renderer); break; } } // Carga las musica del juego en función de la zona void Map::setMapMusic(Uint8 zone) { switch (zone) { case ZONE_SURFACE: music = JA_LoadMusic(asset->get("music_surface.ogg").c_str()); break; case ZONE_VOLCANO: music = JA_LoadMusic(asset->get("music_volcano.ogg").c_str()); break; } } // Comprueba si se ha cambiado de zona bool Map::checkZoneChange(int room) { Uint8 _zone = 0; if ((room >= 0) && (room <= 23)) _zone = ZONE_SURFACE; else if ((room >= 24) && (room <= 255)) _zone = ZONE_VOLCANO; if (_zone == zone) return false; else return true; } // Cambia la zona del mapa void Map::setZone(int room) { if ((room >= 0) && (room <= 23)) zone = ZONE_SURFACE; else if ((room >= 24) && (room <= 255)) zone = ZONE_VOLCANO; setMapGFX(zone); setMapMusic(zone); JA_PlayMusic(music, -1); }