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

@ -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
//
// //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);
// }
// }
//
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.
// 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();
// }*/
// }
} 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();