diff --git a/build-debug/sfml-test b/build-debug/sfml-test index 64af38c..d9c7f87 100755 Binary files a/build-debug/sfml-test and b/build-debug/sfml-test differ diff --git a/build-debug/test_map b/build-debug/test_map index 2e2f488..cb31002 100644 --- a/build-debug/test_map +++ b/build-debug/test_map @@ -2,13 +2,13 @@ size 7 7 map 2 2 2 2 2 2 2 -2 0 0 0 0 6 2 +2 0 3 0 0 6 2 2 0 0 1 0 0 2 2 0 0 1 1 0 2 2 1 1 2 2 2 2 -2 0 0 0 0 5 2 +2 0 0 0 4 0 2 2 2 2 2 2 2 2 -teleport -5 5 1 1 trigger -1 5 4 5 0 +1 5 4 5 6 3 1 3 1 +charge +1 2 2 diff --git a/src/cell.cpp b/src/cell.cpp index a9be75b..a90daaa 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -111,6 +111,11 @@ ChargeCell::ChargeCell(coordinate cell_row, coordinate cell_col, int has_charges ChargeCell::~ChargeCell() {} +void ChargeCell::setCharges(const int &num_charges) +{ + cell_charges = num_charges; +} + bool ChargeCell::onMovingTo(HeroPtr &hero, LevelPtr &level) { // Hero picks up the charge; remove it from the map @@ -159,6 +164,12 @@ TeleportCell::TeleportCell(coordinate cell_row, coordinate cell_col, coordinate TeleportCell::~TeleportCell() {} +void TeleportCell::setDestination(coordinate new_cell_row, coordinate new_cell_col) +{ + new_row = new_cell_row; + new_col = new_cell_col; +} + bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level) { UNUSED(level); @@ -168,12 +179,6 @@ bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } -void TeleportCell::setDestination(coordinate new_cell_row, coordinate new_cell_col) -{ - new_row = new_cell_row; - new_col = new_cell_col; -} - CellPtr TeleportCell::clone() const { return std::make_unique(); @@ -181,8 +186,6 @@ CellPtr TeleportCell::clone() const /////////////////////////////////////// -const std::vector TriggerCell::cells_to_cast { PASSABLE_CELL, WATER_CELL, WALL_CELL, EXIT_CELL }; - TriggerCell::TriggerCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : Cell(cell_row, cell_col, color) { diff --git a/src/cell.h b/src/cell.h index 565592c..c84bacd 100644 --- a/src/cell.h +++ b/src/cell.h @@ -137,6 +137,9 @@ public: int has_charges = 1, const sf::Color &color = palette::Green); + /// Sets value of cell_charges to num_charges + void setCharges(const int &num_charges); + virtual ~ChargeCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; @@ -196,9 +199,6 @@ private: std::vector vector_cells; public: - /// Vector of cell types you can cast in to - static const std::vector cells_to_cast; - TriggerCell(coordinate cell_row = 0, coordinate cell_col = 0, const sf::Color &color = palette::Pink); diff --git a/src/game.cpp b/src/game.cpp index 4c26c6d..50d7a97 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -13,7 +13,7 @@ Game::Game() // Prepare level renderer renderer = std::make_unique(); - main_window.create(sf::VideoMode(renderer->windowSize() * 3, renderer->windowSize() * 3), "SFML-Test Application", sf::Style::Default); + main_window.create(sf::VideoMode(renderer->windowSize() * 3, renderer->windowSize() * 2), "SFML-Test Application", sf::Style::Default); main_window.setActive(); main_window.setFramerateLimit(60); diff --git a/src/level.cpp b/src/level.cpp index 326b4fc..dacd937 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -1,6 +1,5 @@ #include "level.h" -#include #include #include #include @@ -42,11 +41,9 @@ void Level::Map::init(const std::string &path) break; case SECTION::TELEPORT: - readTeleport(sstr); - break; - + case SECTION::CHARGE: case SECTION::TRIGGER: - readTrigger(sstr); + readCellSection(sstr, cur_section); break; default: @@ -85,34 +82,76 @@ void Level::Map::readMapRow(std::istringstream &sstr) } } -void Level::Map::readTeleport(std::istringstream &sstr) +void Level::Map::readCellSection(std::istringstream &sstr, const SECTION §ion) { coordinate src_row, src_col; + sstr >> src_row >> src_col; + + switch (section) { + case SECTION::TELEPORT: + data[src_row][src_col] = readTeleport(sstr, static_unique_pointer_cast(std::move(data[src_row][src_col]))); + break; + + case SECTION::CHARGE: + data[src_row][src_col] = readCharge(sstr, static_unique_pointer_cast(std::move(data[src_row][src_col]))); + break; + + case SECTION::TRIGGER: + data[src_row][src_col] = readTrigger(sstr, static_unique_pointer_cast(std::move(data[src_row][src_col]))); + break; + + default: + break; + } +} + +std::unique_ptr &&Level::Map::readTeleport(std::istringstream &sstr, std::unique_ptr &&cell) +{ coordinate dest_row, dest_col; - sstr >> src_row >> src_col >> dest_row >> dest_col; - auto teleport_cell = static_unique_pointer_cast(std::move(data[src_row][src_col])); - teleport_cell->setDestination(dest_row, dest_col); - data[src_row][src_col] = std::move(teleport_cell); + sstr >> dest_row >> dest_col; + cell->setDestination(dest_row, dest_col); + return std::move(cell); } -void Level::Map::readTrigger(std::istringstream &sstr) +std::unique_ptr &&Level::Map::readCharge(std::istringstream &sstr, std::unique_ptr &&cell) +{ + int num_charges; + + sstr >> num_charges; + cell->setCharges(num_charges); + return std::move(cell); +} + +std::unique_ptr &&Level::Map::readTrigger(std::istringstream &sstr, std::unique_ptr &&cell) { - coordinate src_row, src_col; coordinate dest_row, dest_col; int cell_type; - sstr >> src_row >> src_col >> dest_row >> dest_col >> cell_type; - - if (std::find(TriggerCell::cells_to_cast.begin(), TriggerCell::cells_to_cast.end(), cell_type) == - TriggerCell::cells_to_cast.end()) - return ; + sstr >> dest_row >> dest_col >> cell_type; - auto trigger_cell = static_unique_pointer_cast(std::move(data[src_row][src_col])); auto dest_cell = default_cells[cell_type]->clone(); dest_cell->setPosition(dest_row, dest_col); - trigger_cell->addTarget(std::move(dest_cell)); - data[src_row][src_col] = std::move(trigger_cell); + + switch (cell_type) { + case TELEPORT_CELL: + dest_cell = readTeleport(sstr, static_unique_pointer_cast(std::move(dest_cell))); + break; + + case CHARGE_CELL: + dest_cell = readCharge(sstr, static_unique_pointer_cast(std::move(dest_cell))); + break; + + case TRIGGER_CELL: + dest_cell = readTrigger(sstr, static_unique_pointer_cast(std::move(dest_cell))); + break; + + default: + break; + } + + cell->addTarget(std::move(dest_cell)); + return std::move(cell); } Level::Level(const std::string &path) diff --git a/src/level.h b/src/level.h index dae25f6..4e1e5d9 100644 --- a/src/level.h +++ b/src/level.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "cell.h" @@ -25,6 +26,7 @@ private: SIZE, MAP, TELEPORT, + CHARGE, TRIGGER, NONE }; @@ -34,6 +36,7 @@ private: { "size", SECTION::SIZE }, { "map", SECTION::MAP }, { "teleport", SECTION::TELEPORT }, + { "charge", SECTION::CHARGE }, { "trigger", SECTION::TRIGGER }, { "", SECTION::NONE } }; @@ -50,8 +53,10 @@ private: // Map file section readers void readMapSize(std::istringstream &sstr); void readMapRow(std::istringstream &sstr); - void readTeleport(std::istringstream &sstr); - void readTrigger(std::istringstream &sstr); + void readCellSection(std::istringstream &sstr, const SECTION §ion); + std::unique_ptr &&readTeleport(std::istringstream &sstr, std::unique_ptr &&cell); + std::unique_ptr &&readCharge(std::istringstream &sstr, std::unique_ptr &&cell); + std::unique_ptr &&readTrigger(std::istringstream &sstr, std::unique_ptr &&cell); }; Map map;