Skip to content

Commit

Permalink
[BlockCursor] Now reset breaking timer when switching item or activat…
Browse files Browse the repository at this point in the history
…ing a block. Thanks to obiwac who found these issues.
  • Loading branch information
Unarelith committed Jan 27, 2020
1 parent 2251bc0 commit 54493e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions client/include/hud/BlockCursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_
25 changes: 18 additions & 7 deletions client/source/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -133,15 +137,22 @@ void BlockCursor::update(const Hotbar &hotbar, bool useDepthBuffer) {
const ItemStack &currentStack = 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 = &currentStack;
}
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();
}
}
}

Expand Down

0 comments on commit 54493e3

Please sign in to comment.