This commit is contained in:
Patricia Aas 2021-07-06 16:33:33 +02:00
parent ab1e68a266
commit a7a1565c99

View file

@ -63,34 +63,42 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool
} }
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) { void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) {
const double position_delta = 0.004 * time_delta.count();
auto moveToPosition = [&](Position point, double position_delta, Direction direction) { const size_t right = COLUMNS - 1;
switch (direction) { const size_t left = 0;
if (std::round(pos.x) == right && direction == Direction::RIGHT) {
pos.x = left;
return;
} else if (std::round(pos.x) == left && direction == Direction::LEFT) {
pos.x = right;
return;
}
const double position_delta = 0.004 * time_delta.count();
const auto pacman_size = 1.0;
auto moveToPosition = [&](Position point, Direction move_direction) {
switch (move_direction) {
case Direction::LEFT: case Direction::LEFT:
return Position{ std::floor(point.x - position_delta), std::floor(point.y) }; return GridPosition{ std::size_t(point.x - position_delta), std::size_t(point.y) };
case Direction::RIGHT: case Direction::RIGHT:
return Position{ std::floor(point.x + 1), std::floor(point.y) }; return GridPosition{ std::size_t(point.x + pacman_size), std::size_t(point.y) };
case Direction::UP: case Direction::UP:
return Position{ std::floor(point.x), std::floor(point.y - position_delta) }; return GridPosition{ std::size_t(point.x), std::size_t(point.y - position_delta) };
case Direction::DOWN: case Direction::DOWN:
return Position{ std::floor(point.x), std::floor(point.y + 1) }; return GridPosition{ std::size_t(point.x), std::size_t(point.y + pacman_size) };
case Direction::NONE: case Direction::NONE:
default: default:
return point; return positionToGridPosition(point);
} }
}; };
auto canGo = [&](Direction desired_direction) { auto canGo = [&](Direction move_direction) {
return board.isWalkableForPacMan(positionToGridPosition(moveToPosition(pos, position_delta, desired_direction))); return pacman::Board::isWalkableForPacMan(moveToPosition(pos, move_direction));
}; };
// Handle teleport if (canGo(desired_direction)) {
if (pos.x >= COLUMNS - 1 && direction == Direction::RIGHT) {
pos.x = -1;
} else if (pos.x <= 0 && direction == Direction::LEFT) {
pos.x = COLUMNS;
} else if (canGo(desired_direction)) {
direction = desired_direction; direction = desired_direction;
} }
@ -100,18 +108,18 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar
break; break;
case Direction::LEFT: case Direction::LEFT:
pos.x -= position_delta; pos.x -= position_delta;
pos.y = floor(pos.y); pos.y = std::floor(pos.y);
break; break;
case Direction::RIGHT: case Direction::RIGHT:
pos.x += position_delta; pos.x += position_delta;
pos.y = floor(pos.y); pos.y = std::floor(pos.y);
break; break;
case Direction::UP: case Direction::UP:
pos.x = floor(pos.x); pos.x = std::floor(pos.x);
pos.y -= position_delta; pos.y -= position_delta;
break; break;
case Direction::DOWN: case Direction::DOWN:
pos.x = floor(pos.x); pos.x = std::floor(pos.x);
pos.y += position_delta; pos.y += position_delta;
break; break;
} }