From 370a57f454d4b8d1fa5859c772761defe639863e Mon Sep 17 00:00:00 2001 From: Corentin Jabot Date: Thu, 8 Jul 2021 17:42:24 +0200 Subject: [PATCH] Get rid of board class --- lib/Board.cpp | 36 +++++++++++++++++++++-------- lib/Ghost.cpp | 16 ++++++------- lib/PacMan.cpp | 20 +++++----------- lib/Pellets.cpp | 2 +- lib/SuperPellets.cpp | 2 +- lib/include/Board.hpp | 53 ++++++++++++------------------------------- 6 files changed, 57 insertions(+), 72 deletions(-) diff --git a/lib/Board.cpp b/lib/Board.cpp index fcd4971..21560f8 100644 --- a/lib/Board.cpp +++ b/lib/Board.cpp @@ -2,6 +2,18 @@ namespace pacman { +const std::size_t ROWS = 31; +const std::size_t COLUMNS = 28; + +enum class Cell { + wall = 0, + pellet = 1, + nothing = 2, + door = 3, + power_pellet = 4, + pen = 5, +}; + // Legend // 0 - wall // 1 - pellet @@ -11,7 +23,7 @@ namespace pacman { // 5 - pen doors // clang-format off -std::array, ROWS> Board::board = {{ +constexpr std::array, ROWS> board = {{ // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0 { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, // 1 @@ -47,28 +59,32 @@ std::array, ROWS> Board::board = {{ }}; // clang-format on -bool Board::isWalkableForPacMan(GridPosition point) { +static Cell cellAtPosition(GridPosition point) { + if (point.x >= COLUMNS || point.y >= ROWS) + return Cell::wall; + return Cell(board[point.y][point.x]); +} + +bool isWalkableForPacMan(GridPosition point) { return cellAtPosition(point) != Cell::wall; } -bool Board::isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes) { +bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes) { Cell cell = cellAtPosition(point); if (cell == Cell::wall) return false; return isEyes || (isInPen(origin) || !isInPen(point)); } -bool Board::isInPen(GridPosition point) { +bool isInPen(GridPosition point) { return cellAtPosition(point) == Cell::pen; } -Board::Cell Board::cellAtPosition(GridPosition point) { - if (point.x >= COLUMNS || point.y >= ROWS) - return Cell::wall; - return Cell(board[point.y][point.x]); +bool isPortal(GridPosition point) { + return cellAtPosition(point) == Cell::door; } -std::vector Board::initialPelletPositions() { +std::vector initialPelletPositions() { std::vector positions; for (std::size_t row = 0; row < ROWS; row++) { for (std::size_t column = 0; column < COLUMNS; column++) { @@ -79,7 +95,7 @@ std::vector Board::initialPelletPositions() { return positions; } -std::vector Board::initialSuperPelletPositions() { +std::vector initialSuperPelletPositions() { std::vector positions; for (std::size_t row = 0; row < ROWS; row++) { for (std::size_t column = 0; column < COLUMNS; column++) { diff --git a/lib/Ghost.cpp b/lib/Ghost.cpp index 6dfd48f..6e25df3 100644 --- a/lib/Ghost.cpp +++ b/lib/Ghost.cpp @@ -77,7 +77,7 @@ void Ghost::update(std::chrono::milliseconds time_delta) { } bool Ghost::isInPen() const { - return pacman::Board::isInPen(positionInGrid()); + return pacman::isInPen(positionInGrid()); } void Ghost::updatePosition(std::chrono::milliseconds time_delta) { @@ -161,7 +161,7 @@ void Ghost::updateDirection() { continue; const GridPosition grid_position = {size_t(move.position.x), size_t(move.position.y)}; - const bool can_walk = pacman::Board::isWalkableForGhost(grid_position, current_grid_position, isEyes()); + const bool can_walk = pacman::isWalkableForGhost(grid_position, current_grid_position, isEyes()); if (!can_walk) continue; @@ -181,8 +181,8 @@ Position Ghost::target() const { if (state == State::Eyes) return startingPosition; - if (pacman::Board::isInPen(positionInGrid())) - return pacman::Board::penDoorPosition(); + if (pacman::isInPen(positionInGrid())) + return pacman::penDoorPosition(); return scatterTarget; } @@ -196,19 +196,19 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) { } Blinky::Blinky() - : Ghost(Atlas::Ghost::blinky, pacman::Board::initialBlinkyPosition(), pacman::Board::blinkyScatterTarget()) { + : Ghost(Atlas::Ghost::blinky, pacman::initialBlinkyPosition(), pacman::blinkyScatterTarget()) { } Speedy::Speedy() - : Ghost(Atlas::Ghost::speedy, pacman::Board::initialSpeedyPosition(), pacman::Board::speedyScatterTarget()) { + : Ghost(Atlas::Ghost::speedy, pacman::initialSpeedyPosition(), pacman::speedyScatterTarget()) { } Inky::Inky() - : Ghost(Atlas::Ghost::inky, pacman::Board::initialInkyPosition(), pacman::Board::inkyScatterTarget()) { + : Ghost(Atlas::Ghost::inky, pacman::initialInkyPosition(), pacman::inkyScatterTarget()) { } Clyde::Clyde() - : Ghost(Atlas::Ghost::clyde, pacman::Board::initialClydePosition(), pacman::Board::clydeScatterTarget()) { + : Ghost(Atlas::Ghost::clyde, pacman::initialClydePosition(), pacman::clydeScatterTarget()) { } } // namespace pacman diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index 4060250..d05f697 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -4,7 +4,7 @@ namespace pacman { PacMan::PacMan() - : pos(Board::initialPacManPosition()) {} + : pos(initialPacManPosition()) {} GridPosition PacMan::currentSprite() const { return eaten ? pacManAnimation.deathAnimationFrame() : pacManAnimation.animationFrame(direction); @@ -28,7 +28,7 @@ void PacMan::eat() { void PacMan::reset() { eaten = false; direction = Direction::NONE; - pos = pacman::Board::initialPacManPosition(); + pos = pacman::initialPacManPosition(); } void PacMan::update(std::chrono::milliseconds time_delta, Direction input_direction) { @@ -54,17 +54,9 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) { - // Handle teleport - const size_t right = COLUMNS - 1; - const size_t left = 0; - - if (std::size_t(pos.x) == right && direction == Direction::RIGHT) { - pos.x = left; - return; - } else if (std::size_t(pos.x) == left && direction == Direction::LEFT) { - pos.x = right; - return; - } + if(isPortal(positionInGrid())) { + // TODO: patricia + } const double position_delta = 0.004 * time_delta.count(); const auto pacman_size = 1; @@ -86,7 +78,7 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) { }; auto canGo = [&](Direction move_direction) { - return pacman::Board::isWalkableForPacMan(moveToPosition(pos, move_direction)); + return pacman::isWalkableForPacMan(moveToPosition(pos, move_direction)); }; if (canGo(desired_direction)) { diff --git a/lib/Pellets.cpp b/lib/Pellets.cpp index 75b3958..39de56d 100644 --- a/lib/Pellets.cpp +++ b/lib/Pellets.cpp @@ -4,7 +4,7 @@ namespace pacman { Pellets::Pellets() - : positions(pacman::Board::initialPelletPositions()) {} + : positions(initialPelletPositions()) {} bool Pellets::eatPelletAtPosition(GridPosition p) { auto it = std::find(positions.begin(), positions.end(), p); diff --git a/lib/SuperPellets.cpp b/lib/SuperPellets.cpp index 61e966b..0e851e6 100644 --- a/lib/SuperPellets.cpp +++ b/lib/SuperPellets.cpp @@ -4,7 +4,7 @@ namespace pacman { SuperPellets::SuperPellets() - : positions(pacman::Board::initialSuperPelletPositions()) {} + : positions(initialSuperPelletPositions()) {} bool SuperPellets::eatPelletAtPosition(GridPosition p) { auto it = std::find(positions.begin(), positions.end(), p); diff --git a/lib/include/Board.hpp b/lib/include/Board.hpp index 23ae4a4..cb26917 100644 --- a/lib/include/Board.hpp +++ b/lib/include/Board.hpp @@ -10,50 +10,27 @@ namespace pacman { -const std::size_t ROWS = 31; -const std::size_t COLUMNS = 28; + [[nodiscard]] bool isWalkableForPacMan(GridPosition point); + [[nodiscard]] bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes); + [[nodiscard]] bool isInPen(GridPosition point); -class Board { -public: - enum class Cell { - wall = 0, - pellet = 1, - nothing = 2, - door = 3, - power_pellet = 4, - pen = 5, - }; + [[nodiscard]] std::vector initialPelletPositions(); - [[nodiscard]] static bool isWalkableForPacMan(GridPosition point); - [[nodiscard]] static bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes); - [[nodiscard]] static bool isInPen(GridPosition point); + [[nodiscard]] std::vector initialSuperPelletPositions(); - [[nodiscard]] static std::vector initialPelletPositions(); + inline Position penDoorPosition() { return { 13, 11 }; } + inline Position initialPacManPosition() { return { 13.5, 23 }; } - [[nodiscard]] static std::vector initialSuperPelletPositions(); + inline Position initialBlinkyPosition() { return { 13.5, 11 }; } + inline Position blinkyScatterTarget() { return { 25, -3 }; } - static Position initialPacManPosition() { return { 13.5, 23 }; } + inline Position initialSpeedyPosition() { return { 11.5, 14 }; } + inline Position speedyScatterTarget() { return { 3, -2 }; } - static Position initialBlinkyPosition() { return { 13.5, 11 }; } - static Position penDoorPosition() { return { 13, 11 }; } + inline Position initialInkyPosition() { return { 13.5, 14 }; } + inline Position inkyScatterTarget() { return { 27, 30 }; } - static Position blinkyScatterTarget() { return { 25, -3 }; } - - static Position initialSpeedyPosition() { return { 11.5, 14 }; } - static Position speedyScatterTarget() { return { 3, -2 }; } - - static Position initialInkyPosition() { return { 13.5, 14 }; } - static Position inkyScatterTarget() { return { 27, 30 }; } - - static Position initialClydePosition() { return { 15.5, 14 }; } - static Position clydeScatterTarget() { return { 0, 30 }; } - -private: - [[nodiscard]] static Cell cellAtPosition(GridPosition point); - static std::array< - std::array, - ROWS> - board; -}; + inline Position initialClydePosition() { return { 15.5, 14 }; } + inline Position clydeScatterTarget() { return { 0, 30 }; } } // namespace pacman