KaijuSaveEarth/src/ecs/TileMapComponent.h

139 lines
4.5 KiB
C++

/*
* TileMapComponent.h
*
* Created on: Mar 21, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_TILEMAPCOMPONENT_H_
#define SRC_ECS_TILEMAPCOMPONENT_H_
#include "ECS.h"
#include "SDL2/SDL.h"
#include "../assetmgr/AssetManager.h"
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
// #include <algorithm>
// #include <iterator>
#include "tmxparser.h"
class TileMapComponent : public Component
{
public:
// TransformComponent *transform;
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
// Vector2D position;
// std::tuple <SDL_Rect,SDL_Rect> tile;
tmxparser::TmxMap map;
int globalScale;
std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
// std::array destRects;
int totalTiles;
TileMapComponent() = default;
~TileMapComponent()
{
SDL_DestroyTexture(texture);
}
TileMapComponent(tmxparser::TmxMap loadedMap, int gScale)
{
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;
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;
srcRect.y = r*map.tilesetCollection[0].tileHeight;
srcRect.w = srcRect.h = map.tileWidth;
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;
}
}
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++){
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;
// destRects.push_back(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;
}
void update() override
{
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;
}
}
}
void draw() override
{
//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++){
// Next cycle through each column or tile in that row:
for (int c = 0;c<map.width-1;c++){
int i = r*map.width+c;
int elem = c+r*map.width;
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);
// }
}
}
}
void setTex(std::string id)
{
texture = Game::assets->GetTexture(id);
}
};
#endif /* SRC_ECS_TILEMAPCOMPONENT_H_ */