TileMaps and camera tuning

This commit is contained in:
2022-04-29 21:31:34 -04:00
parent bc4262d552
commit 9f4007bb79
6 changed files with 51 additions and 49 deletions

View File

@ -43,20 +43,24 @@ public:
{
case SDLK_UP:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
if(transform->position.y>0){
transform->velocity.y = -1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_NONE;
}
}
break;
case SDLK_DOWN:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
if(transform->position.y<Game::levelMap.h){
transform->velocity.y = 1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_NONE;
}
}
break;
case SDLK_LEFT:
@ -69,11 +73,13 @@ public:
// }
// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
// printf("lastSafePos .x: %g .y: %g \n",transform->lastSafePos.x,transform->lastSafePos.y);
if(transform->position.x>0){
transform->velocity.x = -1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_NONE;
}
}
break;
case SDLK_RIGHT:
@ -85,11 +91,13 @@ public:
// Do a pre-check for tile-based smooth algorithm
// =========
// int intersectionTileX = collider->center.x;
if(transform->position.x<Game::levelMap.w){
transform->velocity.x = 1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
}
}
break;
// case SDLK_a:

View File

@ -31,6 +31,8 @@ public:
int globalScale;
std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
// std::tuple <int,int> oPosition;
std::vector<std::tuple<int,int>> initialPositions;
// std::array destRects;
int totalTiles;
@ -41,16 +43,18 @@ public:
SDL_DestroyTexture(texture);
}
TileMapComponent(tmxparser::TmxMap loadedMap, int gScale)
TileMapComponent(tmxparser::TmxMap loadedMap, int gScale, int offsetX, int offsetY)
{
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);
globalScale = gScale;
// =========== Setup Tile Set ===========
totalTiles = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
// std::cout << "Number of Tiles: " << totalTiles << std::endl;
tileSet.resize(totalTiles);
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;
@ -62,25 +66,19 @@ public:
tileSet[element] = srcRect;
}
}
destRects.resize(map.width*map.height);
// std::cout << "Number of Tiles on Map: " << destRects.size() << std::endl;
// for (int i=0;i<totalTiles-1;i++){
// SDL_Rect myRect = SDL_Rect();
// myRect.w = myRect.h = map.tileWidth * gScale;
// myRect.x = i*map.tilesetCollection[0].tileWidth;
// myRect.y = 0;
// destRects.push_back(myRect);
// }
for (int r = 0;r<map.height-1;r++){
// Next cycle through each column or tile in that row:
for (int c = 0;c<map.width-1;c++){
// =========== 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.x = c*map.tilesetCollection[0].tileWidth*globalScale-offsetX*globalScale;
thisRect.y = r*map.tilesetCollection[0].tileWidth*globalScale-offsetY*globalScale;
thisRect.w = thisRect.h = map.tileWidth * globalScale;
// destRects.push_back(thisRect);
std::tuple<int,int> ogPos = std::make_tuple(thisRect.x,thisRect.y);
initialPositions[elem] = ogPos;
destRects[elem] = thisRect;
// std::cout << "destRects[" << elem << "].x = " << destRects[elem].x << "].y = " << destRects[elem].y << "].w = " << destRects[elem].w << std::endl;
}
@ -92,16 +90,9 @@ public:
{
if (Game::gsm->currentState == GameStateManager::ST_COREGAME){
for (int i=0;i<destRects.size()-1;i++){
if (Game::pVel.x > 0)
destRects[i].x = destRects[i].x - Game::pVel.x;
if (Game::pVel.y > 0)
destRects[i].y = destRects[i].y - Game::pVel.y;
if (Game::pVel.x < 0)
destRects[i].x = destRects[i].x - Game::pVel.x;
if (Game::pVel.y < 0)
destRects[i].y = destRects[i].y - Game::pVel.y;
for (int i=0;i<destRects.size();i++){
destRects[i].x = std::get<0>(initialPositions[i]) - Game::camera.x;
destRects[i].y = std::get<1>(initialPositions[i]) - Game::camera.y;
}
}
}
@ -110,9 +101,9 @@ public:
{
//iterate through rows and columns of the map to draw the tiles
// First cycle through rows
for (int r = 0;r<map.height-1;r++){
for (int r = 0;r<map.height;r++){
// Next cycle through each column or tile in that row:
for (int c = 0;c<map.width-1;c++){
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;