d03 ex01 refondus avec meilleurs gestion de la classe de base
This commit is contained in:
@@ -1,88 +1,58 @@
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
/*
|
||||
* statics
|
||||
*/
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
int ClapTrap::_totalNumber = 0;
|
||||
|
||||
/*
|
||||
* assign values for default or secondary constructors
|
||||
*/
|
||||
|
||||
void ClapTrap::assignValues( ClapTrap & src ) {
|
||||
src._class = "ClapTrap";
|
||||
src._hit = 10;
|
||||
src._energy = 10;
|
||||
src._attack = 1;
|
||||
src._number = getNumber();
|
||||
}
|
||||
|
||||
/*
|
||||
* default/parametric constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( void ) {
|
||||
assignValues(*this);
|
||||
std::cout << "claptrap default creation without name\n";
|
||||
ClapTrap::ClapTrap( std::string name ) {
|
||||
ClapTrap::_increaseNumber();
|
||||
_name = name;
|
||||
_class = _dClass;
|
||||
_hit = _dHit;
|
||||
_energy = _dEnergy;
|
||||
_attack = _dAttack;
|
||||
_number = getNumber();
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " created\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* destructor
|
||||
*/
|
||||
|
||||
ClapTrap::~ClapTrap( void ) {
|
||||
std::cout << _class << " " << _name << " destructed\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* copy constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( ClapTrap const & src ) {
|
||||
*this = src;
|
||||
std::cout << _class << " " << _name << " copied\n";
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " copied from " << src._class << "-" << src._name << "-" << src._number << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* assignement operator
|
||||
*/
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
ClapTrap::~ClapTrap( void ) {
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " destructed\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* OPERATORS
|
||||
*********************************************/
|
||||
|
||||
ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
|
||||
|
||||
if ( this != &rhs )
|
||||
{
|
||||
ClapTrap::_increaseNumber();
|
||||
_number = getNumber();
|
||||
_class = "ClapTrap";
|
||||
_hit = rhs.getHit();
|
||||
_energy = rhs.getEnergy();
|
||||
_attack = rhs.getAttack();
|
||||
_name = rhs.getName();
|
||||
}
|
||||
|
||||
std::cout << _class << " " << _name << " assigned\n";
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " assigned\n";
|
||||
return *this;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* parameters constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( std::string name ) : _name(name) {
|
||||
ClapTrap::_increaseNumber();
|
||||
assignValues(*this);
|
||||
std::cout << _class << " " << _name << " named creation with number " << _number << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* getters
|
||||
*/
|
||||
/*********************************************
|
||||
* ACCESSORS
|
||||
*********************************************/
|
||||
|
||||
std::string ClapTrap::getName() const {return _name;}
|
||||
std::string ClapTrap::getClass() const {return _class;}
|
||||
@@ -92,9 +62,9 @@ int ClapTrap::getAttack() const {return _attack;}
|
||||
int ClapTrap::getNumber() const {return ClapTrap::_totalNumber;}
|
||||
void ClapTrap::_increaseNumber() {ClapTrap::_totalNumber++;}
|
||||
|
||||
/*
|
||||
* robots
|
||||
*/
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void ClapTrap::attack(const std::string & target) {
|
||||
|
||||
@@ -132,20 +102,15 @@ void ClapTrap::takeDamage(unsigned int amount) {
|
||||
state << B_CYAN "[" B_GREEN << _class[0] << _number << B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
|
||||
action << _class << " " << _name;
|
||||
|
||||
if (_energy > 0 && _hit > 0)
|
||||
if (_hit > 0)
|
||||
{
|
||||
_hit -= amount;
|
||||
if (_hit < 0)
|
||||
_hit = 0;
|
||||
action << " looses " B_YELLOW << amount << RESET << " hit points" << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_energy <= 0)
|
||||
action << " cannot take damage because " B_RED " is out of energy" RESET "\n";
|
||||
else if (_hit <= 0)
|
||||
action << " cannot take damage because " B_RED " is out of hit" RESET "\n";
|
||||
}
|
||||
else if (_hit <= 0)
|
||||
action << " cannot take damage because " B_RED " is out of hit" RESET "\n";
|
||||
|
||||
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
|
||||
std::cout << state.str() << action.str();
|
||||
@@ -178,3 +143,16 @@ void ClapTrap::beRepaired(unsigned int amount) {
|
||||
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
|
||||
std::cout << state.str() << action.str();
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* STATICS
|
||||
*********************************************/
|
||||
|
||||
int ClapTrap::_totalNumber = 0;
|
||||
|
||||
std::string const ClapTrap::_dName = "robot";
|
||||
std::string const ClapTrap::_dClass = "ClapTrap";
|
||||
int const ClapTrap::_dHit = 10;
|
||||
int const ClapTrap::_dEnergy = 10;
|
||||
int const ClapTrap::_dAttack = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user