-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClyde.cpp
More file actions
87 lines (71 loc) · 3.68 KB
/
Clyde.cpp
File metadata and controls
87 lines (71 loc) · 3.68 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
#include "Clyde.hpp"
#include "Pacman.hpp"
Clyde::Clyde(Grid* grid)
: Ghost(sf::IntRect({ 457, 113 }, { 14, 14 }), { 15.5 * 24, 17.5 * 24 }, GHOST_START_SPEED, DirectionEnum::NONE, grid, & grid->getGrid()[14][14], &grid->getGrid()[29][1])
{
// BASE ANIMATION
//NONE ( Wont be used, but needs all directions defined )
Animation noneAnimation = Animation(&getSprite());
noneAnimation.addFrame({ sf::IntRect({457, 113}, {14, 14}), 0.1 });
noneAnimation.addFrame({ sf::IntRect({473, 113}, {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, 113}, {14, 14}), 0.1 });
rightAnimation.addFrame({ sf::IntRect({473, 113}, {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, 113}, {14, 14}), 0.1 });
leftAnimation.addFrame({ sf::IntRect({505, 113}, {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, 113}, {14, 14}), 0.1 });
upAnimation.addFrame({ sf::IntRect({537, 113}, {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, 113}, {14, 14}), 0.1 });
downAnimation.addFrame({ sf::IntRect({569, 113}, {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 Clyde::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 {
// Clydes tactic
// If distance betwwen Cylde and Pacman is more than 8 tiles, charge directly, otherwise go to scatter point
float x1 = ghosts[int(GhostArray::CLYDE)]->getCurrentTile()->getRow(), x2 = pacman.getCurrentTile()->getRow();
float y1 = ghosts[int(GhostArray::CLYDE)]->getCurrentTile()->getCol(), y2 = pacman.getCurrentTile()->getCol();
float distance = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
if (distance < 8) {
newTile = scatterTile;
}
else {
newTile = pacman.getCurrentTile();
}
}
// Apply changes only if needed
if (oldTile != newTile) {
oldTile->getRectangle().setFillColor(sf::Color::Transparent);
this->targetTile = newTile;
targetTile->getRectangle().setFillColor(sf::Color(219, 133, 28));
}
}