Skip to content

Commit

Permalink
[BlockCursor] Fixed right-click holding.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 22, 2021
1 parent ebe9887 commit 451fc71
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions source/client/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
m_animationStart = gk::GameClock::getInstance().getTicks();
m_currentTool = &m_player.inventory().getStack(hotbar.cursorPos(), 0);
}
else if (event.button.button == SDL_BUTTON_RIGHT) {
else if (event.button.button == SDL_BUTTON_RIGHT)
activateBlock(hotbar);
}
}
else if (event.type == SDL_MOUSEBUTTONUP) {
if (event.button.button == SDL_BUTTON_LEFT)
reset();
else if (event.button.button == SDL_BUTTON_RIGHT)
m_activationRepeat = 0;
m_lastActivationTime.reset();
}
}

Expand Down Expand Up @@ -131,16 +130,18 @@ void BlockCursor::update(const Hotbar &hotbar) {
else
m_currentBlock = nullptr;

if (m_activationRepeat
&& gk::GameClock::getInstance().getTicks() > m_activationRepeat + m_activationRepeatDelay)
// Repeat block activation when right click is held
u64 now = gk::GameClock::getInstance().getTicks();
u64 last = m_lastActivationTime.value_or(now);
if (now - last > m_activationRepeatDelay)
activateBlock(hotbar);
}

void BlockCursor::activateBlock(const Hotbar &hotbar) {
if (m_animationStart != 0)
m_animationStart = 0;

m_activationRepeat = gk::GameClock::getInstance().getTicks();
m_lastActivationTime = gk::GameClock::getInstance().getTicks();

u32 blockId = m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z);
const Block &block = Registry::getInstance().getBlock(blockId);
Expand Down
4 changes: 2 additions & 2 deletions source/client/hud/BlockCursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BlockCursor : public gk::Drawable {

void update(const Hotbar &hotbar);

void reset() { m_animationStart = 0; m_activationRepeat = 0; }
void reset() { m_animationStart = 0; m_lastActivationTime.reset(); }

const BlockState *currentBlock() const { return m_currentBlock; }

Expand Down Expand Up @@ -76,7 +76,7 @@ class BlockCursor : public gk::Drawable {
gk::Texture *m_blockDestroyTexture = nullptr;

const u16 m_activationRepeatDelay = 250;
u16 m_activationRepeat = 0;
std::optional<u64> m_lastActivationTime = 0;
};

#endif // BLOCKCURSOR_HPP_

0 comments on commit 451fc71

Please sign in to comment.