-
Notifications
You must be signed in to change notification settings - Fork 0
/
Infantry.cpp
51 lines (41 loc) · 1.25 KB
/
Infantry.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
// Copyright 2021 James Pretorius
#include "./Infantry.h"
Infantry::Infantry(char symbol, Sim* sim, int x, int y)
: Unit(symbol, sim, x, y, attackRange) {
this->symbol = symbol;
this->sim = sim;
this->x = x;
this->y = y;
}
float Infantry::getDefense(const Unit* attacker) {
float defense = .5;
// increase defense when next to other infantry
for (auto ally : getAllies()) {
if (ally->getSymbol() == symbol && distanceTo(ally) == 1) {
defense *= 1.2;
}
}
char attackerType = tolower(attacker->getSymbol());
if (attackerType == sim->ARCHERS_LETTER) defense *= 1.3;
if (attackerType == sim->CALVARY_LETTER) defense *= 0.7;
return defense;
}
void Infantry::takeHit() { isHit = true; }
void Infantry::takeTurn() {
if (!isAlive) return;
for (int i = 0; i < 2; i++) {
Unit* enemy = closestEnemy();
if (closestEnemy() == nullptr) return;
if (distanceTo(enemy) > attackRange) {
*this >> enemy;
break;
} else if (distanceTo(enemy) <= attackRange) {
attack(enemy);
enemy = closestEnemy();
}
}
if (isHit) {
printf("[unit] %c (%i, %i) fell\n", symbol, x, y);
isAlive = false;
}
}