#include "level.h" #include #include void Level::prepareCellInstances() { default_cells[PASSABLE_CELL] = new PassableCell(); default_cells[WATER_CELL] = new WaterCell(); default_cells[WALL_CELL] = new WallCell(); default_cells[CHARGE_CELL] = new ChargeCell(); default_cells[EXIT_CELL] = new ExitCell(); default_cells[TELEPORT_CELL] = new TeleportCell(); default_cells[TRIGGER_CELL] = new TriggerCell(); } void Level::readMap(std::ifstream &file) { int i; for (coordinate j = 0; j < map.size(); ++j) { for (coordinate k = 0; k < map[j].size(); ++k) { file >> i; map[j][k] = default_cells[i]->getDefaultInstance(); map[j][k]->setPosition(j, k); } } } template // [D]erived - [B]ase std::unique_ptr static_unique_pointer_cast (std::unique_ptr&& old) { return std::unique_ptr{static_cast(old.release())}; } Level::Level(const std::string &map_file) { prepareCellInstances(); std::ifstream file; file.open(map_file); std::string cur_line; while (getline(file, cur_line)) { // need fix; see std::string.compare if (cur_line.compare(0, 4, "size") == 0) { file >> level_rows >> level_cols; map.resize(level_rows); for (Row &row : map) row.resize(level_cols); } else if (cur_line.compare(0, 3, "map") == 0) { readMap(file); } else if (cur_line.compare(0, 8, "teleport") == 0) { coordinate src_row, src_col; coordinate dest_row, dest_col; file >> src_row >> src_col >> dest_row >> dest_col; auto teleport_cell = static_unique_pointer_cast(std::move(map[src_row][src_col])); teleport_cell->setDestination(dest_row, dest_col); map[src_row][src_col] = std::move(teleport_cell); } } } Level::~Level() { for (Cell *cell : default_cells) delete cell; } size_t Level::rows() const { return level_rows; } size_t Level::cols() const { return level_cols; } void Level::placeBridge(coordinate row, coordinate col) { map[row][col] = std::make_unique(row, col, palette::Black); } void Level::removeCharge(coordinate row, coordinate col) { map[row][col] = std::make_unique(row, col, color_ground); } Map& Level::mapArray() { return map; } sf::Color Level::defaultGroundColor() { return color_ground; } void Level::setDefaultGroundColor(const sf::Color &color) { color_ground = color; }