No need to pass Board around
This commit is contained in:
parent
705ea717e1
commit
3c638c057e
10 changed files with 43 additions and 43 deletions
14
lib/Game.cpp
14
lib/Game.cpp
|
@ -10,10 +10,10 @@ constexpr int POWER_PELLET_POINTS = 50;
|
|||
constexpr int GHOST_POINTS = 200;
|
||||
|
||||
Game::Game()
|
||||
: pacMan(board),
|
||||
pellets(board),
|
||||
superPellets(board),
|
||||
ghosts(Blinky(board), Speedy(board), Inky(board), Clyde(board)) {
|
||||
: pacMan(),
|
||||
pellets(),
|
||||
superPellets(),
|
||||
ghosts(Blinky(), Speedy(), Inky(), Clyde()) {
|
||||
score.lives = DEFAULT_LIVES;
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ void Game::run() {
|
|||
|
||||
void Game::step(std::chrono::milliseconds delta, InputState inputState) {
|
||||
|
||||
pacMan.update(delta, inputState, board);
|
||||
pacMan.update(delta, inputState);
|
||||
|
||||
if (timeSinceDeath.count() != 0) {
|
||||
timeSinceDeath += delta;
|
||||
|
@ -61,7 +61,7 @@ void Game::step(std::chrono::milliseconds delta, InputState inputState) {
|
|||
(ghost.reset(), ...);
|
||||
},
|
||||
ghosts);
|
||||
pacMan.reset(board);
|
||||
pacMan.reset();
|
||||
timeSinceDeath = std::chrono::milliseconds(0);
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ void Game::step(std::chrono::milliseconds delta, InputState inputState) {
|
|||
return;
|
||||
|
||||
std::apply([&](auto &... ghost) {
|
||||
(ghost.update(delta, board), ...);
|
||||
(ghost.update(delta), ...);
|
||||
(checkCollision(ghost), ...);
|
||||
},
|
||||
ghosts);
|
||||
|
|
|
@ -62,8 +62,8 @@ GridPosition Ghost::positionInGrid() const {
|
|||
return positionToGridPosition(pos);
|
||||
}
|
||||
|
||||
void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) {
|
||||
if (state == State::Eyes && isInPen(board))
|
||||
void Ghost::update(std::chrono::milliseconds time_delta) {
|
||||
if (state == State::Eyes && isInPen())
|
||||
state = State::Scatter;
|
||||
|
||||
if (state == State::Frightened) {
|
||||
|
@ -73,15 +73,15 @@ void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) {
|
|||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
void Ghost::updatePosition(std::chrono::milliseconds time_delta, const Board & board) {
|
||||
updateDirection(board);
|
||||
void Ghost::updatePosition(std::chrono::milliseconds time_delta) {
|
||||
updateDirection();
|
||||
|
||||
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.
|
||||
* 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();
|
||||
if (current_grid_position == last_grid_position)
|
||||
return;
|
||||
|
@ -149,7 +149,7 @@ void Ghost::updateDirection(const Board & board) {
|
|||
Move{ Direction::DOWN, { x, y + 1 } },
|
||||
Move{ Direction::RIGHT, { x + 1, y } } } };
|
||||
|
||||
const Position target_position = target(board);
|
||||
const Position target_position = target();
|
||||
|
||||
for (auto & move : possible_moves) {
|
||||
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;
|
||||
}
|
||||
|
||||
Position Ghost::target(const Board & board) const {
|
||||
Position Ghost::target() const {
|
||||
if (state == State::Eyes)
|
||||
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()) {
|
||||
}
|
||||
|
||||
Speedy::Speedy(const Board & board)
|
||||
Speedy::Speedy()
|
||||
: 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()) {
|
||||
}
|
||||
|
||||
Clyde::Clyde(const Board & board)
|
||||
Clyde::Clyde()
|
||||
: Ghost(Atlas::Ghost::clyde, pacman::Board::initialClydePosition(), pacman::Board::clydeScatterTarget()) {
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
namespace pacman {
|
||||
|
||||
PacMan::PacMan(const Board & board)
|
||||
PacMan::PacMan()
|
||||
: pos(Board::initialPacManPosition()) {}
|
||||
|
||||
GridPosition PacMan::currentSprite() const {
|
||||
|
@ -25,20 +25,20 @@ void PacMan::eat() {
|
|||
direction = Direction::NONE;
|
||||
}
|
||||
|
||||
void PacMan::reset(const Board & b) {
|
||||
void PacMan::reset() {
|
||||
eaten = false;
|
||||
direction = Direction::NONE;
|
||||
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) {
|
||||
updateAnimationPosition(time_delta, false);
|
||||
return;
|
||||
}
|
||||
const auto old = pos;
|
||||
setDirection(state);
|
||||
updateMazePosition(time_delta, board);
|
||||
updateMazePosition(time_delta);
|
||||
const bool paused = pos == old;
|
||||
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
|
||||
const size_t right = COLUMNS - 1;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
namespace pacman {
|
||||
|
||||
Pellets::Pellets(const Board & board)
|
||||
Pellets::Pellets()
|
||||
: positions(pacman::Board::initialPelletPositions()) {}
|
||||
|
||||
bool Pellets::eatPelletAtPosition(GridPosition p) {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
namespace pacman {
|
||||
|
||||
SuperPellets::SuperPellets(const Board & board)
|
||||
SuperPellets::SuperPellets()
|
||||
: positions(pacman::Board::initialSuperPelletPositions()) {}
|
||||
|
||||
bool SuperPellets::eatPelletAtPosition(GridPosition p) {
|
||||
|
|
|
@ -25,7 +25,7 @@ public:
|
|||
|
||||
[[nodiscard]] GridPosition positionInGrid() const;
|
||||
|
||||
void update(std::chrono::milliseconds time_delta, const Board & board);
|
||||
void update(std::chrono::milliseconds time_delta);
|
||||
void frighten();
|
||||
void eat();
|
||||
[[nodiscard]] bool isFrightened() const;
|
||||
|
@ -35,9 +35,9 @@ public:
|
|||
private:
|
||||
[[nodiscard]] double speed() const;
|
||||
void updateAnimation(std::chrono::milliseconds time_delta);
|
||||
void updatePosition(std::chrono::milliseconds time_delta, const Board & board);
|
||||
void updateDirection(const Board & board);
|
||||
[[nodiscard]] Position target(const Board & board) const;
|
||||
void updatePosition(std::chrono::milliseconds time_delta);
|
||||
void updateDirection();
|
||||
[[nodiscard]] Position target() const;
|
||||
|
||||
protected:
|
||||
Atlas::Ghost spritesSet;
|
||||
|
@ -51,27 +51,27 @@ protected:
|
|||
Position startingPosition;
|
||||
Position scatterTarget;
|
||||
GridPosition last_grid_position = { 0, 0 };
|
||||
[[nodiscard]] bool isInPen(const Board & board) const;
|
||||
[[nodiscard]] bool isInPen() const;
|
||||
};
|
||||
|
||||
class Blinky : public Ghost {
|
||||
public:
|
||||
explicit Blinky(const Board & board);
|
||||
explicit Blinky();
|
||||
};
|
||||
|
||||
class Speedy : public Ghost {
|
||||
public:
|
||||
explicit Speedy(const Board & board);
|
||||
explicit Speedy();
|
||||
};
|
||||
|
||||
class Inky : public Ghost {
|
||||
public:
|
||||
explicit Inky(const Board & board);
|
||||
explicit Inky();
|
||||
};
|
||||
|
||||
class Clyde : public Ghost {
|
||||
public:
|
||||
explicit Clyde(const Board & board);
|
||||
explicit Clyde();
|
||||
};
|
||||
|
||||
} // namespace pacman
|
||||
|
|
|
@ -13,7 +13,7 @@ class InputState;
|
|||
|
||||
class PacMan {
|
||||
public:
|
||||
explicit PacMan(const Board & board);
|
||||
explicit PacMan();
|
||||
|
||||
[[nodiscard]] GridPosition currentSprite() const;
|
||||
|
||||
|
@ -21,10 +21,10 @@ public:
|
|||
|
||||
[[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 reset(const Board & b);
|
||||
void reset();
|
||||
[[nodiscard]] bool onTheMove() const {
|
||||
return direction != Direction::NONE;
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ private:
|
|||
void setDirection(const InputState & state);
|
||||
|
||||
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
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace pacman {
|
|||
|
||||
class Pellets {
|
||||
public:
|
||||
explicit Pellets(const Board & board);
|
||||
explicit Pellets();
|
||||
|
||||
[[nodiscard]] GridPosition currentSprite() const {
|
||||
return sprite;
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace pacman {
|
|||
|
||||
class SuperPellets {
|
||||
public:
|
||||
explicit SuperPellets(const Board & board);
|
||||
explicit SuperPellets();
|
||||
|
||||
[[nodiscard]] GridPosition currentSprite() const {
|
||||
return sprite;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
TEST(PacManTest, InitialPosition) {
|
||||
pacman::Board board;
|
||||
pacman::PacMan pacMan(board);
|
||||
pacman::PacMan pacMan();
|
||||
EXPECT_EQ(pacMan.position().x, 13.5);
|
||||
EXPECT_EQ(pacMan.position().y, 23);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue