forked from GianlucaBotteri/CPPVideogame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPC.cpp
More file actions
75 lines (66 loc) · 2.52 KB
/
Copy pathNPC.cpp
File metadata and controls
75 lines (66 loc) · 2.52 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
#include "NPC.h"
void NPC::update() {
sprite.setPosition(rect.getPosition());
}
void NPC::moveSprite(const int *level) {
switch (direction) {
case Direction::Up:
if (collidesUp(level) || outOfbounds(Direction::Up) || collUp) {
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 3, 48, 48));
direction = static_cast<Direction>(generateRandom(4));
collUp = false;
break;
} else {
rect.move(0, -speed);
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 3, 48, 48));
break;
}
case Direction::Down:
if (collidesDown(level) || outOfbounds(Direction::Down) || collDown) {
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 0, 48, 48));
direction = static_cast<Direction>(generateRandom(4));
collDown = false;
break;
} else {
rect.move(0, speed);
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 0, 48, 48));
break;
}
case Direction::Left:
if (collidesLeft(level) || outOfbounds(Direction::Left) || collLeft) {
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 1, 48, 48));
direction = static_cast<Direction>(generateRandom(4));
collLeft = false;
break;
} else {
rect.move(-speed, 0);
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 1, 48, 48));
break;
}
case Direction::Right:
if (collidesRight(level) || outOfbounds(Direction::Right) || collRight) {
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 2, 48, 48));
direction = static_cast<Direction>(generateRandom(4));
collRight = false;
break;
} else {
rect.move(speed, 0);
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 2, 48, 48));
break;
}
}
counterWalking++;
if (counterWalking == 2) {
counterWalking = 0;
}
counter++;
if (counter >= movementLength) {
direction = static_cast<Direction>(generateRandom(6));
counter = 0;
}
}
int NPC::generateRandom(int max) {
int randomNumber = rand();
int random = (randomNumber % (max + 1));
return random;
}