From 0ae2d10bdfd3d4fe7483829fb9c9257973e644c7 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 2 Feb 2025 02:13:52 +0500 Subject: feat: level package --- src/editor.cpp | 23 +++++++++------ src/editor.hpp | 8 ++--- src/floor.cpp | 73 ---------------------------------------------- src/floor.hpp | 46 ----------------------------- src/level.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/level.hpp | 67 ++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 37 +++++++++++++----------- src/package.cpp | 29 +++++++++++++++++++ src/package.hpp | 35 ++++++++++++++++++++++ 9 files changed, 258 insertions(+), 150 deletions(-) delete mode 100644 src/floor.cpp delete mode 100644 src/floor.hpp create mode 100644 src/level.cpp create mode 100644 src/level.hpp create mode 100644 src/package.cpp create mode 100644 src/package.hpp (limited to 'src') diff --git a/src/editor.cpp b/src/editor.cpp index f19cf65..74632b7 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -13,6 +13,7 @@ #include "imgui-SFML.h" #include "imgui.h" +#include "level.hpp" #include "nfd.h" #include "nfd.hpp" #include "tileset.hpp" @@ -68,13 +69,17 @@ namespace silly::editor { void Editor::update(sf::RenderWindow &window) { // tile placement - if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || - sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { + if ((sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || + sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) && + !this->package.get_levels().empty()) { sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window)); - for (int x = 0; x < this->floor.get_width(); x++) { - for (int y = 0; y < this->floor.get_height(); y++) { + TileLevel &level = this->package.get_current_level(); + TileFloor &floor = level.get_current_floor(); + + for (int x = 0; x < floor.get_width(); x++) { + for (int y = 0; y < floor.get_height(); y++) { int rx = x * TILE_WIDTH, ry = y * TILE_HEIGHT; if ((rx < mousePosition.x && mousePosition.x < rx + TILE_WIDTH) && @@ -87,9 +92,9 @@ namespace silly::editor { if (this->selectedTile.has_value()) { auto tile = this->selectedTile.value(); if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { - this->floor.place_tile(tile, pos, this->rotation); + floor.place_tile(tile, pos, this->rotation); } else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { - this->floor.remove_tile(tile->type, pos); + floor.remove_tile(tile->type, pos); } } } @@ -162,8 +167,8 @@ namespace silly::editor { int columns = ImGui::GetContentRegionAvail().x / (imageSize + padding); int count = 0; - for (auto it = this->tileset.get_tiles().begin(); - it != this->tileset.get_tiles().end(); ++it) { + for (auto it = this->package.get_tileset().get_tiles().begin(); + it != this->package.get_tileset().get_tiles().end(); ++it) { if (ImGui::ImageButton(std::to_string(it->get()->id).c_str(), it->get()->texture, sf::Vector2f(imageSize, imageSize))) { @@ -244,7 +249,7 @@ namespace silly::editor { // -- Tile creation -- if (ImGui::Button("Create a new tile")) { - this->tileset.add_tile(state.path, state.type); + this->package.get_tileset().add_tile(state.path, state.type); ImGui::SetWindowCollapsed(true); } diff --git a/src/editor.hpp b/src/editor.hpp index 13f9cac..1e055e6 100644 --- a/src/editor.hpp +++ b/src/editor.hpp @@ -8,7 +8,7 @@ #include #include -#include "floor.hpp" +#include "package.hpp" #include "tileset.hpp" namespace silly::editor { @@ -20,8 +20,7 @@ namespace silly::editor { class Editor { public: - Editor(Tileset &tileset, TileFloor &floor) - : tileset(tileset), floor(floor) {} + Editor(LevelPackage &package) : package(package) {} void update(sf::RenderWindow &window); void update(const sf::Event &event, sf::RenderWindow &window); @@ -29,8 +28,7 @@ namespace silly::editor { const float get_zoom() const; private: - Tileset &tileset; - TileFloor &floor; + LevelPackage &package; float rotation; // need for world movement and zoom diff --git a/src/floor.cpp b/src/floor.cpp deleted file mode 100644 index aacf77e..0000000 --- a/src/floor.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "floor.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#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 &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; } -} \ No newline at end of file diff --git a/src/floor.hpp b/src/floor.hpp deleted file mode 100644 index 4236166..0000000 --- a/src/floor.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "tileset.hpp" - -namespace silly::editor { - struct Tile { - std::shared_ptr tile; - sf::Vector2i position; - float rotation; - }; - - struct TileLayer { - std::vector tiles; - TilesetTileType type; - }; - - class TileFloor { - public: - 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; - void place_tile(std::shared_ptr &tile, - const sf::Vector2i &position, const float &rotation); - void remove_tile(TilesetTileType type, const sf::Vector2i &position); - - const int get_width() const; - const int get_height() const; - - private: - int width, height; - int activeLayerIndex; - std::vector layers; - }; -} \ No newline at end of file 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 +#include +#include +#include +#include +#include +#include +#include + +#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 &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 &TileLevel::get_floors() const { + return this->floors; + } + + const std::string &TileLevel::get_name() const { return this->name; } +} \ No newline at end of file diff --git a/src/level.hpp b/src/level.hpp new file mode 100644 index 0000000..2364413 --- /dev/null +++ b/src/level.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "tileset.hpp" + +namespace silly::editor { + struct Tile { + std::shared_ptr tile; + sf::Vector2i position; + float rotation; + }; + + struct TileLayer { + std::vector tiles; + TilesetTileType type; + }; + + class TileFloor { + public: + 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; + void place_tile(std::shared_ptr &tile, + const sf::Vector2i &position, const float &rotation); + void remove_tile(TilesetTileType type, const sf::Vector2i &position); + + const int get_width() const; + const int get_height() const; + + private: + int width, height; + int activeLayerIndex; + std::vector layers; + }; + + class TileLevel { + public: + TileLevel(const std::string &name) : name(name) {} + ~TileLevel() = default; + + void add_floor(TileFloor &floor); + void move_to_floor(int floor_id); + + TileFloor &get_current_floor(); + const std::vector &get_floors() const; + + const std::string &get_name() const; + + private: + const std::string name; + + int current_floor; + std::vector floors; + }; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 084671a..cc28392 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,8 +11,9 @@ #include #include "editor.hpp" -#include "floor.hpp" +#include "level.hpp" #include "nfd.hpp" +#include "package.hpp" #include "tileset.hpp" int main() { @@ -22,10 +23,8 @@ int main() { window.setFramerateLimit(60); ImGui::SFML::Init(window); - silly::editor::Tileset tileset; - - silly::editor::TileFloor tileFloor(30, 30); - silly::editor::Editor editor(tileset, tileFloor); + silly::editor::LevelPackage package("test"); + silly::editor::Editor editor(package); sf::Clock deltaClock; while (window.isOpen()) { @@ -42,7 +41,7 @@ int main() { // (cv pasted from // https://www.sfml-dev.org/tutorials/3.0/graphics/view/#showing-more-when-the-window-is-resized) // catch the resize events - if (const auto* resized = event->getIf()) { + if (const auto *resized = event->getIf()) { // update the view to the new size of the window sf::FloatRect visibleArea({0.f, 0.f}, sf::Vector2f(resized->size)); sf::View view(visibleArea); @@ -62,17 +61,21 @@ int main() { window.clear(); - tileFloor.render(window); - - // rendering grid - for (int x = 0; x < tileFloor.get_width(); x++) { - for (int y = 0; y < tileFloor.get_height(); y++) { - sf::RectangleShape shape({size, size}); - shape.setFillColor(sf::Color::Transparent); - shape.setOutlineColor(sf::Color(80, 80, 80)); - shape.setOutlineThickness(1.0f); - shape.setPosition({x * size, y * size}); - window.draw(shape); + if (!package.get_levels().empty()) { + silly::editor::TileLevel &level = package.get_current_level(); + silly::editor::TileFloor &floor = level.get_current_floor(); + floor.render(window); + + // rendering grid + for (int x = 0; x < floor.get_width(); x++) { + for (int y = 0; y < floor.get_height(); y++) { + sf::RectangleShape shape({size, size}); + shape.setFillColor(sf::Color::Transparent); + shape.setOutlineColor(sf::Color(80, 80, 80)); + shape.setOutlineThickness(1.0f); + shape.setPosition({x * size, y * size}); + window.draw(shape); + } } } diff --git a/src/package.cpp b/src/package.cpp new file mode 100644 index 0000000..a9d51b8 --- /dev/null +++ b/src/package.cpp @@ -0,0 +1,29 @@ +#include "package.hpp" + +#include + +namespace silly::editor { + Tileset &LevelPackage::get_tileset() { return this->tileset; } + + void LevelPackage::add_level(TileLevel level) { + this->levels.push_back(level); + } + + TileLevel &LevelPackage::get_current_level() { + return this->levels.at(this->currentLevelIndex); + } + + void LevelPackage::move_to_level_index(int index) { + this->currentLevelIndex = std::min(index, (int)this->levels.size() - 1); + } + + const int LevelPackage::get_current_level_index() const { + return this->currentLevelIndex; + } + + const std::vector &LevelPackage::get_levels() const { + return this->levels; + } + + const std::string &LevelPackage::get_name() const { return this->name; } +} \ No newline at end of file diff --git a/src/package.hpp b/src/package.hpp new file mode 100644 index 0000000..d847f83 --- /dev/null +++ b/src/package.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include "level.hpp" +#include "tileset.hpp" + +namespace silly::editor { + class LevelPackage { + public: + LevelPackage(const std::string &name) : name(name) {} + ~LevelPackage() = default; + + Tileset &get_tileset(); + + void add_level(TileLevel level); + TileLevel &get_current_level(); + + void move_to_level_index(int index); + + const int get_current_level_index() const; + const std::vector &get_levels() const; + + const std::string &get_name() const; + + private: + const std::string name; + + Tileset tileset; + std::vector levels; + + int currentLevelIndex; + }; +} \ No newline at end of file -- cgit v1.2.3