From c3a9eadcea4423d7113753973f6327f0800a5587 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Tue, 18 Feb 2020 17:37:59 +0900 Subject: [PATCH] [BlockCursor] No longer possible to place a block inside the player. Fixes #32. --- client/include/world/ClientPlayer.hpp | 2 ++ client/source/hud/BlockCursor.cpp | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/client/include/world/ClientPlayer.hpp b/client/include/world/ClientPlayer.hpp index a43fcda65..d3c22fbf4 100644 --- a/client/include/world/ClientPlayer.hpp +++ b/client/include/world/ClientPlayer.hpp @@ -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; } diff --git a/client/source/hud/BlockCursor.cpp b/client/source/hud/BlockCursor.cpp index a4309d710..e673e1c58 100644 --- a/client/source/hud/BlockCursor.cpp +++ b/client/source/hud/BlockCursor.cpp @@ -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 ¤tStack = 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 ¤tStack = 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(); + } } } }