Skip to content

Commit

Permalink
[Lua API] Added 'daylight_cycle' in sky definition to control its pre…
Browse files Browse the repository at this point in the history
…sence and speed.
  • Loading branch information
Unarelith committed Jul 17, 2020
1 parent 5119ab9 commit 1ded0d9
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 15 deletions.
41 changes: 34 additions & 7 deletions docs/lua-api-sky.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ mod:sky {
day = {50, 153, 204},
},

daylight_cycle = {
speed = 1.0
},

objects = {
sun = {
texture = "texture-sun",
Expand Down Expand Up @@ -50,9 +54,26 @@ color = {
}
```

Possible values:
Attributes:

- `day`: sky color at midday

### `daylight_cycle`

Day/night cycle parameters.

Example:
```lua
daylight_cycle = {
speed = 1.0
}
```

- `day`: Sky color at midday
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`

Expand All @@ -65,9 +86,9 @@ fog_color = {
}
```

Possible values:
Attributes:

- `day`: Fog color at midday
- `day`: gog color at midday

### `id`

Expand All @@ -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"
Expand All @@ -106,7 +129,9 @@ Attributes:

#### `sun`

Sun attribute table. Example:
Sun attribute table.

Example:
```lua
sun = {
texture = "texture-sun",
Expand All @@ -121,7 +146,9 @@ Attributes:

#### `stars`

Stars attribute table. Example:
Stars attribute table.

Example:
```lua
stars = {
count = 1000,
Expand Down
4 changes: 4 additions & 0 deletions mods/default/sky.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 5 additions & 1 deletion source/client/graphics/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
19 changes: 14 additions & 5 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions source/common/world/Sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ 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) {
packet >> m_id >> m_stringID >> m_color >> m_fogColor
>> 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;
}

5 changes: 5 additions & 0 deletions source/common/world/Sky.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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_
5 changes: 5 additions & 0 deletions source/server/lua/loader/LuaSkyLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sol::table>();
sky.setDaylightCycleSpeed(daylightCycleTable["speed"].get_or(0.f));
}

loadObjects(sky, table);
}

Expand Down

0 comments on commit 1ded0d9

Please sign in to comment.