pacman/lib/PacMan.cpp

100 lines
2.5 KiB
C++
Raw Normal View History

#include "PacMan.hpp"
#include <cmath>
PacMan::PacMan(const Board & board)
2021-06-22 10:52:02 +00:00
: pos(Board::initialPacManPosition()) {}
2020-11-27 13:10:09 +00:00
PositionInt PacMan::currentSprite() const {
2021-06-28 10:42:21 +00:00
return eaten ? pacManAnimation.deathAnimationFrame(direction) : pacManAnimation.animationFrame(direction);
2020-11-27 13:10:09 +00:00
}
Position PacMan::position() const {
2020-11-27 13:10:09 +00:00
return pos;
}
2021-06-16 11:59:16 +00:00
Position PacMan::positionInGrid() const {
return { std::round(pos.x), std::round(pos.y) };
2021-06-16 11:59:16 +00:00
}
2021-06-28 10:42:21 +00:00
void PacMan::eat() {
if (eaten)
return;
eaten = true;
direction = Direction::NONE;
}
void PacMan::reset(const Board & b) {
eaten = false;
direction = Direction::NONE;
pos = b.initialPacManPosition();
}
2020-11-27 13:10:09 +00:00
void PacMan::update(std::chrono::milliseconds time_delta, InputState state, const Board & board) {
2021-06-28 10:42:21 +00:00
if (eaten) {
updateAnimationPosition(time_delta, false);
return;
}
const auto old = pos;
2021-06-28 10:42:21 +00:00
setDirection(state);
2020-11-27 13:10:09 +00:00
updateMazePosition(time_delta, board);
const bool paused = pos == old;
updateAnimationPosition(time_delta, paused);
2020-11-27 13:10:09 +00:00
}
void PacMan::setDirection(const InputState & state) {
if (state.left)
2020-11-27 16:01:27 +00:00
desired_direction = Direction::LEFT;
2020-11-27 13:10:09 +00:00
else if (state.right)
2020-11-27 16:01:27 +00:00
desired_direction = Direction::RIGHT;
2020-11-27 13:10:09 +00:00
else if (state.up)
2020-11-27 16:01:27 +00:00
desired_direction = Direction::UP;
2020-11-27 13:10:09 +00:00
else if (state.down)
2020-11-27 16:01:27 +00:00
desired_direction = Direction::DOWN;
2020-11-27 13:10:09 +00:00
}
void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool paused) {
2021-06-24 11:32:52 +00:00
if (paused) {
pacManAnimation.pause();
} else {
2021-06-28 10:42:21 +00:00
pacManAnimation.updateAnimationPosition(time_delta, eaten);
2021-06-24 11:32:52 +00:00
}
2020-11-27 13:10:09 +00:00
}
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) {
float position_delta = 0.004 * time_delta.count();
2020-11-27 13:10:09 +00:00
// Handle teleport
if (pos.x >= COLUMNS - 1 && direction == Direction::RIGHT) {
pos.x = -1;
} else if (pos.x <= 0 && direction == Direction::LEFT) {
pos.x = COLUMNS;
}
2021-06-24 09:40:49 +00:00
else if (board.isWalkableForPacMan(pos, position_delta, desired_direction)) {
2020-11-27 16:01:27 +00:00
direction = desired_direction;
}
2021-06-24 09:40:49 +00:00
if (board.isWalkableForPacMan(pos, position_delta, direction)) {
2020-11-27 13:10:09 +00:00
switch (direction) {
case Direction::NONE:
break;
case Direction::LEFT:
pos.x -= position_delta;
pos.y = floor(pos.y);
break;
case Direction::RIGHT:
pos.x += position_delta;
pos.y = floor(pos.y);
break;
case Direction::UP:
pos.x = floor(pos.x);
pos.y -= position_delta;
break;
case Direction::DOWN:
pos.x = floor(pos.x);
pos.y += position_delta;
break;
}
}
}