summaryrefslogtreecommitdiff
path: root/src/floor.cpp
blob: 8482b4824c4fffd3258969bb688ae3550720e539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "floor.hpp"

#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Vector2.hpp>
#include <algorithm>
#include <iterator>

#include "tileset.hpp"

namespace silly::editor {
  void TileFloor::render(sf::RenderWindow &window) const {
    for (auto l = this->layers.begin(); l != this->layers.end(); ++l) {
      for (auto t = l->tiles.begin(); t != l->tiles.end(); ++t) {
        sf::Sprite s(t->tile->texture);
        s.setPosition({t->position.x * (float)TILE_WIDTH,
                       t->position.y * (float)TILE_HEIGHT});

        window.draw(s);
      }
    }
  }

  void TileFloor::place_tile(std::shared_ptr<TilesetTile> &tile,
                             const sf::Vector2i &position) {
    TileLayer &layer = this->layers.at(tile->type);

    if (!std::any_of(
            layer.tiles.begin(), layer.tiles.end(),
            [&position](const Tile &t) { return t.position == position; })) {
      layer.tiles.push_back({position, tile});
    }
  }
  void TileFloor::remove_tile(const sf::Vector2i &position) {
    TileLayer &layer = this->layers.at(this->activeLayerIndex);

    layer.tiles.resize(std::distance(
        layer.tiles.begin(),
        std::remove_if(
            layer.tiles.begin(), layer.tiles.end(),
            [&position](const Tile &t) { return t.position == position; })));
  }

  const int TileFloor::get_width() const { return this->width; }
  const int TileFloor::get_height() const { return this->height; }
}