-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClapTrap.cpp
109 lines (93 loc) · 3.51 KB
/
ClapTrap.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wcorrea- <wcorrea-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/17 14:03:44 by wcorrea- #+# #+# */
/* Updated: 2023/07/24 16:50:09 by wcorrea- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
// --------------------------- CONSTURCTORS ----------------------------------
ClapTrap::ClapTrap(void)
{
std::cout << "ClapTrap default constructor called" << std::endl;
this->_name = "default";
this->_hitPoints = 10;
this->_energyPoints = 10;
this->_attackDamage = 0;
}
ClapTrap::ClapTrap(std::string name)
{
std::cout << "ClapTrap constructor with parameter called" << std::endl;
this->_name = name;
this->_hitPoints = 10;
this->_energyPoints = 10;
this->_attackDamage = 0;
}
ClapTrap::ClapTrap(ClapTrap const ©)
{
std::cout << "ClapTrap copy constructor called" << std::endl;
*this = copy;
}
ClapTrap::~ClapTrap(void)
{
std::cout << "ClapTrap destructor called" << std::endl;
}
// --------------------------- OPERATORS ----------------------------------
ClapTrap &ClapTrap::operator = (ClapTrap const ©)
{
if (this == ©)
return (*this);
this->_name = copy.getName();
this->_hitPoints = copy.getHitPoints();
this->_energyPoints = copy.getEnergyPoints();
this->_attackDamage = copy.getAttackDamage();
return (*this);
}
// --------------------------- GET FUNCTIONS ----------------------------------
std::string ClapTrap::getName(void) const {return (this->_name);}
int ClapTrap::getHitPoints(void) const {return (this->_hitPoints);}
int ClapTrap::getEnergyPoints(void) const {return (this->_energyPoints);}
int ClapTrap::getAttackDamage(void) const {return (this->_attackDamage);}
// --------------------------- MEMBER FUNCTIONS ----------------------------------
bool ClapTrap::isGameOver(void)
{
if (!this->_energyPoints || !this->_hitPoints)
{
std::cout << "ClapTrap " << this->_name << " is out of action!" << std::endl;
return (true);
}
return (false);
}
void ClapTrap::attack(const std::string& target)
{
if (this->isGameOver())
return ;
std::cout << "ClapTrap " << this->_name << " attacks " << target;
std::cout << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
this->_energyPoints--;
}
void ClapTrap::takeDamage(unsigned int amount)
{
this->_hitPoints -= amount;
if (this->_hitPoints < 0)
this->_hitPoints = 0;
std::cout << "ClapTrap " << this->_name << " loose " << amount << " HP!" << std::endl;
}
void ClapTrap::beRepaired(unsigned int amount)
{
if (this->isGameOver())
return ;
this->_hitPoints += amount;
std::cout << "ClapTrap " << this->_name << " is repaired with " << amount << " HP!" << std::endl;
this->_energyPoints--;
}
std::ostream &operator<<(std::ostream &out, ClapTrap const &trap)
{
out << "ClapTrap " << trap.getName() << " has " << trap.getHitPoints() << " HP and ";
out << trap.getEnergyPoints() << " battery life.";
return (out);
}