CPP03/ex02/src/ClapTrap.cpp
2025-04-07 17:43:15 +02:00

94 lines
2.6 KiB
C++

#include "ClapTrap.hpp"
#include <iostream>
ClapTrap::ClapTrap() : _hitpoints(10), _energy_points(10), _attack_damage(0)
{
_name = "Noname";
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << "A nameless ClapTrap is created!" << std::endl;
status();
}
ClapTrap::ClapTrap(std::string name) : _name(name), _hitpoints(10), _energy_points(10), _attack_damage(0)
{
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << name << " is created!" << std::endl;
status();
}
ClapTrap::ClapTrap(ClapTrap const &src)
{
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << _name << " is copied!" << std::endl;
*this = src;
status();
}
ClapTrap::~ClapTrap()
{
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << _name << " is destroyed!" << std::endl;
status();
}
ClapTrap &ClapTrap::operator=(ClapTrap const &rhs)
{
if (this == &rhs)
return *this;
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << rhs._name
<< " is assigned to ClapTrap formely known as " << _name << "!"
<< std::endl;
_name = rhs._name;
_hitpoints = rhs._hitpoints;
_energy_points = rhs._energy_points;
_attack_damage = rhs._attack_damage;
status();
return *this;
}
void ClapTrap::attack(std::string const &target)
{
if (_energy_points < 1)
{
std::cout << COLOR12 << "Claptrap " << COLOR9 << _name << " is too tired to attack!"
<< COLOR_RESET << std::endl;
return;
}
if (_hitpoints < 1)
{
std::cout << COLOR12 << "Claptrap " << COLOR9 << _name << " should be repaired before it can attack!"
<< COLOR_RESET << std::endl;
return;
}
_energy_points -= 1;
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << _name << " attacks " << target << ", causing "
<< _attack_damage << " points of damage!" << std::endl;
status();
}
void ClapTrap::takeDamage(unsigned int amount)
{
_hitpoints -= amount;
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << _name << " takes " << amount
<< " points of damage!" << std::endl;
status();
}
void ClapTrap::beRepaired(unsigned int amount)
{
if (_energy_points < 1)
{
std::cout << COLOR12 << "Claptrap " << COLOR9 << _name << " is too tired to repair!"
<< COLOR_RESET << std::endl;
return;
}
_hitpoints += amount;
_energy_points -= 1;
std::cout << COLOR12 << "Claptrap " << COLOR_RESET << _name << " is repaired for " << amount
<< " points!" << std::endl;
status();
}
void ClapTrap::status() const
{
std::cout << COLOR12 << "Claptrap " << COLOR10 << _name << " has " << _hitpoints << " hitpoints, "
<< _energy_points << " energy points and " << _attack_damage
<< " attack damage!" << COLOR_RESET << std::endl;
}