/* * * 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, int gScale, int offsetX, int offsetY) { // TILESON ~~~~~~~~~~~ const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath); map = t.parse(jsonPath); if(map->getStatus() == tson::ParseStatus::OK) { tson::Tileset *tileset = map->getTileset("br-tiles"); std::string fullPath = tileset->getImage(); size_t charPos = fullPath.find("assets"); fullPath.erase(0,charPos); tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is a Layer std::string texName = tileLayer->getName(); Game::assets->AddTexture(texName, fullPath.c_str()); setTex(texName); globalScale = gScale; tson::Layer *collisionLayer = map->getLayer("Collision"); tilesWide = map->getSize().x; tilesHigh = map->getSize().y; tileWidth = map->getTileSize().x; 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(); 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; } } else { printf("Failed to load Tileson map\n"); std::cout << map->getStatusMessage(); } } 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_ */