Support teleporting at the edges of the map

This commit is contained in:
Corentin Jabot 2021-06-16 01:35:36 +02:00
parent bbf3731cf4
commit f6efcbbf79
2 changed files with 14 additions and 3 deletions

View file

@ -49,6 +49,9 @@ Board::Board() {
} }
bool Board::isWalkable(Position point, float position_delta, Direction direction) const { bool Board::isWalkable(Position point, float position_delta, Direction direction) const {
if(point.x <= 0 || point.x >= COLUMNS-1)
return true;
switch (direction) { switch (direction) {
case Direction::LEFT: case Direction::LEFT:
return board_state[int(point.y)][int(point.x - position_delta)] != 0; return board_state[int(point.y)][int(point.x - position_delta)] != 0;

View file

@ -33,9 +33,17 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta) {
} }
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) { void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) {
float position_delta = (time_delta.count() / 128.0); float position_delta = std::min(1.0, (time_delta.count() / 128.0));
if (board.isWalkable(pos, position_delta, desired_direction)) { // Handle teleport
if(pos.x >= COLUMNS-1 && direction == Direction::RIGHT) {
pos.x = -1;
}
else if(pos.x <= 0 && direction == Direction::LEFT) {
pos.x = COLUMNS;
}
else if (board.isWalkable(pos, position_delta, desired_direction)) {
direction = desired_direction; direction = desired_direction;
} }