-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cpp
73 lines (67 loc) · 1.29 KB
/
Entity.cpp
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
#include "Entity.h"
namespace Game {
void Entity::move(Direction direction) {
if (this->isAlive) {
if (this->direction != direction) {
this->direction = direction;
} else {
switch (direction) {
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
default:
break;
}
}
}
}
void Entity::entityCollisionAvoidance(Entity *otherEntity) {
if (otherEntity->isAlive) {
if (this->x == otherEntity->x && this->y == otherEntity->y) {
switch (this->direction) {
case UP:
this->y++;
break;
case DOWN:
this->y--;
break;
case LEFT:
this->x++;
break;
case RIGHT:
this->x--;
break;
default:
break;
}
}
}
}
void Entity::wallCollisionAvoidance(int8_t xMax, int8_t yMax) {
if (this->x < 0) {
this->x = 0;
} else if (this->x >= xMax) {
this->x -= 1;
}
if (this->y < 0) {
this->y = 0;
} else if (this->y >= yMax) {
this->y -= 1;
}
}
void Entity::destroy() {
this->isAlive = false;
this->x = 0;
this->y = 0;
this->direction = UP;
}
} // namespace Game