-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInky.cpp
More file actions
124 lines (102 loc) · 4.93 KB
/
Inky.cpp
File metadata and controls
124 lines (102 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "Inky.hpp"
#include "Pacman.hpp"
Inky::Inky(Grid* grid)
: Ghost(sf::IntRect({ 457, 97 }, { 14, 14 }), { 11.5 * 24, 17.5 * 24 }, GHOST_START_SPEED, DirectionEnum::NONE, grid, & grid->getGrid()[14][14], & grid->getGrid()[29][26])
{
// BASE ANIMATION
//NONE ( Wont be used, but needs all directions defined )
Animation noneAnimation = Animation(&getSprite());
noneAnimation.addFrame({ sf::IntRect({457, 97}, {14, 14}), 0.1 });
noneAnimation.addFrame({ sf::IntRect({473, 97}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::BASE, DirectionEnum::NONE, noneAnimation);
animationController.addStateAnimation(GhostState::CHASE, DirectionEnum::NONE, noneAnimation);
animationController.addStateAnimation(GhostState::SCATTER, DirectionEnum::NONE, noneAnimation);
//RIGHT
Animation rightAnimation = Animation(&getSprite());
rightAnimation.addFrame({ sf::IntRect({457, 97}, {14, 14}), 0.1 });
rightAnimation.addFrame({ sf::IntRect({473, 97}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::BASE, DirectionEnum::RIGHT, rightAnimation);
animationController.addStateAnimation(GhostState::CHASE, DirectionEnum::RIGHT, rightAnimation);
animationController.addStateAnimation(GhostState::SCATTER, DirectionEnum::RIGHT, rightAnimation);
//LEFT
Animation leftAnimation = Animation(&getSprite());
leftAnimation.addFrame({ sf::IntRect({489, 97}, {14, 14}), 0.1 });
leftAnimation.addFrame({ sf::IntRect({505, 97}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::BASE, DirectionEnum::LEFT, leftAnimation);
animationController.addStateAnimation(GhostState::CHASE, DirectionEnum::LEFT, leftAnimation);
animationController.addStateAnimation(GhostState::SCATTER, DirectionEnum::LEFT, leftAnimation);
//UP
Animation upAnimation = Animation(&getSprite());
upAnimation.addFrame({ sf::IntRect({521, 97}, {14, 14}), 0.1 });
upAnimation.addFrame({ sf::IntRect({537, 97}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::BASE, DirectionEnum::UP, upAnimation);
animationController.addStateAnimation(GhostState::CHASE, DirectionEnum::UP, upAnimation);
animationController.addStateAnimation(GhostState::SCATTER, DirectionEnum::UP, upAnimation);
//DOWN
Animation downAnimation = Animation(&getSprite());
downAnimation.addFrame({ sf::IntRect({553, 97}, {14, 14}), 0.1 });
downAnimation.addFrame({ sf::IntRect({569, 97}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::BASE, DirectionEnum::DOWN, downAnimation);
animationController.addStateAnimation(GhostState::CHASE, DirectionEnum::DOWN, downAnimation);
animationController.addStateAnimation(GhostState::SCATTER, DirectionEnum::DOWN, downAnimation);
}
void Inky::updateTarget(Pacman& pacman, std::vector<Ghost*> ghosts)
{
Tile* newTile = targetTile, * oldTile = targetTile;
if (state == GhostState::DEAD) {
newTile = homeTile;
}
else if (state == GhostState::SCATTER) {
newTile = scatterTile;
}
else {
// Inkys tactic
/*To determine Inky's target, we must first establish an intermediate offset two tiles in front of Pac-Man,
in the direction he is moving (represented by the tile bracketed in green above).
Now imagine drawing a vector from the center of the red ghost's current tile to the center of the offset tile,
then double the vector length by extending it out just as far again beyond the offset tile.
*/
static DirectionEnum lastDirection;
DirectionEnum pacmanDirection = pacman.getDirection();
// Use last non-NONE direction when stuck in wall
if (pacman.getDirection() == DirectionEnum::NONE) {
pacmanDirection = lastDirection;
}
else {
lastDirection = pacmanDirection;
}
Tile* blinkyTile = ghosts[int(GhostArray::BLINKY)]->getCurrentTile();
int interRow = pacman.getCurrentTile()->getRow();
int interCol = pacman.getCurrentTile()->getCol();
if (pacmanDirection == DirectionEnum::LEFT) {
interCol -= 2;
}
if (pacmanDirection == DirectionEnum::RIGHT) {
interCol += 2;
}
if (pacmanDirection == DirectionEnum::DOWN) {
interRow += 2;
}
if (pacmanDirection == DirectionEnum::UP) {
interRow -= 2;
}
interRow = std::clamp(interRow, 0, grid->getRowCnt() - 1);
interCol = std::clamp(interCol, 0, grid->getColCnt() - 1);
// Get Inter -> B and use reverse values for Inter -> T (Target)
int interToBRow = blinkyTile->getRow() - interRow;
int interToBCol = blinkyTile->getCol() - interCol;
int interToTargetRow = interToBRow * -1;
int interToTargetCol = interToBCol * -1;
int targetRow = interRow + interToTargetRow;
int targetCol = interCol + interToTargetCol;
targetRow = std::clamp(targetRow, 0, grid->getRowCnt() - 1);
targetCol = std::clamp(targetCol, 0, grid->getColCnt() - 1);
newTile = &grid->getGrid()[targetRow][targetCol];
}
// Apply changes only if needed
if (oldTile != newTile) {
oldTile->getRectangle().setFillColor(sf::Color::Transparent);
this->targetTile = newTile;
targetTile->getRectangle().setFillColor(sf::Color(70, 191, 238));
}
}