Extend map parser; now it's available to add complex cells in triggers
This commit is contained in:
parent
b9d19661ac
commit
34f35f4ac0
Binary file not shown.
|
@ -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
|
||||
|
|
19
src/cell.cpp
19
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<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) :
|
||||
Cell(cell_row, cell_col, color)
|
||||
{
|
||||
|
|
|
@ -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<CellPtr> vector_cells;
|
||||
|
||||
public:
|
||||
/// Vector of cell types you can cast in to
|
||||
static const std::vector<CELL_TYPE> cells_to_cast;
|
||||
|
||||
TriggerCell(coordinate cell_row = 0,
|
||||
coordinate cell_col = 0,
|
||||
const sf::Color &color = palette::Pink);
|
||||
|
|
|
@ -13,7 +13,7 @@ Game::Game()
|
|||
// Prepare level 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.setFramerateLimit(60);
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include "level.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
@ -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;
|
||||
coordinate dest_row, dest_col;
|
||||
sstr >> src_row >> src_col;
|
||||
|
||||
sstr >> src_row >> src_col >> dest_row >> dest_col;
|
||||
auto teleport_cell = static_unique_pointer_cast<TeleportCell>(std::move(data[src_row][src_col]));
|
||||
teleport_cell->setDestination(dest_row, dest_col);
|
||||
data[src_row][src_col] = std::move(teleport_cell);
|
||||
switch (section) {
|
||||
case SECTION::TELEPORT:
|
||||
data[src_row][src_col] = readTeleport(sstr, static_unique_pointer_cast<TeleportCell>(std::move(data[src_row][src_col])));
|
||||
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;
|
||||
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();
|
||||
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)
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <string>
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
#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<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;
|
||||
|
|
Loading…
Reference in New Issue