-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.cpp
More file actions
96 lines (77 loc) · 2.76 KB
/
Copy pathentity.cpp
File metadata and controls
96 lines (77 loc) · 2.76 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
#include "entity.h"
#include "game_map.h"
Entity::Entity(EntityType entityType, int start_x, int start_y, int enemy_health, int enemy_damage, bool skipNextTurn)
: position(start_x, start_y), health(enemy_health), damage(enemy_damage),
skipNextTurn(skipNextTurn), type(entityType) {
}
Position& Entity::getPosition() {
return position;
}
const Position& Entity::getPosition() const {
return position;
}
int Entity::getHealth() const { return health; }
int Entity::getDamage() const { return damage; }
int Entity::getLevel() const { return level; }
void Entity::levelUp() { level++; }
bool Entity::isAlive() const { return health > 0; }
void Entity::takeDamage(int amount) {
health -= amount;
if (health < 0) health = 0;
}
std::string Entity::getStatusString() const{
return "HP: " + std::to_string(health) + " | " + "DMG: " + std::to_string(damage);
}
void Entity::makeMove(GameMap& map, Position targetPos) {
if (skipNextTurn) {
setSkipTurn(false);
return;
}
int dx = targetPos.getX() - position.getX();
int dy = targetPos.getY() - position.getY();
Position moves[2] = {position, position};
if (abs(dx) > abs(dy)) {
moves[0] = Position(position.getX() + (dx > 0 ? 1 : -1), position.getY());
moves[1] = Position(position.getX(), position.getY() + (dy > 0 ? 1 : -1));
} else {
moves[0] = Position(position.getX(), position.getY() + (dy > 0 ? 1 : -1));
moves[1] = Position(position.getX() + (dx > 0 ? 1 : -1), position.getY());
}
Position new_pos = position;
bool canMove = false;
for (int i = 0; i < 2; i++) {
if (map.isPositionValid(moves[i])) {
MapCell& cell = map.getCell(moves[i]);
if (!cell.isUsed() && cell.getType() != MapCell::Type::WALL) {
new_pos = moves[i];
canMove = true;
break;
}
}
}
if (canMove) {
MapCell& old_cell = map.getCell(position);
MapCell& new_cell = map.getCell(new_pos);
old_cell.setEntity(nullptr);
old_cell.setUsed(false);
position = new_pos;
new_cell.setEntity(this);
new_cell.setUsed(true);
if( new_cell.getType() == MapCell::Type::TRAP){
this->takeDamage(new_cell.getTrapDamage());
new_cell.setType(MapCell::Type::EMPTY);
}
else if (new_cell.getType() == MapCell::Type::LOW) {
setSkipTurn(true);
}
}
}
bool Entity::canAttack(Position targetPos){
if (skipNextTurn) {
setSkipTurn(false);
return false;
}
int dx = abs(position.getX() - targetPos.getX());
int dy = abs(position.getY() - targetPos.getY());
return (dx <= 1 && dy <= 1) && (dx + dy == 1);
}