From 4b1a80ec40452a1fc0e04e37aa5d71fa0653a92d Mon Sep 17 00:00:00 2001 From: Alan Youngblood Date: Wed, 26 Jul 2023 15:47:36 -0400 Subject: [PATCH] Cleaned up Tileson and TilemapComponent --- src/ecs/KeyboardController.h | 11 ++- src/ecs/TileMapComponent.h | 153 +++++++++++------------------------ src/game/Game.cpp | 22 +++-- 3 files changed, 66 insertions(+), 120 deletions(-) diff --git a/src/ecs/KeyboardController.h b/src/ecs/KeyboardController.h index 5a2d496..e82a6e6 100644 --- a/src/ecs/KeyboardController.h +++ b/src/ecs/KeyboardController.h @@ -70,6 +70,13 @@ public: break; case SDLK_LEFT: if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ + if(transform->position.x>Game::levelMap.x){ + transform->velocity.x = -1; +// if(Game::playerIsGrounded){ + sprite->Play("Walk"); +// } + sprite->spriteFlip = SDL_FLIP_NONE; + } // if (playerCtrl == NULL){ // printf("No player controller found\n"); // } else { @@ -81,7 +88,7 @@ public: // Game::pTileX // if(transform->position.x>0){ - int *borders; + /*int *borders; borders = Game::predictCollisions(); float max = borders[0]; for (int b=0;b<3;b++){ @@ -102,7 +109,7 @@ public: transform->velocity.x = 0; sprite->Play("Idle"); sprite->spriteFlip = SDL_FLIP_NONE; - } + }*/ } break; case SDLK_RIGHT: diff --git a/src/ecs/TileMapComponent.h b/src/ecs/TileMapComponent.h index f28e846..ff5c478 100644 --- a/src/ecs/TileMapComponent.h +++ b/src/ecs/TileMapComponent.h @@ -23,7 +23,6 @@ class TileMapComponent : public Component public: SDL_Texture* texture; SDL_Rect srcRect, destRect; - //tmxparser::TmxMap map; tson::Tileson t; std::unique_ptr map; int globalScale; @@ -45,134 +44,76 @@ public: 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; + tson::Tileset *tileset = map->getTileset("br-tiles"); + std::string fullPath = tileset->getImage(); - //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); + 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; - size_t charPos = fullPath.find("assets"); - fullPath.erase(0,charPos); + tson::Layer *collisionLayer = map->getLayer("Collision"); - //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; + tilesWide = map->getSize().x; + tilesHigh = map->getSize().y; + tileWidth = map->getTileSize().x; - 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; + Game::levelMap.w = tilesWide*tileWidth*globalScale; + Game::levelMap.h = tilesHigh*tileWidth*globalScale; -// =========== Setup Tile Set =========== + // =========== 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; + 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; + for (int r=0;rgetId() << 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; - } + if(tileLayer->getType() == 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(); } - //std::cout << "texture var: " << texture << std::endl; } void update() override diff --git a/src/game/Game.cpp b/src/game/Game.cpp index f99acd9..bb97f86 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -23,11 +23,8 @@ #include "../cjson/cJSON.h" #include "../tileson/tileson.hpp" #include -//#include #include -// tmxparser::TmxMap map; -//tileson::Tileson map; Manager manager; UINineSlice* my9Slice; UINineSlice* scoreboard9Slice; @@ -171,7 +168,8 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g uiInfo.addComponent(camera.w/gScale-94,10,72*gScale,96*gScale,gScale); // uiInfo.addComponent("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1); - uiInfo.addComponent("font", "Player PTiX: PTiY: P.x: P.y : coll: Px2 tson: ", 8, 12, gScale); +// uiInfo.addComponent("font", "Player PTiX: PTiY: P.x: P.y : coll: Px2: tson: ", 8, 12, gScale); + uiInfo.addComponent("font", "Player PTiX: PTiY: P.x: P.y : coll: ", 8, 12, gScale); uiInfo.addGroup(groupUI_Layer3); uiCamXInfo.addComponent(camera.w/gScale-48,23,40*gScale,12*gScale,gScale); @@ -194,13 +192,13 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g uiBoundary1Info.addComponent("font", "nan", 8, 12, gScale); uiBoundary1Info.addGroup(groupUI_Layer3); - uiBoundary2Info.addComponent(camera.w/gScale-64,88,128*gScale,12*gScale,gScale); - uiBoundary2Info.addComponent("font", "nan", 8, 12, gScale); - uiBoundary2Info.addGroup(groupUI_Layer3); - - uiBoundary3Info.addComponent(camera.w/gScale-64,101,128*gScale,12*gScale,gScale); - uiBoundary3Info.addComponent("font", "nan", 8, 12, gScale); - uiBoundary3Info.addGroup(groupUI_Layer3); +// uiBoundary2Info.addComponent(camera.w/gScale-64,88,128*gScale,12*gScale,gScale); +// uiBoundary2Info.addComponent("font", "nan", 8, 12, gScale); +// uiBoundary2Info.addGroup(groupUI_Layer3); +// +// uiBoundary3Info.addComponent(camera.w/gScale-64,101,128*gScale,12*gScale,gScale); +// uiBoundary3Info.addComponent("font", "nan", 8, 12, gScale); +// uiBoundary3Info.addGroup(groupUI_Layer3); // debug UI box SDL_Rect debugBoxRect = SDL_Rect(); @@ -366,7 +364,7 @@ void Game::update() } // uiBoundary2Info.getComponent().updateString(std::to_string((int)foundBoundaries[1])); float difference = player.getComponent().position.x+desiredMovementX; - uiBoundary2Info.getComponent().updateString(std::to_string((float)difference)); + //uiBoundary2Info.getComponent().updateString(std::to_string((float)difference)); //uiBoundary3Info.getComponent().updateString(std::to_string((int)(max*gScale))); // uiBoundary3Info.getComponent().updateString(std::to_string((int)foundBoundaries[2]));