forked from GianlucaBotteri/CPPVideogame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMob.cpp
More file actions
95 lines (88 loc) · 3.34 KB
/
Mob.cpp
File metadata and controls
95 lines (88 loc) · 3.34 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
#include "Mob.h"
void Mob::update() {
sprite.setPosition(rect.getPosition());
}
void Mob::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, -movementSpeed);
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, movementSpeed);
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(-movementSpeed, 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(movementSpeed, 0);
sprite.setTextureRect(sf::IntRect(counterWalking * 48, 48 * 2, 48, 48));
break;
}
}
counterWalking++;
if (counterWalking == 2) {
counterWalking = 0;
}
if (!isAngry()) {
counter++;
if (counter >= movementLength) {
direction = static_cast<Direction>(generateRandom(6));
counter = 0;
}
}
}
int Mob::generateRandom(int max) {
int randomNumber = rand();
int random = (randomNumber % (max + 1));
return random;
}
bool Mob::outOfbounds(Direction direction) {
switch (direction) {
case Direction::Up:
if (static_cast<int>(rect.getPosition().y - movementSpeed) < 0)
return true;
return false;
case Direction::Down:
if (static_cast<int>(rect.getPosition().y + rect.getSize().y + movementSpeed) > 624)
return true;
return false;
case Direction::Left:
if (static_cast<int>(rect.getPosition().x - movementSpeed) < 153)
return true;
return false;
case Direction::Right:
if (static_cast<int>(rect.getPosition().x + rect.getSize().x + movementSpeed) > 912)
return true;
return false;
}
}