Skip to content

Commit

Permalink
[mods/default] 'Torch' added.
Browse files Browse the repository at this point in the history
[BlockState] 'isCollidable' attribute added.
[TextButton] Now rounding position.
  • Loading branch information
Unarelith committed Jul 20, 2020
1 parent 5d6b9e8 commit 64ddacd
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 10 deletions.
11 changes: 11 additions & 0 deletions docs/lua-api-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ If you set this parameter the block will be displayed as an item in the inventor

The texture is loaded from `mods/<your-mod>/textures/items/`.

### `is_collidable`

Defines if the block can block the player or not.

Example:
```lua
is_collidable = true -- this is the default value
```

The default value changes to `false` if the draw type is `liquid` or `xshape`.

### `is_light_source`

Defines if the block is the light source or not.
Expand Down
10 changes: 10 additions & 0 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,16 @@ mod:block {
end
}

mod:block {
id = "torch",
name = "Torch",
tiles = "torch_on.png",
is_light_source = true,
is_collidable = false,
draw_type = "boundingbox",
bounding_box = {7 / 16, 7 / 16, 0, 2 / 16, 2 / 16, 10 / 16},
}

dofile("blocks/workbench.lua")
dofile("blocks/furnace.lua")
dofile("blocks/door.lua")
Expand Down
Binary file added mods/default/textures/blocks/torch_on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions source/client/gui/TextButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ void TextButton::setText(const std::string &text) {
m_text.setString(text);
m_text.updateVertexBuffer();
m_text.setPosition(
std::roundf(m_width / 2.f - m_text.getSize().x / 2.f),
std::roundf(m_height / 2.f - m_text.getSize().y / 2.f)
std::roundf(m_width / 2.f - m_text.getSize().x / 2.f) + 0.5f,
std::roundf(m_height / 2.f - m_text.getSize().y / 2.f) + 0.5f
);
}

Expand Down
8 changes: 4 additions & 4 deletions source/client/world/ChunkBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@ inline u8 ChunkBuilder::getAmbientOcclusion(s8f x, s8f y, s8f z, const gk::Vecto
};

bool blockPresence[4] = {
blocks[0] && blocks[0]->block().id() != 0 && blocks[0]->isOpaque(),
blocks[1] && blocks[1]->block().id() != 0 && blocks[1]->isOpaque(),
blocks[2] && blocks[2]->block().id() != 0 && blocks[2]->isOpaque(),
blocks[3] && blocks[3]->block().id() != 0 && blocks[3]->isOpaque()
blocks[0] && blocks[0]->block().id() != 0 && blocks[0]->isOpaque() && !blocks[0]->isLightSource(),
blocks[1] && blocks[1]->block().id() != 0 && blocks[1]->isOpaque() && !blocks[1]->isLightSource(),
blocks[2] && blocks[2]->block().id() != 0 && blocks[2]->isOpaque() && !blocks[2]->isLightSource(),
blocks[3] && blocks[3]->block().id() != 0 && blocks[3]->isOpaque() && !blocks[3]->isLightSource()
};

bool side1 = blockPresence[(minOffset.x != 0) ? 2 : 1];
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 @@ -186,7 +186,7 @@ void ClientPlayer::checkCollisions(const ClientWorld &world) {

bool passable(const ClientWorld &world, double x, double y, double z) {
const BlockState *blockState = world.getBlockState(floor(x), floor(y), floor(z));
return !blockState || !blockState->block().id() || blockState->drawType() == BlockDrawType::Liquid || blockState->drawType() == BlockDrawType::XShape;
return !blockState || !blockState->block().id() || !blockState->isCollidable();
}

void ClientPlayer::testPoint(const ClientWorld &world, double x, double y, double z, glm::vec3 &vel) {
Expand Down
4 changes: 2 additions & 2 deletions source/common/world/BlockState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void BlockState::serialize(sf::Packet &packet) const {
<< m_boundingBox << u8(m_drawType) << m_colorMultiplier
<< m_isOpaque << m_isLightSource
<< m_inventoryImage << m_fogDepth << m_fogColor
<< m_drawOffset << m_attrs;
<< m_drawOffset << m_isCollidable << m_attrs;
}

void BlockState::deserialize(sf::Packet &packet) {
Expand All @@ -46,7 +46,7 @@ void BlockState::deserialize(sf::Packet &packet) {
>> m_boundingBox >> drawType >> m_colorMultiplier
>> m_isOpaque >> m_isLightSource
>> m_inventoryImage >> m_fogDepth >> m_fogColor
>> m_drawOffset >> m_attrs;
>> m_drawOffset >> m_isCollidable >> m_attrs;

m_drawType = BlockDrawType(drawType);
}
Expand Down
3 changes: 3 additions & 0 deletions source/common/world/BlockState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class BlockState : public gk::ISerializable {
attr_fogDepth = 1 << 12,
attr_fogColor = 1 << 13,
attr_drawOffset = 1 << 14,
attr_isCollidable = 1 << 15,
};

BLOCK_ATTR(std::string, label);
Expand All @@ -154,6 +155,8 @@ class BlockState : public gk::ISerializable {

BLOCK_ATTR_V(gk::Vector3f, drawOffset, (gk::Vector3f{0, 0, 0}));

BLOCK_ATTR_V(bool, isCollidable, true);

u32 m_attrs = 0;

// isOpaque needs a custom getter
Expand Down
9 changes: 8 additions & 1 deletion source/server/lua/loader/LuaBlockLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ void LuaBlockLoader::loadBlockState(BlockState &state, const sol::table &table,
if (!tiles.textureFilenames().empty())
state.tiles(tiles);

loadDrawType(state, table, block);
loadProperties(state, table);
loadBoundingBox(state, table);
loadDrawType(state, table, block);
loadItemDrop(state, table);
loadColorMultiplier(state, table);

Expand Down Expand Up @@ -121,6 +121,9 @@ inline void LuaBlockLoader::loadProperties(BlockState &state, const sol::table &
drawOffset.value().get<float>(3),
});
}

if (table["is_collidable"].get_type() == sol::type::boolean)
state.isCollidable(table["is_collidable"].get<bool>());
}

inline void LuaBlockLoader::loadBoundingBox(BlockState &state, const sol::table &table) const {
Expand Down Expand Up @@ -161,6 +164,10 @@ inline void LuaBlockLoader::loadDrawType(BlockState &state, const sol::table &ta
else
gkError() << "In" << block.stringID() << " definition: Block draw type must be a string";
}

if (state.drawType() == BlockDrawType::Liquid || state.drawType() == BlockDrawType::XShape) {
state.isCollidable(false);
}
}

inline void LuaBlockLoader::loadItemDrop(BlockState &state, const sol::table &table) const {
Expand Down
Binary file added texturepacks/minecraft/blocks/torch_on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 64ddacd

Please sign in to comment.