QJsonObject JsonSaver::saveCity(const City& city) { QJsonObject res; res.insert("name", QString::fromStdString(city.name())); QJsonArray buildings; int i = 0; for(const auto& b : city.buildings()) buildings.insert(i++, saveBuilding(b)); res.insert("buildings", buildings); res.insert("size", saveSizeU(city.map().size())); QJsonArray roads; i = 0; for(std::size_t x = 0; x < city.map().width(); x++) for(std::size_t y = 0; y < city.map().height(); y++) if(city.map().squareType({x, y}) == Map::SquareType::Road) { roads.insert(i++, savePointU({x, y})); } res.insert("roads", roads); return res; }
#include <catch.hpp> #include <world/city.hpp> using namespace World; TEST_CASE("Test Map") { World::BuildingTypeHandler::instance().clear(); const auto bt = World::BuildingTypeHandler::instance().add({"test_building", utils::SizeU(10, 5), {}}); City city{"TestTown", {10, 10}}; const Map& map = city.map(); SECTION("Empty On Creation") { for(std::size_t x = 0; x < map.width(); x++) for(std::size_t y = 0; y < map.height(); y++) CHECK(map.squareIsEmpty(utils::PointU(x, y))); } SECTION("Squares Not Empty After Placing A Building") { city.add(Building(bt, utils::PointU(5, 5), utils::RectU(utils::PointU(2, 2), utils::PointU(5, 4)))); for(std::size_t x = 0; x < map.width(); x++) for(std::size_t y = 0; y < map.height(); y++) { if(x >= 2 && x <= 5 && y >= 2 && y <= 4) CHECK(!map.squareIsEmpty(utils::PointU(x, y))); else CHECK(map.squareIsEmpty(utils::PointU(x, y)));