diff --git a/src/TileMap.h b/src/TileMap.h index dd8fdbb..63ea82a 100644 --- a/src/TileMap.h +++ b/src/TileMap.h @@ -1,11 +1,91 @@ #ifndef TILEMAP_H #define TILEMAP_H -#include #include +#include +#include +#include +#include +#include +#include + +#include + +using json = nlohmann::json; + +struct TileMapData { + std::vector tiles; + unsigned int width; + unsigned int height; + unsigned int tileSize; +}; + class TileMap : public sf::Drawable, public sf::Transformable { public: + std::vector tiles; + unsigned int width; + unsigned int height; + unsigned int tileSize; + + void loadFromJSON(const std::filesystem::path& jsonPath) { + std::ifstream file(jsonPath); + if (!file.is_open()) { + throw std::runtime_error("Failed to open JSON file: " + jsonPath.string()); + } + + json data = json::parse(file); + file.close(); + + //TileMapData result; + //result.tileSize = data["tileSize"].get(); + + tileSize = data["tileSize"].get(); + + // Get the first layer's tiles + if (data["layers"].empty() || data["layers"][0]["tiles"].empty()) { + throw std::runtime_error("No tiles found in JSON"); + } + + const auto& tilesArray = data["layers"][0]["tiles"]; + + // Find the bounds of the tilemap + int minX = std::numeric_limits::max(); + int minY = std::numeric_limits::max(); + int maxX = std::numeric_limits::min(); + int maxY = std::numeric_limits::min(); + + for (const auto& tile : tilesArray) { + int x = tile["x"].get(); + int y = tile["y"].get(); + minX = std::min(minX, x); + minY = std::min(minY, y); + maxX = std::max(maxX, x); + maxY = std::max(maxY, y); + } + + // Calculate grid dimensions + width = (maxX - minX) / tileSize + 1; + height = (maxY - minY) / tileSize + 1; + + // Initialize the grid with zeros (empty tiles) + tiles.resize(width * height, 0); + + // Fill in the tiles from the JSON + for (const auto& tile : tilesArray) { + int x = tile["x"].get(); + int y = tile["y"].get(); + int tileId = std::stoi(tile["id"].get()); + + // Convert pixel position to grid position + int gridX = (x - minX) / tileSize; + int gridY = (y - minY) / tileSize; + + // Store in the grid + tiles[gridX + gridY * width] = tileId; + } + } + bool load(const std::filesystem::path& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height) { // load the tileset texture @@ -52,6 +132,20 @@ public: return true; } + int convert2DCoords(int row, int col, int width) { + return col + (row * width); + } + + int getTileId(int tileX, int tileY) { + //tileX and tileY start from top left 0,0 + //tileX increases going right + //tileY increases going down + + int index = convert2DCoords(tileY, tileX, 28); + + return 0; + } + private: void draw(sf::RenderTarget& target, sf::RenderStates states) const override { diff --git a/src/TileMapLoader.h b/src/TileMapLoader.h deleted file mode 100644 index 08b6b9b..0000000 --- a/src/TileMapLoader.h +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef TILEMAPLOADER_H -#define TILEMAPLOADER_H - -#include -#include -#include -#include -#include -#include - -using json = nlohmann::json; - -struct TileMapData { - std::vector tiles; - unsigned int width; - unsigned int height; - unsigned int tileSize; -}; - -class TileMapLoader { -public: - static TileMapData loadFromJSON(const std::filesystem::path& jsonPath) { - std::ifstream file(jsonPath); - if (!file.is_open()) { - throw std::runtime_error("Failed to open JSON file: " + jsonPath.string()); - } - - json data = json::parse(file); - file.close(); - - TileMapData result; - result.tileSize = data["tileSize"].get(); - - // Get the first layer's tiles - if (data["layers"].empty() || data["layers"][0]["tiles"].empty()) { - throw std::runtime_error("No tiles found in JSON"); - } - - const auto& tilesArray = data["layers"][0]["tiles"]; - - // Find the bounds of the tilemap - int minX = std::numeric_limits::max(); - int minY = std::numeric_limits::max(); - int maxX = std::numeric_limits::min(); - int maxY = std::numeric_limits::min(); - - for (const auto& tile : tilesArray) { - int x = tile["x"].get(); - int y = tile["y"].get(); - minX = std::min(minX, x); - minY = std::min(minY, y); - maxX = std::max(maxX, x); - maxY = std::max(maxY, y); - } - - // Calculate grid dimensions - result.width = (maxX - minX) / result.tileSize + 1; - result.height = (maxY - minY) / result.tileSize + 1; - - // Initialize the grid with zeros (empty tiles) - result.tiles.resize(result.width * result.height, 0); - - // Fill in the tiles from the JSON - for (const auto& tile : tilesArray) { - int x = tile["x"].get(); - int y = tile["y"].get(); - int tileId = std::stoi(tile["id"].get()); - - // Convert pixel position to grid position - int gridX = (x - minX) / result.tileSize; - int gridY = (y - minY) / result.tileSize; - - // Store in the grid - result.tiles[gridX + gridY * result.width] = tileId; - } - - return result; - } -}; - -#endif diff --git a/src/main.cpp b/src/main.cpp index d2a1b32..76dc4d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,15 +6,15 @@ #include #include #include -#include "TileMapLoader.h" +//#include "TileMapLoader.h" #include "TileMap.h" int main() { // Load tilemap data from JSON - TileMapData tilemapData; + TileMap mazeMap; try { - tilemapData = TileMapLoader::loadFromJSON("util/pacmantiles32.json"); + mazeMap.loadFromJSON("util/pacmantiles32.json"); } catch (const std::exception& e) { std::cerr << "Error loading tilemap: " << e.what() << std::endl; return -1; @@ -22,10 +22,10 @@ int main() TileMap map; if (!map.load("assets/maze32.png", - {tilemapData.tileSize, tilemapData.tileSize}, - tilemapData.tiles.data(), - tilemapData.width, - tilemapData.height)) + {mazeMap.tileSize, mazeMap.tileSize}, + mazeMap.tiles.data(), + mazeMap.width, + mazeMap.height)) return -1; auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen"); @@ -38,7 +38,7 @@ int main() sf::CircleShape pacman(20.0f); pacman.setFillColor(sf::Color(255, 255, 0)); - pacman.move({32, 32}); + pacman.move({(mazeMap.tileSize * 1.0f), (mazeMap.tileSize * 1.0f)}); int x = 0; int y = 0; @@ -49,6 +49,11 @@ int main() sf::Text tileY(bitFont, "0"); tileY.move({1100, 100}); + sf::Text tileId(bitFont, ""); + tileId.move({1000, 150}); + + int tileInt = 0; + while (window.isOpen()) { while (const std::optional event = window.pollEvent()) @@ -59,12 +64,13 @@ int main() } } - window.clear(); + window.clear(); window.draw(map); window.draw(currTile); window.draw(tileX); window.draw(tileY); + window.draw(tileId); window.draw(pacman);