forked from buffolu/project_3_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thug.cpp
79 lines (56 loc) · 2.19 KB
/
Thug.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
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
//
// Created by igor on 24/06/2023.
//
#include "Thug.h"
Thug::Thug(const std::string &name_, Point &position): Agent(name_,position,0,5){
}
//attack
void Thug::attack(std::shared_ptr<Peasant>& _peasant,const std::shared_ptr<std::vector<std::shared_ptr<Agent>>>& agents_) {
if(getState() == DEAD || _peasant->getState() == DEAD) {//thug or peasant are dead
std::cerr << "ERROR: peasant or Thug are not alive anymore\n.";
return;
}
else{
//2 necessary conditions for attack
if (Point::distance(_peasant->getLocation(), getLocation()) <= 1 && //peasant is close enough
!check_for_knight(agents_)) //no knights around
{
//initiate attack !
if (getHealth() > _peasant->getHealth()) //attack successful
{
_peasant->setState(STOPPED);
_peasant->setCarriedCrates(0);
setHealth(getHealth() + 1);
std::cout<<"Thug: "<<getName()<<", successfully attacks Peasant: "<<_peasant->getName()<<std::endl;
}
else {
setHealth(getHealth() - 1); //attack unsuccessful
std::cout<<"Thug: "<<getName()<<", unsuccessfully attacks Peasant: "<<_peasant->getName()<<std::endl;
}
//attack completed !
_peasant->setHealth(_peasant->getHealth() - 1);
stop();
return;
}
//one of the conditions is not satisfied - //attack unsuccessful.
else{
setHealth(getHealth() - 1);
std::cout<<"Thug: "<<getName()<<", unsuccessfully attacks Peasant: "<<_peasant->getName()<<std::endl;
stop();
}
}}
void Thug::update() {
Agent::update();
}
void Thug::broadcast_current_state() const noexcept {
Agent::broadcast_current_state();
std::cout<<std::endl;
}
bool Thug::check_for_knight(const std::shared_ptr<std::vector<std::shared_ptr<Agent>>>& agents) {
return std::any_of(agents->begin(),agents->end(),[this](const std::shared_ptr<Agent>& agent)
{
return (std::dynamic_pointer_cast<Knight>(agent) &&
Point::distance(getLocation(),agent->getLocation()) <= 2.5)
;})
;
}