-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGhost.cpp
More file actions
135 lines (108 loc) · 4.46 KB
/
Ghost.cpp
File metadata and controls
135 lines (108 loc) · 4.46 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Ghost.hpp"
#include <iostream>
Ghost::Ghost(sf::IntRect intRect, sf::Vector2f position, float speed, DirectionEnum direction, Grid* grid, Tile* homeTile, Tile* scatterTile)
: Actor(intRect, position, speed, direction, grid), homeTile(homeTile), targetTile(homeTile), scatterTile(scatterTile){
// Initial state is always SCATTER
state = GhostState::SCATTER;
//FRIGHTENED
Animation animationFrightened = Animation(&getSprite());
animationFrightened.addFrame({ sf::IntRect({585, 65}, {14, 14}), 0.2 });
animationFrightened.addFrame({ sf::IntRect({601, 65}, {14, 14}), 0.2 });
animationController.addStateAnimation(GhostState::FRIGHTENED, DirectionEnum::NONE, animationFrightened);
animationController.addStateAnimation(GhostState::FRIGHTENED, DirectionEnum::RIGHT, animationFrightened);
animationController.addStateAnimation(GhostState::FRIGHTENED, DirectionEnum::LEFT, animationFrightened);
animationController.addStateAnimation(GhostState::FRIGHTENED, DirectionEnum::UP, animationFrightened);
animationController.addStateAnimation(GhostState::FRIGHTENED, DirectionEnum::DOWN, animationFrightened);
//DEAD
//NONE ( Wont be used, but needs all directions defined )
Animation noneAnimation = Animation(&getSprite());
noneAnimation.addFrame({ sf::IntRect({585, 81}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::DEAD, DirectionEnum::NONE, noneAnimation);
//RIGHT
Animation rightAnimation = Animation(&getSprite());
rightAnimation.addFrame({ sf::IntRect({585, 81}, {14, 14}), 0.1 });
rightAnimation.addFrame({ sf::IntRect({585, 81}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::DEAD, DirectionEnum::RIGHT, rightAnimation);
//LEFT
Animation leftAnimation = Animation(&getSprite());
leftAnimation.addFrame({ sf::IntRect({601, 81}, {14, 14}), 0.1 });
leftAnimation.addFrame({ sf::IntRect({601, 81}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::DEAD, DirectionEnum::LEFT, leftAnimation);
//UP
Animation upAnimation = Animation(&getSprite());
upAnimation.addFrame({ sf::IntRect({617, 81}, {14, 14}), 0.1 });
upAnimation.addFrame({ sf::IntRect({617, 81}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::DEAD, DirectionEnum::UP, upAnimation);
//DOWN
Animation downAnimation = Animation(&getSprite());
downAnimation.addFrame({ sf::IntRect({633, 81}, {14, 14}), 0.1 });
downAnimation.addFrame({ sf::IntRect({633, 81}, {14, 14}), 0.1 });
animationController.addStateAnimation(GhostState::DEAD, DirectionEnum::DOWN, downAnimation);
}
void Ghost::updateNextDirection()
{
// So it doesnt override itself upon entering tile before doing the turn
if (queuedDirection != DirectionEnum::NONE) {
return;
}
Tile* nextTile = grid->nextTile(currentTile, direction);
std::vector<DirectionEnum> allDirections = { DirectionEnum::UP, DirectionEnum::DOWN, DirectionEnum::LEFT, DirectionEnum::RIGHT };
std::vector<std::pair<DirectionEnum, float>> potentialDirections;
std::vector<std::pair<DirectionEnum, float>> legalDirections;
// Get potential directions
for (auto x : allDirections) {
if (x != getReverse(direction)) {
potentialDirections.push_back({x, 0});
}
}
// Calculate distances
for (auto& potDir : potentialDirections) {
Tile* tile = grid->nextTile(nextTile, potDir.first);
if (tile->isLegal()) {
potDir.second = grid->tileDistance(tile, targetTile, currentTile);
legalDirections.push_back(potDir);
}
}
// Stop if there are no legal paths
if (legalDirections.size() == 0) {
return;
}
// If in frightened state, choose random
if(state == GhostState::FRIGHTENED) {
int randomNumber = std::rand() % legalDirections.size();
queuedDirection = legalDirections[randomNumber].first;
return;
}
// Find shortest distance
auto minIt = std::min_element(
legalDirections.begin(),
legalDirections.end(),
[](const auto& a, const auto& b) {
return a.second < b.second;
}
);
// Set queuedDirection unless its already moving in desired direction
if (minIt->first != direction) {
queuedDirection = minIt->first;
}
}
void Ghost::updateMovement(Game* game)
{
Actor::updateMovement(game);
// Check if reached home base while dead
if (currentTile == homeTile && state == GhostState::DEAD) {
// Revive
state = GhostState::CHASE;
}
}
float Ghost::getSpeed() const
{
switch (state) {
case GhostState::DEAD:
return GHOST_DEAD_SPEED;
case GhostState::FRIGHTENED:
return GHOST_FRIGHT_SPEED;
default:
return GHOST_START_SPEED;
}
}