Debug menu scale fixed, Player Tile Position

This commit is contained in:
Alan Youngblood 2022-05-01 15:38:55 -04:00
parent fb20b8d868
commit b94c2cff38
6 changed files with 31 additions and 37 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -3,5 +3,5 @@
"WindowName":"Beagle Rescue", "WindowName":"Beagle Rescue",
"WindowSize":{"w":320,"h":240}, "WindowSize":{"w":320,"h":240},
"WindowFullScreen": 0, "WindowFullScreen": 0,
"GlobalScale": 1 "GlobalScale": 3
} }

View File

@ -15,25 +15,18 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <tuple> #include <tuple>
// #include <algorithm>
// #include <iterator>
#include "tmxparser.h" #include "tmxparser.h"
class TileMapComponent : public Component class TileMapComponent : public Component
{ {
public: public:
// TransformComponent *transform;
SDL_Texture* texture; SDL_Texture* texture;
SDL_Rect srcRect, destRect; SDL_Rect srcRect, destRect;
// Vector2D position;
// std::tuple <SDL_Rect,SDL_Rect> tile;
tmxparser::TmxMap map; tmxparser::TmxMap map;
int globalScale; int globalScale;
std::vector<SDL_Rect> tileSet; std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects; std::vector<SDL_Rect> destRects;
// std::tuple <int,int> oPosition;
std::vector<std::tuple<int,int>> initialPositions; std::vector<std::tuple<int,int>> initialPositions;
// std::array destRects;
int tileSetTotal; int tileSetTotal;
TileMapComponent() = default; TileMapComponent() = default;
@ -52,7 +45,6 @@ public:
globalScale = gScale; globalScale = gScale;
// =========== Setup Tile Set =========== // =========== Setup Tile Set ===========
tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount; tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
// std::cout << "Number of Tiles: " << tileSetTotal << std::endl;
tileSet.resize(tileSetTotal); tileSet.resize(tileSetTotal);
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){ for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
@ -61,8 +53,6 @@ public:
srcRect.y = r*map.tilesetCollection[0].tileHeight; srcRect.y = r*map.tilesetCollection[0].tileHeight;
srcRect.w = srcRect.h = map.tileWidth; srcRect.w = srcRect.h = map.tileWidth;
int element = r*map.tilesetCollection[0].colCount+c; int element = r*map.tilesetCollection[0].colCount+c;
// std::cout << "element " << element << std::endl;
// std::cout << "srcRect= x: " << srcRect.x << " y: " << srcRect.y << " srcRect.w & h: " << srcRect.w << std::endl;
tileSet[element] = srcRect; tileSet[element] = srcRect;
} }
} }
@ -82,7 +72,6 @@ public:
thisRect.x = thisRect.x-offsetX*globalScale; thisRect.x = thisRect.x-offsetX*globalScale;
thisRect.y = thisRect.y-offsetY*globalScale; thisRect.y = thisRect.y-offsetY*globalScale;
destRects[elem] = thisRect; destRects[elem] = thisRect;
// std::cout << "destRects[" << elem << "].x = " << destRects[elem].x << "].y = " << destRects[elem].y << "].w = " << destRects[elem].w << std::endl;
} }
} }
destRect.w = destRect.h = map.tileWidth * gScale; destRect.w = destRect.h = map.tileWidth * gScale;
@ -101,21 +90,15 @@ public:
void draw() override void draw() override
{ {
//iterate through rows and columns of the map to draw the tiles //iterate through rows and columns of the map to draw the tiles
// First cycle through rows // First cycle through rows
for (int r = 0;r<map.height;r++){ for (int r = 0;r<map.height;r++){
// Next cycle through each column or tile in that row: // Next cycle through each column or tile in that row:
for (int c = 0;c<map.width;c++){ for (int c = 0;c<map.width;c++){
int i = r*map.width+c; int i = r*map.width+c;
int elem = c+r*map.width; int elem = c+r*map.width;
int tileToDraw = map.layerCollection[0].tiles[i].gid-1; int tileToDraw = map.layerCollection[0].tiles[i].gid-1;
// 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;
// destRects[elem] = thisRect;
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE); TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
// }
} }
} }

View File

@ -77,7 +77,7 @@ public:
std::tuple<SDL_Rect, SDL_Rect> getLetterTexture(char currentLetter, int i) std::tuple<SDL_Rect, SDL_Rect> getLetterTexture(char currentLetter, int i)
{ {
std::tuple<SDL_Rect, SDL_Rect> letterTuple; std::tuple<SDL_Rect, SDL_Rect> letterTuple;
int charsPerLine = std::floor(transform->width/letterWidth); int charsPerLine = std::floor(transform->width/(letterWidth*scale));
srcRect.x = ((currentLetter-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth; srcRect.x = ((currentLetter-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
srcRect.y = ((currentLetter-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight; srcRect.y = ((currentLetter-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
srcRect.w = letterWidth; srcRect.w = letterWidth;

View File

@ -164,7 +164,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
// std::string myText = "Find lost puppies!\nThey need your help!"; // std::string myText = "Find lost puppies!\nThey need your help!";
std::string myText = "Press U to Start"; std::string myText = "Press U to Start";
uiTextInstructions.addComponent<TransformComponent>(18,22,138,20,gScale); uiTextInstructions.addComponent<TransformComponent>(18,22,138*gScale,20*gScale,gScale);
uiTextInstructions.addComponent<UITextComponent>("font",myText,8,12,gScale); uiTextInstructions.addComponent<UITextComponent>("font",myText,8,12,gScale);
uiTextInstructions.addGroup(groupUI_Layer1); uiTextInstructions.addGroup(groupUI_Layer1);
@ -176,25 +176,27 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
my9Slice = new UINineSlice("textBox"); my9Slice = new UINineSlice("textBox");
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0); my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0);
uiInfo.addComponent<TransformComponent>((camera.w-94)*gScale,10*gScale,72,96,gScale); // std::cout << "camera.w " << camera.w << std::endl;
uiInfo.addComponent<TransformComponent>(camera.w/gScale-94,10,72*gScale,96*gScale,gScale);
// uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1); // 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.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : P.dy: YVec: ", 8, 12, gScale);
uiInfo.addGroup(groupUI_Layer3); uiInfo.addGroup(groupUI_Layer3);
uiCamXInfo.addComponent<TransformComponent>((camera.w-48)*gScale,24*gScale,40,12,gScale); uiCamXInfo.addComponent<TransformComponent>(camera.w/gScale-48,24,40*gScale,12*gScale,gScale);
uiCamXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale); uiCamXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiCamXInfo.addGroup(groupUI_Layer3); uiCamXInfo.addGroup(groupUI_Layer3);
uiCamYInfo.addComponent<TransformComponent>((camera.w-48)*gScale,36*gScale,40,12,gScale); uiCamYInfo.addComponent<TransformComponent>(camera.w/gScale-48,36,40*gScale,12*gScale,gScale);
uiCamYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, 1); uiCamYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiCamYInfo.addGroup(groupUI_Layer3); uiCamYInfo.addGroup(groupUI_Layer3);
uiPlayerXInfo.addComponent<TransformComponent>((camera.w-48)*gScale,48*gScale,40,12,gScale); uiPlayerXInfo.addComponent<TransformComponent>(camera.w/gScale-48,48,40*gScale,12*gScale,gScale);
uiPlayerXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, 1); uiPlayerXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiPlayerXInfo.addGroup(groupUI_Layer3); uiPlayerXInfo.addGroup(groupUI_Layer3);
uiPlayerYInfo.addComponent<TransformComponent>((camera.w-48)*gScale,60*gScale,40,12,gScale); uiPlayerYInfo.addComponent<TransformComponent>(camera.w/gScale-48,60,40*gScale,12*gScale,gScale);
uiPlayerYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, 1); uiPlayerYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiPlayerYInfo.addGroup(groupUI_Layer3); uiPlayerYInfo.addGroup(groupUI_Layer3);
// uiJumpInfo.addComponent<TransformComponent>(camera.w-48,48,40,12,gScale); // uiJumpInfo.addComponent<TransformComponent>(camera.w-48,48,40,12,gScale);
@ -213,7 +215,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
//ecs implementation //ecs implementation
// player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale); // player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale);
player.addComponent<TransformComponent>(150,100,22,42,globalScale); // 180,120 player.addComponent<TransformComponent>(150*gScale,100*gScale,22,42,globalScale); // 180,120
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json"); player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
// player.addComponent<PlayerController>(0.0,0.0,false,false,Vector2D().Zero()); // player.addComponent<PlayerController>(0.0,0.0,false,false,Vector2D().Zero());
@ -326,8 +328,12 @@ void Game::update()
camera.x = player.getComponent<TransformComponent>().position.x - camera.w/2 + player.getComponent<TransformComponent>().width/2; camera.x = player.getComponent<TransformComponent>().position.x - camera.w/2 + player.getComponent<TransformComponent>().width/2;
camera.y = player.getComponent<TransformComponent>().position.y - camera.h/2 + player.getComponent<TransformComponent>().height/2; camera.y = player.getComponent<TransformComponent>().position.y - camera.h/2 + player.getComponent<TransformComponent>().height/2;
uiCamXInfo.getComponent<UITextComponent>().updateString(std::to_string(camera.x));
uiCamYInfo.getComponent<UITextComponent>().updateString(std::to_string(camera.y)); int pTileX = player.getComponent<TransformComponent>().position.x/gScale/16;
int pTileY = player.getComponent<TransformComponent>().position.y/gScale/16;
uiCamXInfo.getComponent<UITextComponent>().updateString(std::to_string(pTileX));
uiCamYInfo.getComponent<UITextComponent>().updateString(std::to_string(pTileY));
int playerX = player.getComponent<TransformComponent>().position.x; int playerX = player.getComponent<TransformComponent>().position.x;
int playerY = player.getComponent<TransformComponent>().position.y; int playerY = player.getComponent<TransformComponent>().position.y;