-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.cc
96 lines (81 loc) · 2.05 KB
/
enemy.cc
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 "player.h"
#include "merchant.h"
#include <cmath>
#include <vector>
#include <sstream>
//return atk
int Enemy::getAtk(){
return atk;
}
//return def
int Enemy::getDef(){
return def;
}
//return if hp is less than or equal to 0
bool Enemy::isDead(){
return hp <= 0;
}
//set this->hp to hp
void Enemy::setHP(int hp){
if (hp < 0) hp = 0;
this->hp = hp;
}
//return hp
int Enemy::getHP(){
return hp;
}
//destructor
Enemy::~Enemy(){}
//randomly moves enemy to a neighbour if possible
void Enemy::Move(){
std::vector<Cell *> temp;
//check all cells that are free
for(int i = 0; i < 8; i++){
if(cell->getNeighbours()[i]->getBaseType() == '.' && cell->getNeighbours()[i]->getObject() == NULL){
temp.push_back(cell->getNeighbours()[i]);
}
}
if(temp.size() == 0){
return;
}
cell->removeObject();
cell = temp.at(game->rng(temp.size()));
row = cell->getRow();
col = cell->getCol();
cell->setObject(this);
}
//defend from player attack, have 50% chance to dodge if enemy
//is halfling, otherwise take damage
bool Enemy::Defend(Player *player){
if(type == 'L'){
if(game->rng(2) == 1){
player->atkEnemy(this);
return true;
} else {
std::ostringstream oss;
oss << "PC missed " << type << ".";
std::string s = oss.str();
game->updateAction(s);
return false;
}
}
else{
if (type == 'M') Merchant::aggressive = true;
player->atkEnemy(this);
return true;
}
}
//calculate how much damage player takes, if player health
//is below 0, end game.
void Enemy::atkPlayer(Player *player){
int damage = ceil(((100/float(100+ player->getDef()))) * getAtk());
player->setHP(player->getHP() - damage);
std::ostringstream oss;
oss << getType() << " deals " << damage << " to PC.";
std::string s = oss.str();
game->updateAction(s);
if(player->getHP() <= 0){
game->gameOver();
}
}