#include "level.hpp" #include #include #include #include #include #include #include #include #include #include "sets/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(const std::shared_ptr 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; float r = it->rotation; if (rotation == 270.0f && r == 0.0f) { r = 360.0f; } // wall building if (it->tile->type == TILE_WALL && std::abs(r - rotation) == 90.0f) { break; } 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; } const std::vector &TileFloor::get_layers() const { return this->layers; } std::string TileFloor::export_to_string() const { std::ostringstream oss; oss << "[[floor]]\n" << "# width;height\n" << this->width << ";" << this->height << "\n"; oss << "[[[tiles]]]\n" << "# tileset_tile_id;x;y;rotation\n"; for (auto it = this->layers.begin(); it != this->layers.end(); ++it) { for (auto tt = it->tiles.begin(); tt != it->tiles.end(); ++tt) { oss << tt->tile->id << ";" << tt->position.x << ";" << tt->position.y << ";" << tt->rotation << "\n"; } } return oss.str(); } 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 &TileLevel::get_floors() const { return this->floors; } const std::string &TileLevel::get_name() const { return this->name; } std::string TileLevel::export_to_string() const { std::ostringstream oss; oss << "[level]\n" << "# name\n" << this->name << "\n"; for (auto it = this->floors.begin(); it != this->floors.end(); ++it) { oss << it->export_to_string() << "\n"; } return oss.str(); } }