-
Notifications
You must be signed in to change notification settings - Fork 2
/
Enemy.cpp
102 lines (79 loc) · 1.49 KB
/
Enemy.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
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
#include "Enemy.h"
Enemy::Enemy(string name, char tile, int level, int attack, int defense, int health, int xp)
{
_name = name;
_tile = tile;
_level = level;
_attack = attack;
_defense = defense;
_health = health;
_experienceValue = xp;
}
void Enemy::setPosition(int x, int y) {
_x = x;
_y = y;
}
void Enemy::getPosition(int &x, int &y) {
x = _x;
y = _y;
}
int Enemy::attack() {
static default_random_engine randomEngine(time(NULL));
uniform_int_distribution<int> attackRoll(0, _attack);
return attackRoll(randomEngine);
}
int Enemy::takeDamage(int attack) {
attack -= _defense;
//Cjecl of the attack does damage
if (attack > 0) {
_health -= attack;
//Check if he died
if (_health <= 0) {
return _experienceValue;
}
}
return 0;
}
char Enemy::getMove(int playerX, int playerY) {
static default_random_engine randomEngine(time(NULL));
uniform_int_distribution<int> moveRoll(0, 6);
int distance;
int dx = _x - playerX;
int dy = _y - playerY;
int adx = abs(dx);
int ady = abs(dy);
distance = adx + ady;
//You can change 5 if you want
if (distance <= 5) {
//Moving along X axis
if (adx > ady) {
if (dx > 0) {
return 'a';
}
else {
return 'd';
}
}
else { //Move along Y axis
if (dy > 0) {
return 'w';
}
else {
return 's';
}
}
}
int randomMove = moveRoll(randomEngine);
switch (randomMove) {
case 0:
return 'a';
case 1:
return 'w';
case 2:
return 's';
case 3:
return 'd';
default:
return '.';
}
}