Put the code in a namespace

This commit is contained in:
Corentin Jabot 2021-07-05 14:10:01 +02:00
parent d166f552f4
commit 60095363f4
24 changed files with 173 additions and 95 deletions

View File

@ -9,8 +9,8 @@ BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon BreakInheritanceList: BeforeColon
ColumnLimit: '0' ColumnLimit: '0'
ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerAllOnOneLineOrOnePerLine: true
NamespaceIndentation: All
PenaltyReturnTypeOnItsOwnLine: '10000' PenaltyReturnTypeOnItsOwnLine: '10000'
PointerAlignment: Middle PointerAlignment: Middle
FixNamespaceComments: true
NamespaceIndentation: None
... ...

View File

@ -1,5 +1,7 @@
#include "Board.hpp" #include "Board.hpp"
namespace pacman {
// Legend // Legend
// 0 - wall // 0 - wall
// 1 - pellet // 1 - pellet
@ -87,3 +89,5 @@ std::vector<GridPosition> Board::initialSuperPelletPositions() {
} }
return positions; return positions;
} }
} // namespace pacman

View File

@ -4,6 +4,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace pacman {
Canvas::Canvas() Canvas::Canvas()
: window(sf::VideoMode(windowDimensions().width, windowDimensions().height), : window(sf::VideoMode(windowDimensions().width, windowDimensions().height),
"Pacman", "Pacman",
@ -152,3 +154,5 @@ sf::Font Canvas::loadFont(std::string_view path) {
} }
return font; return font;
} }
} // namespace pacman

View File

@ -2,6 +2,8 @@
#include <chrono> #include <chrono>
namespace pacman {
constexpr int DEFAULT_LIVES = 3; constexpr int DEFAULT_LIVES = 3;
constexpr int NORMAL_PELLET_POINTS = 10; constexpr int NORMAL_PELLET_POINTS = 10;
constexpr int POWER_PELLET_POINTS = 50; 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.left = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left);
inputState.right = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right); inputState.right = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right);
} }
} // namespace pacman

View File

@ -2,6 +2,8 @@
#include <array> #include <array>
#include <cmath> #include <cmath>
namespace pacman {
Ghost::Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget) Ghost::Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget)
: spritesSet(spritesSet), : spritesSet(spritesSet),
pos(startingPosition), pos(startingPosition),
@ -119,7 +121,7 @@ void Ghost::updateDirection(const Board & board) {
return; return;
struct NewDirection { struct NewDirection {
Direction direction; Direction direction;
GridPosition position; GridPosition position;
double distance; double distance;
}; };
@ -177,3 +179,5 @@ Inky::Inky(const Board & board)
Clyde::Clyde(const Board & board) Clyde::Clyde(const Board & board)
: Ghost(Atlas::Ghost::clyde, board.initialClydePosition(), board.clydeScatterTarget()) { : Ghost(Atlas::Ghost::clyde, board.initialClydePosition(), board.clydeScatterTarget()) {
} }
} // namespace pacman

View File

@ -1,6 +1,8 @@
#include "PacMan.hpp" #include "PacMan.hpp"
#include <cmath> #include <cmath>
namespace pacman {
PacMan::PacMan(const Board & board) PacMan::PacMan(const Board & board)
: pos(Board::initialPacManPosition()) {} : 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(); double position_delta = 0.004 * time_delta.count();
auto cellAtPosition = [&](Position point, double position_delta, Direction direction) { auto cellAtPosition = [&](Position point, double position_delta, Direction direction) {
switch (direction) { switch (direction) {
case Direction::LEFT: 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: case Direction::RIGHT:
return GridPosition{int(point.x) + 1, int(point.y)}; return GridPosition{ int(point.x) + 1, int(point.y) };
case Direction::UP: 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: case Direction::DOWN:
return GridPosition{int(point.x), int(point.y) + 1}; return GridPosition{ int(point.x), int(point.y) + 1 };
case Direction::NONE: case Direction::NONE:
default: default:
return positionInGrid(); return positionInGrid();
@ -84,8 +86,7 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar
pos.x = -1; pos.x = -1;
} else if (pos.x <= 0 && direction == Direction::LEFT) { } else if (pos.x <= 0 && direction == Direction::LEFT) {
pos.x = COLUMNS; 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; direction = desired_direction;
} }
@ -112,3 +113,5 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar
} }
} }
} }
} // namespace pacman

