summaryrefslogtreecommitdiff
path: root/src/level.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/level.cpp')
-rw-r--r--src/level.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/level.cpp b/src/level.cpp
new file mode 100644
index 0000000..bee3297
--- /dev/null
+++ b/src/level.cpp
@@ -0,0 +1,90 @@
+#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; }
+
+ 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 std::vector<TileFloor> &TileLevel::get_floors() const {
+ return this->floors;
+ }
+
+ const std::string &TileLevel::get_name() const { return this->name; }
+} \ No newline at end of file