From 54493e3d5840128fbc6d79d98bb71c56daed453a Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Mon, 27 Jan 2020 12:41:29 +0900 Subject: [PATCH] [BlockCursor] Now reset breaking timer when switching item or activating a block. Thanks to obiwac who found these issues. --- TODO | 3 ++- client/include/hud/BlockCursor.hpp | 1 + client/source/hud/BlockCursor.cpp | 25 ++++++++++++++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index 8e42d08fa..90af0f93a 100644 --- a/TODO +++ b/TODO @@ -20,7 +20,8 @@ TODO ◦ DONE: When loading chunks with smooth lighting enabled, they have black borders ◦ DONE: Flower lighting is weird ◦ TODO: Trees should block light -• TODO: `ServerWorld::sendWorldData` is useless and should be replaced by `sendSpawnData` +• DONE: Reset breaking timer when right-clicking a block +• DONE: Reset breaking timer when switching items • TODO: Shapeless recipe code isn’t working • TODO: GUI scale issues ◦ TODO: `HUD` doesn’t update when GUI scale is changed diff --git a/client/include/hud/BlockCursor.hpp b/client/include/hud/BlockCursor.hpp index 00763192f..89fea4041 100644 --- a/client/include/hud/BlockCursor.hpp +++ b/client/include/hud/BlockCursor.hpp @@ -53,6 +53,7 @@ class BlockCursor : public gk::Drawable { unsigned int m_animationStart = 0; glm::vec4 m_selectedBlock{0, 0, 0, -1}; const Block *m_currentBlock = nullptr; + const ItemStack *m_currentTool = nullptr; }; #endif // BLOCKCURSOR_HPP_ diff --git a/client/source/hud/BlockCursor.cpp b/client/source/hud/BlockCursor.cpp index ac560741a..165838b59 100644 --- a/client/source/hud/BlockCursor.cpp +++ b/client/source/hud/BlockCursor.cpp @@ -67,8 +67,12 @@ void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) { if (event.type == SDL_MOUSEBUTTONDOWN && m_selectedBlock.w != -1) { if (event.button.button == SDL_BUTTON_LEFT) { m_animationStart = gk::GameClock::getTicks(); + m_currentTool = &m_player.inventory().getStack(hotbar.cursorPos(), 0); } else if (event.button.button == SDL_BUTTON_RIGHT) { + if (m_animationStart != 0) + m_animationStart = 0; + u32 blockId = m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z); const Block &block = Registry::getInstance().getBlock(blockId); const Item &item = Registry::getInstance().getItem(hotbar.currentItem()); @@ -133,15 +137,22 @@ void BlockCursor::update(const Hotbar &hotbar, bool useDepthBuffer) { const ItemStack ¤tStack = m_player.inventory().getStack(hotbar.cursorPos(), 0); float timeToBreak = 0; if (m_animationStart) { - timeToBreak = m_currentBlock->timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed()); - if (gk::GameClock::getTicks() > m_animationStart + timeToBreak * 1000) { - ItemStack itemDrop = m_currentBlock->getItemDrop(); - m_player.inventory().addStack(itemDrop.item().name(), itemDrop.amount()); - m_world.setBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 0); + if (m_currentTool->item().id() != currentStack.item().id()) { m_animationStart = gk::GameClock::getTicks(); + m_currentTool = ¤tStack; + } + else { + timeToBreak = m_currentBlock->timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed()); + + if (gk::GameClock::getTicks() > m_animationStart + timeToBreak * 1000) { + ItemStack itemDrop = m_currentBlock->getItemDrop(); + m_player.inventory().addStack(itemDrop.item().name(), itemDrop.amount()); + m_world.setBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 0); + m_animationStart = gk::GameClock::getTicks(); - m_client.sendPlayerDigBlock(m_selectedBlock); - m_client.sendPlayerInvUpdate(); + m_client.sendPlayerDigBlock(m_selectedBlock); + m_client.sendPlayerInvUpdate(); + } } }