Cleaned up Tileson and TilemapComponent

This commit is contained in:
Alan Youngblood 2023-07-26 15:47:36 -04:00
parent 1a0bd2ea4e
commit 4b1a80ec40
3 changed files with 66 additions and 120 deletions

View File

@ -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:

View File

@ -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<tson::Map> map;
int globalScale;
@ -45,54 +44,30 @@ 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;
//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<int>(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;
@ -102,10 +77,7 @@ public:
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;r<tileSetRows;r++){
for (int c=0;c<tileSetCols;c++){
@ -114,7 +86,6 @@ public:
srcRect.w = srcRect.h = tileWidth;
int element = r*tileSetCols+c;
tileSet[element] = srcRect;
//std::cout << "Element: " << element << " X: " << srcRect.x << " Y: " << srcRect.y << std::endl;
}
}
@ -137,42 +108,12 @@ public:
}
}
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

View File

@ -23,11 +23,8 @@
#include "../cjson/cJSON.h"
#include "../tileson/tileson.hpp"
#include <cmath>
//#include <experimental/filesystem>
#include <filesystem>
// 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<TransformComponent>(camera.w/gScale-94,10,72*gScale,96*gScale,gScale);
// uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1);
uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : coll: Px2 tson: ", 8, 12, gScale);
// uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : coll: Px2: tson: ", 8, 12, gScale);
uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : coll: ", 8, 12, gScale);
uiInfo.addGroup(groupUI_Layer3);
uiCamXInfo.addComponent<TransformComponent>(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<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary1Info.addGroup(groupUI_Layer3);
uiBoundary2Info.addComponent<TransformComponent>(camera.w/gScale-64,88,128*gScale,12*gScale,gScale);
uiBoundary2Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary2Info.addGroup(groupUI_Layer3);
uiBoundary3Info.addComponent<TransformComponent>(camera.w/gScale-64,101,128*gScale,12*gScale,gScale);
uiBoundary3Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary3Info.addGroup(groupUI_Layer3);
// uiBoundary2Info.addComponent<TransformComponent>(camera.w/gScale-64,88,128*gScale,12*gScale,gScale);
// uiBoundary2Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
// uiBoundary2Info.addGroup(groupUI_Layer3);
//
// uiBoundary3Info.addComponent<TransformComponent>(camera.w/gScale-64,101,128*gScale,12*gScale,gScale);
// uiBoundary3Info.addComponent<UITextComponent>("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<UITextComponent>().updateString(std::to_string((int)foundBoundaries[1]));
float difference = player.getComponent<TransformComponent>().position.x+desiredMovementX;
uiBoundary2Info.getComponent<UITextComponent>().updateString(std::to_string((float)difference));
//uiBoundary2Info.getComponent<UITextComponent>().updateString(std::to_string((float)difference));
//uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)(max*gScale)));
// uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[2]));