#include "TilePack.h" #include #include "Logger.hpp" asst::TilePack::~TilePack() = default; bool asst::TilePack::load(const std::string& dir) { LogTraceFunction; if (!std::filesystem::exists(dir)) { return false; } constexpr static const char* filename = "/levels.json"; try { m_tile_calculator = std::make_unique( WindowWidthDefault, WindowHeightDefault, dir + filename); } catch (std::exception& e) { m_last_error = e.what(); return false; } return m_tile_calculator != nullptr; } std::unordered_map asst::TilePack::calc( const std::string& stage_code, bool side) const { LogTraceFunction; std::vector> pos; std::vector> tiles; bool ret = m_tile_calculator->run(stage_code, side, pos, tiles); Log.trace("After tiles cacl run"); if (!ret) { Log.info("Tiles calc error!"); return std::unordered_map(); } std::unordered_map dst; static const std::unordered_map TileKeyMapping = { { "tile_forbidden", TileKey::Forbidden }, { "tile_wall", TileKey::Wall }, { "tile_road", TileKey::Road }, { "tile_end", TileKey::Home }, { "tile_start", TileKey::EnemyHome }, { "tile_floor", TileKey::Floor }, { "tile_hole", TileKey::Hole }, { "tile_telin", TileKey::Telin }, { "tile_telout", TileKey::Telout } }; for (size_t y = 0; y < pos.size(); ++y) { for (size_t x = 0; x < pos[y].size(); ++x) { const auto& cv_p = pos[y][x]; const auto& tile = tiles[y][x]; TileKey key = TileKey::Invalid; if (auto iter = TileKeyMapping.find(tile.tileKey); iter != TileKeyMapping.cend()) { key = iter->second; } else { key = TileKey::Invalid; Log.error("Unknown tile type:", tile.tileKey); } Point loc(static_cast(x), static_cast(y)); dst.emplace(loc, TileInfo{ static_cast(tile.buildableType), static_cast(tile.heightType), key, Point(static_cast(cv_p.x), static_cast(cv_p.y)), loc }); } } return dst; }