summaryrefslogtreecommitdiff
path: root/src/level.cpp
blob: efc203831eb3c1afc54d1c44c7f71274262a3f7f (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "level.hpp"

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

#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});
        s.setRotation(sf::degrees(t->rotation));

        sf::Vector2f origin;
        sf::Vector2u size = t->tile->texture.getSize();

        if (t->rotation == 90.0f) {
          origin.y = size.y;
        } else if (t->rotation == 180.0f) {
          origin.x = size.x;
          origin.y = size.y;
        } else if (t->rotation == 270.0f) {
          origin.x = size.x;
        }

        s.setOrigin(origin);

        window.draw(s);
      }
    }
  }

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

    for (auto it = layer.tiles.begin(); it != layer.tiles.end(); ++it) {
      if (it->position != position) continue;

      it->rotation = rotation;
      it->tile = tile;
      placed = true;
      break;
    }

    if (!placed) {
      layer.tiles.push_back({tile, position, rotation});
    }
  }
  void TileFloor::remove_tile(TilesetTileType type,
                              const sf::Vector2i &position) {
    TileLayer &layer = this->layers.at(type);

    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; }

  const int TileFloor::get_tile_count() const {
    int count = 0;

    std::for_each(this->layers.begin(), this->layers.end(),
                  [&count](const TileLayer &l) { count += l.tiles.size(); });

    return count;
  }

  void TileLevel::add_floor(TileFloor floor) { this->floors.push_back(floor); }

  void TileLevel::move_to_floor(int floor_id) {
    this->current_floor = std::min(floor_id, (int)this->floors.size() - 1);
  }

  TileFloor &TileLevel::get_current_floor() {
    return this->floors.at(this->current_floor);
  }

  const int &TileLevel::get_current_floor_index() const {
    return this->current_floor;
  }

  const std::vector<TileFloor> &TileLevel::get_floors() const {
    return this->floors;
  }

  const std::string &TileLevel::get_name() const { return this->name; }
}