-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.cpp
More file actions
72 lines (59 loc) · 1.75 KB
/
Copy pathEntity.cpp
File metadata and controls
72 lines (59 loc) · 1.75 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
#pragma once
#include "Object.hpp"
#include "Utility.hpp"
#include "Entity.hpp"
#include "Projectile.hpp"
#include <vector>
#include <string>
#include <utility>
#include <stdexcept>
Entity::Entity(char sym, int maxHp, int dmg, int id, util::Point pos) : Object(sym, id, pos), maxHp(maxHp), damage(dmg), hp(maxHp) {}
int Entity::getHp() const {
return hp;
}
void Entity::setHp(int hp) {
this->hp = hp;
}
int Entity::getMaxHp() const {
return maxHp;
}
void Entity::setMaxHp(int hp) {
this->maxHp = hp;
}
int Entity::getDmg() const {
return damage;
}
void Entity::setDmg(int dmg) {
this->damage = dmg;
}
void Entity::interact(Projectile& obj, util::GameInfo& game) {
this->getHit(obj.getDmg(), game);
Object::interact(obj, game);
}
void Entity::getHit(int dmg, util::GameInfo& game) {
hp -= dmg;
if (hp <= 0 && enabled) {
game[pos] = '.';
enabled = false;
pos = util::Point(-1, -1);
}
hp = std::min(hp, maxHp);
}
Object& Entity::findCollision(util::Point pos, util::GameInfo& game) const {
auto it = std::find_if(game.entities.begin(), game.entities.end(), [pos](std::unique_ptr<Entity>& el) {
return el->getPos() == pos;
});
if (it != game.entities.end())
return *it->get();
auto itp = std::find_if(game.projectiles.begin(), game.projectiles.end(), [pos](std::unique_ptr<Entity>& el) {
return el->getPos() == pos;
});
if (itp != game.projectiles.end())
return *itp->get();
auto iti = std::find_if(game.items.begin(), game.items.end(), [pos](std::unique_ptr<Object>& el) {
return el->getPos() == pos;
});
if (iti == game.items.end())
throw std::runtime_error("Bad collision happened");
return *iti->get();
}