TileMaps and camera tuning

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

File diff suppressed because one or more lines are too long

Binary file not shown.

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;

View File

@ -36,7 +36,7 @@ SDL_Renderer* Game::renderer = nullptr;
SDL_Event Game::event;
SDL_Rect Game::camera;
SDL_Rect levelMap;
SDL_Rect Game::levelMap;
AssetManager* Game::assets = new AssetManager(&manager);
@ -161,9 +161,6 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
assets->AddSoundClip("bark2","assets/audio/sfx/Bark2.wav");
// map = new Map("terrain",globalScale,16);
gameScene.addComponent<TileMapComponent>(map,gScale);
gameScene.addGroup(groupMap);
// std::string myText = "Find lost puppies!\nThey need your help!";
std::string myText = "Press U to Start";
@ -179,24 +176,24 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
my9Slice = new UINineSlice("textBox");
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0);
uiInfo.addComponent<TransformComponent>(camera.w-94,10,72,96,gScale);
uiInfo.addComponent<TransformComponent>((camera.w-94)*gScale,10*gScale,72,96,gScale);
// uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1);
uiInfo.addComponent<UITextComponent>("font", "Camera CamX: CamY: P.x: P.y : P.dy: YVec: ", 8, 12, gScale);
uiInfo.addGroup(groupUI_Layer3);
uiCamXInfo.addComponent<TransformComponent>(camera.w-48,24,40,12,gScale);
uiCamXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, 1);
uiCamXInfo.addComponent<TransformComponent>((camera.w-48)*gScale,24*gScale,40,12,gScale);
uiCamXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiCamXInfo.addGroup(groupUI_Layer3);
uiCamYInfo.addComponent<TransformComponent>(camera.w-48,36,40,12,gScale);
uiCamYInfo.addComponent<TransformComponent>((camera.w-48)*gScale,36*gScale,40,12,gScale);
uiCamYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, 1);
uiCamYInfo.addGroup(groupUI_Layer3);
uiPlayerXInfo.addComponent<TransformComponent>(camera.w-48,48,40,12,gScale);
uiPlayerXInfo.addComponent<TransformComponent>((camera.w-48)*gScale,48*gScale,40,12,gScale);
uiPlayerXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, 1);
uiPlayerXInfo.addGroup(groupUI_Layer3);
uiPlayerYInfo.addComponent<TransformComponent>(camera.w-48,60,40,12,gScale);
uiPlayerYInfo.addComponent<TransformComponent>((camera.w-48)*gScale,60*gScale,40,12,gScale);
uiPlayerYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, 1);
uiPlayerYInfo.addGroup(groupUI_Layer3);
@ -216,7 +213,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
//ecs implementation
// player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale);
player.addComponent<TransformComponent>(150*globalScale,100*globalScale,22,42,globalScale); // 180,120
player.addComponent<TransformComponent>(150,100,22,42,globalScale); // 180,120
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
// player.addComponent<PlayerController>(0.0,0.0,false,false,Vector2D().Zero());
@ -224,6 +221,9 @@ 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.addGroup(groupMap);
playerPosition = Vector2D().Zero();
pVel = Vector2D().Zero();
@ -330,23 +330,25 @@ void Game::update()
uiCamYInfo.getComponent<UITextComponent>().updateString(std::to_string(camera.y));
int playerX = player.getComponent<TransformComponent>().position.x;
int playerY = player.getComponent<TransformComponent>().position.y;
uiPlayerXInfo.getComponent<UITextComponent>().updateString(std::to_string(playerX));
uiPlayerYInfo.getComponent<UITextComponent>().updateString(std::to_string(playerY));
playerPosition.x = playerX;
playerPosition.y = playerY;
pVel.x = player.getComponent<TransformComponent>().velocity.x;
pVel.y = player.getComponent<TransformComponent>().velocity.y;
if(camera.x < 0)
camera.x = 0;
if (camera.y < 0)
camera.y = 0;
if (camera.x > levelMap.w-camera.w)
camera.x = levelMap.w-camera.w;
if (camera.y > levelMap.h-camera.h)
camera.y = levelMap.h-camera.h;
// if(camera.x < 0)
// camera.x = 0;
// if (camera.y < 0)
// camera.y = 0;
// if (camera.x > levelMap.w-camera.w*1.5-player.getComponent<TransformComponent>().width)
// camera.x = levelMap.w-camera.w*1.5-player.getComponent<TransformComponent>().width;
// if (camera.y > levelMap.h-camera.h*1.5-player.getComponent<TransformComponent>().height)
// camera.y = levelMap.h-camera.h*1.5-player.getComponent<TransformComponent>().height;
if (Game::debugCollisionBoxes)
{
for (auto& c: colliders)

View File

@ -45,6 +45,7 @@ public:
static Vector2D playerPosition;
static Vector2D pVel;
static SDL_Rect camera;
static SDL_Rect levelMap;
static AssetManager* assets;
static GameStateManager* gsm;
std::string BoolToString(bool b);