Skip to content

Commit

Permalink
[BlockCursor] No longer possible to place a block inside the player. F…
Browse files Browse the repository at this point in the history
…ixes #32.
  • Loading branch information
Unarelith committed Feb 18, 2020
1 parent 0103951 commit c3a9ead
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions client/include/world/ClientPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ClientPlayer : public Player {
float pointTargetedY() const { return m_y + sin(m_angleV * RADIANS_PER_DEGREES); }
float pointTargetedZ() const { return m_z + sin(m_angleH * RADIANS_PER_DEGREES) * cos(m_angleV * RADIANS_PER_DEGREES) - 0.00001; }

const gk::FloatBox &hitbox() const { return m_hitbox; }

static ClientPlayer &getInstance() { return *s_instance; }
static void setInstance(ClientPlayer *instance) { s_instance = instance; }

Expand Down
17 changes: 12 additions & 5 deletions client/source/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,24 @@ void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
if(face == 2) z++;
if(face == 5) z--;

// First, we check if the new block is not replacing another block
u32 blockId = m_world.getBlock(x, y, z);
const Block &block = Registry::getInstance().getBlock(blockId);
if (!blockId || block.drawType() == BlockDrawType::Liquid) {
m_world.setBlock(x, y, z, hotbar.currentItem());
// Second, we check if the new block is not inside the player
const Block &newBlock = Registry::getInstance().getBlock(hotbar.currentItem());
gk::FloatBox boundingBox = newBlock.boundingBox() + gk::Vector3i{x, y, z};
gk::FloatBox playerBoundingBox = m_player.hitbox() + gk::Vector3f{m_player.x(), m_player.y(), m_player.z()};
if (!boundingBox.intersects(playerBoundingBox)) {
m_world.setBlock(x, y, z, hotbar.currentItem());

m_client.sendPlayerPlaceBlock(x, y, z, hotbar.currentItem());
m_client.sendPlayerPlaceBlock(x, y, z, hotbar.currentItem());

const ItemStack &currentStack = m_player.inventory().getStack(hotbar.cursorPos(), 0);
m_player.inventory().setStack(hotbar.cursorPos(), 0, currentStack.amount() > 1 ? currentStack.item().stringID() : "", currentStack.amount() - 1);
const ItemStack &currentStack = m_player.inventory().getStack(hotbar.cursorPos(), 0);
m_player.inventory().setStack(hotbar.cursorPos(), 0, currentStack.amount() > 1 ? currentStack.item().stringID() : "", currentStack.amount() - 1);

m_client.sendPlayerInvUpdate();
m_client.sendPlayerInvUpdate();
}
}
}
}
Expand Down

0 comments on commit c3a9ead

Please sign in to comment.