Room* RoomLoader::LoadRoom( const std::string& aRoomName ) { Tmx::Map map; map.ParseFile(aRoomName); if (map.HasError()) { printf("error code: %d\n", map.GetErrorCode()); printf("error text: %s\n", map.GetErrorText().c_str()); return NULL; } Room* room = new Room(GetLayer(map, "background"), GetLayer(map, "middle"), GetLayer(map, "foreground")); std::vector<Entity*> entities = GetEntities(map, "entities"); float2 topleft(0,0); float2 bottomright((map.GetWidth()) * map.GetTileWidth(), (map.GetHeight()) * map.GetTileHeight()); std::vector<std::pair<float2, float2>> rects = GetCameraRects(map, "camera"); room->setCameraRect(topleft, bottomright); room->setCamera(new Camera(rects)); for (unsigned int i = 0; i < entities.size(); i++) { room->addEntity(entities[i]); } return room; }
Room *RoomLoader::loadRoom(const std::string& filename) { TileMap* tileMap = loadTileMap(filename); std::cout << "Loading room " + filename + "..."; std::vector<std::string> data = tokenize(loadFile(filename), "\n", true); std::vector<std::string> tileSetInfo = tokenize(data[data.size()-1], " "); if (tileSetInfo.size() == 0) { throw DB_EXCEPTION("Tilset info is missing in file " + filename); } std::string tileSetFileName = "graphics/" + tileSetInfo[0]; int numTiles = fromString<int>(tileSetInfo[1]); Room* room = new Room(tileMap, new Animation(tileSetFileName, numTiles)); int width = 0; int height = 0; int row; int col; // Load entities int x = 0; int y = 0; for (row = 0; row < tileMap->getHeight(); row++) { for (col = 0; col < tileMap->getWidth(); col++) { char c = data[row].at(col); // Ignore solidities. if (c != '.' && c != '#') { Entity* entity = createEntity(c, x * TileMap::TILE_SIZE, y * TileMap::TILE_SIZE, Random::get()); room->addEntity(entity); } x++; } x = 0; y++; } std::cout << " Done!" << std::endl; return room; }
Room* RoomLoader::LoadRoom( int x, int y ) { const Tmx::ObjectGroup *roomObjectGroup = map.GetObjectGroup(0); int roomX = -1; int roomY = -1; int roomW = -1; int roomH = -1; for (int i = 0; i < roomObjectGroup->GetNumObjects(); i++) { const Tmx::Object *roomObject = roomObjectGroup->GetObject(i); int rX = roomObject->GetX() / 10; int rY = roomObject->GetY() / 10; int rW = roomObject->GetWidth() / 10; int rH = roomObject->GetHeight() / 10; if (x >= rX && y >= rY && x < rX + rW && y < rY + rH) { roomX = rX; roomY = rY; roomW = rW; roomH = rH; break; } } if (roomX < 0) { //throw std::exception("Found no room at these coordinates"); throw std::runtime_error("Found no room at these coordinates"); } Room* room = new Room(GetLayer(map, roomX, roomY, roomW, roomH, "tiles"), roomX, roomY); std::vector<Entity*> entities = GetEntities(map, roomX, roomY, roomW, roomH, "entities"); room->setCamera(new Camera()); for (unsigned int i = 0; i < entities.size(); i++) { room->addEntity(entities[i]); } return room; }