More work on Tileson integration

This commit is contained in:
Alan Youngblood 2023-07-24 13:41:02 -04:00
parent bbab18d361
commit b15316c40e
2 changed files with 91 additions and 56 deletions

View File

@ -42,37 +42,65 @@ public:
SDL_DestroyTexture(texture);
}
TileMapComponent(/*tileson::Tileson loadedMap,*/ int gScale, int offsetX, int offsetY)
TileMapComponent(std::string mapPath, int gScale, int offsetX, int offsetY)
{
// ***********************************************************************************
// TILESON ~~~~~~~~~~~
tson::Tileson t;
std::unique_ptr<tson::Map> map = t.parse(fs::path("../../assets/maps/testmap.json"));
const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath);
std::unique_ptr<tson::Map> map = t.parse(jsonPath);
if(map->getStatus() == tson::ParseStatus::OK)
{
printf("Parsed the map file okay.");
std::cout << jsonPath << std::endl;
if(map->getStatus() == tson::ParseStatus::OK)
{
printf("Tileson successfully parsed the tilemap\n");
std::cout << map->getStatusMessage() << std::endl;
//Gets the layer called "Object Layer" from the "ultimate_demo.json map
tson::Layer *objectLayer = map->getLayer("Object Layer"); //This is an Object Layer
//tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//Example from a Tile Layer
//I know for a fact that this is a Tile Layer, but you can check it this way to be sure.
//if(tileLayer->getType() == tson::LayerType::TileLayer)
//{
//pos = position in tile units
// printf("Tileson found layer of tiles");
// for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
// {
// tson::Tileset *tileset = tileObject.getTile()->getTileset();
// tson::Rect drawingRect = tileObject.getDrawingRect();
// tson::Vector2f position = tileObject.getPosition();
// //printf("X: %f, Y: %f\n",position.x,position.y);
// }
//}
}
// *******************************************************************************************
/*map = loadedMap;
std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str());
setTex(map.tilesetCollection[0].name);
//map = loadedMap;
tson::Tileset *tileset = map->getTileset("br-tiles");
std::cout << "Image Path: " << tileset->getImage() << std::endl;
//tson::Tile *tile = tileset->getTile(1);
//std::string myTexPath = tile->getImage();
//tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//std::cout << "\nTexturePath: \n" << myTexPath << "\n" << std::endl;
//std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
//Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str());
//setTex(map.tilesetCollection[0].name);
globalScale = gScale;
colliders.resize(map.height, std::vector<int>(map.width, 0));
tilesWide = map.width;
tilesHigh = map.height;
tileWidth = map.tileWidth;
//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);
// =========== Setup Tile Set ===========
tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;*/
tileSet.resize(tileSetTotal);
//tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
//tileSet.resize(tileSetTotal);
/*
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
for (int c=0;c<map.tilesetCollection[0].colCount;c++){
@ -104,7 +132,13 @@ if(map->getStatus() == tson::ParseStatus::OK)
}
}
destRect.w = destRect.h = map.tileWidth * gScale;
*/}
*/
} else {
printf("Failed to load Tileson map\n");
std::cout << map->getStatusMessage();
//printf(map->getStatus());
}
}
void update() override
{

View File

@ -243,48 +243,49 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
player.addComponent<KeyboardController>();
player.addGroup(groupPlayers);
gameScene.addComponent<TileMapComponent>(/*map,*/gScale,player.getComponent<TransformComponent>().position.x+player.getComponent<TransformComponent>().width/2,player.getComponent<TransformComponent>().position.y+player.getComponent<TransformComponent>().height/2); //150,100
gameScene.addComponent<TileMapComponent>("assets/maps/testmap.json",gScale,player.getComponent<TransformComponent>().position.x+player.getComponent<TransformComponent>().width/2,player.getComponent<TransformComponent>().position.y+player.getComponent<TransformComponent>().height/2); //150,100
gameScene.addGroup(groupMap);
// ---------------------------------------
// Tileson tryout
printf("\nTrying out Tileson, does it work?\n");
// printf("\nTrying out Tileson, does it work?\n");
// //
// //
// tson::Map myMap;
// //std::string jsonStr = "assets/maps/testmap.json";
// const char * jsonStr = "assets/maps/testmap.json";
// const std::filesystem::path jsonPath = std::filesystem::u8path(jsonStr);
// std::unique_ptr<tson::Map> map = t.parse(jsonPath);
// //myMap.parse("./assets/maps/testmap.json");
// // std::unique_ptr<tson::Map> map = t.parse(tson_files::_ULTIMATE_TEST_JSON, tson_files::_ULTIMATE_TEST_JSON_SIZE);
// std::cout << jsonPath << std::endl;
// if(map->getStatus() == tson::ParseStatus::OK)
// {
// printf("Tileson successfully parsed the tilemap\n");
// std::cout << map->getStatusMessage();
// //Gets the layer called "Object Layer" from the "ultimate_demo.json map
// tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//
//
tson::Map myMap;
//std::string jsonStr = "assets/maps/testmap.json";
const char * jsonStr = "assets/maps/testmap.json";
const std::filesystem::path jsonPath = std::filesystem::u8path(jsonStr);
std::unique_ptr<tson::Map> map = t.parse(jsonPath);
//myMap.parse("./assets/maps/testmap.json");
// std::unique_ptr<tson::Map> map = t.parse(tson_files::_ULTIMATE_TEST_JSON, tson_files::_ULTIMATE_TEST_JSON_SIZE);
std::cout << jsonPath << std::endl;
if(map->getStatus() == tson::ParseStatus::OK)
{
printf("Tileson successfully parsed the tilemap\n");
std::cout << map->getStatusMessage();
//Gets the layer called "Object Layer" from the "ultimate_demo.json map
//tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//Example from a Tile Layer
//I know for a fact that this is a Tile Layer, but you can check it this way to be sure.
// //Example from a Tile Layer
// //I know for a fact that this is a Tile Layer, but you can check it this way to be sure.
// if(tileLayer->getType() == tson::LayerType::TileLayer)
// {
// //pos = position in tile units
// printf("Tileson found layer of tiles");
// /*for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
// for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
// {
// tson::Tileset *tileset = tileObject.getTile()->getTileset();
// tson::Rect drawingRect = tileObject.getDrawingRect();
// tson::Vector2f position = tileObject.getPosition();
// }*/
// printf("X: %f, Y: %f\n",position.x,position.y);
// }
// }
//
// } else {
// printf("Failed to load Tileson map\n");
// std::cout << map->getStatusMessage();
// //printf(map->getStatus());
// }
} else {
printf("Failed to load Tileson map\n");
std::cout << map->getStatusMessage();
//printf(map->getStatus());
}
playerPosition = Vector2D().Zero();
pVel = Vector2D().Zero();