New tileset & map; todo fix tile sanity check

This commit is contained in:
Alan Youngblood
2023-08-04 22:05:07 -04:00
parent 6568026886
commit 7b2ed8e27b
11 changed files with 271 additions and 112 deletions

View File

@ -42,7 +42,7 @@ public:
SDL_DestroyTexture(texture);
}
TileMapComponent(std::string mapPath, int gScale, int offsetX, int offsetY)
TileMapComponent(std::string mapPath, std::string tsName, std::string tileLayerName, std::string collisionLayerName, int gScale, int offsetX, int offsetY)
{
// TILESON ~~~~~~~~~~~
@ -51,18 +51,22 @@ public:
if(map->getStatus() == tson::ParseStatus::OK)
{
tson::Tileset *tileset = map->getTileset("br-tiles");
tson::Tileset *tileset = map->getTileset(tsName);
std::cout << "tsName: " << tsName << std::endl;
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::cout << "fullPath: " << fullPath << std::endl;
//size_t charPos = fullPath.find("assets");
//fullPath.erase(0,charPos);
std::string prependPath = std::string("assets/maps/");
std::string wholePath = prependPath + fullPath;
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, fullPath.c_str());
Game::assets->AddTexture(texName, wholePath.c_str());
setTex(texName);
globalScale = gScale;
tson::Layer *collisionLayer = map->getLayer("Collision");
tson::Layer *collisionLayer = map->getLayer(collisionLayerName);
tilesWide = map->getSize().x;
tilesHigh = map->getSize().y;
@ -71,7 +75,7 @@ public:
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();
@ -109,7 +113,31 @@ public:
}
destRect.w = destRect.h = tileWidth * gScale;
}
tson::Layer *myLayer = map->getLayer("Tile Layer 1");
for (int r = 0;r<map->getSize().y;r++){
// Next cycle through each column or tile in that row:
for (int c = 0;c<map->getSize().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;
//std::cout << myLayer->getTileData(1,1)->getId() << std::endl;
// TextureManager::Draw(texture, tileSet[tid], destRects[elem], SDL_FLIP_NONE);
std::cout << "c,r: " << c << ", " << r << std::endl;
if(!tid){
printf("got nullptr\n");
// printf("found valid tid\n");
} else {
std::cout << "tid: " << tid << std::endl;
}
//std::cout << " elem: " << elem << std::endl;
//std::cout << "i" << i << std::endl;
}
}
// tson::Tile *testTile = myLayer->getTileData(0,4);
// int myTid = testTile->getId()-1;
printf("Completed tilemap init\n");
//std::cout << "destRects.size " << destRects.size() << std::endl;
} else {
printf("Failed to load Tileson map\n");
std::cout << map->getStatusMessage();
@ -130,7 +158,7 @@ public:
void draw() override
{
//iterate through rows and columns of the map to draw the tiles
// First cycle through rows
//First cycle through rows
tson::Layer *myLayer = map->getLayer("Tile Layer 1");
tson::Layer *collisionLayer = map->getLayer("Collision");
for (int r = 0;r<map->getSize().y;r++){
@ -139,14 +167,16 @@ public:
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(myTile){
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]);
}
// if (collisionLayer->getTileData(c,r)) {
// SDL_SetRenderDrawColor(Game::renderer,255,0,255,134);
// SDL_RenderDrawRect(Game::renderer, &destRects[elem]);
// }
}
}
}