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),
@ -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()) {}
@ -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,7 +6,7 @@
#include <array> #include <array>
namespace Atlas { namespace pacman::Atlas {
enum class Ghost { enum class Ghost {
blinky = 2, blinky = 2,
@ -89,4 +89,4 @@ namespace Atlas {
Atlas::ghost_white_frightened2 }; Atlas::ghost_white_frightened2 };
return positions[animationIndex]; 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;
@ -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);
} }