pacman/lib/PacMan.cpp

116 lines
2.9 KiB
C++
Raw Permalink Normal View History

#include "PacMan.hpp"
#include <cmath>
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
}
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-09-06 14:40:42 +00:00
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-28 13:41:32 +00:00
pos = 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-09-06 14:40:42 +00:00
2021-07-08 14:57:18 +00:00
if (input_direction != Direction::NONE)
desired_direction = input_direction;
2021-09-06 14:40:42 +00:00
const auto old = pos;
2021-07-07 09:39:09 +00:00
updateMazePosition(time_delta);
const bool paused = pos == old;
updateAnimationPosition(time_delta, paused);
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-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-08 16:00:39 +00:00
if (isPortal(positionInGrid(), direction)) {
pos = gridPositionToPosition(teleport(positionInGrid()));
return;
}
2021-07-06 14:33:33 +00:00
2021-09-10 12:44:40 +00:00
const double position_delta = 0.004 * double(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-09-10 09:41:20 +00:00
auto moveToPosition = [position_delta, pacman_size](Position point, Direction move_direction) {
2021-07-06 14:33:33 +00:00
switch (move_direction) {
2021-07-05 11:54:54 +00:00
case Direction::LEFT:
2021-09-10 09:02:37 +00:00
return GridPosition{ size_t(point.x - position_delta), size_t(point.y) };
2021-07-05 11:54:54 +00:00
case Direction::RIGHT:
2021-09-10 09:02:37 +00:00
return GridPosition{ size_t(point.x + pacman_size), size_t(point.y) };
2021-07-05 11:54:54 +00:00
case Direction::UP:
2021-09-10 09:02:37 +00:00
return GridPosition{ size_t(point.x), size_t(point.y - position_delta) };
2021-07-05 11:54:54 +00:00
case Direction::DOWN:
2021-09-10 09:02:37 +00:00
return GridPosition{ size_t(point.x), 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-08-12 08:19:54 +00:00
auto canGo = [&moveToPosition, this](Direction move_direction) {
2021-07-28 13:41:32 +00:00
return isWalkableForPacMan(moveToPosition(pos, move_direction));
2021-07-06 10:35:23 +00:00
};
if (desired_direction != direction && canGo(desired_direction)) {
2020-11-27 16:01:27 +00:00
direction = desired_direction;
}
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;
2020-11-27 13:10:09 +00:00
}
}
2021-07-05 12:10:01 +00:00
} // namespace pacman