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()) {}
@ -66,13 +68,13 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar
auto cellAtPosition = [&](Position point, double position_delta, Direction 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

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,31 +6,31 @@
#include <array>
namespace Atlas {
namespace pacman::Atlas {
enum class Ghost {
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) {
constexpr GridPosition eyeSprite(Direction direction) {
int x = 0;
switch (direction) {
case Direction::RIGHT:
@ -50,9 +50,9 @@ namespace Atlas {
break;
}
return { x, 6 };
}
}
constexpr GridPosition 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<int>(ghost);
int x = 0;
@ -76,17 +76,17 @@ namespace Atlas {
if (alternative)
x++;
return { x, y };
}
}
constexpr GridPosition initialFrightened(int animationIndex) {
constexpr GridPosition initialFrightened(int animationIndex) {
return (animationIndex % 2) == 0 ? Atlas::ghost_blue_frightened2 : Atlas::ghost_blue_frightened;
}
}
constexpr GridPosition endingFrightened(int animationIndex) {
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 <vector>
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<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 }; }
@ -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);
}