From f6efcbbf7909a3698e30cc9bf774965b0aa55859 Mon Sep 17 00:00:00 2001 From: Corentin Jabot Date: Wed, 16 Jun 2021 01:35:36 +0200 Subject: [PATCH] Support teleporting at the edges of the map --- lib/Board.cpp | 5 ++++- lib/PacMan.cpp | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/Board.cpp b/lib/Board.cpp index 18cfb79..47f8a02 100644 --- a/lib/Board.cpp +++ b/lib/Board.cpp @@ -49,7 +49,10 @@ Board::Board() { } bool Board::isWalkable(Position point, float position_delta, Direction direction) const { - switch (direction) { + if(point.x <= 0 || point.x >= COLUMNS-1) + return true; + + switch (direction) { case Direction::LEFT: return board_state[int(point.y)][int(point.x - position_delta)] != 0; case Direction::RIGHT: diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index 1234703..24a52fa 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -33,9 +33,17 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta) { } 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; }