diff --git a/docs/lua-api-sky.md b/docs/lua-api-sky.md index 44a489f6..ebfe3ca5 100644 --- a/docs/lua-api-sky.md +++ b/docs/lua-api-sky.md @@ -14,6 +14,10 @@ mod:sky { day = {50, 153, 204}, }, + daylight_cycle = { + speed = 1.0 + }, + objects = { sun = { texture = "texture-sun", @@ -50,9 +54,26 @@ color = { } ``` -Possible values: +Attributes: -- `day`: Sky color at midday +- `day`: sky color at midday + +### `daylight_cycle` + +Day/night cycle parameters. + +Example: +```lua +daylight_cycle = { + speed = 1.0 +} +``` + +The example above is the minimal code required to add a day/night cycle to a sky. + +Attributes: + +- `speed`: speed of the cycle (default: `1.0`) ### `fog_color` @@ -65,9 +86,9 @@ fog_color = { } ``` -Possible values: +Attributes: -- `day`: Fog color at midday +- `day`: gog color at midday ### `id` @@ -84,7 +105,9 @@ IDs are usually of the form `mod:sky` but the `mod:` prefix is prepended automat #### `moon` -Moon attributes table. Example: +Moon attributes table. + +Example: ```lua moon = { texture = "texture-moon_phases" @@ -106,7 +129,9 @@ Attributes: #### `sun` -Sun attribute table. Example: +Sun attribute table. + +Example: ```lua sun = { texture = "texture-sun", @@ -121,7 +146,9 @@ Attributes: #### `stars` -Stars attribute table. Example: +Stars attribute table. + +Example: ```lua stars = { count = 1000, diff --git a/mods/default/sky.lua b/mods/default/sky.lua index bce188ca..2c032ec5 100644 --- a/mods/default/sky.lua +++ b/mods/default/sky.lua @@ -36,6 +36,10 @@ mod:sky { day = {50, 153, 204}, }, + daylight_cycle = { + speed = 1.0 + }, + objects = { sun = { texture = "texture-sun", -- FIXME: Use a path instead like block attribute 'tiles' diff --git a/source/client/graphics/Skybox.cpp b/source/client/graphics/Skybox.cpp index 96277f24..4839547c 100644 --- a/source/client/graphics/Skybox.cpp +++ b/source/client/graphics/Skybox.cpp @@ -41,6 +41,7 @@ void Skybox::loadSky(const Sky &sky) { m_sun = CelestialObject{}; m_sun.setSize(sun.size, sun.size); m_sun.setPosition(500, -m_sun.width() / 2, -m_sun.height() / 2); + m_sun.setRotationSpeed(sky.daylightCycleSpeed()); try { m_sun.setTexture(sun.texture); @@ -56,6 +57,7 @@ void Skybox::loadSky(const Sky &sky) { m_moon.setPosition(-500, -m_moon.width() / 2, -m_moon.height() / 2); m_moon.setPhaseCount(moon.phaseCount, moon.phaseSize); m_moon.setCurrentPhase(0); + m_moon.setRotationSpeed(sky.daylightCycleSpeed()); try { m_moon.setTexture(moon.texture); @@ -75,13 +77,15 @@ void Skybox::loadSky(const Sky &sky) { star.setPosition(650 * ((rand() % 2) * 2 - 1), (rand() % 500) * 2 - 500, (rand() % 500) * 2 - 500); star.setRotationOffset(rand() % GameTime::dayLength); star.setRotationAxis({rand() % 100 / 100.f, rand() % 100 / 100.f, rand() % 100 / 100.f}); + star.setRotationSpeed(sky.daylightCycleSpeed()); } } void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const { if (!m_world.sky()) return; - gk::Color skyColor = GameTime::getSkyColorFromTime(*m_world.sky(), GameTime::getCurrentTime()); + float time = GameTime::getCurrentTime(0, m_world.sky()->daylightCycleSpeed()); + gk::Color skyColor = GameTime::getSkyColorFromTime(*m_world.sky(), time); gk::Color starColor = m_world.sky()->color(); gk::Shader::bind(&m_shader); diff --git a/source/client/states/GameState.cpp b/source/client/states/GameState.cpp index a7f4e95c..c86f1f9f 100644 --- a/source/client/states/GameState.cpp +++ b/source/client/states/GameState.cpp @@ -210,13 +210,22 @@ void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) { void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { gk::Shader::bind(&m_shader); - float time = GameTime::getCurrentTime(); if (m_world.sky()) { - const gk::Color &color = GameTime::getSkyColorFromTime(*m_world.sky(), time); - glClearColor(color.r, color.g, color.b, color.a); + if (m_world.sky()->daylightCycleSpeed()) { + float time = GameTime::getCurrentTime(0, m_world.sky()->daylightCycleSpeed()); + const gk::Color &color = GameTime::getSkyColorFromTime(*m_world.sky(), time); + glClearColor(color.r, color.g, color.b, color.a); - m_shader.setUniform("u_skyColor", color); - m_shader.setUniform("u_sunlightIntensity", GameTime::getSunlightIntensityFromTime(time)); + m_shader.setUniform("u_skyColor", color); + m_shader.setUniform("u_sunlightIntensity", GameTime::getSunlightIntensityFromTime(time)); + } + else { + const gk::Color &color = m_world.sky()->color(); + glClearColor(color.r, color.g, color.b, color.a); + + m_shader.setUniform("u_skyColor", m_world.sky()->color()); + m_shader.setUniform("u_sunlightIntensity", 1.f); + } } gk::Shader::bind(nullptr); diff --git a/source/common/world/Sky.cpp b/source/common/world/Sky.cpp index 9195aa9c..820b5113 100644 --- a/source/common/world/Sky.cpp +++ b/source/common/world/Sky.cpp @@ -37,7 +37,8 @@ void Sky::serialize(sf::Packet &packet) const { << m_sunDefinition.texture << m_sunDefinition.size << m_moonDefinition.texture << m_moonDefinition.size << m_moonDefinition.phaseCount << m_moonDefinition.phaseSize - << m_starsDefinition.count << m_starsDefinition.size; + << m_starsDefinition.count << m_starsDefinition.size + << m_daylightCycleSpeed; } void Sky::deserialize(sf::Packet &packet) { @@ -45,6 +46,7 @@ void Sky::deserialize(sf::Packet &packet) { >> m_sunDefinition.texture >> m_sunDefinition.size >> m_moonDefinition.texture >> m_moonDefinition.size >> m_moonDefinition.phaseCount >> m_moonDefinition.phaseSize - >> m_starsDefinition.count >> m_starsDefinition.size; + >> m_starsDefinition.count >> m_starsDefinition.size + >> m_daylightCycleSpeed; } diff --git a/source/common/world/Sky.hpp b/source/common/world/Sky.hpp index 8e73dd95..31b20d75 100644 --- a/source/common/world/Sky.hpp +++ b/source/common/world/Sky.hpp @@ -74,6 +74,9 @@ class Sky : public gk::ISerializable { void setMoonDefinition(const MoonDefinition &moonDefinition) { m_moonDefinition = moonDefinition; } void setStarsDefinition(const StarsDefinition &starsDefinition) { m_starsDefinition = starsDefinition; } + float daylightCycleSpeed() const { return m_daylightCycleSpeed; } + void setDaylightCycleSpeed(float daylightCycleSpeed) { m_daylightCycleSpeed = daylightCycleSpeed; } + private: u16 m_id; std::string m_stringID; @@ -84,6 +87,8 @@ class Sky : public gk::ISerializable { SunDefinition m_sunDefinition; MoonDefinition m_moonDefinition; StarsDefinition m_starsDefinition; + + float m_daylightCycleSpeed = 0.f; }; #endif // SKY_HPP_ diff --git a/source/server/lua/loader/LuaSkyLoader.cpp b/source/server/lua/loader/LuaSkyLoader.cpp index 50e49fcf..b2b775ab 100644 --- a/source/server/lua/loader/LuaSkyLoader.cpp +++ b/source/server/lua/loader/LuaSkyLoader.cpp @@ -51,6 +51,11 @@ void LuaSkyLoader::loadSky(const sol::table &table) const { sky.setFogColor(gk::Color{r, g, b, a}); } + if (sol::object obj = table["daylight_cycle"] ; obj.valid()) { + sol::table daylightCycleTable = obj.as(); + sky.setDaylightCycleSpeed(daylightCycleTable["speed"].get_or(0.f)); + } + loadObjects(sky, table); }