Make triggers work

This commit is contained in:
NaiJi ✨ 2020-03-15 04:33:39 +03:00
parent 0037de691f
commit b44ac5752d
6 changed files with 27 additions and 13 deletions

Binary file not shown.

Binary file not shown.

View File

@ -134,10 +134,10 @@ bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
///////////////////////////////////////
TriggerCell::TriggerCell(coordinate cell_x, coordinate cell_y, const std::vector<CellPtr> &cells_to_change, const sf::Color &color) :
TriggerCell::TriggerCell(std::vector<CellPtr> &&cells_to_change, coordinate cell_x, coordinate cell_y, const sf::Color &color) :
Cell(cell_x, cell_y, color)
{
cells.assign(std::move(cells_to_change));
cells = std::move(cells_to_change);
}
TriggerCell::~TriggerCell()
@ -150,11 +150,14 @@ bool TriggerCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
Map &map = level->mapArray();
// We replace needed cells with the ones that the trigger provides.
for (const &cell : cells)
map[cell->x()][cell->y()].reset(cell.release());
// Trigger works only once.
cells.clear();
for (CellPtr &cell : cells)
{
const coordinate &y = cell->y();
const coordinate &x = cell->x();
map[x][y].release();
map[x][y] = std::move(cell);
}
// It's an impassable object, so player can't move to here.
return false;

View File

@ -1,12 +1,16 @@
#ifndef CELL_H
#define CELL_H
#include "entity.h"
#include <memory>
#include <vector>
#include <SFML/Graphics/Color.hpp>
#include "entity.h"
class Hero;
class Level;
class Cell;
using HeroPtr = std::unique_ptr<Hero>;
using LevelPtr = std::unique_ptr<Level>;
@ -142,9 +146,9 @@ private:
std::vector<CellPtr> cells;
public:
TriggerCell(coordinate cell_x = 0,
coordinate cell_y = 0,
const std::vector<CellPtr> &cells_to_change = std::vector<CellPtr>(), // Pink
TriggerCell(std::vector<CellPtr> &&cells_to_change,
coordinate cell_x = 0,
coordinate cell_y = 0, // Pink
const sf::Color &color = sf::Color(255, 192, 203));
virtual ~TriggerCell() override;

View File

@ -171,6 +171,12 @@ void Game::loadLevel(int level_index)
for (coordinate y = 0; y < side; ++y)
map[x][y] = std::make_unique<WaterCell>(x, y);
std::vector<CellPtr> trigger_cells;
trigger_cells.emplace_back(std::make_unique<PassableCell>(0, 0));
trigger_cells.emplace_back(std::make_unique<PassableCell>(1, 0));
trigger_cells.emplace_back(std::make_unique<PassableCell>(0, 1));
switch (level_index)
{
case 1:
@ -197,6 +203,7 @@ void Game::loadLevel(int level_index)
map[9][6] = std::make_unique<PassableCell>(9, 6);
map[8][7] = std::make_unique<ExitCell>(8, 7);
map[2][3] = std::make_unique<ChargeCell>(2, 3, 5);
map[3][3] = std::make_unique<TriggerCell>(std::move(trigger_cells), 3, 3);
break;
default:
main_window.close();

View File

@ -1,12 +1,12 @@
#ifndef LEVEL_H
#define LEVEL_H
#include "cell.h"
#include <array>
#include "cell.h"
constexpr coordinate side = 32;
using Row = std::array<std::unique_ptr<Cell>, side>;
using Row = std::array<CellPtr, side>;
using Map = std::array<Row, side>;
/// Abstraction over 2D array to quickly get access to level cells