[Lua API] Added 'daylight_cycle' in sky definition to control its presence and speed.

This commit is contained in:
Quentin Bazin 2020-07-17 06:05:12 +02:00
parent 5119ab9985
commit 1ded0d929e
7 changed files with 71 additions and 15 deletions

View File

@ -14,6 +14,10 @@ mod:sky {
day = {50, 153, 204}, day = {50, 153, 204},
}, },
daylight_cycle = {
speed = 1.0
},
objects = { objects = {
sun = { sun = {
texture = "texture-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` ### `fog_color`
@ -65,9 +86,9 @@ fog_color = {
} }
``` ```
Possible values: Attributes:
- `day`: Fog color at midday - `day`: gog color at midday
### `id` ### `id`
@ -84,7 +105,9 @@ IDs are usually of the form `mod:sky` but the `mod:` prefix is prepended automat
#### `moon` #### `moon`
Moon attributes table. Example: Moon attributes table.
Example:
```lua ```lua
moon = { moon = {
texture = "texture-moon_phases" texture = "texture-moon_phases"
@ -106,7 +129,9 @@ Attributes:
#### `sun` #### `sun`
Sun attribute table. Example: Sun attribute table.
Example:
```lua ```lua
sun = { sun = {
texture = "texture-sun", texture = "texture-sun",
@ -121,7 +146,9 @@ Attributes:
#### `stars` #### `stars`
Stars attribute table. Example: Stars attribute table.
Example:
```lua ```lua
stars = { stars = {
count = 1000, count = 1000,

View File

@ -36,6 +36,10 @@ mod:sky {
day = {50, 153, 204}, day = {50, 153, 204},
}, },
daylight_cycle = {
speed = 1.0
},
objects = { objects = {
sun = { sun = {
texture = "texture-sun", -- FIXME: Use a path instead like block attribute 'tiles' texture = "texture-sun", -- FIXME: Use a path instead like block attribute 'tiles'

View File

@ -41,6 +41,7 @@ void Skybox::loadSky(const Sky &sky) {
m_sun = CelestialObject{}; m_sun = CelestialObject{};
m_sun.setSize(sun.size, sun.size); m_sun.setSize(sun.size, sun.size);
m_sun.setPosition(500, -m_sun.width() / 2, -m_sun.height() / 2); m_sun.setPosition(500, -m_sun.width() / 2, -m_sun.height() / 2);
m_sun.setRotationSpeed(sky.daylightCycleSpeed());
try { try {
m_sun.setTexture(sun.texture); 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.setPosition(-500, -m_moon.width() / 2, -m_moon.height() / 2);
m_moon.setPhaseCount(moon.phaseCount, moon.phaseSize); m_moon.setPhaseCount(moon.phaseCount, moon.phaseSize);
m_moon.setCurrentPhase(0); m_moon.setCurrentPhase(0);
m_moon.setRotationSpeed(sky.daylightCycleSpeed());
try { try {
m_moon.setTexture(moon.texture); 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.setPosition(650 * ((rand() % 2) * 2 - 1), (rand() % 500) * 2 - 500, (rand() % 500) * 2 - 500);
star.setRotationOffset(rand() % GameTime::dayLength); star.setRotationOffset(rand() % GameTime::dayLength);
star.setRotationAxis({rand() % 100 / 100.f, rand() % 100 / 100.f, rand() % 100 / 100.f}); 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 { void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (!m_world.sky()) return; 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::Color starColor = m_world.sky()->color();
gk::Shader::bind(&m_shader); gk::Shader::bind(&m_shader);

View File

@ -210,13 +210,22 @@ void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
gk::Shader::bind(&m_shader); gk::Shader::bind(&m_shader);
float time = GameTime::getCurrentTime();
if (m_world.sky()) { if (m_world.sky()) {
const gk::Color &color = GameTime::getSkyColorFromTime(*m_world.sky(), time); if (m_world.sky()->daylightCycleSpeed()) {
glClearColor(color.r, color.g, color.b, color.a); 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_skyColor", color);
m_shader.setUniform("u_sunlightIntensity", GameTime::getSunlightIntensityFromTime(time)); 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); gk::Shader::bind(nullptr);

View File

@ -37,7 +37,8 @@ void Sky::serialize(sf::Packet &packet) const {
<< m_sunDefinition.texture << m_sunDefinition.size << m_sunDefinition.texture << m_sunDefinition.size
<< m_moonDefinition.texture << m_moonDefinition.size << m_moonDefinition.texture << m_moonDefinition.size
<< m_moonDefinition.phaseCount << m_moonDefinition.phaseSize << 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) { void Sky::deserialize(sf::Packet &packet) {
@ -45,6 +46,7 @@ void Sky::deserialize(sf::Packet &packet) {
>> m_sunDefinition.texture >> m_sunDefinition.size >> m_sunDefinition.texture >> m_sunDefinition.size
>> m_moonDefinition.texture >> m_moonDefinition.size >> m_moonDefinition.texture >> m_moonDefinition.size
>> m_moonDefinition.phaseCount >> m_moonDefinition.phaseSize >> m_moonDefinition.phaseCount >> m_moonDefinition.phaseSize
>> m_starsDefinition.count >> m_starsDefinition.size; >> m_starsDefinition.count >> m_starsDefinition.size
>> m_daylightCycleSpeed;
} }

View File

@ -74,6 +74,9 @@ class Sky : public gk::ISerializable {
void setMoonDefinition(const MoonDefinition &moonDefinition) { m_moonDefinition = moonDefinition; } void setMoonDefinition(const MoonDefinition &moonDefinition) { m_moonDefinition = moonDefinition; }
void setStarsDefinition(const StarsDefinition &starsDefinition) { m_starsDefinition = starsDefinition; } void setStarsDefinition(const StarsDefinition &starsDefinition) { m_starsDefinition = starsDefinition; }
float daylightCycleSpeed() const { return m_daylightCycleSpeed; }
void setDaylightCycleSpeed(float daylightCycleSpeed) { m_daylightCycleSpeed = daylightCycleSpeed; }
private: private:
u16 m_id; u16 m_id;
std::string m_stringID; std::string m_stringID;
@ -84,6 +87,8 @@ class Sky : public gk::ISerializable {
SunDefinition m_sunDefinition; SunDefinition m_sunDefinition;
MoonDefinition m_moonDefinition; MoonDefinition m_moonDefinition;
StarsDefinition m_starsDefinition; StarsDefinition m_starsDefinition;
float m_daylightCycleSpeed = 0.f;
}; };
#endif // SKY_HPP_ #endif // SKY_HPP_

View File

@ -51,6 +51,11 @@ void LuaSkyLoader::loadSky(const sol::table &table) const {
sky.setFogColor(gk::Color{r, g, b, a}); sky.setFogColor(gk::Color{r, g, b, a});
} }
if (sol::object obj = table["daylight_cycle"] ; obj.valid()) {
sol::table daylightCycleTable = obj.as<sol::table>();
sky.setDaylightCycleSpeed(daylightCycleTable["speed"].get_or(0.f));
}
loadObjects(sky, table); loadObjects(sky, table);
} }