d03 ex01 refondus avec meilleurs gestion de la classe de base
This commit is contained in:
235
d03/jodufour/srcs/class/ClapTrap.cpp
Normal file
235
d03/jodufour/srcs/class/ClapTrap.cpp
Normal file
@@ -0,0 +1,235 @@
|
||||
#include "class/ClapTrap.hpp"
|
||||
|
||||
// ************************************************************************** //
|
||||
// Constructors //
|
||||
// ************************************************************************** //
|
||||
|
||||
ClapTrap::ClapTrap(std::string const &name) :
|
||||
_name(name),
|
||||
_hitPoints(ClapTrap::_defaultHitPoints),
|
||||
_energyPoints(ClapTrap::_defaultEnergyPoints),
|
||||
_attackDamages(ClapTrap::_defaultAttackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Creating ClapTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(ClapTrap const &src) :
|
||||
_name(src._name),
|
||||
_hitPoints(src._hitPoints),
|
||||
_energyPoints(src._energyPoints),
|
||||
_attackDamages(src._attackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Creating ClapTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(
|
||||
std::string const &name,
|
||||
unsigned int const hitPoints,
|
||||
unsigned int const energyPoints,
|
||||
unsigned int const attackDamages) :
|
||||
_name(name),
|
||||
_hitPoints(hitPoints),
|
||||
_energyPoints(energyPoints),
|
||||
_attackDamages(attackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Creating ClapTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Destructors //
|
||||
// ************************************************************************* //
|
||||
|
||||
ClapTrap::~ClapTrap(void)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Destroying ClapTrap "
|
||||
<< this->_name
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Accessors //
|
||||
// ************************************************************************* //
|
||||
|
||||
std::string const &ClapTrap::getName(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getName()"
|
||||
<< std::endl;
|
||||
return this->_name;
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getHitPoints(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getHitPoints()"
|
||||
<< std::endl;
|
||||
return this->_hitPoints;
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getEnergyPoints(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getEnergyPoints()"
|
||||
<< std::endl;
|
||||
return this->_energyPoints;
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getAttackDamages(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getAttackDamages()"
|
||||
<< std::endl;
|
||||
return this->_attackDamages;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Public Member Functions //
|
||||
// ************************************************************************* //
|
||||
|
||||
void ClapTrap::attack(std::string const &target)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::attack()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ClapTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
std::cout
|
||||
<< " deals "
|
||||
<< this->_attackDamages
|
||||
<< " damages to ";
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to attack ";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to attack ";
|
||||
std::cout
|
||||
<< target
|
||||
<< std::endl;
|
||||
this->_energyPoints -= !!this->_energyPoints;
|
||||
}
|
||||
|
||||
void ClapTrap::beRepaired(unsigned int const amount)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::beRepaired()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ClapTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
{
|
||||
std::cout
|
||||
<< " repairs itself for an amount of "
|
||||
<< amount;
|
||||
this->_hitPoints += amount;
|
||||
}
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to repair itself";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to repair itself";
|
||||
std::cout << std::endl;
|
||||
this->_energyPoints -= !!this->_energyPoints;
|
||||
}
|
||||
|
||||
void ClapTrap::takeDamage(unsigned int const amount)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::takeDamage()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ClapTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
{
|
||||
std::cout
|
||||
<< " takes "
|
||||
<< amount
|
||||
<< " damages";
|
||||
if (this->_hitPoints < amount)
|
||||
this->_hitPoints = 0;
|
||||
else
|
||||
this->_hitPoints -= amount;
|
||||
}
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to take damages";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to take damages";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Operators //
|
||||
// ************************************************************************* //
|
||||
|
||||
ClapTrap &ClapTrap::operator=(ClapTrap const &rhs)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::operator=()"
|
||||
<< std::endl;
|
||||
if (this != &rhs)
|
||||
{
|
||||
this->_name = rhs._name;
|
||||
this->_hitPoints = rhs._hitPoints;
|
||||
this->_energyPoints = rhs._energyPoints;
|
||||
this->_attackDamages = rhs._attackDamages;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &o, ClapTrap const &rhs)
|
||||
{
|
||||
o
|
||||
<< "ClapTrap:" << std::endl
|
||||
<< "\t" "name: " << rhs.getName() << std::endl
|
||||
<< "\t" "hitPoints: " << rhs.getHitPoints() << std::endl
|
||||
<< "\t" "energyPoints: " << rhs.getEnergyPoints() << std::endl
|
||||
<< "\t" "attackDamages: " << rhs.getAttackDamages() << std::endl;
|
||||
return o;
|
||||
}
|
||||
|
||||
// ************************************************************************** //
|
||||
// Private Attributes //
|
||||
// ************************************************************************** //
|
||||
|
||||
std::string const ClapTrap::_defaultName = std::string("defaultName");
|
||||
unsigned int const ClapTrap::_defaultHitPoints = 10;
|
||||
unsigned int const ClapTrap::_defaultEnergyPoints = 10;
|
||||
unsigned int const ClapTrap::_defaultAttackDamages = 0;
|
||||
148
d03/jodufour/srcs/class/ScavTrap.cpp
Normal file
148
d03/jodufour/srcs/class/ScavTrap.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
#include "class/ScavTrap.hpp"
|
||||
|
||||
// ************************************************************************** //
|
||||
// Constructors //
|
||||
// ************************************************************************** //
|
||||
|
||||
ScavTrap::ScavTrap(std::string const &name) :
|
||||
ClapTrap(
|
||||
name,
|
||||
ScavTrap::_defaultHitPoints,
|
||||
ScavTrap::_defaultEnergyPoints,
|
||||
ScavTrap::_defaultAttackDamages),
|
||||
_activeMode(false)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< std::boolalpha
|
||||
<< "Creating ScavTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< " (" << this->_activeMode << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
ScavTrap::ScavTrap(ScavTrap const &src) :
|
||||
ClapTrap(
|
||||
src._name,
|
||||
src._hitPoints,
|
||||
src._energyPoints,
|
||||
src._attackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< std::boolalpha
|
||||
<< "Creating ScavTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< " (" << this->_activeMode << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Destructors //
|
||||
// ************************************************************************* //
|
||||
|
||||
ScavTrap::~ScavTrap(void)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Destroying ScavTrap "
|
||||
<< this->_name
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Public Member Functions //
|
||||
// ************************************************************************* //
|
||||
|
||||
void ScavTrap::attack(std::string const &target)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ScavTrap::attack()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ScavTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
std::cout
|
||||
<< " deals "
|
||||
<< this->_attackDamages
|
||||
<< " damages to ";
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to attack ";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to attack ";
|
||||
std::cout
|
||||
<< target
|
||||
<< std::endl;
|
||||
this->_energyPoints -= !!this->_energyPoints;
|
||||
}
|
||||
|
||||
void ScavTrap::guardGate(void)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ScavTrap::guardGate()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ScavTrap "
|
||||
<< this->_name;
|
||||
if (!this->_activeMode)
|
||||
std::cout
|
||||
<< " enters ";
|
||||
else
|
||||
std::cout
|
||||
<< " leaves ";
|
||||
std::cout
|
||||
<< "Gate keeper mode"
|
||||
<< std::endl;
|
||||
this->_activeMode ^= true;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Operators //
|
||||
// ************************************************************************* //
|
||||
|
||||
ScavTrap &ScavTrap::operator=(ScavTrap const &rhs)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ScavTrap::operator=()"
|
||||
<< std::endl;
|
||||
if (this != &rhs)
|
||||
{
|
||||
this->_name = rhs._name;
|
||||
this->_hitPoints = rhs._hitPoints;
|
||||
this->_energyPoints = rhs._energyPoints;
|
||||
this->_attackDamages = rhs._attackDamages;
|
||||
this->_activeMode = rhs._activeMode;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &o, ScavTrap const &rhs)
|
||||
{
|
||||
o << "ScavTrap:" << std::endl
|
||||
<< "\t" "name: " << rhs.getName() << std::endl
|
||||
<< "\t" "hitPoints: " << rhs.getHitPoints() << std::endl
|
||||
<< "\t" "energyPoints: " << rhs.getEnergyPoints() << std::endl
|
||||
<< "\t" "attackDamages: " << rhs.getAttackDamages() << std::endl;
|
||||
return o;
|
||||
}
|
||||
|
||||
// ************************************************************************** //
|
||||
// Private Attributes //
|
||||
// ************************************************************************** //
|
||||
|
||||
std::string const ScavTrap::_defaultName = std::string("defaultName");
|
||||
unsigned int const ScavTrap::_defaultHitPoints = 100;
|
||||
unsigned int const ScavTrap::_defaultEnergyPoints = 50;
|
||||
unsigned int const ScavTrap::_defaultAttackDamages = 20;
|
||||
Reference in New Issue
Block a user