/* * * 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; 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, std::string tsName, std::string tileLayerName, std::string collisionLayerName, int gScale, int offsetX, int offsetY) { // TILESON ~~~~~~~~~~~ //const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath); const std::filesystem::path jsonPath = std::filesystem::u8path("/Users/ayoungblood/Projects/KaijuSaveEarth/assets/maps/kaiju-city-map.json"); map = t.parse(jsonPath); //std::cout << "Trying to load json tile map from: " << jsonPath << std::endl; if(map->getStatus() == tson::ParseStatus::OK) { // printf("Map File Loaded!! \n"); tson::Tileset *tileset = map->getTileset(tsName); //std::cout << "tsName: " << tsName << std::endl; std::string fullPath = tileset->getImage(); //std::cout << "fullPath: " << fullPath << std::endl; //size_t charPos = fullPath.find("assets"); //fullPath.erase(0,charPos); std::string prependPath = std::string("assets/maps/"); std::string localPath = prependPath + fullPath; std::string wholePath = "/Users/ayoungblood/Projects/KaijuSaveEarth/" + localPath; //std::cout << "fullPath: " << wholePath << std::endl; tson::Layer *tileLayer = map->getLayer(tileLayerName); //This is a Layer std::string texName = tileLayer->getName(); Game::assets->AddTexture(texName, wholePath.c_str()); setTex(texName); globalScale = gScale; //printf("Added texture for tilemap.\n"); tson::Layer *collisionLayer = map->getLayer(collisionLayerName); tilesWide = map->getSize().x; tilesHigh = map->getSize().y; tileWidth = map->getTileSize().x; Game::levelMap.w = tilesWide*tileWidth*globalScale; Game::levelMap.h = tilesHigh*tileWidth*globalScale; // printf("First phase tile map init done, moving to Tileset\n"); // =========== Setup Tile Set =========== tileSetTotal = tileset->getTileCount(); tileSet.resize(tileSetTotal); int tileSetCols = tileset->getColumns(); int tileSetRows = tileSetTotal/tileSetCols; 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; } //printf("Tilemap setup completed\n"); //std::cout << "destRects.size " << destRects.size() << std::endl; } else { // printf("Failed to load Tileson map\n"); std::cout << map->getStatusMessage() << 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); if(!myTile){ // Found a nullptr, nothing to do or see here, move along } else { 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)) { // Nothing to see here, just returning a nullptr, move along... } else { 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_ */