View File

@ -1,5 +1,7 @@
#include "PacManAnimation.hpp" #include "PacManAnimation.hpp"
namespace pacman {
GridPosition PacManAnimation::animationFrame(Direction direction) const { GridPosition PacManAnimation::animationFrame(Direction direction) const {
switch (direction) { switch (direction) {
case Direction::LEFT: case Direction::LEFT:
@ -37,3 +39,5 @@ void PacManAnimation::pause() {
animation_position = 0; animation_position = 0;
animation_position_delta = 0; animation_position_delta = 0;
} }
} // namespace pacman

View File

@ -1,6 +1,8 @@
#include "Pellets.hpp" #include "Pellets.hpp"
#include <algorithm> #include <algorithm>
namespace pacman {
Pellets::Pellets(const Board & board) Pellets::Pellets(const Board & board)
: positions(board.initialPelletPositions()) {} : positions(board.initialPelletPositions()) {}
@ -11,3 +13,5 @@ bool Pellets::eatPelletAtPosition(GridPosition p) {
positions.erase(it); positions.erase(it);
return true; return true;
} }
} // namespace pacman

View File

@ -1,6 +1,8 @@
#include "SuperPellets.hpp" #include "SuperPellets.hpp"
#include <algorithm> #include <algorithm>
namespace pacman {
SuperPellets::SuperPellets(const Board & board) SuperPellets::SuperPellets(const Board & board)
: positions(board.initialSuperPelletPositions()) {} : positions(board.initialSuperPelletPositions()) {}
@ -11,3 +13,5 @@ bool SuperPellets::eatPelletAtPosition(GridPosition p) {
positions.erase(it); positions.erase(it);
return true; return true;
} }
} // namespace pacman

View File

@ -6,87 +6,87 @@
#include <array> #include <array>
namespace Atlas { namespace pacman::Atlas {
enum class Ghost { enum class Ghost {
blinky = 2, blinky = 2,
speedy = 3, speedy = 3,
inky = 4, inky = 4,
clyde = 5, clyde = 5,
}; };
constexpr GridPosition pacman_right_wide = { 0, 0 }; constexpr GridPosition pacman_right_wide = { 0, 0 };
constexpr GridPosition pacman_right_narrow = { 1, 0 }; constexpr GridPosition pacman_right_narrow = { 1, 0 };
constexpr GridPosition pacman_closed = { 2, 0 }; constexpr GridPosition pacman_closed = { 2, 0 };
constexpr GridPosition pacman_left_narrow = { 3, 0 }; constexpr GridPosition pacman_left_narrow = { 3, 0 };
constexpr GridPosition pacman_left_wide = { 4, 0 }; constexpr GridPosition pacman_left_wide = { 4, 0 };
constexpr GridPosition pacman_up_wide = { 5, 0 }; constexpr GridPosition pacman_up_wide = { 5, 0 };
constexpr GridPosition pacman_up_narrow = { 6, 0 }; constexpr GridPosition pacman_up_narrow = { 6, 0 };
constexpr GridPosition pacman_down_wide = { 7, 0 }; constexpr GridPosition pacman_down_wide = { 7, 0 };
constexpr GridPosition pacman_down_narrow = { 8, 0 }; constexpr GridPosition pacman_down_narrow = { 8, 0 };
constexpr GridPosition ghost_blue_frightened = { 0, 7 }; constexpr GridPosition ghost_blue_frightened = { 0, 7 };
constexpr GridPosition ghost_blue_frightened2 = { 1, 7 }; constexpr GridPosition ghost_blue_frightened2 = { 1, 7 };
constexpr GridPosition ghost_white_frightened = { 2, 7 }; constexpr GridPosition ghost_white_frightened = { 2, 7 };
constexpr GridPosition ghost_white_frightened2 = { 3, 7 }; constexpr GridPosition ghost_white_frightened2 = { 3, 7 };
constexpr GridPosition eyeSprite(Direction direction) { constexpr GridPosition eyeSprite(Direction direction) {
int x = 0; int x = 0;
switch (direction) { switch (direction) {
case Direction::RIGHT: case Direction::RIGHT:
x = 0; x = 0;
break; break;
case Direction::DOWN: case Direction::DOWN:
x = 2; x = 2;
break; break;
case Direction::LEFT: case Direction::LEFT:
x = 4; x = 4;
break; break;
case Direction::UP: case Direction::UP:
x = 6; x = 6;
break; break;
default: default:
x = 0; x = 0;
break; 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<int>(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<GridPosition, 4> positions = { Atlas::ghost_blue_frightened,
Atlas::ghost_blue_frightened2,
Atlas::ghost_white_frightened,
Atlas::ghost_white_frightened2 };
return positions[animationIndex];
} }
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<int>(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<GridPosition, 4> positions = { Atlas::ghost_blue_frightened,
Atlas::ghost_blue_frightened2,
Atlas::ghost_white_frightened,
Atlas::ghost_white_frightened2 };
return positions[animationIndex];
}
} // namespace pacman::Atlas

View File

@ -8,6 +8,8 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
namespace pacman {
const std::size_t ROWS = 31; const std::size_t ROWS = 31;
const std::size_t COLUMNS = 28; const std::size_t COLUMNS = 28;
@ -23,12 +25,12 @@ public:
}; };
[[nodiscard]] static bool isWalkableForPacMan(GridPosition point); [[nodiscard]] static bool isWalkableForPacMan(GridPosition point);
[[nodiscard]] static bool isWalkableForGost(GridPosition point, GridPosition origin, bool isEyes) ; [[nodiscard]] static bool isWalkableForGost(GridPosition point, GridPosition origin, bool isEyes);
[[nodiscard]] static bool isInPen(GridPosition point) ; [[nodiscard]] static bool isInPen(GridPosition point);
[[nodiscard]] static std::vector<GridPosition> initialPelletPositions() ; [[nodiscard]] static std::vector<GridPosition> initialPelletPositions();
[[nodiscard]] static std::vector<GridPosition> initialSuperPelletPositions() ; [[nodiscard]] static std::vector<GridPosition> initialSuperPelletPositions();
static Position initialPacManPosition() { return { 13.5, 23 }; } static Position initialPacManPosition() { return { 13.5, 23 }; }
@ -53,3 +55,5 @@ private:
ROWS> ROWS>
board; board;
}; };
} // namespace pacman

View File

@ -4,6 +4,8 @@
#include "Score.hpp" #include "Score.hpp"
#include <optional> #include <optional>
namespace pacman {
class Game; class Game;
class Ghost; class Ghost;
class PacMan; class PacMan;
@ -49,3 +51,5 @@ private:
sf::Texture sprites_texture; sf::Texture sprites_texture;
sf::Font game_font; sf::Font game_font;
}; };
} // namespace pacman

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
namespace pacman {
enum class Direction { enum class Direction {
NONE, NONE,
LEFT, LEFT,
@ -23,3 +25,5 @@ inline Direction oppositeDirection(Direction d) {
} }
return d; return d;
} }
} // namespace pacman

