Rename PositionInt

This commit is contained in:
Corentin Jabot 2021-07-05 11:46:49 +02:00
parent a5bf577e26
commit e4991ab1dd
14 changed files with 53 additions and 53 deletions

View File

@ -15,22 +15,22 @@ namespace Atlas {
clyde = 5, clyde = 5,
}; };
constexpr PositionInt pacman_right_wide = { 0, 0 }; constexpr GridPosition pacman_right_wide = { 0, 0 };
constexpr PositionInt pacman_right_narrow = { 1, 0 }; constexpr GridPosition pacman_right_narrow = { 1, 0 };
constexpr PositionInt pacman_closed = { 2, 0 }; constexpr GridPosition pacman_closed = { 2, 0 };
constexpr PositionInt pacman_left_narrow = { 3, 0 }; constexpr GridPosition pacman_left_narrow = { 3, 0 };
constexpr PositionInt pacman_left_wide = { 4, 0 }; constexpr GridPosition pacman_left_wide = { 4, 0 };
constexpr PositionInt pacman_up_wide = { 5, 0 }; constexpr GridPosition pacman_up_wide = { 5, 0 };
constexpr PositionInt pacman_up_narrow = { 6, 0 }; constexpr GridPosition pacman_up_narrow = { 6, 0 };
constexpr PositionInt pacman_down_wide = { 7, 0 }; constexpr GridPosition pacman_down_wide = { 7, 0 };
constexpr PositionInt pacman_down_narrow = { 8, 0 }; constexpr GridPosition pacman_down_narrow = { 8, 0 };
constexpr PositionInt ghost_blue_frightened = { 0, 7 }; constexpr GridPosition ghost_blue_frightened = { 0, 7 };
constexpr PositionInt ghost_blue_frightened2 = { 1, 7 }; constexpr GridPosition ghost_blue_frightened2 = { 1, 7 };
constexpr PositionInt ghost_white_frightened = { 2, 7 }; constexpr GridPosition ghost_white_frightened = { 2, 7 };
constexpr PositionInt ghost_white_frightened2 = { 3, 7 }; constexpr GridPosition ghost_white_frightened2 = { 3, 7 };
constexpr PositionInt eyeSprite(Direction direction) { constexpr GridPosition eyeSprite(Direction direction) {
int x = 0; int x = 0;
switch (direction) { switch (direction) {
case Direction::RIGHT: case Direction::RIGHT:
@ -52,7 +52,7 @@ namespace Atlas {
return { x, 6 }; return { x, 6 };
} }
constexpr PositionInt ghostSprite(Ghost ghost, Direction direction, bool alternative) { constexpr GridPosition ghostSprite(Ghost ghost, Direction direction, bool alternative) {
assert(ghost >= Ghost::blinky && ghost <= Ghost::clyde && "Invalid Ghost"); assert(ghost >= Ghost::blinky && ghost <= Ghost::clyde && "Invalid Ghost");
int y = static_cast<int>(ghost); int y = static_cast<int>(ghost);
int x = 0; int x = 0;
@ -78,12 +78,12 @@ namespace Atlas {
return { x, y }; return { x, y };
} }
constexpr PositionInt initialFrightened(int animationIndex) { constexpr GridPosition initialFrightened(int animationIndex) {
return (animationIndex % 2) == 0 ? Atlas::ghost_blue_frightened2 : Atlas::ghost_blue_frightened; return (animationIndex % 2) == 0 ? Atlas::ghost_blue_frightened2 : Atlas::ghost_blue_frightened;
} }
constexpr PositionInt endingFrightened(int animationIndex) { constexpr GridPosition endingFrightened(int animationIndex) {
std::array<PositionInt, 4> positions = { Atlas::ghost_blue_frightened, std::array<GridPosition, 4> positions = { Atlas::ghost_blue_frightened,
Atlas::ghost_blue_frightened2, Atlas::ghost_blue_frightened2,
Atlas::ghost_white_frightened, Atlas::ghost_white_frightened,
Atlas::ghost_white_frightened2 }; Atlas::ghost_white_frightened2 };

View File

@ -86,8 +86,8 @@ bool Board::isWalkable(Position point, double position_delta, Direction directio
return pacman ? cell != Cell::wall : cell != Cell::wall && cell != Cell::pen; return pacman ? cell != Cell::wall : cell != Cell::wall && cell != Cell::pen;
} }
std::vector<PositionInt> Board::initialPelletPositions() { std::vector<GridPosition> Board::initialPelletPositions() {
std::vector<PositionInt> positions; std::vector<GridPosition> positions;
for (int row = 0; row < ROWS; row++) { for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) { for (int column = 0; column < COLUMNS; column++) {
if (board[row][column] == int(Cell::pellet)) if (board[row][column] == int(Cell::pellet))
@ -97,8 +97,8 @@ std::vector<PositionInt> Board::initialPelletPositions() {
return positions; return positions;
} }
std::vector<PositionInt> Board::initialSuperPelletPositions() { std::vector<GridPosition> Board::initialSuperPelletPositions() {
std::vector<PositionInt> positions; std::vector<GridPosition> positions;
for (int row = 0; row < ROWS; row++) { for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) { for (int column = 0; column < COLUMNS; column++) {
if (board[row][column] == int(Cell::power_pellet)) if (board[row][column] == int(Cell::power_pellet))

View File

@ -29,9 +29,9 @@ public:
[[nodiscard]] static bool isWalkable(Position point) ; [[nodiscard]] static bool isWalkable(Position point) ;
[[nodiscard]] static bool isInPen(Position point) ; [[nodiscard]] static bool isInPen(Position point) ;
[[nodiscard]] static std::vector<PositionInt> initialPelletPositions() ; [[nodiscard]] static std::vector<GridPosition> initialPelletPositions() ;
[[nodiscard]] static std::vector<PositionInt> initialSuperPelletPositions() ; [[nodiscard]] static std::vector<GridPosition> initialSuperPelletPositions() ;
static Position initialPacManPosition() { return { 13.5, 23 }; } static Position initialPacManPosition() { return { 13.5, 23 }; }

View File

@ -58,7 +58,7 @@ void Canvas::renderMaze() {
void Canvas::renderPellets(const Pellets & pellets) { void Canvas::renderPellets(const Pellets & pellets) {
Sprite pellet = getSprite(pellets.currentSprite()); Sprite pellet = getSprite(pellets.currentSprite());
std::vector<PositionInt> pelletPositions = pellets.currentPositions(); std::vector<GridPosition> pelletPositions = pellets.currentPositions();
for (const auto & pos : pelletPositions) { for (const auto & pos : pelletPositions) {
renderSprite(pellet, { double(pos.x), double(pos.y) }); renderSprite(pellet, { double(pos.x), double(pos.y) });
} }
@ -66,7 +66,7 @@ void Canvas::renderPellets(const Pellets & pellets) {
void Canvas::renderSuperPellets(const SuperPellets & superPellets) { void Canvas::renderSuperPellets(const SuperPellets & superPellets) {
Sprite pellet = getSprite(superPellets.currentSprite()); Sprite pellet = getSprite(superPellets.currentSprite());
std::vector<PositionInt> superPelletPositions = superPellets.currentPositions(); std::vector<GridPosition> superPelletPositions = superPellets.currentPositions();
for (const auto & pos : superPelletPositions) { for (const auto & pos : superPelletPositions) {
renderSprite(pellet, { double(pos.x), double(pos.y) }); renderSprite(pellet, { double(pos.x), double(pos.y) });
} }
@ -98,13 +98,13 @@ void Canvas::renderScore(int score) {
} }
void Canvas::renderLives(int lives) { void Canvas::renderLives(int lives) {
constexpr PositionInt liveSprite = Atlas::pacman_left_narrow; constexpr GridPosition liveSprite = Atlas::pacman_left_narrow;
const int x = LEFT_MARGIN + MAZE_WIDTH + LEFT_MARGIN; const int x = LEFT_MARGIN + MAZE_WIDTH + LEFT_MARGIN;
const int y = maze_texture.getSize().y; const int y = maze_texture.getSize().y;
Sprite pacmanSprite = getSprite(liveSprite); Sprite pacmanSprite = getSprite(liveSprite);
for (int i = 0; i < lives - 1; i++) { for (int i = 0; i < lives - 1; i++) {
PositionInt pos{ x + i * pacmanSprite.getTextureRect().width, y }; GridPosition pos{ x + i * pacmanSprite.getTextureRect().width, y };
pacmanSprite.setPosition(pos.x, pos.y); pacmanSprite.setPosition(pos.x, pos.y);
window.draw(pacmanSprite); window.draw(pacmanSprite);
} }
@ -114,7 +114,7 @@ Rect Canvas::windowDimensions() {
return { 0, 0, LEFT_MARGIN + MAZE_WIDTH + SCORE_WIDTH, TOP_MARGIN + MAZE_HEIGHT + BOTTOM_MARGIN }; return { 0, 0, LEFT_MARGIN + MAZE_WIDTH + SCORE_WIDTH, TOP_MARGIN + MAZE_HEIGHT + BOTTOM_MARGIN };
} }
Sprite Canvas::getSprite(PositionInt coordinate) const { Sprite Canvas::getSprite(GridPosition coordinate) const {
sf::Sprite sprite; sf::Sprite sprite;
sprite.setTexture(sprites_texture); sprite.setTexture(sprites_texture);
sprite.setTextureRect(sf::IntRect{ coordinate.x * DEFAULT_SPRITE_WIDTH, sprite.setTextureRect(sf::IntRect{ coordinate.x * DEFAULT_SPRITE_WIDTH,

View File

@ -42,7 +42,7 @@ private:
static sf::Texture loadTexture(std::string_view path); static sf::Texture loadTexture(std::string_view path);
static sf::Font loadFont(std::string_view path); static sf::Font loadFont(std::string_view path);
Sprite getSprite(PositionInt rect) const; Sprite getSprite(GridPosition rect) const;
sf::RenderWindow window; sf::RenderWindow window;
sf::Texture maze_texture; sf::Texture maze_texture;

View File

@ -38,7 +38,7 @@ void Ghost::reset() {
pos = startingPosition; pos = startingPosition;
} }
[[nodiscard]] PositionInt Ghost::currentSprite() const { [[nodiscard]] GridPosition Ghost::currentSprite() const {
switch (state) { switch (state) {
default: default:
return Atlas::ghostSprite(spritesSet, direction, (animationIndex % 2) == 0); return Atlas::ghostSprite(spritesSet, direction, (animationIndex % 2) == 0);

View File

@ -17,7 +17,7 @@ public:
explicit Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget); explicit Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget);
[[nodiscard]] PositionInt currentSprite() const; [[nodiscard]] GridPosition currentSprite() const;
[[nodiscard]] Position position() const; [[nodiscard]] Position position() const;

View File

@ -4,7 +4,7 @@
PacMan::PacMan(const Board & board) PacMan::PacMan(const Board & board)
: pos(Board::initialPacManPosition()) {} : pos(Board::initialPacManPosition()) {}
PositionInt PacMan::currentSprite() const { GridPosition PacMan::currentSprite() const {
return eaten ? pacManAnimation.deathAnimationFrame(direction) : pacManAnimation.animationFrame(direction); return eaten ? pacManAnimation.deathAnimationFrame(direction) : pacManAnimation.animationFrame(direction);
} }

View File

@ -13,7 +13,7 @@ class PacMan {
public: public:
explicit PacMan(const Board & board); explicit PacMan(const Board & board);
[[nodiscard]] PositionInt currentSprite() const; [[nodiscard]] GridPosition currentSprite() const;
[[nodiscard]] Position position() const; [[nodiscard]] Position position() const;

View File

@ -1,6 +1,6 @@
#include "PacManAnimation.hpp" #include "PacManAnimation.hpp"
PositionInt PacManAnimation::animationFrame(Direction direction) const { GridPosition PacManAnimation::animationFrame(Direction direction) const {
switch (direction) { switch (direction) {
case Direction::LEFT: case Direction::LEFT:
return left_animation[animation_position]; return left_animation[animation_position];
@ -16,8 +16,8 @@ PositionInt PacManAnimation::animationFrame(Direction direction) const {
} }
} }
[[nodiscard]] PositionInt PacManAnimation::deathAnimationFrame(Direction direction) const { [[nodiscard]] GridPosition PacManAnimation::deathAnimationFrame(Direction direction) const {
return PositionInt{ animation_position, 1 }; return GridPosition{ animation_position, 1 };
} }
void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_delta, bool dead) { void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_delta, bool dead) {

View File

@ -10,8 +10,8 @@
class PacManAnimation { class PacManAnimation {
public: public:
[[nodiscard]] PositionInt animationFrame(Direction direction) const; [[nodiscard]] GridPosition animationFrame(Direction direction) const;
[[nodiscard]] PositionInt deathAnimationFrame(Direction direction) const; [[nodiscard]] GridPosition deathAnimationFrame(Direction direction) const;
void updateAnimationPosition(std::chrono::milliseconds time_delta, bool dead); void updateAnimationPosition(std::chrono::milliseconds time_delta, bool dead);
void pause(); void pause();
@ -19,8 +19,8 @@ public:
private: private:
uint8_t animation_position = 0; uint8_t animation_position = 0;
double animation_position_delta = 0.0; double animation_position_delta = 0.0;
const PositionInt down_animation[4]{ Atlas::pacman_down_wide, Atlas::pacman_down_narrow, Atlas::pacman_closed, Atlas::pacman_down_narrow }; const GridPosition down_animation[4]{ Atlas::pacman_down_wide, Atlas::pacman_down_narrow, Atlas::pacman_closed, Atlas::pacman_down_narrow };
const PositionInt left_animation[4]{ Atlas::pacman_left_wide, Atlas::pacman_left_narrow, Atlas::pacman_closed, Atlas::pacman_left_narrow }; const GridPosition left_animation[4]{ Atlas::pacman_left_wide, Atlas::pacman_left_narrow, Atlas::pacman_closed, Atlas::pacman_left_narrow };
const PositionInt right_animation[4]{ Atlas::pacman_right_wide, Atlas::pacman_right_narrow, Atlas::pacman_closed, Atlas::pacman_right_narrow }; const GridPosition right_animation[4]{ Atlas::pacman_right_wide, Atlas::pacman_right_narrow, Atlas::pacman_closed, Atlas::pacman_right_narrow };
const PositionInt up_animation[4]{ Atlas::pacman_up_wide, Atlas::pacman_up_narrow, Atlas::pacman_closed, Atlas::pacman_up_narrow }; const GridPosition up_animation[4]{ Atlas::pacman_up_wide, Atlas::pacman_up_narrow, Atlas::pacman_closed, Atlas::pacman_up_narrow };
}; };

View File

@ -7,17 +7,17 @@ class Pellets {
public: public:
explicit Pellets(const Board & board); explicit Pellets(const Board & board);
[[nodiscard]] PositionInt currentSprite() const { [[nodiscard]] GridPosition currentSprite() const {
return sprite; return sprite;
}; };
[[nodiscard]] std::vector<PositionInt> currentPositions() const { [[nodiscard]] std::vector<GridPosition> currentPositions() const {
return positions; return positions;
} }
bool eatPelletAtPosition(Position p); bool eatPelletAtPosition(Position p);
private: private:
const PositionInt sprite = { 1, 9 }; const GridPosition sprite = { 1, 9 };
std::vector<PositionInt> positions; std::vector<GridPosition> positions;
}; };

View File

@ -7,7 +7,7 @@ struct Position {
double y; double y;
}; };
struct PositionInt { struct GridPosition {
int x; int x;
int y; int y;
}; };
@ -16,7 +16,7 @@ using Rect = sf::Rect<int>;
using Sprite = sf::Sprite; using Sprite = sf::Sprite;
inline bool operator==(const PositionInt & b, const Position & a) { inline bool operator==(const GridPosition & b, const Position & a) {
return a.x == b.x && a.y == b.y; return a.x == b.x && a.y == b.y;
} }

View File

@ -7,17 +7,17 @@ class SuperPellets {
public: public:
explicit SuperPellets(const Board & board); explicit SuperPellets(const Board & board);
[[nodiscard]] PositionInt currentSprite() const { [[nodiscard]] GridPosition currentSprite() const {
return sprite; return sprite;
} }
[[nodiscard]] std::vector<PositionInt> currentPositions() const { [[nodiscard]] std::vector<GridPosition> currentPositions() const {
return positions; return positions;
} }
bool eatPelletAtPosition(Position p); bool eatPelletAtPosition(Position p);
private: private:
const PositionInt sprite = { 0, 9 }; const GridPosition sprite = { 0, 9 };
std::vector<PositionInt> positions; std::vector<GridPosition> positions;
}; };