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
|
#pragma once
|
||||||
|
|
||||||
#include "Direction.hpp"
|
#include "Direction.hpp"
|
||||||
#include "Position.hpp"
|
#include "Position.hpp"
|
||||||
#include "assert.h"
|
#include "assert.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
namespace Atlas {
|
namespace Atlas {
|
||||||
|
|
||||||
enum class Ghost {
|
enum class Ghost {
|
||||||
|
@ -74,4 +77,16 @@ namespace Atlas {
|
||||||
x++;
|
x++;
|
||||||
return { x, y };
|
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 "Ghost.hpp"
|
||||||
#include <cmath>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
Ghost::Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget)
|
Ghost::Ghost(Atlas::Ghost spritesSet, Position startingPosition, Position scatterTarget)
|
||||||
: spritesSet(spritesSet),
|
: spritesSet(spritesSet),
|
||||||
|
@ -42,17 +42,14 @@ void Ghost::reset() {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
default:
|
default:
|
||||||
return Atlas::ghostSprite(spritesSet, direction, (animationIndex % 2) == 0);
|
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:
|
case State::Eyes:
|
||||||
return Atlas::eyeSprite(direction);
|
return Atlas::eyeSprite(direction);
|
||||||
|
case State::Frightened:
|
||||||
|
if (timeFrighten < 3500)
|
||||||
|
return Atlas::initialFrightened(animationIndex);
|
||||||
|
else
|
||||||
|
return Atlas::endingFrightened(animationIndex);
|
||||||
}
|
}
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Position Ghost::position() const {
|
Position Ghost::position() const {
|
||||||
|
@ -64,7 +61,7 @@ Position Ghost::positionInGrid() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) {
|
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;
|
state = State::Scatter;
|
||||||
|
|
||||||
if (state == State::Frightened) {
|
if (state == State::Frightened) {
|
||||||
|
@ -77,6 +74,10 @@ void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) {
|
||||||
updatePosition(time_delta, 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) {
|
void Ghost::updatePosition(std::chrono::milliseconds time_delta, const Board & board) {
|
||||||
updateDirection(board);
|
updateDirection(board);
|
||||||
|
|
||||||
|
@ -124,12 +125,10 @@ void Ghost::updateDirection(const Board & board) {
|
||||||
};
|
};
|
||||||
|
|
||||||
auto [x, y] = cell;
|
auto [x, y] = cell;
|
||||||
std::array<NewDirection, 4> directions = {{
|
std::array<NewDirection, 4> directions = { { NewDirection{ Direction::UP, { x, y - 1 }, 0 },
|
||||||
NewDirection{ Direction::UP, { x, y - 1 }, 0 },
|
NewDirection{ Direction::LEFT, { x - 1, y }, 0 },
|
||||||
NewDirection{ Direction::LEFT, { x - 1, y }, 0 },
|
NewDirection{ Direction::DOWN, { x, y + 1 }, 0 },
|
||||||
NewDirection{ Direction::DOWN, { x, y + 1 }, 0 },
|
NewDirection{ Direction::RIGHT, { x + 1, y }, 0 } } };
|
||||||
NewDirection{ Direction::RIGHT, { x + 1, y }, 0 }
|
|
||||||
}};
|
|
||||||
const Position target = this->target(board);
|
const Position target = this->target(board);
|
||||||
|
|
||||||
for (auto && d : directions) {
|
for (auto && d : directions) {
|
||||||
|
|
|
@ -49,6 +49,7 @@ protected:
|
||||||
Position startingPosition;
|
Position startingPosition;
|
||||||
Position scatterTarget;
|
Position scatterTarget;
|
||||||
Position lastIntersection = { -1, -1 };
|
Position lastIntersection = { -1, -1 };
|
||||||
|
bool isInPen(const Board & board) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Blinky : public Ghost {
|
class Blinky : public Ghost {
|
||||||
|
|
Loading…
Reference in a new issue