pacman/lib/Inky.cpp

71 lines
1.9 KiB
C++
Raw Normal View History

2021-07-28 13:28:36 +00:00
#include "Inky.hpp"
2021-07-29 09:16:08 +00:00
#include "GameState.hpp"
2021-07-28 13:28:36 +00:00
namespace pacman {
Inky::Inky()
: Ghost(Atlas::Ghost::inky) {
2021-07-28 15:01:22 +00:00
pos = initialPosition();
2021-07-28 13:28:36 +00:00
}
2021-08-02 12:29:25 +00:00
double Inky::speed(const GameState &) 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;
}
Position Inky::target(const GameState & gameState) const {
if (state == State::Eyes)
return initialPosition();
2021-07-28 13:28:36 +00:00
if (isInPen())
2021-07-28 13:41:32 +00:00
return penDoorPosition();
2021-07-28 13:28:36 +00:00
2021-07-29 09:16:08 +00:00
if (state == State::Scatter)
return scatterTarget();
// Inky first selects a position 2 cell away from pacman in his direction.
GridPosition targetPosition = gameState.pacMan.positionInGrid();
switch (gameState.pacMan.currentDirection()) {
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
const auto & blinkyPosition = gameState.blinky.positionInGrid();
2021-09-10 12:44:40 +00:00
double distanceBetweenBlinkyAndTarget = std::hypot(blinkyPosition.x - targetPosition.x, blinkyPosition.y - targetPosition.y);
2021-07-29 09:16:08 +00:00
// And selects a point on the line crossing blinky and this position that is at twice that distance
// away from blinky
2021-09-10 12:44:40 +00:00
targetPosition.x += std::size_t((double(targetPosition.x) - double(blinkyPosition.x)) / distanceBetweenBlinkyAndTarget) * 2;
targetPosition.y += std::size_t((double(targetPosition.y) - double(blinkyPosition.y)) / distanceBetweenBlinkyAndTarget) * 2;
2021-07-29 09:16:08 +00:00
return gridPositionToPosition(targetPosition);
2021-07-28 13:28:36 +00:00
}
Position Inky::initialPosition() const {
return { 13.5, 14 };
}
Position Inky::scatterTarget() const {
return { 27, 30 };
}
}