diff --git a/lib/Atlas.hpp b/lib/Atlas.hpp index 08f6739..8852657 100644 --- a/lib/Atlas.hpp +++ b/lib/Atlas.hpp @@ -15,22 +15,22 @@ namespace Atlas { clyde = 5, }; - constexpr PositionInt pacman_right_wide = { 0, 0 }; - constexpr PositionInt pacman_right_narrow = { 1, 0 }; - constexpr PositionInt pacman_closed = { 2, 0 }; - constexpr PositionInt pacman_left_narrow = { 3, 0 }; - constexpr PositionInt pacman_left_wide = { 4, 0 }; - constexpr PositionInt pacman_up_wide = { 5, 0 }; - constexpr PositionInt pacman_up_narrow = { 6, 0 }; - constexpr PositionInt pacman_down_wide = { 7, 0 }; - constexpr PositionInt 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 PositionInt ghost_blue_frightened = { 0, 7 }; - constexpr PositionInt ghost_blue_frightened2 = { 1, 7 }; - constexpr PositionInt ghost_white_frightened = { 2, 7 }; - constexpr PositionInt 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 PositionInt eyeSprite(Direction direction) { + constexpr GridPosition eyeSprite(Direction direction) { int x = 0; switch (direction) { case Direction::RIGHT: @@ -52,7 +52,7 @@ namespace Atlas { return { x, 6 }; } - constexpr PositionInt ghostSprite(Ghost ghost, Direction direction, bool alternative) { + 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; @@ -78,12 +78,12 @@ namespace Atlas { return { x, y }; } - constexpr PositionInt initialFrightened(int animationIndex) { + constexpr GridPosition initialFrightened(int animationIndex) { return (animationIndex % 2) == 0 ? Atlas::ghost_blue_frightened2 : Atlas::ghost_blue_frightened; } - constexpr PositionInt endingFrightened(int animationIndex) { - std::array positions = { 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 }; diff --git a/lib/Board.cpp b/lib/Board.cpp index 658689a..3930be4 100644 --- a/lib/Board.cpp +++ b/lib/Board.cpp @@ -86,8 +86,8 @@ bool Board::isWalkable(Position point, double position_delta, Direction directio return pacman ? cell != Cell::wall : cell != Cell::wall && cell != Cell::pen; } -std::vector Board::initialPelletPositions() { - std::vector positions; +std::vector Board::initialPelletPositions() { + std::vector positions; for (int row = 0; row < ROWS; row++) { for (int column = 0; column < COLUMNS; column++) { if (board[row][column] == int(Cell::pellet)) @@ -97,8 +97,8 @@ std::vector Board::initialPelletPositions() { return positions; } -std::vector Board::initialSuperPelletPositions() { - std::vector positions; +std::vector Board::initialSuperPelletPositions() { + std::vector positions; for (int row = 0; row < ROWS; row++) { for (int column = 0; column < COLUMNS; column++) { if (board[row][column] == int(Cell::power_pellet)) diff --git a/lib/Board.hpp b/lib/Board.hpp index d60bbdb..638355a 100644 --- a/lib/Board.hpp +++ b/lib/Board.hpp @@ -29,9 +29,9 @@ public: [[nodiscard]] static bool isWalkable(Position point) ; [[nodiscard]] static bool isInPen(Position 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 }; } diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp index 0c63d34..8decacf 100644 --- a/lib/Canvas.cpp +++ b/lib/Canvas.cpp @@ -58,7 +58,7 @@ void Canvas::renderMaze() { void Canvas::renderPellets(const Pellets & pellets) { Sprite pellet = getSprite(pellets.currentSprite()); - std::vector pelletPositions = pellets.currentPositions(); + std::vector pelletPositions = pellets.currentPositions(); for (const auto & pos : pelletPositions) { renderSprite(pellet, { double(pos.x), double(pos.y) }); } @@ -66,7 +66,7 @@ void Canvas::renderPellets(const Pellets & pellets) { void Canvas::renderSuperPellets(const SuperPellets & superPellets) { Sprite pellet = getSprite(superPellets.currentSprite()); - std::vector superPelletPositions = superPellets.currentPositions(); + std::vector superPelletPositions = superPellets.currentPositions(); for (const auto & pos : superPelletPositions) { renderSprite(pellet, { double(pos.x), double(pos.y) }); } @@ -98,13 +98,13 @@ void Canvas::renderScore(int score) { } void Canvas::renderLives(int lives) { - constexpr PositionInt liveSprite = Atlas::pacman_left_narrow; + constexpr GridPosition liveSprite = Atlas::pacman_left_narrow; const int x = LEFT_MARGIN + MAZE_WIDTH + LEFT_MARGIN; const int y = maze_texture.getSize().y; Sprite pacmanSprite = getSprite(liveSprite); for (int i = 0; i < lives - 1; i++) { - PositionInt pos{ x + i * pacmanSprite.getTextureRect().width, y }; + GridPosition pos{ x + i * pacmanSprite.getTextureRect().width, y }; pacmanSprite.setPosition(pos.x, pos.y); window.draw(pacmanSprite); } @@ -114,7 +114,7 @@ Rect Canvas::windowDimensions() { return { 0, 0, LEFT_MARGIN + MAZE_WIDTH + SCORE_WIDTH, TOP_MARGIN + MAZE_HEIGHT + BOTTOM_MARGIN }; } -Sprite Canvas::getSprite(PositionInt coordinate) const { +Sprite Canvas::getSprite(GridPosition coordinate) const { sf::Sprite sprite; sprite.setTexture(sprites_texture); sprite.setTextureRect(sf::IntRect{ coordinate.x * DEFAULT_SPRITE_WIDTH, diff --git a/lib/Canvas.hpp b/lib/Canvas.hpp index dfe11ec..e86f58f 100644 --- a/lib/Canvas.hpp +++ b/lib/Canvas.hpp @@ -42,7 +42,7 @@ private: static sf::Texture loadTexture(std::string_view path); static sf::Font loadFont(std::string_view path); - Sprite getSprite(PositionInt rect) const; + Sprite getSprite(GridPosition rect) const; sf::RenderWindow window; sf::Texture maze_texture; diff --git a/lib/Ghost.cpp b/lib/Ghost.cpp index cfb06eb..725da05 100644 --- a/lib/Ghost.cpp +++ b/lib/Ghost.cpp @@ -38,7 +38,7 @@ void Ghost::reset() { pos = startingPosition; } -[[nodiscard]] PositionInt Ghost::currentSprite() const { +[[nodiscard]] GridPosition Ghost::currentSprite() const { switch (state) { default: return Atlas::ghostSprite(spritesSet, direction, (animationIndex % 2) == 0); diff --git a/lib/Ghost.hpp b/lib/Ghost.hpp index 5e30e5f..7aad475 100644 --- a/lib/Ghost.hpp +++ b/lib/Ghost.hpp @@ -17,7 +17,7 @@ public: explicit Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget); - [[nodiscard]] PositionInt currentSprite() const; + [[nodiscard]] GridPosition currentSprite() const; [[nodiscard]] Position position() const; diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index dc3f748..3be89a2 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -4,7 +4,7 @@ PacMan::PacMan(const Board & board) : pos(Board::initialPacManPosition()) {} -PositionInt PacMan::currentSprite() const { +GridPosition PacMan::currentSprite() const { return eaten ? pacManAnimation.deathAnimationFrame(direction) : pacManAnimation.animationFrame(direction); } diff --git a/lib/PacMan.hpp b/lib/PacMan.hpp index a4c7f97..c150dba 100644 --- a/lib/PacMan.hpp +++ b/lib/PacMan.hpp @@ -13,7 +13,7 @@ class PacMan { public: explicit PacMan(const Board & board); - [[nodiscard]] PositionInt currentSprite() const; + [[nodiscard]] GridPosition currentSprite() const; [[nodiscard]] Position position() const; diff --git a/lib/PacManAnimation.cpp b/lib/PacManAnimation.cpp index 8448480..6bd8322 100644 --- a/lib/PacManAnimation.cpp +++ b/lib/PacManAnimation.cpp @@ -1,6 +1,6 @@ #include "PacManAnimation.hpp" -PositionInt PacManAnimation::animationFrame(Direction direction) const { +GridPosition PacManAnimation::animationFrame(Direction direction) const { switch (direction) { case Direction::LEFT: return left_animation[animation_position]; @@ -16,8 +16,8 @@ PositionInt PacManAnimation::animationFrame(Direction direction) const { } } -[[nodiscard]] PositionInt PacManAnimation::deathAnimationFrame(Direction direction) const { - return PositionInt{ animation_position, 1 }; +[[nodiscard]] GridPosition PacManAnimation::deathAnimationFrame(Direction direction) const { + return GridPosition{ animation_position, 1 }; } void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_delta, bool dead) { diff --git a/lib/PacManAnimation.hpp b/lib/PacManAnimation.hpp index bd00ffb..146ccda 100644 --- a/lib/PacManAnimation.hpp +++ b/lib/PacManAnimation.hpp @@ -10,8 +10,8 @@ class PacManAnimation { public: - [[nodiscard]] PositionInt animationFrame(Direction direction) const; - [[nodiscard]] PositionInt deathAnimationFrame(Direction direction) const; + [[nodiscard]] GridPosition animationFrame(Direction direction) const; + [[nodiscard]] GridPosition deathAnimationFrame(Direction direction) const; void updateAnimationPosition(std::chrono::milliseconds time_delta, bool dead); void pause(); @@ -19,8 +19,8 @@ public: private: uint8_t animation_position = 0; double animation_position_delta = 0.0; - const PositionInt down_animation[4]{ Atlas::pacman_down_wide, Atlas::pacman_down_narrow, Atlas::pacman_closed, Atlas::pacman_down_narrow }; - const PositionInt left_animation[4]{ Atlas::pacman_left_wide, Atlas::pacman_left_narrow, Atlas::pacman_closed, Atlas::pacman_left_narrow }; - const PositionInt right_animation[4]{ Atlas::pacman_right_wide, Atlas::pacman_right_narrow, Atlas::pacman_closed, Atlas::pacman_right_narrow }; - const PositionInt up_animation[4]{ Atlas::pacman_up_wide, Atlas::pacman_up_narrow, Atlas::pacman_closed, Atlas::pacman_up_narrow }; + const GridPosition down_animation[4]{ Atlas::pacman_down_wide, Atlas::pacman_down_narrow, Atlas::pacman_closed, Atlas::pacman_down_narrow }; + const GridPosition left_animation[4]{ Atlas::pacman_left_wide, Atlas::pacman_left_narrow, Atlas::pacman_closed, Atlas::pacman_left_narrow }; + 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 }; }; diff --git a/lib/Pellets.hpp b/lib/Pellets.hpp index b224dd3..20e9e1e 100644 --- a/lib/Pellets.hpp +++ b/lib/Pellets.hpp @@ -7,17 +7,17 @@ class Pellets { public: explicit Pellets(const Board & board); - [[nodiscard]] PositionInt currentSprite() const { + [[nodiscard]] GridPosition currentSprite() const { return sprite; }; - [[nodiscard]] std::vector currentPositions() const { + [[nodiscard]] std::vector currentPositions() const { return positions; } bool eatPelletAtPosition(Position p); private: - const PositionInt sprite = { 1, 9 }; - std::vector positions; + const GridPosition sprite = { 1, 9 }; + std::vector positions; }; diff --git a/lib/Position.hpp b/lib/Position.hpp index c35fe9b..0e44cb9 100644 --- a/lib/Position.hpp +++ b/lib/Position.hpp @@ -7,7 +7,7 @@ struct Position { double y; }; -struct PositionInt { +struct GridPosition { int x; int y; }; @@ -16,7 +16,7 @@ using Rect = sf::Rect; using Sprite = sf::Sprite; -inline bool operator==(const PositionInt & b, const Position & a) { +inline bool operator==(const GridPosition & b, const Position & a) { return a.x == b.x && a.y == b.y; } diff --git a/lib/SuperPellets.hpp b/lib/SuperPellets.hpp index 343241e..d5e736a 100644 --- a/lib/SuperPellets.hpp +++ b/lib/SuperPellets.hpp @@ -7,17 +7,17 @@ class SuperPellets { public: explicit SuperPellets(const Board & board); - [[nodiscard]] PositionInt currentSprite() const { + [[nodiscard]] GridPosition currentSprite() const { return sprite; } - [[nodiscard]] std::vector currentPositions() const { + [[nodiscard]] std::vector currentPositions() const { return positions; } bool eatPelletAtPosition(Position p); private: - const PositionInt sprite = { 0, 9 }; - std::vector positions; + const GridPosition sprite = { 0, 9 }; + std::vector positions; };