From 27b6e1b3248702d87cc024cd19a119e1d6c2f300 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Sat, 17 Jun 2023 19:48:51 +0200 Subject: [PATCH] First tile map properly working --- CMakeLists.txt | 2 +- src/coordinates/coordinates.h | 9 ++- src/coordinates/translated_coordinates.cpp | 10 +-- src/coordinates/translated_coordinates.h | 8 +- src/game/camera/tracking_view.cpp | 2 +- src/game/camera/tracking_view_options.hpp | 14 ++-- src/game/collectables/collectable_factory.cpp | 2 +- src/game/level/level_config.hpp | 14 ++-- src/game/level/level_loader.cpp | 8 +- src/game/physics/map/map_player.cpp | 5 +- src/game/physics/map/map_simulation.cpp | 2 +- src/game/physics/map/map_simulation.hpp | 2 +- src/game/player/player.cpp | 7 +- src/game/player/player.hpp | 2 - src/levels.hpp | 16 +++- src/logging/easylogging++.h | 34 ++++----- src/sprites/animated_sprite.cpp | 5 ++ src/sprites/animated_sprite.hpp | 6 +- src/sprites/single_sprite.cpp | 5 ++ src/sprites/single_sprite.hpp | 2 + src/sprites/sprite.hpp | 16 +++- src/sprites/sprite_factory.cpp | 49 +++++------- src/sprites/sprite_factory.hpp | 6 +- src/sprites/tilemap/tilemap.cpp | 1 - src/sprites/tilemap/tilemap.hpp | 13 ---- src/sprites/tilemap/tilemap_config.hpp | 12 --- src/sprites/tiling/tilemap.cpp | 74 +++++++------------ src/sprites/tiling/tilemap.hpp | 12 +-- src/sprites/tiling/tilemap_config.hpp | 20 ++--- src/sprites/tiling/tileset.cpp | 22 +++++- src/sprites/tiling/tileset.hpp | 6 ++ src/sprites/versatile_sprite.cpp | 14 ++++ src/sprites/versatile_sprite.hpp | 6 +- src/texture_config.h | 16 ++-- src/tilemaps.hpp | 21 ------ 35 files changed, 216 insertions(+), 227 deletions(-) delete mode 100644 src/sprites/tilemap/tilemap.cpp delete mode 100644 src/sprites/tilemap/tilemap.hpp delete mode 100644 src/sprites/tilemap/tilemap_config.hpp delete mode 100644 src/tilemaps.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a267cf..feb6247 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,7 @@ set(SOURCES src/levels.hpp src/sprites/tiling/tilemap.cpp src/sprites/tiling/tilemap.hpp - src/sprites/tiling/tilemap_config.hpp src/tilemaps.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp) + src/sprites/tiling/tilemap_config.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp) set(PHYSICS_00_SOURCES src/prototypes/physics_00.cpp) diff --git a/src/coordinates/coordinates.h b/src/coordinates/coordinates.h index 0e971ca..de1419d 100644 --- a/src/coordinates/coordinates.h +++ b/src/coordinates/coordinates.h @@ -10,7 +10,7 @@ struct WorldCoordinates WorldCoordinates(float x, float y, float z = 0) : x(x), y(y), z(z) {} - WorldCoordinates(sf::Vector2f vector) : x(vector.x), y(vector.y), z(0) + explicit WorldCoordinates(sf::Vector2f vector) : x(vector.x), y(vector.y), z(0) {} float x; @@ -62,7 +62,7 @@ struct IsometricCoordinates float y; float depth; // Bigger means further back. Can be used for accurate rendering order. - sf::Vector2f toScreen() const + [[nodiscard]] sf::Vector2f toScreen() const { return {x, y}; } @@ -72,7 +72,7 @@ struct IsometricCoordinates IsometricCoordinates(float x, float y, float depth = 0) : x(x), y(y), depth(depth) {} - explicit IsometricCoordinates (sf::Vector2f vector) : x(vector.x), y(vector.y), depth(0) + explicit IsometricCoordinates(sf::Vector2f vector) : x(vector.x), y(vector.y), depth(0) {} }; @@ -85,6 +85,9 @@ struct GridCoordinates GridCoordinates(float x, float y) : x(x), y(y) {} + + GridCoordinates(int x, int y) : x(static_cast(x)), y(static_cast(y)) + {} }; diff --git a/src/coordinates/translated_coordinates.cpp b/src/coordinates/translated_coordinates.cpp index f2cc6be..e761c19 100644 --- a/src/coordinates/translated_coordinates.cpp +++ b/src/coordinates/translated_coordinates.cpp @@ -23,15 +23,15 @@ GridCoordinates TranslatedCoordinates::grid() const { return {referenceWordCoordinates.x - 0.5f, referenceWordCoordinates.y - 0.5f}; } -void TranslatedCoordinates::set(WorldCoordinates newWorldCoordinates) { +void TranslatedCoordinates::setWorld(WorldCoordinates newWorldCoordinates) { this->worldCoordinates = newWorldCoordinates; } -void TranslatedCoordinates::set(IsometricCoordinates newIsometricCoordinates) { +void TranslatedCoordinates::setIsometric(IsometricCoordinates newIsometricCoordinates) { this->worldCoordinates = CoordinateTransformer::isometricToWorld(newIsometricCoordinates); } -void TranslatedCoordinates::set(const TranslatedCoordinates& newCoordinates) { +void TranslatedCoordinates::setTranslated(const TranslatedCoordinates& newCoordinates) { this->worldCoordinates = newCoordinates.world(); } @@ -54,7 +54,7 @@ void TranslatedCoordinates::setParent(std::shared_ptr par this->worldCoordinates = offset; } -void TranslatedCoordinates::set(GridCoordinates newGridCoordinates) +void TranslatedCoordinates::setGrid(GridCoordinates newGridCoordinates) { this->worldCoordinates = {newGridCoordinates.x + 0.5f, newGridCoordinates.y + 0.5f, 0}; } @@ -71,5 +71,5 @@ void TranslatedCoordinates::setScreenOffset(IsometricCoordinates offset) TranslatedCoordinates::TranslatedCoordinates(GridCoordinates gridCoordinates) { - set(gridCoordinates); + setGrid(gridCoordinates); } diff --git a/src/coordinates/translated_coordinates.h b/src/coordinates/translated_coordinates.h index 7a7bd10..d182394 100644 --- a/src/coordinates/translated_coordinates.h +++ b/src/coordinates/translated_coordinates.h @@ -17,13 +17,13 @@ public: [[nodiscard]] GridCoordinates grid() const; - void set(WorldCoordinates newWorldCoordinates); + void setWorld(WorldCoordinates newWorldCoordinates); - void set(const TranslatedCoordinates& newCoordinates); + void setTranslated(const TranslatedCoordinates& newCoordinates); - void set(IsometricCoordinates newIsometricCoordinates); + void setIsometric(IsometricCoordinates newIsometricCoordinates); - void set(GridCoordinates newGridCoordinates); + void setGrid(GridCoordinates newGridCoordinates); void move(WorldCoordinates deltaWorldCoordinates); diff --git a/src/game/camera/tracking_view.cpp b/src/game/camera/tracking_view.cpp index 57bf798..daaafa5 100644 --- a/src/game/camera/tracking_view.cpp +++ b/src/game/camera/tracking_view.cpp @@ -98,7 +98,7 @@ void TrackingView::followTrackables() auto trackingPoint = getTrackingArea().getCenter(); if (DEVELOPER_MODE) { - marker->coordinates->set(IsometricCoordinates(trackingPoint)); + marker->coordinates->setIsometric(IsometricCoordinates(trackingPoint)); } // Calculate distance to target to check how to handle it diff --git a/src/game/camera/tracking_view_options.hpp b/src/game/camera/tracking_view_options.hpp index fee18de..9f2bd11 100644 --- a/src/game/camera/tracking_view_options.hpp +++ b/src/game/camera/tracking_view_options.hpp @@ -7,8 +7,8 @@ struct TrackingViewOptions { /** - * Value >1 to set pixel radius. - * Value between 0 and 1 to set relative radius based on smallest half-axis-size. + * Value >1 to setWorld pixel radius. + * Value between 0 and 1 to setWorld relative radius based on smallest half-axis-size. */ float freeMoveThreshold = DEF_TV_FREE_MOVE_THRESHOLD; @@ -22,24 +22,24 @@ struct TrackingViewOptions float softResizeSpeed = DEF_TV_SOFT_RESIZE_SPEED; /** - * If set to 0, view will not be limited. + * If setWorld to 0, view will not be limited. */ sf::Vector2f minViewSize = DEF_TV_MIN_VIEW_SIZE; /** - * If set to 0, view will not be limited. + * If setWorld to 0, view will not be limited. */ sf::Vector2f maxViewSize = DEF_TV_MAX_VIEW_SIZE; /** * Will be added to tracked area size twice, as padding for each side. - * Value >1 to set pixel padding. - * Value between 0 and 1 to set relative padding. + * Value >1 to setWorld pixel padding. + * Value between 0 and 1 to setWorld relative padding. */ sf::Vector2f viewSizePadding = DEF_TV_VIEW_SIZE_PADDING; /** - * If set to true, view will add all new players automatically and remove them accordingly, based on PlayerCollection. + * If setWorld to true, view will add all new players automatically and remove them accordingly, based on PlayerCollection. */ bool addPlayersDynamically = DEF_TV_ADD_PLAYERS_DYNAMICALLY; }; diff --git a/src/game/collectables/collectable_factory.cpp b/src/game/collectables/collectable_factory.cpp index ed5c64b..8fcca64 100644 --- a/src/game/collectables/collectable_factory.cpp +++ b/src/game/collectables/collectable_factory.cpp @@ -5,7 +5,7 @@ std::shared_ptr CollectableFactory::createFromInLevelConfig(const C auto collectableConfig = config.collectableConfig; auto collectable = std::make_shared(); - collectable->coordinates->set(config.position); + collectable->coordinates->setGrid(config.position); collectable->setSprite(collectableConfig.spriteName); return collectable; diff --git a/src/game/level/level_config.hpp b/src/game/level/level_config.hpp index 2d35897..e268f99 100644 --- a/src/game/level/level_config.hpp +++ b/src/game/level/level_config.hpp @@ -8,28 +8,28 @@ #include "../collectables/collectable_in_level.hpp" #include #include "../../sprites/tiling/tilemap_config.hpp" +#include "../../sprites/sprite_factory.hpp" struct LevelConfig { std::string name; - sf::Vector2f worldMapSize = {}; + sf::Vector2i worldMapSize = {}; TileMapConfig tileMapConfig = {}; std::vector playerSpawnPoints = {}; std::vector collectables = {}; LevelConfig(std::string name, - const std::string &tileMapName, const std::vector &playerSpawnPoints, - const std::vector &collectables = {}) + const std::vector &collectables, + const TileMapConfig &tileMapConfig) : name(std::move(name)), - worldMapSize(worldMapSize), - playerSpawnPoints(playerSpawnPoints) + playerSpawnPoints(playerSpawnPoints), + tileMapConfig(tileMapConfig) { - tileMapConfig = TileMapConfig(tileMapName, {}); worldMapSize = tileMapConfig.getSize(); // Remove invalid collectables - for (auto &collectable : collectables) + for (auto &collectable: collectables) { if (collectable.isValid()) { diff --git a/src/game/level/level_loader.cpp b/src/game/level/level_loader.cpp index a899265..1733788 100644 --- a/src/game/level/level_loader.cpp +++ b/src/game/level/level_loader.cpp @@ -20,6 +20,8 @@ void LevelLoader::loadLevel(const LevelConfig &levelConfig) PlayerCollection::getInstance()->clear(); // Add basic game objects + game->addGameObject(SpriteFactory::createTileMap(levelConfig.tileMapConfig)); + if (DEVELOPER_MODE) { game->addGameObject(std::make_shared(0, 50, 0, 50)); @@ -43,12 +45,6 @@ void LevelLoader::loadLevel(const LevelConfig &levelConfig) // Add physics simulations game->addGameObject(MapSimulation::getInstance()); - // For every depth, add a hole depth simulation - for (int depth = 0; depth < maxDepth; depth++) - { - auto depthCollection = collectablesCollection->getDepthCollection(depth); - game->addGameObject(std::make_shared(depthCollection)); - } LOG(INFO) << "Finished loading level '" << levelConfig.name << "'."; } diff --git a/src/game/physics/map/map_player.cpp b/src/game/physics/map/map_player.cpp index 6b2c701..27140dd 100644 --- a/src/game/physics/map/map_player.cpp +++ b/src/game/physics/map/map_player.cpp @@ -20,7 +20,7 @@ void MapPlayer::updateSimulationPosition() void MapPlayer::updatePlayerPosition() const { b2Vec2 playerPosition = body->GetPosition(); - player->coordinates->set(sf::Vector2f(playerPosition.x, playerPosition.y)); + player->coordinates->setWorld({playerPosition.x, playerPosition.y}); } void MapPlayer::updateShape() @@ -28,7 +28,8 @@ void MapPlayer::updateShape() shapeRadius = player->getWorldRadius(); b2Fixture *oldFixture = body->GetFixtureList(); - if (oldFixture != nullptr) { + if (oldFixture != nullptr) + { body->DestroyFixture(oldFixture); } diff --git a/src/game/physics/map/map_simulation.cpp b/src/game/physics/map/map_simulation.cpp index 4cd6047..33fe263 100644 --- a/src/game/physics/map/map_simulation.cpp +++ b/src/game/physics/map/map_simulation.cpp @@ -33,7 +33,7 @@ void MapSimulation::physicsUpdate() } } -void MapSimulation::resetMap(sf::Vector2f worldMapSize) +void MapSimulation::resetMap(sf::Vector2 worldMapSize) { // Clear all players for (auto &mapPlayer: mapPlayersById) diff --git a/src/game/physics/map/map_simulation.hpp b/src/game/physics/map/map_simulation.hpp index b6288fb..5b57bd6 100644 --- a/src/game/physics/map/map_simulation.hpp +++ b/src/game/physics/map/map_simulation.hpp @@ -16,7 +16,7 @@ public: void physicsUpdate() override; - void resetMap(sf::Vector2f worldMapSize); + void resetMap(sf::Vector2 worldMapSize); static std::shared_ptr getInstance(); diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 2099b32..02143f7 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -8,7 +8,7 @@ Player::Player(std::shared_ptr assignedInput, const std::string & : spawnPosition(initCoordinates) { playerId = playerCreationCounter++; - coordinates->set(spawnPosition); + coordinates->setTranslated(spawnPosition); input = std::move(assignedInput); @@ -94,8 +94,3 @@ void Player::setWorldRadius(float newWorldRadius) skinSprite->setSize(newSize); skinSprite->coordinates->setScreenOffset(IsometricCoordinates(-newSize / 2.f)); } - -Player::~Player() -{ - LOG(INFO) << "Player " << playerId << " destroyed."; -} diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index 9a92707..cd59d08 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -13,8 +13,6 @@ public: Player(std::shared_ptr assignedInput, const std::string &skinRessourceName, GridCoordinates initCoordinates); - ~Player(); - void update() override; [[nodiscard]] sf::Vector2f getTrackablePosition() const override; diff --git a/src/levels.hpp b/src/levels.hpp index 4cfc376..c6625d6 100644 --- a/src/levels.hpp +++ b/src/levels.hpp @@ -8,9 +8,19 @@ #define INITIAL_LEVEL "default" std::map const all_levels = { - {"default", LevelConfig("Default", "grass", {{0, 0}}, { - CollectableInLevel("box", {5, 5}) - })} + {"default", LevelConfig("Default", {{0, 0}}, { + CollectableInLevel("box", {5, 5})}, + TileMapConfig("iso-tiles", {{4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 0, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 1, 4, 4, 4, 4, 4, 4, 4, 4}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}}) + )} }; #endif //HOLESOME_LEVELS_HPP diff --git a/src/logging/easylogging++.h b/src/logging/easylogging++.h index e23c332..d0bbe8d 100644 --- a/src/logging/easylogging++.h +++ b/src/logging/easylogging++.h @@ -694,7 +694,7 @@ class ConfigurationTypeHelper : base::StaticClass { /// This bool represent whether or not to exit iterating through configurations. static inline void forEachConfigType(base::type::EnumType* startIndex, const std::function& fn); }; -/// @brief Flags used while writing logs. This flags are set by user +/// @brief Flags used while writing logs. This flags are setWorld by user enum class LoggingFlag : base::type::EnumType { /// @brief Makes sure we have new line for each container log entry NewLineForContainer = 1, @@ -1701,7 +1701,7 @@ class Configuration : public Loggable { } /// @brief Set string based configuration value - /// @param value Value to set. Values have to be std::string; For boolean values use "true", "false", for any integral values + /// @param value Value to setWorld. Values have to be std::string; For boolean values use "true", "false", for any integral values /// use them in quotes. They will be parsed when configuring inline void setValue(const std::string& value) { m_value = value; @@ -1735,9 +1735,9 @@ class Configurations : public base::utils::RegistryWithPred* populateAllLoggerIds(std::vector* targetList); /// @brief Sets configurations from global configuration file. static void configureFromGlobal(const char* globalConfigurationFilePath); - /// @brief Configures loggers using command line arg. Ensure you have already set command line args, + /// @brief Configures loggers using command line arg. Ensure you have already setWorld command line args, /// @return False if invalid argument or argument with no value provided, true if attempted to configure logger. /// If true is returned that does not mean it has been configured successfully, it only means that it /// has attempted to configure logger using configuration file provided in argument diff --git a/src/sprites/animated_sprite.cpp b/src/sprites/animated_sprite.cpp index e890e98..f64eb96 100644 --- a/src/sprites/animated_sprite.cpp +++ b/src/sprites/animated_sprite.cpp @@ -44,3 +44,8 @@ sf::Vector2f AnimatedSprite::getSize() const auto textureSize = sprites[currentFrame].getTextureRect(); return {textureSize.width * scale.x, textureSize.height * scale.y}; } + +sf::Sprite AnimatedSprite::getSprite() const +{ + return sprites[currentFrame]; +} diff --git a/src/sprites/animated_sprite.hpp b/src/sprites/animated_sprite.hpp index 5357499..adccf23 100644 --- a/src/sprites/animated_sprite.hpp +++ b/src/sprites/animated_sprite.hpp @@ -9,7 +9,7 @@ class AnimatedSprite : public GameObject, public Sprite { public: - AnimatedSprite(const std::vector& sprites, const sf::Vector2f& size = sf::Vector2f(0, 0)); + explicit AnimatedSprite(const std::vector& sprites, const sf::Vector2f& size = sf::Vector2f(0, 0)); void update() override; @@ -19,7 +19,9 @@ public: void setSize(const sf::Vector2f &size) override; - sf::Vector2f getSize() const override; + [[nodiscard]] sf::Vector2f getSize() const override; + + sf::Sprite getSprite() const override; private: int currentFrame = 0; diff --git a/src/sprites/single_sprite.cpp b/src/sprites/single_sprite.cpp index 94c5a34..62405b8 100644 --- a/src/sprites/single_sprite.cpp +++ b/src/sprites/single_sprite.cpp @@ -37,3 +37,8 @@ sf::Vector2f SingleSprite::getSize() const auto textureSize = sprite.getTextureRect(); return {textureSize.width * sprite.getScale().x, textureSize.height * sprite.getScale().y}; } + +sf::Sprite SingleSprite::getSprite() const +{ + return sprite; +} diff --git a/src/sprites/single_sprite.hpp b/src/sprites/single_sprite.hpp index 9757bcc..0ced242 100644 --- a/src/sprites/single_sprite.hpp +++ b/src/sprites/single_sprite.hpp @@ -19,6 +19,8 @@ public: sf::Vector2f getSize() const override; + sf::Sprite getSprite() const override; + private: sf::Sprite sprite; }; diff --git a/src/sprites/sprite.hpp b/src/sprites/sprite.hpp index fd7302e..f709b3d 100644 --- a/src/sprites/sprite.hpp +++ b/src/sprites/sprite.hpp @@ -3,16 +3,26 @@ #include #include +#include #include class Sprite { public: virtual void setSize(const sf::Vector2f &size) - {} + { + throw std::runtime_error("Sprite::setSize() not implemented"); + } - virtual sf::Vector2f getSize() const - { return {0, 0}; } + [[nodiscard]] virtual sf::Vector2f getSize() const + { + throw std::runtime_error("Sprite::getSize() not implemented"); + } + + [[nodiscard]] virtual sf::Sprite getSprite() const + { + throw std::runtime_error("Sprite::getSprite() not implemented"); + } }; #endif //HOLESOME_SPRITE_HPP diff --git a/src/sprites/sprite_factory.cpp b/src/sprites/sprite_factory.cpp index 777d6cf..63fb81b 100644 --- a/src/sprites/sprite_factory.cpp +++ b/src/sprites/sprite_factory.cpp @@ -2,7 +2,7 @@ #include "../texture_config.h" #include "texture_manager.hpp" -std::shared_ptr SpriteFactory::createSingleSprite(const std::string& name, sf::Vector2f size) +std::shared_ptr SpriteFactory::createSingleSprite(const std::string &name, sf::Vector2f size) { // Get sprite config auto sprite_config = all_sprites.find(name); @@ -44,7 +44,7 @@ std::shared_ptr SpriteFactory::createSingleSprite(const std::strin return sprite; } -std::shared_ptr SpriteFactory::createAnimatedSprite(const std::string& name, sf::Vector2f size) +std::shared_ptr SpriteFactory::createAnimatedSprite(const std::string &name, sf::Vector2f size) { // Get animation config auto animation_config = all_animations.find(name); @@ -72,7 +72,7 @@ std::shared_ptr SpriteFactory::createAnimatedSprite(const std::s return animation; } -std::shared_ptr SpriteFactory::createSheet(const std::string& name) +std::shared_ptr SpriteFactory::createSheet(const std::string &name) { // Get config auto sheet_config = all_sheets.find(name); @@ -96,32 +96,6 @@ std::shared_ptr SpriteFactory::createSheet(const std::string& name) return std::make_shared(texture, config.columns, config.rows); } -std::shared_ptr SpriteFactory::createTileMap(const std::string &name) -{ - // Get config - auto tilemap_config = all_tilemaps.find(name); - - if (tilemap_config == all_tilemaps.end()) - { - LOG(ERROR) << "Tilemap " << name << " not found. Could not create tilemap."; - return nullptr; - } - - // Construct tilemap - auto config = tilemap_config->second; - std::shared_ptr tileSet = createTileSet(config.tileSheet); - if (sheet == nullptr) - { - LOG(ERROR) << "Sheet " << config.sheetName << " not found. Could not create tilemap."; - return nullptr; - } - - LOG(INFO) << "Creating tilemap " << name; - auto tilemap = std::make_shared(sheet, config.tileSize, config.tileCount); - tilemap->setTilemap(config.tilemap); - return tilemap; -} - std::shared_ptr SpriteFactory::createTileSet(const std::string &name) { // Get config @@ -145,3 +119,20 @@ std::shared_ptr SpriteFactory::createTileSet(const std::string &name) LOG(INFO) << "Creating tileset " << name; return std::make_shared(sheet, config.tileIndices); } + +std::shared_ptr SpriteFactory::copySingleSprite(const std::shared_ptr& sprite) +{ + return std::make_shared(sprite->getSprite(), sprite->getSize()); +} + +std::shared_ptr SpriteFactory::createTileMap(TileMapConfig config) +{ + auto tileSet = createTileSet(config.tileSet); + if (tileSet == nullptr) + { + LOG(ERROR) << "Tileset " << config.tileSet << " not found. Could not create tilemap."; + return nullptr; + } + + return std::make_shared(tileSet, config.tiles); +} diff --git a/src/sprites/sprite_factory.hpp b/src/sprites/sprite_factory.hpp index d351941..e3dec4e 100644 --- a/src/sprites/sprite_factory.hpp +++ b/src/sprites/sprite_factory.hpp @@ -10,6 +10,7 @@ #include "../logging/easylogging++.h" #include "tiling/tilemap.hpp" #include "tiling/tileset.hpp" +#include "tiling/tilemap_config.hpp" class SpriteFactory { @@ -17,8 +18,9 @@ public: static std::shared_ptr createSingleSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0)); static std::shared_ptr createAnimatedSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0)); static std::shared_ptr createSheet(const std::string& name); - static std::shared_ptr createTileMap(const std::string& name); - std::shared_ptr createTileSet(const std::string &name); + static std::shared_ptr createTileSet(const std::string &name); + static std::shared_ptr copySingleSprite(const std::shared_ptr& sprite); + static std::shared_ptr createTileMap(TileMapConfig config); }; diff --git a/src/sprites/tilemap/tilemap.cpp b/src/sprites/tilemap/tilemap.cpp deleted file mode 100644 index bafdf69..0000000 --- a/src/sprites/tilemap/tilemap.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "tilemap.hpp" diff --git a/src/sprites/tilemap/tilemap.hpp b/src/sprites/tilemap/tilemap.hpp deleted file mode 100644 index 05306e7..0000000 --- a/src/sprites/tilemap/tilemap.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef HOLESOME_TILEMAP_HPP -#define HOLESOME_TILEMAP_HPP - - -#include "../../game/game_object.h" - -class Tilemap : public GameObject -{ - -}; - - -#endif //HOLESOME_TILEMAP_HPP diff --git a/src/sprites/tilemap/tilemap_config.hpp b/src/sprites/tilemap/tilemap_config.hpp deleted file mode 100644 index 483c376..0000000 --- a/src/sprites/tilemap/tilemap_config.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef HOLESOME_TILEMAP_CONFIG_HPP -#define HOLESOME_TILEMAP_CONFIG_HPP - -#include -#include - -struct TilemapConfig { - std::string tileSheetName; - std::vector> tiles; -}; - -#endif //HOLESOME_TILEMAP_CONFIG_HPP diff --git a/src/sprites/tiling/tilemap.cpp b/src/sprites/tiling/tilemap.cpp index 44dc87a..46f8ae4 100644 --- a/src/sprites/tiling/tilemap.cpp +++ b/src/sprites/tiling/tilemap.cpp @@ -1,57 +1,35 @@ #include "tilemap.hpp" +#include "../sprite_factory.hpp" -TileMap::TileMap(std::shared_ptr tileSet, std::vector> tiles) +TileMap::TileMap(const std::shared_ptr &tileSet, std::vector> tiles) { - // resize the vertex array to fit the level size - vertices.setPrimitiveType(sf::Quads); - unsigned int size = tiles.size(); - vertices.resize(size * size * 4); - - // populate the vertex array, with one quad per tile - for (unsigned int y = 0; y < size; ++y) - for (unsigned int x = 0; x < size; ++x) - { - // get the current tile number - int tileNumber = tiles[size - 1 - y][x]; - - // TODO - // Follow: https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map - // find its position in the tileset texture - int tu = tileNumber % (tileset.getSize().y / tileSize.y); - int tv = tileNumber / (tileset.getSize().y / tileSize.y); - - // get a pointer to the current tile's quad - sf::Vertex *quad = &vertices[(y + x * width) * 4]; - - // define its 4 corners - quad[0].position = sf::Vector2f(y * tileSize.y, x * tileSize.y); - quad[1].position = sf::Vector2f((y + 1) * tileSize.y, x * tileSize.y); - quad[2].position = sf::Vector2f((y + 1) * tileSize.y, (x + 1) * tileSize.y); - quad[3].position = sf::Vector2f(y * tileSize.y, (x + 1) * tileSize.y); - - // define its 4 texture coordinates - quad[0].texCoords = sf::Vector2f(tu * tileSize.y, tv * tileSize.y); - quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.y, tv * tileSize.y); - quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.y, (tv + 1) * tileSize.y); - quad[3].texCoords = sf::Vector2f(tu * tileSize.y, (tv + 1) * tileSize.y); + for (int i = 2 * (size - 1); i >= 0 ; i--) + { + int x, y; + if (i < size) { + x = i; + y = 0; + } else { + x = size - 1; + y = i - size + 1; } + + for (; x >= 0 && y < size; x--, y++) + { + auto tileId = tiles[size - 1 - y][x]; + auto sprite = tileSet->getTile(tileId); + createTileAtGridPosition(sprite, {x, y}); + } + } } -void TileMap::draw(sf::RenderWindow *window) +void TileMap::createTileAtGridPosition(const std::shared_ptr &sprite, GridCoordinates coordinates) { - window->draw(*this); -} - -void TileMap::draw(sf::RenderTarget &target, sf::RenderStates states) const -{ - // apply the transform - // TODO? NEEDED? -// states.transform *= getTransform(); - - // apply the tileset texture - states.texture = &tileMapTexture; - - // draw the vertex array - target.draw(vertices, states); + auto tile = SpriteFactory::copySingleSprite(sprite); + + // Move to tile diagonal top left, since center of that tile is correct position for sprite corner + coordinates.y += 1; + + addChildScreenOffset(tile, TranslatedCoordinates(coordinates).isometric()); } diff --git a/src/sprites/tiling/tilemap.hpp b/src/sprites/tiling/tilemap.hpp index ac33d5f..3cec8c0 100644 --- a/src/sprites/tiling/tilemap.hpp +++ b/src/sprites/tiling/tilemap.hpp @@ -8,18 +8,12 @@ #include "../../game/game_object.h" #include "tileset.hpp" -class TileMap : public GameObject, public sf::Drawable +class TileMap : public GameObject { public: - TileMap(std::shared_ptr tileSet, std::vector> tiles); + TileMap(const std::shared_ptr& tileSet, std::vector> tiles); - void draw(sf::RenderWindow *window) override; - - void draw(sf::RenderTarget &target, sf::RenderStates states) const override; - -private: - sf::VertexArray vertices; - sf::Texture tileMapTexture; + void createTileAtGridPosition(const std::shared_ptr& sprite, GridCoordinates coordinates); }; diff --git a/src/sprites/tiling/tilemap_config.hpp b/src/sprites/tiling/tilemap_config.hpp index 017a62b..9450ae8 100644 --- a/src/sprites/tiling/tilemap_config.hpp +++ b/src/sprites/tiling/tilemap_config.hpp @@ -9,22 +9,18 @@ struct TileMapConfig { - std::string tileSheet; - - std::vector backgroundTileSelection = {}; + std::string tileSet; std::vector> tiles = {}; - TileMapConfig(std::string tileSheet, std::vector> tiles, - std::vector backgroundTileSelection = {}) + TileMapConfig(std::string tileSheet, const std::vector> &tiles) { - this->tileSheet = std::move(tileSheet); - this->backgroundTileSelection = std::move(backgroundTileSelection); - this->tiles = std::move(tiles); + this->tileSet = std::move(tileSheet); + this->tiles = tiles; // Make sure all rows are the same length and that the map is square - int size = tiles.size(); - for (auto &row : tiles) + unsigned int size = tiles.size(); + for (auto &row: tiles) { if (row.size() != size) { @@ -35,10 +31,10 @@ struct TileMapConfig TileMapConfig() = default; - sf::Vector2f getSize() const + [[nodiscard]] sf::Vector2 getSize() const { int size = tiles.size(); - return {static_cast(size), static_cast(size)}; + return {size, size}; } }; diff --git a/src/sprites/tiling/tileset.cpp b/src/sprites/tiling/tileset.cpp index 55ec019..916e011 100644 --- a/src/sprites/tiling/tileset.cpp +++ b/src/sprites/tiling/tileset.cpp @@ -1,6 +1,26 @@ #include "tileset.hpp" -TileSet::TileSet(const std::shared_ptr& sheet, const std::vector& tileIndices) +TileSet::TileSet(const std::shared_ptr &sheet, const std::vector &tileIndices) { + float tileWidth = TranslatedCoordinates(WorldCoordinates{1, 0}).isometric().x - + TranslatedCoordinates(WorldCoordinates{0, 1}).isometric().x; + sf::Vector2f tileSize = sf::Vector2f(1, 1) * tileWidth; + // Load desired tiles from sheet + for (auto index: tileIndices) + { + auto sprite = sheet->getSprite(index); + sprite->setSize(tileSize); + tiles.push_back(sprite); + } +} + +std::shared_ptr TileSet::getTile(int index) const +{ + if (index < 0 || index >= tiles.size()) + { + throw std::runtime_error("Tile index out of range"); + } + + return tiles[index]; } diff --git a/src/sprites/tiling/tileset.hpp b/src/sprites/tiling/tileset.hpp index 38a12c7..69ec0bb 100644 --- a/src/sprites/tiling/tileset.hpp +++ b/src/sprites/tiling/tileset.hpp @@ -3,11 +3,17 @@ #include "../sprite_sheet.hpp" +#include "../versatile_sprite.hpp" class TileSet { public: TileSet(const std::shared_ptr& sheet, const std::vector& tileIndices); + + [[nodiscard]] std::shared_ptr getTile(int index) const; + +private: + std::vector> tiles; }; diff --git a/src/sprites/versatile_sprite.cpp b/src/sprites/versatile_sprite.cpp index 02b4a18..cacc7dd 100644 --- a/src/sprites/versatile_sprite.cpp +++ b/src/sprites/versatile_sprite.cpp @@ -47,3 +47,17 @@ sf::Vector2f VersatileSprite::getSize() const return Sprite::getSize(); } } + +sf::Sprite VersatileSprite::getSprite() const +{ + if (singleSprite != nullptr) + { + return singleSprite->getSprite(); + } else if (animatedSprite != nullptr) + { + return animatedSprite->getSprite(); + } else + { + return {}; + } +} diff --git a/src/sprites/versatile_sprite.hpp b/src/sprites/versatile_sprite.hpp index 7c97978..ad863e2 100644 --- a/src/sprites/versatile_sprite.hpp +++ b/src/sprites/versatile_sprite.hpp @@ -12,11 +12,13 @@ class VersatileSprite : public GameObject, public Sprite { public: - VersatileSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0)); + explicit VersatileSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0)); void setSize(const sf::Vector2f &size) override; - sf::Vector2f getSize() const override; + [[nodiscard]] sf::Vector2f getSize() const override; + + sf::Sprite getSprite() const override; private: std::shared_ptr singleSprite = nullptr; diff --git a/src/texture_config.h b/src/texture_config.h index efb7e85..5ff6a8c 100644 --- a/src/texture_config.h +++ b/src/texture_config.h @@ -20,7 +20,8 @@ std::map const all_textures = { {"edge", "assets/edge.png"}, {"ring", "assets/ring.png"}, {"grasses", "assets/grass_plus.png"}, - {"hole", "assets/hole.png"} + {"hole", "assets/hole.png"}, + {"iso-tiles", "assets/isometric-tiles.png"} }; /** @@ -28,8 +29,9 @@ std::map const all_textures = { * The key is the name of the sheet, the value is the sheet config. */ std::map const all_sheets = { - {"numbers", SheetConfig("numbers", 4, 2)}, - {"grasses", SheetConfig("grasses", 25, 14)} + {"numbers", SheetConfig("numbers", 4, 2)}, + {"grasses", SheetConfig("grasses", 25, 14)}, + {"iso-tiles", SheetConfig("iso-tiles", 6, 2)} }; /** @@ -45,14 +47,18 @@ std::map const all_animations = { * The key is the name of the singleSprite, the value is the singleSprite config. */ std::map const all_sprites = { - {"64", SpriteConfig("64")}, + {"64", SpriteConfig("64")}, {"edge", SpriteConfig("edge")}, {"ring", SpriteConfig("ring")}, {"hole", SpriteConfig("hole")} }; +/** + * All tilesets used in the game. + * The key is the name of the tileset, the value is the tileset config. + */ std::map const all_tilesets = { - {"grass", TileSetConfig("grasses", {58})} + {"iso-tiles", TileSetConfig("iso-tiles", {0, 1, 2, 3, 4, 5})} }; #endif //HOLESOME_TEXTURE_CONFIG_H diff --git a/src/tilemaps.hpp b/src/tilemaps.hpp deleted file mode 100644 index 139d72d..0000000 --- a/src/tilemaps.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef HOLESOME_TILEMAPS_HPP -#define HOLESOME_TILEMAPS_HPP - -#include -#include -#include "sprites/tiling/tilemap_config.hpp" - -std::map const all_tilemaps = { - {"grass", TileMapConfig("grass", {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},})} -}; - -#endif //HOLESOME_TILEMAPS_HPP