Files
cpp03/ex02/ClapTrap.cpp
T
2026-09-06 18:54:24 +02:00

74 lines
2.4 KiB
C++

#include "ClapTrap.hpp"
ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
{
std::cout << "ClapTrap Default Constructor called" << std::endl;
}
ClapTrap::ClapTrap(const ClapTrap &copy)
{
std::cout << "ClapTrap Copy Constructor called" << std::endl;
*this = copy;
}
ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
{
std::cout << "ClapTrap Constructor called" <<std::endl;
}
ClapTrap::~ClapTrap()
{
std::cout << "ClapTrap Deconstructor" << std::endl;
}
ClapTrap &ClapTrap::operator=(const ClapTrap &src)
{
this->_name = src._name;
this->_hit_pts = src._hit_pts;
this->_energy_pts = src._energy_pts;
this->_attack_dmg = src._attack_dmg;
return *this;
}
void ClapTrap::takeDamage(unsigned int amount)
{
if (this->_hit_pts > amount)
this->_hit_pts -= amount;
else if (this->_hit_pts > 0)
this->_hit_pts = 0;
else
{
std::cout << "ClapTrap " << this->_name << " is already dead" << std::endl;
return ;
}
std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hp, he now has " << this->_hit_pts << " hp." << std::endl;
}
void ClapTrap::beRepaired(unsigned int amount)
{
if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10)
{
this->_hit_pts += amount;
std::cout << "ClapTrap " << this->_name << " repaired itself and get " << amount << " hit points, he has " << this->_hit_pts << " hp" << std::endl;
this->_energy_pts--;
}
else if (this->_energy_pts == 0)
std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough energy" << std::endl;
else if (this->_hit_pts == 0)
std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough hit points." << std::endl;
else
std::cout << "ClapTrap " << this->_name << " can't be repaired to have more than 10 hit points." << std::endl;
}
void ClapTrap::attack(const std::string &target)
{
if (this->_energy_pts > 0 && this->_hit_pts > 0)
{
std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl;
this->_energy_pts--;
}
else if (this->_energy_pts == 0)
std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl;
else
std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl;
}