View File

@ -8,6 +8,8 @@
#include "Score.hpp" #include "Score.hpp"
#include "SuperPellets.hpp" #include "SuperPellets.hpp"
namespace pacman {
class InputState; class InputState;
class Game { class Game {
@ -34,3 +36,5 @@ private:
[[nodiscard]] static auto now(); [[nodiscard]] static auto now();
}; };
} // namespace pacman

View File

@ -6,6 +6,8 @@
#include "Board.hpp" #include "Board.hpp"
#include "Position.hpp" #include "Position.hpp"
namespace pacman {
class Ghost { class Ghost {
public: public:
enum class State { enum class State {
@ -71,3 +73,5 @@ class Clyde : public Ghost {
public: public:
explicit Clyde(const Board & board); explicit Clyde(const Board & board);
}; };
} // namespace pacman

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
namespace pacman {
class InputState { class InputState {
public: public:
bool close = false; bool close = false;
@ -8,3 +10,5 @@ public:
bool left = false; bool left = false;
bool right = false; bool right = false;
}; };
} // namespace pacman

View File

@ -6,6 +6,8 @@
#include <chrono> #include <chrono>
namespace pacman {
class Board; class Board;
class InputState; class InputState;
@ -39,3 +41,5 @@ private:
void updateAnimationPosition(std::chrono::milliseconds time_delta, bool paused); void updateAnimationPosition(std::chrono::milliseconds time_delta, bool paused);
void updateMazePosition(std::chrono::milliseconds time_delta, const Board & board); void updateMazePosition(std::chrono::milliseconds time_delta, const Board & board);
}; };
} // namespace pacman

View File

@ -8,6 +8,8 @@
#include <chrono> #include <chrono>
namespace pacman {
class PacManAnimation { class PacManAnimation {
public: public:
[[nodiscard]] GridPosition animationFrame(Direction direction) const; [[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 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 }; const GridPosition up_animation[4]{ Atlas::pacman_up_wide, Atlas::pacman_up_narrow, Atlas::pacman_closed, Atlas::pacman_up_narrow };
}; };
} // namespace pacman

View File

@ -3,6 +3,8 @@
#include "Board.hpp" #include "Board.hpp"
#include "Position.hpp" #include "Position.hpp"
namespace pacman {
class Pellets { class Pellets {
public: public:
explicit Pellets(const Board & board); explicit Pellets(const Board & board);
@ -21,3 +23,5 @@ private:
const GridPosition sprite = { 1, 9 }; const GridPosition sprite = { 1, 9 };
std::vector<GridPosition> positions; std::vector<GridPosition> positions;
}; };
} // namespace pacman

View File

@ -2,6 +2,8 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
namespace pacman {
struct Position { struct Position {
double x; double x;
double y; double y;
@ -31,3 +33,5 @@ inline bool operator==(const Position & a, const Position & b) {
inline bool operator!=(const Position & a, const Position & b) { inline bool operator!=(const Position & a, const Position & b) {
return !(a == b); return !(a == b);
} }
} // namespace pacman

View File

@ -1,7 +1,10 @@
#pragma once #pragma once
namespace pacman {
struct Score { struct Score {
int lives = 0; int lives = 0;
int points = 0; int points = 0;
int eatenPellets = 0; int eatenPellets = 0;
}; };
} // namespace pacman

View File

@ -3,6 +3,8 @@
#include "Board.hpp" #include "Board.hpp"
#include "Position.hpp" #include "Position.hpp"
namespace pacman {
class SuperPellets { class SuperPellets {
public: public:
explicit SuperPellets(const Board & board); explicit SuperPellets(const Board & board);
@ -21,3 +23,5 @@ private:
const GridPosition sprite = { 0, 9 }; const GridPosition sprite = { 0, 9 };
std::vector<GridPosition> positions; std::vector<GridPosition> positions;
}; };
} // namespace pacman

View File

@ -1,7 +1,7 @@
#include "Game.hpp" #include "Game.hpp"
int main() { int main() {
Game game; pacman::Game game;
game.run(); game.run();
return 0; return 0;
} }

View File

@ -2,8 +2,8 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
TEST(PacManTest, InitialPosition) { TEST(PacManTest, InitialPosition) {
Board board; pacman::Board board;
PacMan pacMan(board); pacman::PacMan pacMan(board);
EXPECT_EQ(pacMan.position().x, 13.5); EXPECT_EQ(pacMan.position().x, 13.5);
EXPECT_EQ(pacMan.position().y, 23); EXPECT_EQ(pacMan.position().y, 23);
} }