2021-07-28 13:28:36 +00:00
|
|
|
#include "Inky.hpp"
|
|
|
|
|
|
|
|
namespace pacman {
|
|
|
|
|
|
|
|
Inky::Inky()
|
2021-07-28 14:20:19 +00:00
|
|
|
: Ghost(Atlas::Ghost::inky) {
|
2021-07-28 15:01:22 +00:00
|
|
|
pos = initialPosition();
|
2021-07-28 13:28:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 12:57:53 +00:00
|
|
|
double Inky::speed() const {
|
2021-07-28 13:28:36 +00:00
|
|
|
if (state == State::Eyes)
|
|
|
|
return 2;
|
|
|
|
if (state == State::Frightened)
|
|
|
|
return 0.5;
|
|
|
|
return 0.75;
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:57:53 +00:00
|
|
|
void Inky::setTarget(GridPosition pacManPos, Direction pacManDir, GridPosition blinkyPos) {
|
|
|
|
if (state == State::Eyes) {
|
|
|
|
target = initialPosition();
|
|
|
|
return;
|
|
|
|
}
|
2021-07-28 13:28:36 +00:00
|
|
|
|
2021-09-23 12:57:53 +00:00
|
|
|
if (isInPen()) {
|
|
|
|
target = penDoorPosition();
|
|
|
|
return;
|
|
|
|
}
|
2021-07-28 13:28:36 +00:00
|
|
|
|
2021-09-23 12:57:53 +00:00
|
|
|
if (state == State::Scatter) {
|
|
|
|
target = scatterTarget();
|
|
|
|
return;
|
|
|
|
}
|
2021-07-29 09:16:08 +00:00
|
|
|
|
|
|
|
// Inky first selects a position 2 cell away from pacman in his direction.
|
2021-09-23 12:57:53 +00:00
|
|
|
GridPosition targetPosition = pacManPos;
|
|
|
|
switch (pacManDir) {
|
2021-07-29 09:16:08 +00:00
|
|
|
case Direction::LEFT:
|
|
|
|
targetPosition.x -= 2;
|
|
|
|
break;
|
|
|
|
case Direction::RIGHT:
|
|
|
|
targetPosition.x += 2;
|
|
|
|
break;
|
|
|
|
case Direction::UP:
|
|
|
|
targetPosition.y -= 2;
|
|
|
|
targetPosition.x -= 2;
|
|
|
|
break;
|
|
|
|
case Direction::DOWN:
|
|
|
|
targetPosition.y += 2;
|
|
|
|
break;
|
|
|
|
case Direction::NONE:
|
2021-09-10 13:49:33 +00:00
|
|
|
assert(false && "Pacman should be moving");
|
2021-07-29 09:16:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then it calculates the distance between Blinky and this position
|
2021-09-23 12:57:53 +00:00
|
|
|
const double distanceBetweenBlinkyAndTarget = std::hypot(blinkyPos.x - targetPosition.x, blinkyPos.y - targetPosition.y);
|
2021-07-29 09:16:08 +00:00
|
|
|
|
2021-09-23 12:57:53 +00:00
|
|
|
// And selects a point on the line crossing blinky and
|
|
|
|
// this position that is at twice that distance away from blinky
|
|
|
|
targetPosition.x += std::size_t((double(targetPosition.x) - double(blinkyPos.x)) / distanceBetweenBlinkyAndTarget) * 2;
|
|
|
|
targetPosition.y += std::size_t((double(targetPosition.y) - double(blinkyPos.y)) / distanceBetweenBlinkyAndTarget) * 2;
|
2021-07-29 09:16:08 +00:00
|
|
|
|
2021-09-23 12:57:53 +00:00
|
|
|
target = gridPositionToPosition(targetPosition);
|
2021-07-28 13:28:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 14:20:19 +00:00
|
|
|
Position Inky::initialPosition() const {
|
2021-07-28 14:39:27 +00:00
|
|
|
return { 13.5, 14 };
|
2021-07-28 14:20:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 14:39:27 +00:00
|
|
|
Position Inky::scatterTarget() const {
|
|
|
|
return { 27, 30 };
|
|
|
|
}
|
2021-07-28 14:20:19 +00:00
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
} // namespace pacman
|