diff --git a/src/config.h b/src/config.h index c8a9ebd..85f0e82 100644 --- a/src/config.h +++ b/src/config.h @@ -13,6 +13,7 @@ // World #define WORLD_GRAVITY b2Vec2(0.f, 9.8f) +#define SKY_HEIGHT_SCALE 2.f // FPS #define FRAME_RATE 60 diff --git a/src/game/level/level_config.hpp b/src/game/level/level_config.hpp index e268f99..c31a428 100644 --- a/src/game/level/level_config.hpp +++ b/src/game/level/level_config.hpp @@ -17,14 +17,17 @@ struct LevelConfig TileMapConfig tileMapConfig = {}; std::vector playerSpawnPoints = {}; std::vector collectables = {}; + std::vector skyColors = {}; LevelConfig(std::string name, const std::vector &playerSpawnPoints, const std::vector &collectables, + std::vector skyColors, const TileMapConfig &tileMapConfig) : name(std::move(name)), playerSpawnPoints(playerSpawnPoints), - tileMapConfig(tileMapConfig) + tileMapConfig(tileMapConfig), + skyColors(std::move(skyColors)) { worldMapSize = tileMapConfig.getSize(); diff --git a/src/game/level/level_loader.cpp b/src/game/level/level_loader.cpp index 37ab24d..56dd31d 100644 --- a/src/game/level/level_loader.cpp +++ b/src/game/level/level_loader.cpp @@ -25,7 +25,7 @@ void LevelLoader::loadLevel(const LevelConfig &levelConfig) std::shared_ptr levelRenderer = std::make_shared(); game->addGameObject(levelRenderer); - levelRenderer->addChild(std::make_shared()); + levelRenderer->addChild(std::make_shared(levelConfig.skyColors, levelConfig.worldMapSize)); levelRenderer->addChild(SpriteFactory::createTileMap(levelConfig.tileMapConfig)); if (DB_WORLD_GRID_RENDER) diff --git a/src/levels.hpp b/src/levels.hpp index 5c255ea..4820b93 100644 --- a/src/levels.hpp +++ b/src/levels.hpp @@ -9,7 +9,24 @@ std::map const all_levels = { {"default", LevelConfig("Default", {{0, 0}}, { - CollectableInLevel("box", {5, 5})}, + CollectableInLevel("box", {5, 5}) + }, + { + // Blues + sf::Color(2, 100, 234), + sf::Color(2, 100, 234), + sf::Color(2, 100, 234), + sf::Color(2, 195, 234), + + // Neutral + sf::Color::White, + + // Browns + sf::Color(163, 128, 68), + sf::Color(100, 80, 40), + sf::Color(20, 18, 11), + sf::Color::Black + }, 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}, diff --git a/src/sprites/skymap/skymap.cpp b/src/sprites/skymap/skymap.cpp index 566d739..aba1ca7 100644 --- a/src/sprites/skymap/skymap.cpp +++ b/src/sprites/skymap/skymap.cpp @@ -1,28 +1,68 @@ #include #include "skymap.hpp" +#include "../../logging/easylogging++.h" +#include "../../config.h" + +Skymap::Skymap(std::vector skyColors, sf::Vector2 mapSize) +{ + // Sizes + float mapWidth = 2 * TranslatedCoordinates(GridCoordinates(mapSize.x, 0)).isometric().x; + float mapHeight = TranslatedCoordinates(GridCoordinates(mapSize.x, mapSize.y)).isometric().y; + float halfSkyWidth = mapWidth * 1.5f; + float skyHeight = mapHeight * SKY_HEIGHT_SCALE; + + // If only one color is provided, use it for both the top and bottom + if (skyColors.empty()) + { + LOG(ERROR) << "No sky colors provided. Using defaults instead."; + skyColors = { + // Blues + sf::Color(2, 100, 234), + sf::Color(2, 100, 234), + sf::Color(2, 100, 234), + sf::Color(2, 195, 234), + + // Neutral + sf::Color::White, + + // Browns + sf::Color(163, 128, 68), + sf::Color(100, 80, 40), + sf::Color(20, 18, 11), + sf::Color::Black + }; + } else if (skyColors.size() == 1) + { + skyColors.push_back(skyColors[0]); + } + + // Create the background gradient + int colorCount = skyColors.size(); + int vertexCount = colorCount * 2; + backgroundGradient = sf::VertexArray(sf::TriangleStrip, vertexCount); + + // Starting top from top, going down + // Always alternating between left and right + for (int i = 0; i < vertexCount; i += 2) + { + int colorIndex = i / 2; + auto color = skyColors[colorIndex]; + float heightProgress = (float) colorIndex / (float) (colorCount - 1); + + // Set position + float y = mapHeight / 2.f + skyHeight / 2.f - skyHeight * heightProgress; + + // Left + backgroundGradient[i].position = sf::Vector2f(-halfSkyWidth, y); + backgroundGradient[i].color = color; + + // Right + backgroundGradient[i + 1].position = sf::Vector2f(halfSkyWidth, y); + backgroundGradient[i + 1].color = color; + } +} void Skymap::draw(sf::RenderWindow *window) { - float mapHeight = 1000; - - sf::VertexArray backgroundGradient(sf::Quads, 4); - - // Defining the coordinates clockwise - // Top Left - backgroundGradient[0].position = sf::Vector2f(-2 * mapHeight, -2 * mapHeight); - backgroundGradient[0].color = sf::Color(150, 200, 255); - - // Top Right - backgroundGradient[1].position = sf::Vector2f(2 * mapHeight, -2 * mapHeight); - backgroundGradient[1].color = sf::Color(150, 200, 255); - - // Bottom Right - backgroundGradient[2].position = sf::Vector2f(2 * mapHeight, mapHeight); - backgroundGradient[2].color = sf::Color::White; - - // Bottom Left - backgroundGradient[3].position = sf::Vector2f(-2 * mapHeight, mapHeight); - backgroundGradient[3].color = sf::Color::White; - window->draw(backgroundGradient); } diff --git a/src/sprites/skymap/skymap.hpp b/src/sprites/skymap/skymap.hpp index db5099a..ee016ac 100644 --- a/src/sprites/skymap/skymap.hpp +++ b/src/sprites/skymap/skymap.hpp @@ -7,7 +7,12 @@ class Skymap : public GameObject { public: + explicit Skymap(std::vector skyColors, sf::Vector2 mapSize); + void draw(sf::RenderWindow *window) override; + +private: + sf::VertexArray backgroundGradient; };