76 lines
1.9 KiB
C++
76 lines
1.9 KiB
C++
#include "game.h"
|
|
|
|
BlockInfo *Game::pointedBlockInfo() const
|
|
{
|
|
return this->world->blockInfoAt(this->cursorPos);
|
|
}
|
|
|
|
bool Game::movePlayerBy(WorldCoord dir, bool camera_follow)
|
|
{
|
|
WorldCoord candidatePos = this->playerPos + dir;
|
|
candidatePos = this->world->limits.clampPoint(candidatePos);
|
|
if(candidatePos == this->playerPos)
|
|
return false;
|
|
|
|
BlockInfo *info = Nooncraft::getBlockInfo(this->world->cellAt(
|
|
candidatePos.x, candidatePos.y));
|
|
if(!info || !info->walkable)
|
|
return false;
|
|
|
|
this->playerPos = candidatePos;
|
|
if(camera_follow)
|
|
this->camera->moveToFollow(this->playerPos);
|
|
this->moveCursorBy(dir);
|
|
return true;
|
|
}
|
|
|
|
bool Game::moveCursorBy(WorldCoord dir)
|
|
{
|
|
WorldCoord candidatePos = this->cursorPos + dir;
|
|
int reachDistance = 3;
|
|
|
|
WorldRect reach(
|
|
this->playerPos.x - reachDistance,
|
|
this->playerPos.x + reachDistance,
|
|
this->playerPos.y - reachDistance,
|
|
this->playerPos.y + reachDistance);
|
|
|
|
if(!reach.contains(candidatePos))
|
|
return false;
|
|
|
|
this->cursorPos = this->world->limits.clampPoint(candidatePos);
|
|
return true;
|
|
}
|
|
|
|
int Game::blockBreakingDuration() const
|
|
{
|
|
BlockInfo *info = this->pointedBlockInfo();
|
|
if(!info)
|
|
return -1;
|
|
|
|
/* Determine how many ticks would be needed with the current tool */
|
|
// TODO: Query the user's current tool
|
|
int requiredTicks = info->baseBreakDurationTicks;
|
|
return requiredTicks;
|
|
}
|
|
|
|
int8_t Game::blockBreakingProgress() const
|
|
{
|
|
return this->damageTicks * 4 / this->blockBreakingDuration();
|
|
}
|
|
|
|
void Game::updateBlockBreaking()
|
|
{
|
|
if(this->damageTicks < 0)
|
|
return;
|
|
this->damageTicks++;
|
|
|
|
int requiredTicks = this->blockBreakingDuration();
|
|
|
|
if(this->damageTicks >= requiredTicks) {
|
|
this->world->trySetCellAt(this->cursorPos.x, this->cursorPos.y,
|
|
BlockID(AIR));
|
|
this->damageTicks = -1;
|
|
}
|
|
}
|