mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
add boilerplate map loading from json file
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
struct TilemapData {
|
||||
std::vector<int> 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<unsigned int>();
|
||||
|
||||
// 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<int>::max();
|
||||
int minY = std::numeric_limits<int>::max();
|
||||
int maxX = std::numeric_limits<int>::min();
|
||||
int maxY = std::numeric_limits<int>::min();
|
||||
|
||||
for (const auto& tile : tilesArray) {
|
||||
int x = tile["x"].get<int>();
|
||||
int y = tile["y"].get<int>();
|
||||
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>();
|
||||
int y = tile["y"].get<int>();
|
||||
int tileId = std::stoi(tile["id"].get<std::string>());
|
||||
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
+15
-11
@@ -2,6 +2,8 @@
|
||||
#include <SFML/Graphics/CircleShape.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include <iostream>
|
||||
#include "TilemapLoader.h"
|
||||
|
||||
class TileMap : public sf::Drawable, public sf::Transformable {
|
||||
public:
|
||||
@@ -70,19 +72,21 @@ private:
|
||||
|
||||
int main()
|
||||
{
|
||||
constexpr std::array level = {
|
||||
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
|
||||
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
|
||||
0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
|
||||
0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
|
||||
0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
|
||||
2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
|
||||
0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
|
||||
};
|
||||
// Load tilemap data from JSON
|
||||
TilemapData tilemapData;
|
||||
try {
|
||||
tilemapData = TilemapLoader::loadFromJSON("util/pacmantiles.json");
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error loading tilemap: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
TileMap map;
|
||||
if (!map.load("assets/tileset.png", {32, 32}, level.data(), 16, 8))
|
||||
if (!map.load("assets/maze.png",
|
||||
{tilemapData.tileSize, tilemapData.tileSize},
|
||||
tilemapData.tiles.data(),
|
||||
tilemapData.width,
|
||||
tilemapData.height))
|
||||
return -1;
|
||||
|
||||
auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen");
|
||||
|
||||
Reference in New Issue
Block a user