-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
96 lines (84 loc) · 1.58 KB
/
Enemy.cpp
File metadata and controls
96 lines (84 loc) · 1.58 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
#include "Enemy.h"
#include <random>
#include <ctime>
using namespace std;
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;
}
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;
if (attack > 0) {
_health -= attack;
if (_health <= 0) {
return _experienceValue;
}
}
return 0;
}
void
Enemy::getPosition(int& x, int& y)
{
x = _x;
y = _y;
}
void
Enemy::setPosition(int x, int y)
{
_x = x;
_y = y;
}
char
Enemy::getMove(int playerX, int playerY)
{
static default_random_engine randomEngine( time( NULL ));
uniform_int_distribution <int> moveRoll( 0, 6 );
int dist;
int dx = _x - playerX;
int dy = _y - playerY;
int adx = abs( dx );
int ady = abs( dy );
dist = adx + ady;
if (dist <= 5) {
if (adx > ady) {
if (dx > 0)
return 'a';
else
return 'd';
}
else {
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 '.';
}
}