Tileson fully refactored into engine
This commit is contained in:
parent
b4b83f098d
commit
1a0bd2ea4e
@ -17,7 +17,6 @@
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include "../tileson/tileson.hpp"
|
||||
//#include "../../libtmx-parser/src/tmxparser.h"
|
||||
|
||||
class TileMapComponent : public Component
|
||||
{
|
||||
@ -26,6 +25,7 @@ public:
|
||||
SDL_Rect srcRect, destRect;
|
||||
//tmxparser::TmxMap map;
|
||||
tson::Tileson t;
|
||||
std::unique_ptr<tson::Map> map;
|
||||
int globalScale;
|
||||
std::vector<SDL_Rect> tileSet;
|
||||
std::vector<SDL_Rect> destRects;
|
||||
@ -50,7 +50,7 @@ public:
|
||||
|
||||
//tson::Tileson t;
|
||||
const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath);
|
||||
std::unique_ptr<tson::Map> map = t.parse(jsonPath);
|
||||
map = t.parse(jsonPath);
|
||||
|
||||
std::cout << jsonPath << std::endl;
|
||||
if(map->getStatus() == tson::ParseStatus::OK)
|
||||
@ -60,41 +60,52 @@ public:
|
||||
|
||||
//map = loadedMap;
|
||||
tson::Tileset *tileset = map->getTileset("br-tiles");
|
||||
std::cout << "Image Path: " << tileset->getImage() << std::endl;
|
||||
//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::Tile *tile = tileset->getTile(1);
|
||||
//std::string myTexPath = fullPath;
|
||||
tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is a Layer
|
||||
//std::cout << "\nTexturePath: \n" << myTexPath << "\n" << std::endl;
|
||||
|
||||
//std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
|
||||
std::string texturePath = fullPath;
|
||||
Game::assets->AddTexture("terrain", texturePath.c_str());
|
||||
setTex("terrain");
|
||||
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);
|
||||
// printf("tilesWide: %d\n",tilesWide);
|
||||
tilesHigh = map->getSize().y;
|
||||
printf("tilesHigh: %d\n",tilesHigh);
|
||||
// printf("tilesHigh: %d\n",tilesHigh);
|
||||
tileWidth = map->getTileSize().x;
|
||||
printf("tileSizeWidth: %d\n",tileWidth);
|
||||
// printf("tileSizeWidth: %d\n",tileWidth);
|
||||
Game::levelMap.w = tilesWide*tileWidth*globalScale;
|
||||
Game::levelMap.h = tilesHigh*tileWidth*globalScale;
|
||||
|
||||
|
||||
// =========== Setup Tile Set ===========
|
||||
|
||||
tileSetTotal = tileset->getTileCount();
|
||||
int tileSetCols = tileset->getColumns();
|
||||
std::cout << "tileSet Cols: " << tileSetCols << std::endl;
|
||||
int tileSetRows = tileSetTotal/tileSetCols;
|
||||
std::cout << "tileSet Rows: " << tileSetRows << std::endl;
|
||||
tileSet.resize(tileSetTotal);
|
||||
std::cout << tileSetTotal << std::endl;
|
||||
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++){
|
||||
@ -103,42 +114,37 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
if(tileLayer->getType() == tson::LayerType::TileLayer)
|
||||
{
|
||||
{
|
||||
destRects.resize(tilesWide*tilesHigh);
|
||||
initialPositions.resize(tilesWide*tilesHigh);
|
||||
// =================================================
|
||||
// for (int r = 0;r<map.height;r++){
|
||||
// for (int c = 0;c<map.width;c++){
|
||||
// int elem = c+r*map.width;
|
||||
// SDL_Rect thisRect = SDL_Rect();
|
||||
// thisRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
// thisRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
// thisRect.w = thisRect.h = map.tileWidth * globalScale;
|
||||
// std::tuple<int,int> 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;
|
||||
// colliders[r][c] = map.layerCollection[1].tiles[elem].gid;
|
||||
// // std::cout << "colliders[" << std::to_string(r) << "][" << std::to_string(c) << "]= " << std::to_string(colliders[r][c]) << std::endl;
|
||||
// }
|
||||
// }
|
||||
//=================================================
|
||||
for (int r=0;r<tilesHigh;r++){
|
||||
for (int c=0;c<tilesWide;c++){
|
||||
int elem = c+r*tilesWide;
|
||||
SDL_Rect thisRect = SDL_Rect();
|
||||
thisRect.x = c*tileWidth*globalScale;
|
||||
thisRect.y = r*tileWidth*globalScale;
|
||||
thisRect.w = thisRect.h = tileWidth*globalScale;
|
||||
std::tuple<int,int> 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;
|
||||
|
||||
int col = 0;
|
||||
int row = 0;
|
||||
|
||||
for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
|
||||
{
|
||||
//int elem = 0;
|
||||
tson::Tileset *tileset = tileObject.getTile()->getTileset();
|
||||
tson::Rect drawingRect = tileObject.getDrawingRect();
|
||||
tson::Vector2f position = tileObject.getPosition();
|
||||
//int thiscount = tileObject.getTileCount();
|
||||
// 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;
|
||||
@ -148,54 +154,25 @@ public:
|
||||
//std::cout << "Width: " << drawingRect.width << std::endl;
|
||||
//std::cout << "height: " << drawingRect.height << std::endl;
|
||||
//std::cout << "col % tilesWide: " << col%tilesWide << std::endl;
|
||||
if ( col%tilesWide == 0) {
|
||||
col=0;
|
||||
//printf("New Row\n");
|
||||
row++;
|
||||
}
|
||||
col++;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
|
||||
for (int c=0;c<map.tilesetCollection[0].colCount;c++){
|
||||
srcRect.x = c*map.tilesetCollection[0].tileWidth;
|
||||
srcRect.y = r*map.tilesetCollection[0].tileHeight;
|
||||
srcRect.w = srcRect.h = map.tileWidth;
|
||||
int element = r*map.tilesetCollection[0].colCount+c;
|
||||
tileSet[element] = srcRect;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
// =========== Setup Tile Map ============
|
||||
destRects.resize(map.width*map.height);
|
||||
initialPositions.resize(map.width*map.height);
|
||||
for (int r = 0;r<map.height;r++){
|
||||
for (int c = 0;c<map.width;c++){
|
||||
int elem = c+r*map.width;
|
||||
SDL_Rect thisRect = SDL_Rect();
|
||||
thisRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
thisRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
thisRect.w = thisRect.h = map.tileWidth * globalScale;
|
||||
std::tuple<int,int> 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;
|
||||
colliders[r][c] = map.layerCollection[1].tiles[elem].gid;
|
||||
// std::cout << "colliders[" << std::to_string(r) << "][" << std::to_string(c) << "]= " << std::to_string(colliders[r][c]) << std::endl;
|
||||
}
|
||||
}*/
|
||||
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
|
||||
@ -213,21 +190,25 @@ public:
|
||||
{
|
||||
//iterate through rows and columns of the map to draw the tiles
|
||||
// First cycle through rows
|
||||
/* for (int r = 0;r<map.height;r++){
|
||||
tson::Layer *myLayer = map->getLayer("Tile Layer 1");
|
||||
tson::Layer *collisionLayer = map->getLayer("Collision");
|
||||
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.width;c++){
|
||||
int i = r*map.width+c;
|
||||
int elem = c+r*map.width;
|
||||
int tileToDraw = map.layerCollection[0].tiles[i].gid-1;
|
||||
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;
|
||||
int tileToDraw = tid;
|
||||
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
|
||||
if (Game::debugMenu){
|
||||
if (map.layerCollection[1].tiles[i].gid != 0) {
|
||||
if (collisionLayer->getTileData(c,r)) {
|
||||
SDL_SetRenderDrawColor(Game::renderer,255,0,255,134);
|
||||
SDL_RenderDrawRect(Game::renderer, &destRects[elem]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -138,27 +138,6 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL audio mixer!, Error: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
// ---------------------------------------
|
||||
// Trying out the halsafar/libtmx-parser
|
||||
/* tmxparser::TmxReturn error;
|
||||
tmxparser::TmxMap map;
|
||||
error = tmxparser::parseFromFile("assets/maps/testmapb64.tmx", &map, "assets/textures/tiles/");
|
||||
|
||||
if (!error)
|
||||
{
|
||||
// printf("Yay! Tile map loaded with no errors.\n");
|
||||
tmxparser::TmxLayer layer = map.layerCollection[0];
|
||||
levelMap.x = 0;
|
||||
levelMap.y = 0;
|
||||
levelMap.w = map.width*map.tileWidth*gScale;
|
||||
levelMap.h = map.height*map.tileHeight*gScale;
|
||||
// std::cout << "Map Width Tiles: " << map.width << " Map Tile Width: " << map.tileWidth <<std::endl;
|
||||
} else {
|
||||
std::cout << "Encountered error loading map file: " << error << std::endl;
|
||||
}
|
||||
*/
|
||||
// std::cout << "levelMap.w - camera.w: " << levelMap.w-camera.w << std::endl;
|
||||
|
||||
assets->AddTexture("player", "assets/textures/actors/firefighter.png");
|
||||
assets->AddTexture("font", "assets/textures/ui/ui-font-cloud-sans.png");
|
||||
assets->AddTexture("textBox", "assets/textures/ui/ui-element-cloud.png");
|
||||
@ -243,50 +222,12 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
|
||||
player.addComponent<KeyboardController>();
|
||||
player.addGroup(groupPlayers);
|
||||
|
||||
levelMap.x = 0;
|
||||
levelMap.y = 0;
|
||||
|
||||
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");
|
||||
// //
|
||||
// //
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// printf("Failed to load Tileson map\n");
|
||||
// std::cout << map->getStatusMessage();
|
||||
// //printf(map->getStatus());
|
||||
// }
|
||||
|
||||
playerPosition = Vector2D().Zero();
|
||||
pVel = Vector2D().Zero();
|
||||
|
||||
@ -301,7 +242,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
|
||||
}
|
||||
|
||||
// ====== Setup groups
|
||||
//auto& tiles(manager.getGroup(Game::groupMap));
|
||||
auto& tiles(manager.getGroup(Game::groupMap));
|
||||
auto& players(manager.getGroup(Game::groupPlayers));
|
||||
auto& colliders(manager.getGroup(Game::groupColliders));
|
||||
auto& objects(manager.getGroup(Game::groupObjects));
|
||||
@ -407,13 +348,13 @@ void Game::update()
|
||||
uiPlayerXInfo.getComponent<UITextComponent>().updateString(std::to_string(playerX));
|
||||
uiPlayerYInfo.getComponent<UITextComponent>().updateString(std::to_string(playerY));
|
||||
|
||||
int * foundBoundaries = predictCollisions();
|
||||
int max = foundBoundaries[0];
|
||||
for (int b=0;b<3;b++){
|
||||
if (foundBoundaries[b]>max){
|
||||
max = foundBoundaries[b];
|
||||
}
|
||||
}
|
||||
// int * foundBoundaries = predictCollisions();
|
||||
// int max = foundBoundaries[0];
|
||||
// for (int b=0;b<3;b++){
|
||||
// if (foundBoundaries[b]>max){
|
||||
// max = foundBoundaries[b];
|
||||
// }
|
||||
// }
|
||||
float desiredMovementX = -1*player.getComponent<TransformComponent>().speed*0.016;
|
||||
// int desiredMovementY = player.getComponent<TransformComponent>().velocity.y*player.getComponent<TransformComponent>().speed*player.getComponent<TransformComponent>().scale;
|
||||
// uiBoundary1Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[0]));
|
||||
@ -426,7 +367,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));
|
||||
uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)(max*gScale)));
|
||||
//uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)(max*gScale)));
|
||||
|
||||
// uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[2]));
|
||||
|
||||
@ -473,10 +414,10 @@ void Game::render()
|
||||
// {
|
||||
// printf("Core Game state\n");
|
||||
// }
|
||||
/* for (auto& t : tiles)
|
||||
for (auto& t : tiles)
|
||||
{
|
||||
t->draw();
|
||||
} */
|
||||
}
|
||||
|
||||
for (auto& c : colliders)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user