KaijuSaveEarth/src/ecs/TileMapComponent.h
2023-07-24 13:41:02 -04:00

184 lines
6.2 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 "../tileson/tileson.hpp"
//#include "../../libtmx-parser/src/tmxparser.h"
class TileMapComponent : public Component
{
public:
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
//tmxparser::TmxMap map;
//tileson::Tileson map;
int globalScale;
std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
std::vector<std::tuple<int,int>> initialPositions;
int tileSetTotal;
std::vector<std::vector<int>> colliders;
int tilesWide;
int tilesHigh;
int tileWidth;
TileMapComponent() = default;
~TileMapComponent()
{
SDL_DestroyTexture(texture);
}
TileMapComponent(std::string mapPath, int gScale, int offsetX, int offsetY)
{
// ***********************************************************************************
// TILESON ~~~~~~~~~~~
tson::Tileson t;
const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath);
std::unique_ptr<tson::Map> map = t.parse(jsonPath);
std::cout << jsonPath << std::endl;
if(map->getStatus() == tson::ParseStatus::OK)
{
printf("Tileson successfully parsed the tilemap\n");
std::cout << map->getStatusMessage() << std::endl;
//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);
// }
//}
// *******************************************************************************************
//map = loadedMap;
tson::Tileset *tileset = map->getTileset("br-tiles");
std::cout << "Image Path: " << tileset->getImage() << std::endl;
//tson::Tile *tile = tileset->getTile(1);
//std::string myTexPath = tile->getImage();
//tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//std::cout << "\nTexturePath: \n" << myTexPath << "\n" << std::endl;
//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;
//colliders.resize(map.height, std::vector<int>(map.width, 0));
tilesWide = map->getSize().x;
//printf("tilesWide: %d\n",tilesWide);
tilesHigh = map->getSize().y;
//printf("tilesHigh: %d\n",tilesHigh);
tileWidth = map->getTileSize().x;
//printf("tileSizeWidth: %d\n",tileWidth);
// =========== Setup Tile Set ===========
//tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
//tileSet.resize(tileSetTotal);
/*
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;
}
}
// =========== 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 = map.tileWidth * gScale;
*/
} else {
printf("Failed to load Tileson map\n");
std::cout << map->getStatusMessage();
//printf(map->getStatus());
}
}
void update() override
{
// if (Game::gsm->currentState == GameStateManager::ST_COREGAME){
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;
}
// }
}
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;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;
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
if (Game::debugMenu){
if (map.layerCollection[1].tiles[i].gid != 0) {
SDL_SetRenderDrawColor(Game::renderer,255,0,255,134);
SDL_RenderDrawRect(Game::renderer, &destRects[elem]);
}
}
}
}*/
}
void setTex(std::string id)
{
texture = Game::assets->GetTexture(id);
}
};
#endif /* SRC_ECS_TILEMAPCOMPONENT_H_ */