KaijuSaveEarth/src/ecs/TileMapComponent.h
2023-08-12 14:44:08 -04:00

178 lines
5.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 <SDL.h>
#include "../assetmgr/AssetManager.h"
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
#include "../tileson/tileson.hpp"
class TileMapComponent : public Component
{
public:
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
tson::Tileson t;
std::unique_ptr<tson::Map> 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, std::string tsName, std::string tileLayerName, std::string collisionLayerName, int gScale, int offsetX, int offsetY)
{
// TILESON ~~~~~~~~~~~
//const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath);
const std::filesystem::path jsonPath = std::filesystem::u8path("/Users/ayoungblood/Projects/KaijuSaveEarth/assets/maps/kaiju-city-map.json");
map = t.parse(jsonPath);
//std::cout << "Trying to load json tile map from: " << jsonPath << std::endl;
if(map->getStatus() == tson::ParseStatus::OK)
{
// printf("Map File Loaded!! \n");
tson::Tileset *tileset = map->getTileset(tsName);
//std::cout << "tsName: " << tsName << std::endl;
std::string fullPath = tileset->getImage();
//std::cout << "fullPath: " << fullPath << std::endl;
//size_t charPos = fullPath.find("assets");
//fullPath.erase(0,charPos);
std::string prependPath = std::string("assets/maps/");
std::string localPath = prependPath + fullPath;
std::string wholePath = "/Users/ayoungblood/Projects/KaijuSaveEarth/" + localPath;
//std::cout << "fullPath: " << wholePath << std::endl;
tson::Layer *tileLayer = map->getLayer(tileLayerName); //This is a Layer
std::string texName = tileLayer->getName();
Game::assets->AddTexture(texName, wholePath.c_str());
setTex(texName);
globalScale = gScale;
//printf("Added texture for tilemap.\n");
tson::Layer *collisionLayer = map->getLayer(collisionLayerName);
tilesWide = map->getSize().x;
tilesHigh = map->getSize().y;
tileWidth = map->getTileSize().x;
Game::levelMap.w = tilesWide*tileWidth*globalScale;
Game::levelMap.h = tilesHigh*tileWidth*globalScale;
// printf("First phase tile map init done, moving to Tileset\n");
// =========== Setup Tile Set ===========
tileSetTotal = tileset->getTileCount();
tileSet.resize(tileSetTotal);
int tileSetCols = tileset->getColumns();
int tileSetRows = tileSetTotal/tileSetCols;
for (int r=0;r<tileSetRows;r++){
for (int c=0;c<tileSetCols;c++){
srcRect.x = c*tileWidth;
srcRect.y = r*tileWidth;
srcRect.w = srcRect.h = tileWidth;
int element = r*tileSetCols+c;
tileSet[element] = srcRect;
}
}
//printf("Tile Set Setup Completed.\n");
if(tileLayer->getType() == tson::LayerType::TileLayer)
{
destRects.resize(tilesWide*tilesHigh);
initialPositions.resize(tilesWide*tilesHigh);
for (int r=0;r<tilesHigh;r++){
for (int c=0;c<tilesWide;c++){
int elem = c+r*tilesWide;
SDL_Rect thisRect = SDL_Rect();
thisRect.x = c*tileWidth*globalScale;
thisRect.y = r*tileWidth*globalScale;
thisRect.w = thisRect.h = 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;
}
}
destRect.w = destRect.h = tileWidth * gScale;
}
//printf("Tilemap setup completed\n");
//std::cout << "destRects.size " << destRects.size() << std::endl;
} else {
// printf("Failed to load Tileson map\n");
std::cout << map->getStatusMessage() << std::endl;
}
}
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
tson::Layer *myLayer = map->getLayer("Tile Layer 1");
tson::Layer *collisionLayer = map->getLayer("Collision");
for (int r = 0;r<map->getSize().y;r++){
// Next cycle through each column or tile in that row:
for (int c = 0;c<map->getSize().x;c++){
int i = r*map->getSize().x+c;
int elem = c+r*map->getSize().x;
tson::Tile *myTile = myLayer->getTileData(c,r);
if(!myTile){
// Found a nullptr, nothing to do or see here, move along
} else {
int tid = myTile->getId()-1;
int tileToDraw = tid;
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
}
if (Game::debugMenu){
if (!collisionLayer->getTileData(c,r)) {
// Nothing to see here, just returning a nullptr, move along...
} else {
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_ */