Simplify Ghost::currentSprite
This commit is contained in:
parent
a0fa2b52bb
commit
a62d668ead
3 changed files with 31 additions and 16 deletions
|
@ -1,8 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include "Direction.hpp"
|
||||
#include "Position.hpp"
|
||||
#include "assert.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace Atlas {
|
||||
|
||||
enum class Ghost {
|
||||
|
@ -74,4 +77,16 @@ namespace Atlas {
|
|||
x++;
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
constexpr PositionInt initialFrightened(int animationIndex) {
|
||||
return (animationIndex % 2) == 0 ? Atlas::ghost_frightened2 : Atlas::ghost_frightened1;
|
||||
}
|
||||
|
||||
constexpr PositionInt endingFrightened(int animationIndex) {
|
||||
std::array<PositionInt, 4> positions = { Atlas::ghost_frightened1,
|
||||
Atlas::ghost_frightened2,
|
||||
Atlas::ghost_frightened3,
|
||||
Atlas::ghost_frightened4 };
|
||||
return positions[animationIndex];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "Ghost.hpp"
|
||||
#include <cmath>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
|
||||
Ghost::Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget)
|
||||
: spritesSet(spritesSet),
|
||||
|
@ -42,17 +42,14 @@ void Ghost::reset() {
|
|||
switch (state) {
|
||||
default:
|
||||
return Atlas::ghostSprite(spritesSet, direction, (animationIndex % 2) == 0);
|
||||
case State::Frightened:
|
||||
if (timeFrighten < 3500)
|
||||
return (animationIndex % 2) == 0 ? Atlas::ghost_frightened2 : Atlas::ghost_frightened1;
|
||||
return std::array<PositionInt, 4>{{ Atlas::ghost_frightened1,
|
||||
Atlas::ghost_frightened2,
|
||||
Atlas::ghost_frightened3,
|
||||
Atlas::ghost_frightened4 }}[animationIndex];
|
||||
case State::Eyes:
|
||||
return Atlas::eyeSprite(direction);
|
||||
case State::Frightened:
|
||||
if (timeFrighten < 3500)
|
||||
return Atlas::initialFrightened(animationIndex);
|
||||
else
|
||||
return Atlas::endingFrightened(animationIndex);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Position Ghost::position() const {
|
||||
|
@ -64,7 +61,7 @@ Position Ghost::positionInGrid() const {
|
|||
}
|
||||
|
||||
void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) {
|
||||
if (state == State::Eyes && board.isInPen(positionInGrid()))
|
||||
if (state == State::Eyes && isInPen(board))
|
||||
state = State::Scatter;
|
||||
|
||||
if (state == State::Frightened) {
|
||||
|
@ -77,6 +74,10 @@ void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) {
|
|||
updatePosition(time_delta, board);
|
||||
}
|
||||
|
||||
bool Ghost::isInPen(const Board & board) const {
|
||||
return board.isInPen(positionInGrid());
|
||||
}
|
||||
|
||||
void Ghost::updatePosition(std::chrono::milliseconds time_delta, const Board & board) {
|
||||
updateDirection(board);
|
||||
|
||||
|
@ -124,12 +125,10 @@ void Ghost::updateDirection(const Board & board) {
|
|||
};
|
||||
|
||||
auto [x, y] = cell;
|
||||
std::array<NewDirection, 4> directions = {{
|
||||
NewDirection{ Direction::UP, { x, y - 1 }, 0 },
|
||||
NewDirection{ Direction::LEFT, { x - 1, y }, 0 },
|
||||
NewDirection{ Direction::DOWN, { x, y + 1 }, 0 },
|
||||
NewDirection{ Direction::RIGHT, { x + 1, y }, 0 }
|
||||
}};
|
||||
std::array<NewDirection, 4> directions = { { NewDirection{ Direction::UP, { x, y - 1 }, 0 },
|
||||
NewDirection{ Direction::LEFT, { x - 1, y }, 0 },
|
||||
NewDirection{ Direction::DOWN, { x, y + 1 }, 0 },
|
||||
NewDirection{ Direction::RIGHT, { x + 1, y }, 0 } } };
|
||||
const Position target = this->target(board);
|
||||
|
||||
for (auto && d : directions) {
|
||||
|
|
|
@ -49,6 +49,7 @@ protected:
|
|||
Position startingPosition;
|
||||
Position scatterTarget;
|
||||
Position lastIntersection = { -1, -1 };
|
||||
bool isInPen(const Board & board) const;
|
||||
};
|
||||
|
||||
class Blinky : public Ghost {
|
||||
|
|
Loading…
Reference in a new issue