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-05 09:46:49 +00:00
|
|
|
GridPosition PacMan::currentSprite() const {
|
2021-07-13 12:26:57 +00:00
|
|
|
return dead ? 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-07-13 12:26:57 +00:00
|
|
|
void PacMan::die() {
|
|
|
|
if (dead)
|
2021-06-28 10:42:21 +00:00
|
|
|
return;
|
2021-07-13 12:26:57 +00:00
|
|
|
dead = true;
|
2021-06-28 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 09:39:09 +00:00
|
|
|
void PacMan::reset() {
|
2021-07-13 12:26:57 +00:00
|
|
|
dead = false;
|
2021-06-28 10:42:21 +00:00
|
|
|
direction = Direction::NONE;
|
2021-07-13 12:26:57 +00:00
|
|
|
desired_direction = Direction::NONE;
|
2021-07-08 15:42:24 +00:00
|
|
|
pos = pacman::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-07-13 12:26:57 +00:00
|
|
|
if (dead) {
|
2021-06-28 10:42:21 +00:00
|
|
|
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-07-13 12:26:57 +00:00
|
|
|
pacManAnimation.updateAnimationPosition(time_delta, dead);
|
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-08 16:00:39 +00:00
|
|
|
if (isPortal(positionInGrid(), direction)) {
|
|
|
|
pos = gridPositionToPosition(teleport(positionInGrid()));
|
|
|
|
return;
|
|
|
|
}
|
2021-07-06 14:33:33 +00:00
|
|
|
|
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) {
|
2021-07-08 15:42:24 +00:00
|
|
|
return pacman::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
|