From 79c7edc335fc162e9d2ef9acc5cdf41fbd1bac76 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 1 Feb 2025 22:51:37 +0500 Subject: feat: layers --- src/floor.cpp | 31 ++++++++++++++++++------------- src/floor.hpp | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/floor.cpp b/src/floor.cpp index 4365c73..8482b48 100644 --- a/src/floor.cpp +++ b/src/floor.cpp @@ -11,29 +11,34 @@ namespace silly::editor { void TileFloor::render(sf::RenderWindow &window) const { - std::for_each(this->tiles.begin(), this->tiles.end(), - [&window](const Tile &t) { - sf::Sprite s(t.tile->texture); - s.setPosition({t.position.x * (float)TILE_WIDTH, - t.position.y * (float)TILE_HEIGHT}); - - window.draw(s); - }); + 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 &tile, const sf::Vector2i &position) { + TileLayer &layer = this->layers.at(tile->type); + if (!std::any_of( - this->tiles.begin(), this->tiles.end(), + layer.tiles.begin(), layer.tiles.end(), [&position](const Tile &t) { return t.position == position; })) { - this->tiles.push_back({position, tile}); + layer.tiles.push_back({position, tile}); } } void TileFloor::remove_tile(const sf::Vector2i &position) { - this->tiles.resize(std::distance( - this->tiles.begin(), + TileLayer &layer = this->layers.at(this->activeLayerIndex); + + layer.tiles.resize(std::distance( + layer.tiles.begin(), std::remove_if( - this->tiles.begin(), this->tiles.end(), + layer.tiles.begin(), layer.tiles.end(), [&position](const Tile &t) { return t.position == position; }))); } diff --git a/src/floor.hpp b/src/floor.hpp index a04fc4b..1a6fe47 100644 --- a/src/floor.hpp +++ b/src/floor.hpp @@ -13,9 +13,20 @@ namespace silly::editor { std::shared_ptr tile; }; + struct TileLayer { + std::vector tiles; + TilesetTileType type; + }; + class TileFloor { public: - TileFloor(int width, int height) : width(width), height(height) {} + TileFloor(int width, int height) : width(width), height(height) { + // creating vectors for every tile type + for (int i = 0; i < 2; i++) { + this->layers.push_back({{}, (TilesetTileType)i}); + } + } + ~TileFloor() = default; void render(sf::RenderWindow &window) const; @@ -28,6 +39,7 @@ namespace silly::editor { private: int width, height; - std::vector tiles; + int activeLayerIndex; + std::vector layers; }; } \ No newline at end of file -- cgit v1.2.3