Skip to content

Commit

Permalink
[Dimension] 'gravity' field added to change gravity per-dimension.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 22, 2020
1 parent 7d3738b commit 4a6bbbe
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 15 deletions.
11 changes: 11 additions & 0 deletions docs/lua-api-dimension.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ Example:
biomes = {"default:grassland", "default:desert"}
```

### `gravity`

Gravity of the dimension.

Example:
```lua
gravity = 0.5
```

Default value is `1`.

### `id`

ID of the dimension. **Mandatory field.**
Expand Down
2 changes: 2 additions & 0 deletions mods/default/dimensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ mod:dimension {
biomes = {"default:netherland"},

sky = "default:sky_nether",

gravity = 1.4,
}

2 changes: 1 addition & 1 deletion source/client/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void ClientCommandHandler::setupCallbacks() {
m_player.setDimension(dimension);
m_player.setPosition(x, y, z);
m_world.clear();
m_world.updateSky(dimension);
m_world.changeDimension(dimension);
m_entityMap.clear();
}
});
Expand Down
2 changes: 1 addition & 1 deletion source/client/states/ServerLoadingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void ServerLoadingState::update() {
if (m_showLoadingState)
std::this_thread::sleep_for(std::chrono::milliseconds(500));

m_game.world().updateSky(0);
m_game.world().changeDimension(0);

m_stateStack->pop();

Expand Down
9 changes: 7 additions & 2 deletions source/client/world/ClientChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@
#include "Chunk.hpp"
#include "ChunkBuilder.hpp"
#include "Config.hpp"
#include "Dimension.hpp"

class TextureAtlas;

class ClientChunk : public Chunk {
public:
ClientChunk(s32 x, s32 y, s32 z, World &world, TextureAtlas &textureAtlas)
: Chunk(x, y, z, world), m_textureAtlas(textureAtlas), m_builder{textureAtlas} {}
ClientChunk(s32 x, s32 y, s32 z, const Dimension &dimension, World &world, TextureAtlas &textureAtlas)
: Chunk(x, y, z, world), m_dimension(dimension), m_textureAtlas(textureAtlas), m_builder{textureAtlas} {}

void update();

void drawLayer(gk::RenderTarget &target, gk::RenderStates states, u8 layer) const;

const Dimension &dimension() const { return m_dimension; }

bool hasBeenRequested() const { return m_hasBeenRequested; }
void setHasBeenRequested(bool hasBeenRequested) { m_hasBeenRequested = hasBeenRequested; }

Expand All @@ -53,6 +56,8 @@ class ClientChunk : public Chunk {
bool areAllNeighboursTooFar() const;

private:
const Dimension &m_dimension;

TextureAtlas &m_textureAtlas;

ChunkBuilder m_builder;
Expand Down
2 changes: 1 addition & 1 deletion source/client/world/ClientPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void ClientPlayer::updatePosition(const ClientWorld &world) {
ClientChunk *chunk = (ClientChunk *)world.getChunkAtBlockPos(m_x, m_y, m_z);
if (chunk && chunk->isInitialized()) {
if (!Config::isFlyModeEnabled) {
m_velocity.z -= m_gravity; // Gravity
m_velocity.z -= chunk->dimension().gravity() * 0.001f;

m_isJumping = true;

Expand Down
1 change: 0 additions & 1 deletion source/client/world/ClientPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class ClientPlayer : public Player {
glm::vec3 m_velocity{0.f};
bool m_isJumping = false;

const float m_gravity = 0.001f;
const float m_jumpSpeed = 0.06f;
};

Expand Down
13 changes: 7 additions & 6 deletions source/client/world/ClientWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void ClientWorld::checkPlayerChunk(double playerX, double playerY, double player

ClientChunk *chunk = (ClientChunk *)getChunk(pcx, pcy, pcz);
if (!chunk) {
m_chunks.emplace(gk::Vector3i{pcx, pcy, pcz}, new ClientChunk(pcx, pcy, pcz, *this, m_textureAtlas));
m_chunks.emplace(gk::Vector3i{pcx, pcy, pcz}, new ClientChunk(pcx, pcy, pcz, *m_dimension, *this, m_textureAtlas));
}
}

Expand All @@ -104,10 +104,11 @@ void ClientWorld::clear() {
m_scene.registry().clear();
}

void ClientWorld::updateSky(u16 dimensionID) {
void ClientWorld::changeDimension(u16 dimensionID) {
const Dimension &dimension = Registry::getInstance().getDimension(dimensionID);
const Sky &sky = Registry::getInstance().getSkyFromStringID(dimension.sky());
m_dimension = &dimension;

const Sky &sky = Registry::getInstance().getSkyFromStringID(dimension.sky());
m_sky = &sky;
}

Expand All @@ -118,7 +119,7 @@ void ClientWorld::receiveChunkData(Network::Packet &packet) {
// Get the chunk from the map or create it if it doesn't exist
ClientChunk *chunk = (ClientChunk *)getChunk(cx, cy, cz);
if (!chunk) {
auto it = m_chunks.emplace(gk::Vector3i{cx, cy, cz}, new ClientChunk(cx, cy, cz, *this, m_textureAtlas));
auto it = m_chunks.emplace(gk::Vector3i{cx, cy, cz}, new ClientChunk(cx, cy, cz, *m_dimension, *this, m_textureAtlas));
chunk = it.first->second.get();
}

Expand Down Expand Up @@ -204,7 +205,7 @@ void ClientWorld::createChunkNeighbours(ClientChunk *chunk) {

ClientChunk *neighbour = (ClientChunk *)getChunk(scx, scy, scz);
if (!neighbour) {
auto it = m_chunks.emplace(gk::Vector3i{scx, scy, scz}, new ClientChunk(scx, scy, scz, *this, m_textureAtlas));
auto it = m_chunks.emplace(gk::Vector3i{scx, scy, scz}, new ClientChunk(scx, scy, scz, *m_dimension, *this, m_textureAtlas));
neighbour = it.first->second.get();
}

Expand All @@ -214,7 +215,7 @@ void ClientWorld::createChunkNeighbours(ClientChunk *chunk) {
}

void ClientWorld::draw(gk::RenderTarget &target, gk::RenderStates states) const {
// glClearColor used to be called in updateSky(), but when "toggling fullscreen mode"
// glClearColor used to be called in changeDimension(), but when "toggling fullscreen mode"
// with SFML, you actually recreate a window. Thus, all the glEnable/glDisable/glClearColor
// states are cleared when fullscreen mode is enabled/disabled.
if (m_sky)
Expand Down
4 changes: 3 additions & 1 deletion source/client/world/ClientWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ClientWorld : public World, public gk::Drawable {

void clear();

void updateSky(u16 dimensionID);
void changeDimension(u16 dimensionID);

void receiveChunkData(Network::Packet &packet);
void removeChunk(ChunkMap::iterator &it);
Expand All @@ -73,6 +73,8 @@ class ClientWorld : public World, public gk::Drawable {

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

const Dimension *m_dimension = nullptr;

ClientScene m_scene;

ChunkMap m_chunks;
Expand Down
2 changes: 2 additions & 0 deletions source/common/world/Chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Chunk : public gk::NonCopyable {
s32 y() const { return m_y; }
s32 z() const { return m_z; }

World &world() { return m_world; }

Chunk *getSurroundingChunk(u8 i) { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
const Chunk *getSurroundingChunk(u8 i) const { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
void setSurroundingChunk(u8 i, Chunk *chunk) { if (i < 6) m_surroundingChunks[i] = chunk; }
Expand Down
4 changes: 2 additions & 2 deletions source/common/world/Dimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include "NetworkUtils.hpp"

void Dimension::serialize(sf::Packet &packet) const {
packet << m_id << m_stringID << m_name << m_biomes << m_sky;
packet << m_id << m_stringID << m_name << m_biomes << m_sky << m_gravity;
}

void Dimension::deserialize(sf::Packet &packet) {
packet >> m_id >> m_stringID >> m_name >> m_biomes >> m_sky;
packet >> m_id >> m_stringID >> m_name >> m_biomes >> m_sky >> m_gravity;
}

// Please update 'docs/lua-api-cpp.md' if you change this
Expand Down
5 changes: 5 additions & 0 deletions source/common/world/Dimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Dimension : public ISerializable {
const std::string &sky() const { return m_sky; }
void setSky(const std::string &sky) { m_sky = sky; }

float gravity() const { return m_gravity; }
void setGravity(float gravity) { m_gravity = gravity; }

static void initUsertype(sol::state &lua);

private:
Expand All @@ -65,6 +68,8 @@ class Dimension : public ISerializable {
std::vector<std::string> m_biomes;

std::string m_sky;

float m_gravity = 1.f;
};

#endif // DIMENSION_HPP_
1 change: 1 addition & 0 deletions source/server/lua/loader/LuaDimensionLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void LuaDimensionLoader::loadDimension(const sol::table &table) const {
if (biomesObject.valid() && biomesObject.get_type() == sol::type::table) {
Dimension &dimension = Registry::getInstance().registerDimension(id, name);
dimension.setSky(table["sky"].get<std::string>());
dimension.setGravity(table["gravity"].get_or(1.f));

sol::table biomesTable = biomesObject.as<sol::table>();
for (auto &it : biomesTable) {
Expand Down

0 comments on commit 4a6bbbe

Please sign in to comment.