diff --git a/.clang-format b/.clang-format index 4f3eafb..edbdd37 100644 --- a/.clang-format +++ b/.clang-format @@ -9,8 +9,8 @@ BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon ColumnLimit: '0' ConstructorInitializerAllOnOneLineOrOnePerLine: true -NamespaceIndentation: All PenaltyReturnTypeOnItsOwnLine: '10000' PointerAlignment: Middle - +FixNamespaceComments: true +NamespaceIndentation: None ... diff --git a/lib/Board.cpp b/lib/Board.cpp index 52e94a8..f550bf6 100644 --- a/lib/Board.cpp +++ b/lib/Board.cpp @@ -1,5 +1,7 @@ #include "Board.hpp" +namespace pacman { + // Legend // 0 - wall // 1 - pellet @@ -87,3 +89,5 @@ std::vector Board::initialSuperPelletPositions() { } return positions; } + +} // namespace pacman diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp index 8decacf..790ae2a 100644 --- a/lib/Canvas.cpp +++ b/lib/Canvas.cpp @@ -4,6 +4,8 @@ #include #include +namespace pacman { + Canvas::Canvas() : window(sf::VideoMode(windowDimensions().width, windowDimensions().height), "Pacman", @@ -152,3 +154,5 @@ sf::Font Canvas::loadFont(std::string_view path) { } return font; } + +} // namespace pacman diff --git a/lib/Game.cpp b/lib/Game.cpp index 2ab6831..2ca5767 100644 --- a/lib/Game.cpp +++ b/lib/Game.cpp @@ -2,6 +2,8 @@ #include +namespace pacman { + constexpr int DEFAULT_LIVES = 3; constexpr int NORMAL_PELLET_POINTS = 10; constexpr int POWER_PELLET_POINTS = 50; @@ -127,3 +129,5 @@ void Game::processEvents(InputState & inputState) { inputState.left = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left); inputState.right = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right); } + +} // namespace pacman diff --git a/lib/Ghost.cpp b/lib/Ghost.cpp index ea1ca2c..08a49b6 100644 --- a/lib/Ghost.cpp +++ b/lib/Ghost.cpp @@ -2,6 +2,8 @@ #include #include +namespace pacman { + Ghost::Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget) : spritesSet(spritesSet), pos(startingPosition), @@ -119,7 +121,7 @@ void Ghost::updateDirection(const Board & board) { return; struct NewDirection { - Direction direction; + Direction direction; GridPosition position; double distance; }; @@ -177,3 +179,5 @@ Inky::Inky(const Board & board) Clyde::Clyde(const Board & board) : Ghost(Atlas::Ghost::clyde, board.initialClydePosition(), board.clydeScatterTarget()) { } + +} // namespace pacman diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index c889fa1..5f6f4f5 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -1,6 +1,8 @@ #include "PacMan.hpp" #include +namespace pacman { + PacMan::PacMan(const Board & board) : pos(Board::initialPacManPosition()) {} @@ -64,15 +66,15 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar double position_delta = 0.004 * time_delta.count(); auto cellAtPosition = [&](Position point, double position_delta, Direction direction) { - switch (direction) { + switch (direction) { case Direction::LEFT: - return GridPosition{int(point.x - position_delta), int(point.y)}; + return GridPosition{ int(point.x - position_delta), int(point.y) }; case Direction::RIGHT: - return GridPosition{int(point.x) + 1, int(point.y)}; + return GridPosition{ int(point.x) + 1, int(point.y) }; case Direction::UP: - return GridPosition{int(point.x), int(point.y - position_delta)}; + return GridPosition{ int(point.x), int(point.y - position_delta) }; case Direction::DOWN: - return GridPosition{int(point.x), int(point.y) + 1}; + return GridPosition{ int(point.x), int(point.y) + 1 }; case Direction::NONE: default: return positionInGrid(); @@ -84,8 +86,7 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar pos.x = -1; } else if (pos.x <= 0 && direction == Direction::LEFT) { pos.x = COLUMNS; - } - else if (board.isWalkableForPacMan(cellAtPosition(pos, position_delta, desired_direction))) { + } else if (board.isWalkableForPacMan(cellAtPosition(pos, position_delta, desired_direction))) { direction = desired_direction; } @@ -112,3 +113,5 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar } } } + +} // namespace pacman diff --git a/lib/PacManAnimation.cpp b/lib/PacManAnimation.cpp index 6bd8322..bd0d7dc 100644 --- a/lib/PacManAnimation.cpp +++ b/lib/PacManAnimation.cpp @@ -1,5 +1,7 @@ #include "PacManAnimation.hpp" +namespace pacman { + GridPosition PacManAnimation::animationFrame(Direction direction) const { switch (direction) { case Direction::LEFT: @@ -37,3 +39,5 @@ void PacManAnimation::pause() { animation_position = 0; animation_position_delta = 0; } + +} // namespace pacman diff --git a/lib/Pellets.cpp b/lib/Pellets.cpp index bd43f70..d24fa63 100644 --- a/lib/Pellets.cpp +++ b/lib/Pellets.cpp @@ -1,6 +1,8 @@ #include "Pellets.hpp" #include +namespace pacman { + Pellets::Pellets(const Board & board) : positions(board.initialPelletPositions()) {} @@ -11,3 +13,5 @@ bool Pellets::eatPelletAtPosition(GridPosition p) { positions.erase(it); return true; } + +} // namespace pacman diff --git a/lib/SuperPellets.cpp b/lib/SuperPellets.cpp index d49861e..d4bcb42 100644 --- a/lib/SuperPellets.cpp +++ b/lib/SuperPellets.cpp @@ -1,6 +1,8 @@ #include "SuperPellets.hpp" #include +namespace pacman { + SuperPellets::SuperPellets(const Board & board) : positions(board.initialSuperPelletPositions()) {} @@ -11,3 +13,5 @@ bool SuperPellets::eatPelletAtPosition(GridPosition p) { positions.erase(it); return true; } + +} // namespace pacman diff --git a/lib/include/Atlas.hpp b/lib/include/Atlas.hpp index 8852657..16a1123 100644 --- a/lib/include/Atlas.hpp +++ b/lib/include/Atlas.hpp @@ -6,87 +6,87 @@ #include -namespace Atlas { +namespace pacman::Atlas { - enum class Ghost { - blinky = 2, - speedy = 3, - inky = 4, - clyde = 5, - }; +enum class Ghost { + blinky = 2, + speedy = 3, + inky = 4, + clyde = 5, +}; - constexpr GridPosition pacman_right_wide = { 0, 0 }; - constexpr GridPosition pacman_right_narrow = { 1, 0 }; - constexpr GridPosition pacman_closed = { 2, 0 }; - constexpr GridPosition pacman_left_narrow = { 3, 0 }; - constexpr GridPosition pacman_left_wide = { 4, 0 }; - constexpr GridPosition pacman_up_wide = { 5, 0 }; - constexpr GridPosition pacman_up_narrow = { 6, 0 }; - constexpr GridPosition pacman_down_wide = { 7, 0 }; - constexpr GridPosition pacman_down_narrow = { 8, 0 }; +constexpr GridPosition pacman_right_wide = { 0, 0 }; +constexpr GridPosition pacman_right_narrow = { 1, 0 }; +constexpr GridPosition pacman_closed = { 2, 0 }; +constexpr GridPosition pacman_left_narrow = { 3, 0 }; +constexpr GridPosition pacman_left_wide = { 4, 0 }; +constexpr GridPosition pacman_up_wide = { 5, 0 }; +constexpr GridPosition pacman_up_narrow = { 6, 0 }; +constexpr GridPosition pacman_down_wide = { 7, 0 }; +constexpr GridPosition pacman_down_narrow = { 8, 0 }; - constexpr GridPosition ghost_blue_frightened = { 0, 7 }; - constexpr GridPosition ghost_blue_frightened2 = { 1, 7 }; - constexpr GridPosition ghost_white_frightened = { 2, 7 }; - constexpr GridPosition ghost_white_frightened2 = { 3, 7 }; +constexpr GridPosition ghost_blue_frightened = { 0, 7 }; +constexpr GridPosition ghost_blue_frightened2 = { 1, 7 }; +constexpr GridPosition ghost_white_frightened = { 2, 7 }; +constexpr GridPosition ghost_white_frightened2 = { 3, 7 }; - constexpr GridPosition eyeSprite(Direction direction) { - int x = 0; - switch (direction) { - case Direction::RIGHT: - x = 0; - break; - case Direction::DOWN: - x = 2; - break; - case Direction::LEFT: - x = 4; - break; - case Direction::UP: - x = 6; - break; - default: - x = 0; - break; - } - return { x, 6 }; - } - - constexpr GridPosition ghostSprite(Ghost ghost, Direction direction, bool alternative) { - assert(ghost >= Ghost::blinky && ghost <= Ghost::clyde && "Invalid Ghost"); - int y = static_cast(ghost); - int x = 0; - switch (direction) { - case Direction::RIGHT: - x = 0; - break; - case Direction::DOWN: - x = 2; - break; - case Direction::LEFT: - x = 4; - break; - case Direction::UP: - x = 6; - break; - default: - x = 0; - break; - } - if (alternative) - x++; - return { x, y }; - } - - constexpr GridPosition initialFrightened(int animationIndex) { - return (animationIndex % 2) == 0 ? Atlas::ghost_blue_frightened2 : Atlas::ghost_blue_frightened; - } - - constexpr GridPosition endingFrightened(int animationIndex) { - std::array positions = { Atlas::ghost_blue_frightened, - Atlas::ghost_blue_frightened2, - Atlas::ghost_white_frightened, - Atlas::ghost_white_frightened2 }; - return positions[animationIndex]; +constexpr GridPosition eyeSprite(Direction direction) { + int x = 0; + switch (direction) { + case Direction::RIGHT: + x = 0; + break; + case Direction::DOWN: + x = 2; + break; + case Direction::LEFT: + x = 4; + break; + case Direction::UP: + x = 6; + break; + default: + x = 0; + break; } + return { x, 6 }; } + +constexpr GridPosition ghostSprite(Ghost ghost, Direction direction, bool alternative) { + assert(ghost >= Ghost::blinky && ghost <= Ghost::clyde && "Invalid Ghost"); + int y = static_cast(ghost); + int x = 0; + switch (direction) { + case Direction::RIGHT: + x = 0; + break; + case Direction::DOWN: + x = 2; + break; + case Direction::LEFT: + x = 4; + break; + case Direction::UP: + x = 6; + break; + default: + x = 0; + break; + } + if (alternative) + x++; + return { x, y }; +} + +constexpr GridPosition initialFrightened(int animationIndex) { + return (animationIndex % 2) == 0 ? Atlas::ghost_blue_frightened2 : Atlas::ghost_blue_frightened; +} + +constexpr GridPosition endingFrightened(int animationIndex) { + std::array positions = { Atlas::ghost_blue_frightened, + Atlas::ghost_blue_frightened2, + Atlas::ghost_white_frightened, + Atlas::ghost_white_frightened2 }; + return positions[animationIndex]; +} +} // namespace pacman::Atlas diff --git a/lib/include/Board.hpp b/lib/include/Board.hpp index 7af4be4..8172784 100644 --- a/lib/include/Board.hpp +++ b/lib/include/Board.hpp @@ -8,6 +8,8 @@ #include #include +namespace pacman { + const std::size_t ROWS = 31; const std::size_t COLUMNS = 28; @@ -23,12 +25,12 @@ public: }; [[nodiscard]] static bool isWalkableForPacMan(GridPosition point); - [[nodiscard]] static bool isWalkableForGost(GridPosition point, GridPosition origin, bool isEyes) ; - [[nodiscard]] static bool isInPen(GridPosition point) ; + [[nodiscard]] static bool isWalkableForGost(GridPosition point, GridPosition origin, bool isEyes); + [[nodiscard]] static bool isInPen(GridPosition point); - [[nodiscard]] static std::vector initialPelletPositions() ; + [[nodiscard]] static std::vector initialPelletPositions(); - [[nodiscard]] static std::vector initialSuperPelletPositions() ; + [[nodiscard]] static std::vector initialSuperPelletPositions(); static Position initialPacManPosition() { return { 13.5, 23 }; } @@ -53,3 +55,5 @@ private: ROWS> board; }; + +} // namespace pacman diff --git a/lib/include/Canvas.hpp b/lib/include/Canvas.hpp index e86f58f..fdc8fd4 100644 --- a/lib/include/Canvas.hpp +++ b/lib/include/Canvas.hpp @@ -4,6 +4,8 @@ #include "Score.hpp" #include +namespace pacman { + class Game; class Ghost; class PacMan; @@ -49,3 +51,5 @@ private: sf::Texture sprites_texture; sf::Font game_font; }; + +} // namespace pacman diff --git a/lib/include/Direction.hpp b/lib/include/Direction.hpp index a1b5371..7b3959d 100644 --- a/lib/include/Direction.hpp +++ b/lib/include/Direction.hpp @@ -1,5 +1,7 @@ #pragma once +namespace pacman { + enum class Direction { NONE, LEFT, @@ -23,3 +25,5 @@ inline Direction oppositeDirection(Direction d) { } return d; } + +} // namespace pacman diff --git a/lib/include/Game.hpp b/lib/include/Game.hpp index c7ab3c7..2a2d46b 100644 --- a/lib/include/Game.hpp +++ b/lib/include/Game.hpp @@ -8,6 +8,8 @@ #include "Score.hpp" #include "SuperPellets.hpp" +namespace pacman { + class InputState; class Game { @@ -34,3 +36,5 @@ private: [[nodiscard]] static auto now(); }; + +} // namespace pacman diff --git a/lib/include/Ghost.hpp b/lib/include/Ghost.hpp index 647c8b5..69fed88 100644 --- a/lib/include/Ghost.hpp +++ b/lib/include/Ghost.hpp @@ -6,6 +6,8 @@ #include "Board.hpp" #include "Position.hpp" +namespace pacman { + class Ghost { public: enum class State { @@ -71,3 +73,5 @@ class Clyde : public Ghost { public: explicit Clyde(const Board & board); }; + +} // namespace pacman diff --git a/lib/include/InputState.hpp b/lib/include/InputState.hpp index 4bd57dd..ce36138 100644 --- a/lib/include/InputState.hpp +++ b/lib/include/InputState.hpp @@ -1,5 +1,7 @@ #pragma once +namespace pacman { + class InputState { public: bool close = false; @@ -8,3 +10,5 @@ public: bool left = false; bool right = false; }; + +} // namespace pacman diff --git a/lib/include/PacMan.hpp b/lib/include/PacMan.hpp index d63134f..9522a77 100644 --- a/lib/include/PacMan.hpp +++ b/lib/include/PacMan.hpp @@ -6,6 +6,8 @@ #include +namespace pacman { + class Board; class InputState; @@ -39,3 +41,5 @@ private: void updateAnimationPosition(std::chrono::milliseconds time_delta, bool paused); void updateMazePosition(std::chrono::milliseconds time_delta, const Board & board); }; + +} // namespace pacman diff --git a/lib/include/PacManAnimation.hpp b/lib/include/PacManAnimation.hpp index 146ccda..49ea51a 100644 --- a/lib/include/PacManAnimation.hpp +++ b/lib/include/PacManAnimation.hpp @@ -8,6 +8,8 @@ #include +namespace pacman { + class PacManAnimation { public: [[nodiscard]] GridPosition animationFrame(Direction direction) const; @@ -24,3 +26,5 @@ private: const GridPosition right_animation[4]{ Atlas::pacman_right_wide, Atlas::pacman_right_narrow, Atlas::pacman_closed, Atlas::pacman_right_narrow }; const GridPosition up_animation[4]{ Atlas::pacman_up_wide, Atlas::pacman_up_narrow, Atlas::pacman_closed, Atlas::pacman_up_narrow }; }; + +} // namespace pacman diff --git a/lib/include/Pellets.hpp b/lib/include/Pellets.hpp index 0790411..6972ab1 100644 --- a/lib/include/Pellets.hpp +++ b/lib/include/Pellets.hpp @@ -3,6 +3,8 @@ #include "Board.hpp" #include "Position.hpp" +namespace pacman { + class Pellets { public: explicit Pellets(const Board & board); @@ -21,3 +23,5 @@ private: const GridPosition sprite = { 1, 9 }; std::vector positions; }; + +} // namespace pacman diff --git a/lib/include/Position.hpp b/lib/include/Position.hpp index 08dc616..63e7d7c 100644 --- a/lib/include/Position.hpp +++ b/lib/include/Position.hpp @@ -2,6 +2,8 @@ #include +namespace pacman { + struct Position { double x; double y; @@ -31,3 +33,5 @@ inline bool operator==(const Position & a, const Position & b) { inline bool operator!=(const Position & a, const Position & b) { return !(a == b); } + +} // namespace pacman diff --git a/lib/include/Score.hpp b/lib/include/Score.hpp index fe780a6..1978fa5 100644 --- a/lib/include/Score.hpp +++ b/lib/include/Score.hpp @@ -1,7 +1,10 @@ #pragma once +namespace pacman { struct Score { int lives = 0; int points = 0; int eatenPellets = 0; }; + +} // namespace pacman diff --git a/lib/include/SuperPellets.hpp b/lib/include/SuperPellets.hpp index 7f02d31..72bffe2 100644 --- a/lib/include/SuperPellets.hpp +++ b/lib/include/SuperPellets.hpp @@ -3,6 +3,8 @@ #include "Board.hpp" #include "Position.hpp" +namespace pacman { + class SuperPellets { public: explicit SuperPellets(const Board & board); @@ -21,3 +23,5 @@ private: const GridPosition sprite = { 0, 9 }; std::vector positions; }; + +} // namespace pacman diff --git a/src/main.cpp b/src/main.cpp index ff60ccf..206967f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include "Game.hpp" int main() { - Game game; + pacman::Game game; game.run(); return 0; } diff --git a/test/tests.cpp b/test/tests.cpp index 0347585..29b92a3 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -2,8 +2,8 @@ #include TEST(PacManTest, InitialPosition) { - Board board; - PacMan pacMan(board); + pacman::Board board; + pacman::PacMan pacMan(board); EXPECT_EQ(pacMan.position().x, 13.5); EXPECT_EQ(pacMan.position().y, 23); }