/* * * TileMapComponent.h * * Created on: Mar 21, 2020 * Author: ayoungblood */ #ifndef SRC_ECS_TILEMAPCOMPONENT_H_ #define SRC_ECS_TILEMAPCOMPONENT_H_ #include "ECS.h" #include #include "../assetmgr/AssetManager.h" #include #include #include #include #include "../tileson/tileson.hpp" class TileMapComponent : public Component { public: SDL_Texture* texture; SDL_Rect srcRect, destRect; //tmxparser::TmxMap map; tson::Tileson t; std::unique_ptr map; int globalScale; std::vector tileSet; std::vector destRects; std::vector> initialPositions; int tileSetTotal; std::vector> colliders; int tilesWide; int tilesHigh; int tileWidth; TileMapComponent() = default; ~TileMapComponent() { SDL_DestroyTexture(texture); } TileMapComponent(std::string mapPath, int gScale, int offsetX, int offsetY) { // *********************************************************************************** // TILESON ~~~~~~~~~~~ //tson::Tileson t; const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath); map = t.parse(jsonPath); std::cout << jsonPath << std::endl; if(map->getStatus() == tson::ParseStatus::OK) { printf("Tileson successfully parsed the tilemap\n"); std::cout << map->getStatusMessage() << std::endl; //map = loadedMap; tson::Tileset *tileset = map->getTileset("br-tiles"); //tson::Tileset *collisionTS = map->getTileset("Collision"); //std::cout << "Image Path: " << tileset->getImage() << std::endl; std::string fullPath = tileset->getImage(); size_t charPos = fullPath.find("assets"); fullPath.erase(0,charPos); //std::cout << "Updated path: " << fullPath << std::endl; tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is a Layer std::string texName = tileLayer->getName(); std::cout << "texName: " << texName << std::endl; std::cout << "fullPath: " << fullPath << std::endl; //std::string texturePath = fullPath; Game::assets->AddTexture(texName, fullPath.c_str()); setTex(texName); globalScale = gScale; tson::Layer *collisionLayer = map->getLayer("Collision"); // std::cout << "Collider 0,2: " << collisionLayer->getTileData(0,2)->getId() << std::endl; // std::cout << "Collider 1,2: " << collisionLayer->getTileData(1,2)->getId() << std::endl; // std::cout << "Collider 2,2: " << collisionLayer->getTileData(2,2)->getId() << std::endl; // if (collisionLayer->getTileData(3,2)){ // std::cout << "Collider 3,2: " << collisionLayer->getTileData(3,2)->getId() << std::endl; // } else { // printf("null found instead of collider \n"); // } //colliders.resize(map.height, std::vector(map.width, 0)); tilesWide = map->getSize().x; // printf("tilesWide: %d\n",tilesWide); tilesHigh = map->getSize().y; // printf("tilesHigh: %d\n",tilesHigh); tileWidth = map->getTileSize().x; // printf("tileSizeWidth: %d\n",tileWidth); Game::levelMap.w = tilesWide*tileWidth*globalScale; Game::levelMap.h = tilesHigh*tileWidth*globalScale; // =========== Setup Tile Set =========== tileSetTotal = tileset->getTileCount(); tileSet.resize(tileSetTotal); int tileSetCols = tileset->getColumns(); // std::cout << "tileSet Cols: " << tileSetCols << std::endl; int tileSetRows = tileSetTotal/tileSetCols; // std::cout << "tileSet Rows: " << tileSetRows << std::endl; // std::cout << "TileSetTotal: " << tileSetTotal << std::endl; for (int r=0;rgetType() == tson::LayerType::TileLayer) { destRects.resize(tilesWide*tilesHigh); initialPositions.resize(tilesWide*tilesHigh); for (int r=0;r ogPos = std::make_tuple(thisRect.x,thisRect.y); initialPositions[elem] = ogPos; thisRect.x = thisRect.x-offsetX*globalScale; thisRect.y = thisRect.y=offsetY*globalScale; destRects[elem] = thisRect; } } destRect.w = destRect.h = tileWidth * gScale; // std::cout << "destRect[0] .x: " << destRects[0].x << " .y " << destRects[0].y << " .w " << destRects[0].w << " .h " << destRects[0].h << std::endl; // std::cout << "destRect[1] .x: " << destRects[1].x << " .y " << destRects[1].y << " .w " << destRects[1].w << " .h " << destRects[1].h << std::endl; // // std::cout << "tileSet[0] .x: " << tileSet[0].x << " .y " << tileSet[0].y << " .w " << tileSet[0].w << " .h " << tileSet[0].h << std::endl; // std::cout << "tileSet[1] .x: " << tileSet[1].x << " .y " << tileSet[1].y << " .w " << tileSet[1].w << " .h " << tileSet[0].h << std::endl; //std::cout << "Tile id: " << tileObject.getTile()->getId() << std::endl; //std::cout << "Tile column: " << col << std::endl; //std::cout << "Tile row: " << row << std::endl; //std::cout << "X: " << drawingRect.x << std::endl; //std::cout << "Y: " << drawingRect.y << std::endl; //std::cout << "Width: " << drawingRect.width << std::endl; //std::cout << "height: " << drawingRect.height << std::endl; //std::cout << "col % tilesWide: " << col%tilesWide << std::endl; // tson::Layer *myLayer = map->getLayer("Tile Layer 1"); // tson::Tile *myTile1 = myLayer->getTileData(0,0); // int tid1 = myTile1->getId()-1; // tson::Tile *myTile2 = myLayer->getTileData(1,0); // int tid2 = myTile2->getId()-1; // tson::Tile *myTile3 = myLayer->getTileData(2,0); // int tid3 = myTile3->getId()-1; // tson::Tile *myTile4 = myLayer->getTileData(3,0); // int tid4 = myTile4->getId()-1; // std::cout << "First tile row's Ids: " << tid1 << ", " << tid2 << ", " << tid3 << ", " << tid4 << ", " << std::endl; } } else { printf("Failed to load Tileson map\n"); std::cout << map->getStatusMessage(); } //std::cout << "texture var: " << texture << std::endl; } void update() override { // if (Game::gsm->currentState == GameStateManager::ST_COREGAME){ for (int i=0;i(initialPositions[i]) - Game::camera.x; destRects[i].y = std::get<1>(initialPositions[i]) - Game::camera.y; } // } } void draw() override { //iterate through rows and columns of the map to draw the tiles // First cycle through rows tson::Layer *myLayer = map->getLayer("Tile Layer 1"); tson::Layer *collisionLayer = map->getLayer("Collision"); for (int r = 0;rgetSize().y;r++){ // Next cycle through each column or tile in that row: for (int c = 0;cgetSize().x;c++){ int i = r*map->getSize().x+c; int elem = c+r*map->getSize().x; tson::Tile *myTile = myLayer->getTileData(c,r); int tid = myTile->getId()-1; int tileToDraw = tid; TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE); if (Game::debugMenu){ if (collisionLayer->getTileData(c,r)) { SDL_SetRenderDrawColor(Game::renderer,255,0,255,134); SDL_RenderDrawRect(Game::renderer, &destRects[elem]); } } } } } void setTex(std::string id) { texture = Game::assets->GetTexture(id); } }; #endif /* SRC_ECS_TILEMAPCOMPONENT_H_ */