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();
while (true) {
auto newTime = std::chrono::system_clock::now();
auto frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(newTime - current_time);
const auto newTime = std::chrono::system_clock::now();
const auto frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(newTime - current_time);
current_time = newTime;
accumulator += frameTime;
@ -31,7 +31,7 @@ void Game::run() {
}
void Game::processEvents(InputState & inputState) {
auto event = canvas.pollEvent();
const auto event = canvas.pollEvent();
if (event && event.value().type == sf::Event::Closed) {
inputState.close = true;
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) {
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) {
direction = oppositeDirection(direction);
state = newState;
@ -204,13 +204,17 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) {
Ghost::State Ghost::defaultStateAtDuration(std::chrono::seconds seconds) {
// This array denotes the duration of each state, alternating between scatter and chase
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
// This gives us {7, 27, 34, 54, 59, 79, 84}
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
auto it = std::upper_bound(std::begin(changes), std::end(changes), seconds.count());
// We get the position of that iterator in the array
auto count = std::distance(std::begin(changes), it);
// Because the first positition is scatter, all the even positions will be scatter
// all the odd positions will be 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));
};
if (canGo(desired_direction)) {
if (desired_direction != direction && canGo(desired_direction)) {
direction = desired_direction;
}
if (canGo(direction)) {
switch (direction) {
case Direction::NONE:
break;
case Direction::LEFT:
pos.x -= position_delta;
pos.y = std::floor(pos.y);
break;
case Direction::RIGHT:
pos.x += position_delta;
pos.y = std::floor(pos.y);
break;
case Direction::UP:
pos.x = std::floor(pos.x);
pos.y -= position_delta;
break;
case Direction::DOWN:
pos.x = std::floor(pos.x);
pos.y += position_delta;
break;
}
if (!canGo(direction)) {
return;
}
switch (direction) {
case Direction::LEFT:
pos.x -= position_delta;
pos.y = std::floor(pos.y);
break;
case Direction::RIGHT:
pos.x += position_delta;
pos.y = std::floor(pos.y);
break;
case Direction::UP:
pos.x = std::floor(pos.x);
pos.y -= position_delta;
break;
case Direction::DOWN:
pos.x = std::floor(pos.x);
pos.y += position_delta;
break;
case Direction::NONE:
default:
break;
}
}

View File

@ -23,9 +23,7 @@ public:
virtual ~Ghost() = default;
GridPosition currentSprite() const;
Position position() const;
GridPosition positionInGrid() const;
void update(std::chrono::milliseconds time_delta, const GameState & gameState);
@ -41,12 +39,6 @@ private:
void updateDirection(const GameState & gameState);
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;
Direction direction = Direction::NONE;
double timeForAnimation = 0;
@ -56,6 +48,13 @@ protected:
std::chrono::milliseconds timeChase = {};
Position pos;
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;
};