Skip to content

Commit

Permalink
[Lua API] Added moon, sun and stars attributes in sky definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 17, 2020
1 parent 886aba1 commit 5119ab9
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 11 deletions.
75 changes: 75 additions & 0 deletions docs/lua-api-sky.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ mod:sky {
fog_color = {
day = {50, 153, 204},
},

objects = {
sun = {
texture = "texture-sun",
size = 256,
},

moon = {
texture = "texture-moon_phases",
size = 256,
phases = {
count = 8,
size = 32
}
},

stars = {
count = 1000,
size = 4,
}
}
}
```

Expand Down Expand Up @@ -59,3 +80,57 @@ id = "sky_nether"

IDs are usually of the form `mod:sky` but the `mod:` prefix is prepended automatically so it's not needed.

### `objects`

#### `moon`

Moon attributes table. Example:
```lua
moon = {
texture = "texture-moon_phases"
size = 256,
phases = {
count = 8,
size = 32
}
},
```

Attributes:

- `texture`: texture to use (without texture, the moon will use this color: (240, 240, 240))
- `size`: size of the moon (default: `256`)
- `phases`
- `count`: amount of phases (default: `1`)
- `size`: size of the phase texture (default: `32`)

#### `sun`

Sun attribute table. Example:
```lua
sun = {
texture = "texture-sun",
size = 256,
},
```

Attributes:

- `texture`: texture to use (without texture, the sun will use this color: (255, 255, 0))
- `size`: size of the sun (default: `256`)

#### `stars`

Stars attribute table. Example:
```lua
stars = {
count = 1000,
size = 4,
}
```

Attributes:

- `count`: size of the sun (default: `1000`)
- `size`: size of the sun (default: `4`)

21 changes: 21 additions & 0 deletions mods/default/sky.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ mod:sky {
fog_color = {
day = {50, 153, 204},
},

objects = {
sun = {
texture = "texture-sun", -- FIXME: Use a path instead like block attribute 'tiles'
size = 256,
},

moon = {
texture = "texture-moon_phases", -- FIXME: ^
size = 256,
phases = {
count = 8,
size = 32
}
},

stars = {
count = 1000,
size = 4,
}
}
}

mod:sky {
Expand Down
2 changes: 2 additions & 0 deletions source/client/graphics/CelestialObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ CelestialObject::CelestialObject() {
}

void CelestialObject::setTexture(const std::string &textureName) {
if (textureName.empty()) return;

m_texture = &gk::ResourceHandler::getInstance().get<gk::Texture>(textureName);

m_isUpdateNeeded = true;
Expand Down
44 changes: 35 additions & 9 deletions source/client/graphics/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,44 @@ Skybox::Skybox(gk::Camera &camera, ClientWorld &world) : m_camera(camera), m_wor
m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/skybox.v.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/skybox.f.glsl");
m_shader.linkProgram();
}

m_sun.setSize(256, 256);
void Skybox::loadSky(const Sky &sky) {
const Sky::SunDefinition &sun = sky.sunDefinition();
m_sun = CelestialObject{};
m_sun.setSize(sun.size, sun.size);
m_sun.setPosition(500, -m_sun.width() / 2, -m_sun.height() / 2);
m_sun.setTexture("texture-sun");

m_moon.setSize(256, 256);
try {
m_sun.setTexture(sun.texture);
}
catch (...) {
m_sun.setColor(gk::Color::Yellow);
gkWarning() << "Failed to load sun texture" << sun.texture;
}

const Sky::MoonDefinition &moon = sky.moonDefinition();
m_moon = CelestialObject{};
m_moon.setSize(moon.size, moon.size);
m_moon.setPosition(-500, -m_moon.width() / 2, -m_moon.height() / 2);
m_moon.setTexture("texture-moon_phases");
m_moon.setPhaseCount(8, 32);
m_moon.setPhaseCount(moon.phaseCount, moon.phaseSize);
m_moon.setCurrentPhase(0);

for (int i = 0 ; i < 1000 ; ++i) {
try {
m_moon.setTexture(moon.texture);
}
catch (...) {
m_moon.setColor(gk::Color{240, 240, 240});
gkWarning() << "Failed to load moon texture" << sun.texture;
}

const Sky::StarsDefinition &stars = sky.starsDefinition();
m_stars.clear();
m_stars.reserve(stars.count);
for (int i = 0 ; i < stars.count ; ++i) {
auto &star = m_stars.emplace_back();
star.setColor(gk::Color{0, 0, 0, 0});
star.setSize(4, 4);
star.setSize(stars.size, stars.size);
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});
Expand All @@ -74,8 +97,11 @@ void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
const gk::Vector3d &cameraPosition = m_camera.getDPosition();
states.transform.translate(cameraPosition.x, cameraPosition.y, cameraPosition.z - 50);

target.draw(m_sun, states);
target.draw(m_moon, states);
if (m_sun.width() && m_sun.height())
target.draw(m_sun, states);

if (m_moon.width() && m_moon.height())
target.draw(m_moon, states);

if (Config::isStarRenderingEnabled && skyColor != starColor) {
glDisable(GL_CULL_FACE);
Expand Down
3 changes: 3 additions & 0 deletions source/client/graphics/Skybox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
#include "CelestialObject.hpp"

class ClientWorld;
class Sky;

class Skybox : public gk::Drawable, public gk::Transformable {
public:
Skybox(gk::Camera &camera, ClientWorld &world);

void loadSky(const Sky &sky);

private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Expand Down
6 changes: 6 additions & 0 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ void GameState::update() {
}

m_client.update();

static const Sky *sky = nullptr;
if (sky != m_world.sky() && m_world.sky()) {
sky = m_world.sky();
m_skybox.loadSky(*sky);
}
}

void GameState::initShaders() {
Expand Down
12 changes: 10 additions & 2 deletions source/common/world/Sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ Sky::Sky(u16 id, const std::string &stringID) {
}

void Sky::serialize(sf::Packet &packet) const {
packet << m_id << m_stringID << m_color << m_fogColor;
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;
}

void Sky::deserialize(sf::Packet &packet) {
packet >> m_id >> m_stringID >> m_color >> m_fogColor;
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;
}

29 changes: 29 additions & 0 deletions source/common/world/Sky.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,41 @@ class Sky : public gk::ISerializable {
void setColor(const gk::Color &color) { m_color = color; }
void setFogColor(const gk::Color &fogColor) { m_fogColor = fogColor; }

struct SunDefinition {
std::string texture;
float size;
};

struct MoonDefinition {
std::string texture;
float size;
u16 phaseCount;
u16 phaseSize;
};

struct StarsDefinition {
u16 count;
float size;
};

const SunDefinition &sunDefinition() const { return m_sunDefinition; }
const MoonDefinition &moonDefinition() const { return m_moonDefinition; }
const StarsDefinition &starsDefinition() const { return m_starsDefinition; }

void setSunDefinition(const SunDefinition &sunDefinition) { m_sunDefinition = sunDefinition; }
void setMoonDefinition(const MoonDefinition &moonDefinition) { m_moonDefinition = moonDefinition; }
void setStarsDefinition(const StarsDefinition &starsDefinition) { m_starsDefinition = starsDefinition; }

private:
u16 m_id;
std::string m_stringID;

gk::Color m_color;
gk::Color m_fogColor;

SunDefinition m_sunDefinition;
MoonDefinition m_moonDefinition;
StarsDefinition m_starsDefinition;
};

#endif // SKY_HPP_
44 changes: 44 additions & 0 deletions source/server/lua/loader/LuaSkyLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,49 @@ void LuaSkyLoader::loadSky(const sol::table &table) const {
u8 a = fogColor["day"][4].get_or<u8>(255);
sky.setFogColor(gk::Color{r, g, b, a});
}

loadObjects(sky, table);
}

void LuaSkyLoader::loadObjects(Sky &sky, const sol::table &table) const {
if (sol::object obj = table["objects"] ; obj.valid()) {
sol::table objectsTable = obj.as<sol::table>();

if (sol::object obj = objectsTable["sun"] ; obj.valid()) {
sol::table sunTable = obj.as<sol::table>();

Sky::SunDefinition sunDefinition;
sunDefinition.texture = sunTable["texture"].get_or<std::string>("");
sunDefinition.size = sunTable["size"].get_or(256);

sky.setSunDefinition(sunDefinition);
}

if (sol::object obj = objectsTable["moon"] ; obj.valid()) {
sol::table moonTable = obj.as<sol::table>();

Sky::MoonDefinition moonDefinition;
moonDefinition.texture = moonTable["texture"].get_or<std::string>("");
moonDefinition.size = moonTable["size"].get_or(256);

if (sol::object obj = moonTable["phases"] ; obj.valid()) {
sol::table phasesTable = obj.as<sol::table>();
moonDefinition.phaseCount = phasesTable["count"].get_or(1);
moonDefinition.phaseSize = phasesTable["size"].get_or(32);
}

sky.setMoonDefinition(moonDefinition);
}

if (sol::object obj = objectsTable["stars"] ; obj.valid()) {
sol::table starsTable = obj.as<sol::table>();

Sky::StarsDefinition starsDefinition;
starsDefinition.count = starsTable["count"].get_or(1000);
starsDefinition.size = starsTable["size"].get_or(4);

sky.setStarsDefinition(starsDefinition);
}
}
}

3 changes: 3 additions & 0 deletions source/server/lua/loader/LuaSkyLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <sol/sol.hpp>

class LuaMod;
class Sky;

class LuaSkyLoader {
public:
Expand All @@ -38,6 +39,8 @@ class LuaSkyLoader {
void loadSky(const sol::table &table) const;

private:
void loadObjects(Sky &sky, const sol::table &table) const;

LuaMod &m_mod;
};

Expand Down

0 comments on commit 5119ab9

Please sign in to comment.