Extend map parser; now it's available to add complex cells in triggers

This commit is contained in:
oss 2020-03-21 01:40:54 +03:00
parent b9d19661ac
commit 34f35f4ac0
7 changed files with 87 additions and 40 deletions

Binary file not shown.

View File

@ -2,13 +2,13 @@ size
7 7 7 7
map map
2 2 2 2 2 2 2 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 0 0 2
2 0 0 1 1 0 2 2 0 0 1 1 0 2
2 1 1 2 2 2 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 2 2 2 2 2 2 2
teleport
5 5 1 1
trigger trigger
1 5 4 5 0 1 5 4 5 6 3 1 3 1
charge
1 2 2

View File

@ -111,6 +111,11 @@ ChargeCell::ChargeCell(coordinate cell_row, coordinate cell_col, int has_charges
ChargeCell::~ChargeCell() ChargeCell::~ChargeCell()
{} {}
void ChargeCell::setCharges(const int &num_charges)
{
cell_charges = num_charges;
}
bool ChargeCell::onMovingTo(HeroPtr &hero, LevelPtr &level) bool ChargeCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{ {
// Hero picks up the charge; remove it from the map // 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() 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) bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{ {
UNUSED(level); UNUSED(level);
@ -168,12 +179,6 @@ bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
return true; 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 CellPtr TeleportCell::clone() const
{ {
return std::make_unique<TeleportCell>(); return std::make_unique<TeleportCell>();
@ -181,8 +186,6 @@ CellPtr TeleportCell::clone() const
/////////////////////////////////////// ///////////////////////////////////////
const std::vector<CELL_TYPE> TriggerCell::cells_to_cast { PASSABLE_CELL, WATER_CELL, WALL_CELL, EXIT_CELL };
TriggerCell::TriggerCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : TriggerCell::TriggerCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
Cell(cell_row, cell_col, color) Cell(cell_row, cell_col, color)
{ {

View File

@ -137,6 +137,9 @@ public:
int has_charges = 1, int has_charges = 1,
const sf::Color &color = palette::Green); const sf::Color &color = palette::Green);
/// Sets value of cell_charges to num_charges
void setCharges(const int &num_charges);
virtual ~ChargeCell() override; virtual ~ChargeCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
@ -196,9 +199,6 @@ private:
std::vector<CellPtr> vector_cells; std::vector<CellPtr> vector_cells;
public: public:
/// Vector of cell types you can cast in to
static const std::vector<CELL_TYPE> cells_to_cast;
TriggerCell(coordinate cell_row = 0, TriggerCell(coordinate cell_row = 0,
coordinate cell_col = 0, coordinate cell_col = 0,
const sf::Color &color = palette::Pink); const sf::Color &color = palette::Pink);

View File

@ -13,7 +13,7 @@ Game::Game()
// Prepare level renderer // Prepare level renderer
renderer = std::make_unique<Renderer>(); renderer = std::make_unique<Renderer>();
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.setActive();
main_window.setFramerateLimit(60); main_window.setFramerateLimit(60);

View File

@ -1,6 +1,5 @@
#include "level.h" #include "level.h"
#include <sstream>
#include <fstream> #include <fstream>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -42,11 +41,9 @@ void Level::Map::init(const std::string &path)
break; break;
case SECTION::TELEPORT: case SECTION::TELEPORT:
readTeleport(sstr); case SECTION::CHARGE:
break;
case SECTION::TRIGGER: case SECTION::TRIGGER:
readTrigger(sstr); readCellSection(sstr, cur_section);
break; break;
default: 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 &section)
{ {
coordinate src_row, src_col; coordinate src_row, src_col;
coordinate dest_row, dest_col; sstr >> src_row >> src_col;
sstr >> src_row >> src_col >> dest_row >> dest_col; switch (section) {
auto teleport_cell = static_unique_pointer_cast<TeleportCell>(std::move(data[src_row][src_col])); case SECTION::TELEPORT:
teleport_cell->setDestination(dest_row, dest_col); data[src_row][src_col] = readTeleport(sstr, static_unique_pointer_cast<TeleportCell>(std::move(data[src_row][src_col])));
data[src_row][src_col] = std::move(teleport_cell); break;
case SECTION::CHARGE:
data[src_row][src_col] = readCharge(sstr, static_unique_pointer_cast<ChargeCell>(std::move(data[src_row][src_col])));
break;
case SECTION::TRIGGER:
data[src_row][src_col] = readTrigger(sstr, static_unique_pointer_cast<TriggerCell>(std::move(data[src_row][src_col])));
break;
default:
break;
}
} }
void Level::Map::readTrigger(std::istringstream &sstr) std::unique_ptr<TeleportCell> &&Level::Map::readTeleport(std::istringstream &sstr, std::unique_ptr<TeleportCell> &&cell)
{
coordinate dest_row, dest_col;
sstr >> dest_row >> dest_col;
cell->setDestination(dest_row, dest_col);
return std::move(cell);
}
std::unique_ptr<ChargeCell> &&Level::Map::readCharge(std::istringstream &sstr, std::unique_ptr<ChargeCell> &&cell)
{
int num_charges;
sstr >> num_charges;
cell->setCharges(num_charges);
return std::move(cell);
}
std::unique_ptr<TriggerCell> &&Level::Map::readTrigger(std::istringstream &sstr, std::unique_ptr<TriggerCell> &&cell)
{ {
coordinate src_row, src_col;
coordinate dest_row, dest_col; coordinate dest_row, dest_col;
int cell_type; int cell_type;
sstr >> src_row >> src_col >> dest_row >> dest_col >> cell_type; sstr >> 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 ;
auto trigger_cell = static_unique_pointer_cast<TriggerCell>(std::move(data[src_row][src_col]));
auto dest_cell = default_cells[cell_type]->clone(); auto dest_cell = default_cells[cell_type]->clone();
dest_cell->setPosition(dest_row, dest_col); 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<TeleportCell>(std::move(dest_cell)));
break;
case CHARGE_CELL:
dest_cell = readCharge(sstr, static_unique_pointer_cast<ChargeCell>(std::move(dest_cell)));
break;
case TRIGGER_CELL:
dest_cell = readTrigger(sstr, static_unique_pointer_cast<TriggerCell>(std::move(dest_cell)));
break;
default:
break;
}
cell->addTarget(std::move(dest_cell));
return std::move(cell);
} }
Level::Level(const std::string &path) Level::Level(const std::string &path)

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <array> #include <array>
#include <map> #include <map>
#include <sstream>
#include "cell.h" #include "cell.h"
@ -25,6 +26,7 @@ private:
SIZE, SIZE,
MAP, MAP,
TELEPORT, TELEPORT,
CHARGE,
TRIGGER, TRIGGER,
NONE NONE
}; };
@ -34,6 +36,7 @@ private:
{ "size", SECTION::SIZE }, { "size", SECTION::SIZE },
{ "map", SECTION::MAP }, { "map", SECTION::MAP },
{ "teleport", SECTION::TELEPORT }, { "teleport", SECTION::TELEPORT },
{ "charge", SECTION::CHARGE },
{ "trigger", SECTION::TRIGGER }, { "trigger", SECTION::TRIGGER },
{ "", SECTION::NONE } { "", SECTION::NONE }
}; };
@ -50,8 +53,10 @@ private:
// Map file section readers // Map file section readers
void readMapSize(std::istringstream &sstr); void readMapSize(std::istringstream &sstr);
void readMapRow(std::istringstream &sstr); void readMapRow(std::istringstream &sstr);
void readTeleport(std::istringstream &sstr); void readCellSection(std::istringstream &sstr, const SECTION &section);
void readTrigger(std::istringstream &sstr); std::unique_ptr<TeleportCell> &&readTeleport(std::istringstream &sstr, std::unique_ptr<TeleportCell> &&cell);
std::unique_ptr<ChargeCell> &&readCharge(std::istringstream &sstr, std::unique_ptr<ChargeCell> &&cell);
std::unique_ptr<TriggerCell> &&readTrigger(std::istringstream &sstr, std::unique_ptr<TriggerCell> &&cell);
}; };
Map map; Map map;