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
ColumnLimit: '0'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
NamespaceIndentation: All
PenaltyReturnTypeOnItsOwnLine: '10000'
PointerAlignment: Middle
FixNamespaceComments: true
NamespaceIndentation: None
...

View file

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

View file

@ -4,6 +4,8 @@
#include <string>
#include <vector>
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

View file

@ -2,6 +2,8 @@
#include <chrono>
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

View file

@ -2,6 +2,8 @@
#include <array>
#include <cmath>
namespace pacman {
Ghost::Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget)
: spritesSet(spritesSet),
pos(startingPosition),
@ -177,3 +179,5 @@ Inky::Inky(const Board & board)
Clyde::Clyde(const Board & board)
: Ghost(Atlas::Ghost::clyde, board.initialClydePosition(), board.clydeScatterTarget()) {
}
} // namespace pacman

View file

@ -1,6 +1,8 @@
#include "PacMan.hpp"
#include <cmath>
namespace pacman {
PacMan::PacMan(const Board & board)
: pos(Board::initialPacManPosition()) {}
@ -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

View file

@ -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

View file

@ -1,6 +1,8 @@
#include "Pellets.hpp"
#include <algorithm>
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

View file

@ -1,6 +1,8 @@
#include "SuperPellets.hpp"
#include <algorithm>
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

View file

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

View file

@ -8,6 +8,8 @@
#include <cstdint>
#include <vector>
namespace pacman {
const std::size_t ROWS = 31;
const std::size_t COLUMNS = 28;
@ -53,3 +55,5 @@ private:
ROWS>
board;
};
} // namespace pacman

View file

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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -6,6 +6,8 @@
#include <chrono>
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

View file

@ -8,6 +8,8 @@
#include <chrono>
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

View file

@ -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<GridPosition> positions;
};
} // namespace pacman

View file

@ -2,6 +2,8 @@
#include <SFML/Graphics.hpp>
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

View file

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

View file

@ -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<GridPosition> positions;
};
} // namespace pacman

View file

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

View file

@ -2,8 +2,8 @@
#include <gtest/gtest.h>
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);
}