No need to pass Board around

This commit is contained in:
Patricia Aas 2021-07-07 11:39:09 +02:00
parent 705ea717e1
commit 3c638c057e
10 changed files with 43 additions and 43 deletions

View file

@ -10,10 +10,10 @@ constexpr int POWER_PELLET_POINTS = 50;
constexpr int GHOST_POINTS = 200; constexpr int GHOST_POINTS = 200;
Game::Game() Game::Game()
: pacMan(board), : pacMan(),
pellets(board), pellets(),
superPellets(board), superPellets(),
ghosts(Blinky(board), Speedy(board), Inky(board), Clyde(board)) { ghosts(Blinky(), Speedy(), Inky(), Clyde()) {
score.lives = DEFAULT_LIVES; score.lives = DEFAULT_LIVES;
} }
@ -50,7 +50,7 @@ void Game::run() {
void Game::step(std::chrono::milliseconds delta, InputState inputState) { void Game::step(std::chrono::milliseconds delta, InputState inputState) {
pacMan.update(delta, inputState, board); pacMan.update(delta, inputState);
if (timeSinceDeath.count() != 0) { if (timeSinceDeath.count() != 0) {
timeSinceDeath += delta; timeSinceDeath += delta;
@ -61,7 +61,7 @@ void Game::step(std::chrono::milliseconds delta, InputState inputState) {
(ghost.reset(), ...); (ghost.reset(), ...);
}, },
ghosts); ghosts);
pacMan.reset(board); pacMan.reset();
timeSinceDeath = std::chrono::milliseconds(0); timeSinceDeath = std::chrono::milliseconds(0);
} }
@ -72,7 +72,7 @@ void Game::step(std::chrono::milliseconds delta, InputState inputState) {
return; return;
std::apply([&](auto &... ghost) { std::apply([&](auto &... ghost) {
(ghost.update(delta, board), ...); (ghost.update(delta), ...);
(checkCollision(ghost), ...); (checkCollision(ghost), ...);
}, },
ghosts); ghosts);

View file

@ -62,8 +62,8 @@ GridPosition Ghost::positionInGrid() const {
return positionToGridPosition(pos); return positionToGridPosition(pos);
} }
void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) { void Ghost::update(std::chrono::milliseconds time_delta) {
if (state == State::Eyes && isInPen(board)) if (state == State::Eyes && isInPen())
state = State::Scatter; state = State::Scatter;
if (state == State::Frightened) { if (state == State::Frightened) {
@ -73,15 +73,15 @@ void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) {
} }
updateAnimation(time_delta); updateAnimation(time_delta);
updatePosition(time_delta, board); updatePosition(time_delta);
} }
bool Ghost::isInPen(const Board & board) const { bool Ghost::isInPen() const {
return pacman::Board::isInPen(positionInGrid()); return pacman::Board::isInPen(positionInGrid());
} }
void Ghost::updatePosition(std::chrono::milliseconds time_delta, const Board & board) { void Ghost::updatePosition(std::chrono::milliseconds time_delta) {
updateDirection(board); updateDirection();
double position_delta = (0.004 * time_delta.count()) * speed(); double position_delta = (0.004 * time_delta.count()) * speed();
@ -131,7 +131,7 @@ double Ghost::speed() const {
* In the scatter state, each ghost tries to reach an unreachable position outside of the map. * In the scatter state, each ghost tries to reach an unreachable position outside of the map.
* This makes ghosts run in circle around the island at each of the 4 map corner. * This makes ghosts run in circle around the island at each of the 4 map corner.
*/ */
void Ghost::updateDirection(const Board & board) { void Ghost::updateDirection() {
const auto current_grid_position = positionInGrid(); const auto current_grid_position = positionInGrid();
if (current_grid_position == last_grid_position) if (current_grid_position == last_grid_position)
return; return;
@ -149,7 +149,7 @@ void Ghost::updateDirection(const Board & board) {
Move{ Direction::DOWN, { x, y + 1 } }, Move{ Direction::DOWN, { x, y + 1 } },
Move{ Direction::RIGHT, { x + 1, y } } } }; Move{ Direction::RIGHT, { x + 1, y } } } };
const Position target_position = target(board); const Position target_position = target();
for (auto & move : possible_moves) { for (auto & move : possible_moves) {
const bool invalid_position = (move.position.x < 0 || move.position.y < 0); const bool invalid_position = (move.position.x < 0 || move.position.y < 0);
@ -177,7 +177,7 @@ void Ghost::updateDirection(const Board & board) {
last_grid_position = current_grid_position; last_grid_position = current_grid_position;
} }
Position Ghost::target(const Board & board) const { Position Ghost::target() const {
if (state == State::Eyes) if (state == State::Eyes)
return startingPosition; return startingPosition;
@ -195,19 +195,19 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) {
} }
} }
Blinky::Blinky(const Board & board) Blinky::Blinky()
: Ghost(Atlas::Ghost::blinky, pacman::Board::initialBlinkyPosition(), pacman::Board::blinkyScatterTarget()) { : Ghost(Atlas::Ghost::blinky, pacman::Board::initialBlinkyPosition(), pacman::Board::blinkyScatterTarget()) {
} }
Speedy::Speedy(const Board & board) Speedy::Speedy()
: Ghost(Atlas::Ghost::speedy, pacman::Board::initialSpeedyPosition(), pacman::Board::speedyScatterTarget()) { : Ghost(Atlas::Ghost::speedy, pacman::Board::initialSpeedyPosition(), pacman::Board::speedyScatterTarget()) {
} }
Inky::Inky(const Board & board) Inky::Inky()
: Ghost(Atlas::Ghost::inky, pacman::Board::initialInkyPosition(), pacman::Board::inkyScatterTarget()) { : Ghost(Atlas::Ghost::inky, pacman::Board::initialInkyPosition(), pacman::Board::inkyScatterTarget()) {
} }
Clyde::Clyde(const Board & board) Clyde::Clyde()
: Ghost(Atlas::Ghost::clyde, pacman::Board::initialClydePosition(), pacman::Board::clydeScatterTarget()) { : Ghost(Atlas::Ghost::clyde, pacman::Board::initialClydePosition(), pacman::Board::clydeScatterTarget()) {
} }

View file

@ -3,7 +3,7 @@
namespace pacman { namespace pacman {
PacMan::PacMan(const Board & board) PacMan::PacMan()
: pos(Board::initialPacManPosition()) {} : pos(Board::initialPacManPosition()) {}
GridPosition PacMan::currentSprite() const { GridPosition PacMan::currentSprite() const {
@ -25,20 +25,20 @@ void PacMan::eat() {
direction = Direction::NONE; direction = Direction::NONE;
} }
void PacMan::reset(const Board & b) { void PacMan::reset() {
eaten = false; eaten = false;
direction = Direction::NONE; direction = Direction::NONE;
pos = pacman::Board::initialPacManPosition(); pos = pacman::Board::initialPacManPosition();
} }
void PacMan::update(std::chrono::milliseconds time_delta, InputState state, const Board & board) { void PacMan::update(std::chrono::milliseconds time_delta, InputState state) {
if (eaten) { if (eaten) {
updateAnimationPosition(time_delta, false); updateAnimationPosition(time_delta, false);
return; return;
} }
const auto old = pos; const auto old = pos;
setDirection(state); setDirection(state);
updateMazePosition(time_delta, board); updateMazePosition(time_delta);
const bool paused = pos == old; const bool paused = pos == old;
updateAnimationPosition(time_delta, paused); updateAnimationPosition(time_delta, paused);
} }
@ -62,7 +62,7 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool
} }
} }
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) { void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) {
// Handle teleport // Handle teleport
const size_t right = COLUMNS - 1; const size_t right = COLUMNS - 1;

View file

@ -3,7 +3,7 @@
namespace pacman { namespace pacman {
Pellets::Pellets(const Board & board) Pellets::Pellets()
: positions(pacman::Board::initialPelletPositions()) {} : positions(pacman::Board::initialPelletPositions()) {}
bool Pellets::eatPelletAtPosition(GridPosition p) { bool Pellets::eatPelletAtPosition(GridPosition p) {

View file

@ -3,7 +3,7 @@
namespace pacman { namespace pacman {
SuperPellets::SuperPellets(const Board & board) SuperPellets::SuperPellets()
: positions(pacman::Board::initialSuperPelletPositions()) {} : positions(pacman::Board::initialSuperPelletPositions()) {}
bool SuperPellets::eatPelletAtPosition(GridPosition p) { bool SuperPellets::eatPelletAtPosition(GridPosition p) {

View file

@ -25,7 +25,7 @@ public:
[[nodiscard]] GridPosition positionInGrid() const; [[nodiscard]] GridPosition positionInGrid() const;
void update(std::chrono::milliseconds time_delta, const Board & board); void update(std::chrono::milliseconds time_delta);
void frighten(); void frighten();
void eat(); void eat();
[[nodiscard]] bool isFrightened() const; [[nodiscard]] bool isFrightened() const;
@ -35,9 +35,9 @@ public:
private: private:
[[nodiscard]] double speed() const; [[nodiscard]] double speed() const;
void updateAnimation(std::chrono::milliseconds time_delta); void updateAnimation(std::chrono::milliseconds time_delta);
void updatePosition(std::chrono::milliseconds time_delta, const Board & board); void updatePosition(std::chrono::milliseconds time_delta);
void updateDirection(const Board & board); void updateDirection();
[[nodiscard]] Position target(const Board & board) const; [[nodiscard]] Position target() const;
protected: protected:
Atlas::Ghost spritesSet; Atlas::Ghost spritesSet;
@ -51,27 +51,27 @@ protected:
Position startingPosition; Position startingPosition;
Position scatterTarget; Position scatterTarget;
GridPosition last_grid_position = { 0, 0 }; GridPosition last_grid_position = { 0, 0 };
[[nodiscard]] bool isInPen(const Board & board) const; [[nodiscard]] bool isInPen() const;
}; };
class Blinky : public Ghost { class Blinky : public Ghost {
public: public:
explicit Blinky(const Board & board); explicit Blinky();
}; };
class Speedy : public Ghost { class Speedy : public Ghost {
public: public:
explicit Speedy(const Board & board); explicit Speedy();
}; };
class Inky : public Ghost { class Inky : public Ghost {
public: public:
explicit Inky(const Board & board); explicit Inky();
}; };
class Clyde : public Ghost { class Clyde : public Ghost {
public: public:
explicit Clyde(const Board & board); explicit Clyde();
}; };
} // namespace pacman } // namespace pacman

View file

@ -13,7 +13,7 @@ class InputState;
class PacMan { class PacMan {
public: public:
explicit PacMan(const Board & board); explicit PacMan();
[[nodiscard]] GridPosition currentSprite() const; [[nodiscard]] GridPosition currentSprite() const;
@ -21,10 +21,10 @@ public:
[[nodiscard]] GridPosition positionInGrid() const; [[nodiscard]] GridPosition positionInGrid() const;
void update(std::chrono::milliseconds time_delta, InputState state, const Board & board); void update(std::chrono::milliseconds time_delta, InputState state);
void eat(); void eat();
void reset(const Board & b); void reset();
[[nodiscard]] bool onTheMove() const { [[nodiscard]] bool onTheMove() const {
return direction != Direction::NONE; return direction != Direction::NONE;
} }
@ -39,7 +39,7 @@ private:
void setDirection(const InputState & state); void setDirection(const InputState & state);
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);
}; };
} // namespace pacman } // namespace pacman

View file

@ -7,7 +7,7 @@ namespace pacman {
class Pellets { class Pellets {
public: public:
explicit Pellets(const Board & board); explicit Pellets();
[[nodiscard]] GridPosition currentSprite() const { [[nodiscard]] GridPosition currentSprite() const {
return sprite; return sprite;

View file

@ -7,7 +7,7 @@ namespace pacman {
class SuperPellets { class SuperPellets {
public: public:
explicit SuperPellets(const Board & board); explicit SuperPellets();
[[nodiscard]] GridPosition currentSprite() const { [[nodiscard]] GridPosition currentSprite() const {
return sprite; return sprite;

View file

@ -3,7 +3,7 @@
TEST(PacManTest, InitialPosition) { TEST(PacManTest, InitialPosition) {
pacman::Board board; pacman::Board board;
pacman::PacMan pacMan(board); pacman::PacMan pacMan();
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);
} }