You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6 KiB
C++

#include "level.h"
#include <fstream>
#include <cstring>
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].reset(default_cells[i]);
map[j][k]->setPosition(j, k);
}
}
}
template<typename D, typename B> // [D]erived - [B]ase
unique_ptr<D> static_unique_pointer_cast (unique_ptr<B>&& old)
{
return unique_ptr<D>{static_cast<D*>(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 (strstr(cur_line.data(), "size") != NULL)
{
file >> level_width >> level_height;
map.resize(level_height);
for (Row &row : map)
row.resize(level_width);
}
else if (strstr(cur_line.data(), "map") != NULL)
{
readMap(file);
}
else if (strstr(cur_line.data(), "teleport") != NULL)
{
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<TeleportCell>(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::width() const
{
return level_width;
}
size_t Level::height() const
{
return level_height;
}
void Level::placeBridge(coordinate row, coordinate col)
{
map[row][col] = std::make_unique<PassableCell>(row, col, palette::Black);
}
void Level::removeCharge(coordinate row, coordinate col)
{
map[row][col] = std::make_unique<PassableCell>(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;
}