2020-11-28 13:10:25 +00:00
|
|
|
#include "PacMan.hpp"
|
2021-06-20 16:52:34 +00:00
|
|
|
#include <cmath>
|
2020-11-28 13:10:25 +00:00
|
|
|
|
2021-07-05 12:10:01 +00:00
|
|
|
namespace pacman {
|
|
|
|
|
2021-07-07 09:39:09 +00:00
|
|
|
PacMan::PacMan()
|
2021-06-22 10:52:02 +00:00
|
|
|
: pos(Board::initialPacManPosition()) {}
|
2020-11-27 13:10:09 +00:00
|
|
|
|
2021-07-05 09:46:49 +00:00
|
|
|
GridPosition PacMan::currentSprite() const {
|
2021-07-07 09:24:12 +00:00
|
|
|
return eaten ? pacManAnimation.deathAnimationFrame() : pacManAnimation.animationFrame(direction);
|
2020-11-27 13:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:26:03 +00:00
|
|
|
Position PacMan::position() const {
|
2020-11-27 13:10:09 +00:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2021-07-05 11:54:54 +00:00
|
|
|
GridPosition PacMan::positionInGrid() const {
|
2021-07-06 10:35:23 +00:00
|
|
|
return positionToGridPosition(pos);
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-07 09:39:09 +00:00
|
|
|
void PacMan::reset() {
|
2021-06-28 10:42:21 +00:00
|
|
|
eaten = false;
|
|
|
|
direction = Direction::NONE;
|
2021-07-07 09:24:12 +00:00
|
|
|
pos = pacman::Board::initialPacManPosition();
|
2021-06-28 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 14:57:18 +00:00
|
|
|
void PacMan::update(std::chrono::milliseconds time_delta, Direction input_direction) {
|
2021-06-28 10:42:21 +00:00
|
|
|
if (eaten) {
|
|
|
|
updateAnimationPosition(time_delta, false);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-08 14:57:18 +00:00
|
|
|
if (input_direction != Direction::NONE)
|
|
|
|
desired_direction = input_direction;
|
2021-06-24 07:46:58 +00:00
|
|
|
const auto old = pos;
|
2021-07-07 09:39:09 +00:00
|
|
|
updateMazePosition(time_delta);
|
2021-06-24 07:46:58 +00:00
|
|
|
const bool paused = pos == old;
|
|
|
|
updateAnimationPosition(time_delta, paused);
|
2020-11-27 13:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 07:46:58 +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
|
|
|
}
|
|
|
|
|
2021-07-07 09:39:09 +00:00
|
|
|
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) {
|
2021-07-06 14:33:33 +00:00
|
|
|
|
2021-07-06 15:11:18 +00:00
|
|
|
// Handle teleport
|
2021-07-06 14:33:33 +00:00
|
|
|
const size_t right = COLUMNS - 1;
|
|
|
|
const size_t left = 0;
|
|
|
|
|
2021-07-06 15:11:49 +00:00
|
|
|
if (std::size_t(pos.x) == right && direction == Direction::RIGHT) {
|
2021-07-06 14:33:33 +00:00
|
|
|
pos.x = left;
|
|
|
|
return;
|
2021-07-06 15:11:49 +00:00
|
|
|
} else if (std::size_t(pos.x) == left && direction == Direction::LEFT) {
|
2021-07-06 14:33:33 +00:00
|
|
|
pos.x = right;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:35:23 +00:00
|
|
|
const double position_delta = 0.004 * time_delta.count();
|
2021-07-06 15:10:17 +00:00
|
|
|
const auto pacman_size = 1;
|
2020-11-27 13:10:09 +00:00
|
|
|
|
2021-07-06 14:33:33 +00:00
|
|
|
auto moveToPosition = [&](Position point, Direction move_direction) {
|
|
|
|
switch (move_direction) {
|
2021-07-05 11:54:54 +00:00
|
|
|
case Direction::LEFT:
|
2021-07-06 14:33:33 +00:00
|
|
|
return GridPosition{ std::size_t(point.x - position_delta), std::size_t(point.y) };
|
2021-07-05 11:54:54 +00:00
|
|
|
case Direction::RIGHT:
|
2021-07-06 14:33:33 +00:00
|
|
|
return GridPosition{ std::size_t(point.x + pacman_size), std::size_t(point.y) };
|
2021-07-05 11:54:54 +00:00
|
|
|
case Direction::UP:
|
2021-07-06 14:33:33 +00:00
|
|
|
return GridPosition{ std::size_t(point.x), std::size_t(point.y - position_delta) };
|
2021-07-05 11:54:54 +00:00
|
|
|
case Direction::DOWN:
|
2021-07-06 14:33:33 +00:00
|
|
|
return GridPosition{ std::size_t(point.x), std::size_t(point.y + pacman_size) };
|
2021-07-05 11:54:54 +00:00
|
|
|
case Direction::NONE:
|
|
|
|
default:
|
2021-07-06 14:33:33 +00:00
|
|
|
return positionToGridPosition(point);
|
2021-07-05 11:54:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-06 14:33:33 +00:00
|
|
|
auto canGo = [&](Direction move_direction) {
|
|
|
|
return pacman::Board::isWalkableForPacMan(moveToPosition(pos, move_direction));
|
2021-07-06 10:35:23 +00:00
|
|
|
};
|
|
|
|
|
2021-07-06 14:33:33 +00:00
|
|
|
if (canGo(desired_direction)) {
|
2020-11-27 16:01:27 +00:00
|
|
|
direction = desired_direction;
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:35:23 +00:00
|
|
|
if (canGo(direction)) {
|
2020-11-27 13:10:09 +00:00
|
|
|
switch (direction) {
|
|
|
|
case Direction::NONE:
|
|
|
|
break;
|
|
|
|
case Direction::LEFT:
|
|
|
|
pos.x -= position_delta;
|
2021-07-06 14:33:33 +00:00
|
|
|
pos.y = std::floor(pos.y);
|
2020-11-27 13:10:09 +00:00
|
|
|
break;
|
|
|
|
case Direction::RIGHT:
|
|
|
|
pos.x += position_delta;
|
2021-07-06 14:33:33 +00:00
|
|
|
pos.y = std::floor(pos.y);
|
2020-11-27 13:10:09 +00:00
|
|
|
break;
|
|
|
|
case Direction::UP:
|
2021-07-06 14:33:33 +00:00
|
|
|
pos.x = std::floor(pos.x);
|
2020-11-27 13:10:09 +00:00
|
|
|
pos.y -= position_delta;
|
|
|
|
break;
|
|
|
|
case Direction::DOWN:
|
2021-07-06 14:33:33 +00:00
|
|
|
pos.x = std::floor(pos.x);
|
2020-11-27 13:10:09 +00:00
|
|
|
pos.y += position_delta;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 12:10:01 +00:00
|
|
|
|
|
|
|
} // namespace pacman
|