Formatting and const fixes. Also check for extra canGo calls.

This commit is contained in:
Ólafur Waage 2021-09-08 14:49:43 +02:00
parent b79b2a29e8
commit 60b3cdeb40
4 changed files with 40 additions and 34 deletions

View File

@ -11,8 +11,8 @@ void Game::run() {
auto current_time = std::chrono::system_clock::now(); auto current_time = std::chrono::system_clock::now();
while (true) { while (true) {
auto newTime = std::chrono::system_clock::now(); const auto newTime = std::chrono::system_clock::now();
auto frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(newTime - current_time); const auto frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(newTime - current_time);
current_time = newTime; current_time = newTime;
accumulator += frameTime; accumulator += frameTime;
@ -31,7 +31,7 @@ void Game::run() {
} }
void Game::processEvents(InputState & inputState) { void Game::processEvents(InputState & inputState) {
auto event = canvas.pollEvent(); const auto event = canvas.pollEvent();
if (event && event.value().type == sf::Event::Closed) { if (event && event.value().type == sf::Event::Closed) {
inputState.close = true; inputState.close = true;
return; return;

View File

@ -75,7 +75,7 @@ void Ghost::update(std::chrono::milliseconds time_delta, const GameState & gameS
if (state == State::Scatter || state == State::Chase) { if (state == State::Scatter || state == State::Chase) {
timeChase += time_delta; timeChase += time_delta;
auto newState = defaultStateAtDuration(std::chrono::duration_cast<std::chrono::seconds>(timeChase)); const auto newState = defaultStateAtDuration(std::chrono::duration_cast<std::chrono::seconds>(timeChase));
if (newState != state) { if (newState != state) {
direction = oppositeDirection(direction); direction = oppositeDirection(direction);
state = newState; state = newState;
@ -204,13 +204,17 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) {
Ghost::State Ghost::defaultStateAtDuration(std::chrono::seconds seconds) { Ghost::State Ghost::defaultStateAtDuration(std::chrono::seconds seconds) {
// This array denotes the duration of each state, alternating between scatter and chase // This array denotes the duration of each state, alternating between scatter and chase
std::array changes = { /*scatter*/ 7, 20, 7, 20, 5, 20, 5 }; std::array changes = { /*scatter*/ 7, 20, 7, 20, 5, 20, 5 };
// To know the current state we first compute the cumulative time using std::partial_sum // To know the current state we first compute the cumulative time using std::partial_sum
// This gives us {7, 27, 34, 54, 59, 79, 84} // This gives us {7, 27, 34, 54, 59, 79, 84}
std::partial_sum(std::begin(changes), std::end(changes), std::begin(changes)); std::partial_sum(std::begin(changes), std::end(changes), std::begin(changes));
// Then we look for the first value in the array greater than the time spent in chase/scatter states // Then we look for the first value in the array greater than the time spent in chase/scatter states
auto it = std::upper_bound(std::begin(changes), std::end(changes), seconds.count()); auto it = std::upper_bound(std::begin(changes), std::end(changes), seconds.count());
// We get the position of that iterator in the array // We get the position of that iterator in the array
auto count = std::distance(std::begin(changes), it); auto count = std::distance(std::begin(changes), it);
// Because the first positition is scatter, all the even positions will be scatter // Because the first positition is scatter, all the even positions will be scatter
// all the odd positions will be chase // all the odd positions will be chase
return count % 2 == 0 ? State::Scatter : State::Chase; return count % 2 == 0 ? State::Scatter : State::Chase;

View File

@ -81,31 +81,34 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) {
return isWalkableForPacMan(moveToPosition(pos, move_direction)); return isWalkableForPacMan(moveToPosition(pos, move_direction));
}; };
if (canGo(desired_direction)) { if (desired_direction != direction && canGo(desired_direction)) {
direction = desired_direction; direction = desired_direction;
} }
if (canGo(direction)) { if (!canGo(direction)) {
switch (direction) { return;
case Direction::NONE: }
break;
case Direction::LEFT: switch (direction) {
pos.x -= position_delta; case Direction::LEFT:
pos.y = std::floor(pos.y); pos.x -= position_delta;
break; pos.y = std::floor(pos.y);
case Direction::RIGHT: break;
pos.x += position_delta; case Direction::RIGHT:
pos.y = std::floor(pos.y); pos.x += position_delta;
break; pos.y = std::floor(pos.y);
case Direction::UP: break;
pos.x = std::floor(pos.x); case Direction::UP:
pos.y -= position_delta; pos.x = std::floor(pos.x);
break; pos.y -= position_delta;
case Direction::DOWN: break;
pos.x = std::floor(pos.x); case Direction::DOWN:
pos.y += position_delta; pos.x = std::floor(pos.x);
break; pos.y += position_delta;
} break;
case Direction::NONE:
default:
break;
} }
} }

View File

@ -23,9 +23,7 @@ public:
virtual ~Ghost() = default; virtual ~Ghost() = default;
GridPosition currentSprite() const; GridPosition currentSprite() const;
Position position() const; Position position() const;
GridPosition positionInGrid() const; GridPosition positionInGrid() const;
void update(std::chrono::milliseconds time_delta, const GameState & gameState); void update(std::chrono::milliseconds time_delta, const GameState & gameState);
@ -41,12 +39,6 @@ private:
void updateDirection(const GameState & gameState); void updateDirection(const GameState & gameState);
protected: protected:
State defaultStateAtDuration(std::chrono::seconds seconds);
virtual double speed(const GameState & gameState) const = 0;
virtual Position target(const GameState & gameState) const = 0;
virtual Position initialPosition() const = 0;
Atlas::Ghost spriteSet; Atlas::Ghost spriteSet;
Direction direction = Direction::NONE; Direction direction = Direction::NONE;
double timeForAnimation = 0; double timeForAnimation = 0;
@ -56,6 +48,13 @@ protected:
std::chrono::milliseconds timeChase = {}; std::chrono::milliseconds timeChase = {};
Position pos; Position pos;
GridPosition last_grid_position = { 0, 0 }; GridPosition last_grid_position = { 0, 0 };
State defaultStateAtDuration(std::chrono::seconds seconds);
virtual double speed(const GameState & gameState) const = 0;
virtual Position target(const GameState & gameState) const = 0;
virtual Position initialPosition() const = 0;
bool isInPen() const; bool isInPen() const;
}; };