Files
cpp03/ex02/ClapTrap/ClapTrap.cpp
Angel Ortigosa Perez 46b7b99abb Min fixes
2025-10-26 20:20:14 +01:00

102 lines
2.8 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
ClapTrap::ClapTrap() :
name("(NULL)"), health(10),
energy(10), attackDamage(0)
{
std::cout << "Default constructor called" << std::endl;
}
ClapTrap::ClapTrap(std::string name) :
name(name), health(10),
energy(10), attackDamage(0)
{
std::cout << "Default constructor called for "
<< name << std::endl;
}
ClapTrap::ClapTrap(const ClapTrap &target)
{
this->name = target.name;
this->health = target.health;
this->energy = target.energy;
this->attackDamage = target.attackDamage;
}
ClapTrap& ClapTrap::operator=(const ClapTrap &target)
{
if (this != &target)
{
this->name = target.name;
this->health = target.health;
this->energy = target.energy;
this->attackDamage = target.attackDamage;
}
return (*this);
}
ClapTrap::~ClapTrap()
{
std::cout << "Destructor called for "
<< this->name << std::endl;
}
void ClapTrap::attack(const std::string &target)
{
if (this->health > 0 && this->energy > 0)
{
std::cout << "ClapTrap " << this->name
<< " attacks " << target
<< ", causing " << this->attackDamage
<< " points of damage!" << std::endl;
this->energy -= 1;
} else {
std::cout << "ClapTrap " << this->name
<< " can't attack anymore..." << std::endl;
}
}
void ClapTrap::takeDamage(unsigned int amount)
{
std::cout << "ClapTrap " << this->name
<< " receives " << amount
<< " points of damage!" << std::endl;
if (amount >= this->health)
this->health = 0;
else
this->health -= amount;
}
void ClapTrap::beRepaired(unsigned int amount)
{
if (this->health == 0)
{
std::cout << "ClapTrap " << this->name << " can't repair itself because it's destroyed" << std::endl;
return ;
}
if (this->energy == 0)
{
std::cout << "ClapTrap " << this->name << " has no energy left to repair!" << std::endl;
return ;
}
std::cout << "ClapTrap " << this->name
<< " repairs itself, recovering " << amount
<< " hit points!" << std::endl;
this->health += amount;
this->energy -= 1;
}