You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

91 lines
2.7 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */
/* Updated: 2022/07/19 02:12:17 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
int ClapTrap::amount = 0;
ClapTrap *ClapTrap::registery[128] = {NULL};
ClapTrap::ClapTrap (void) {}
ClapTrap::ClapTrap (std::string name)
: _name(name), _health(10), _energy(10), _attack(0) {
if (ClapTrap::amount >= 128)
{
cout << "There are too many Traps " << ClapTrap::registery[amount % 128]->getName() << " destroyed\n";
delete ClapTrap::registery[amount % 128];
}
cout << "ClapTrap " << name << " was created" << endl;
_id = ClapTrap::amount % 128;
ClapTrap::registery[ClapTrap::amount % 128] = this;
ClapTrap::amount++;
}
ClapTrap::ClapTrap (ClapTrap const & src) {
_name = src.getName();
}
ClapTrap & ClapTrap::operator= (ClapTrap const & src) {
_name = src.getName();
return (*this);
}
ClapTrap::~ClapTrap (void) {}
void ClapTrap::attack(const std::string& target) {
if (_energy == 0 || _health == 0)
cout << _name << " is no more able to attack anyone" << endl;
else
{
cout << _name << " attacked " << target << endl;
getClapTrap(target)->takeDamage(_attack);
_energy--;
}
}
void ClapTrap::takeDamage(unsigned int amount) {
if (_health == 0)
cout << _name << " is already death" << endl;
else
cout << _name << " lose " << amount << " hp" << endl;
if (amount >= _health)
_health = 0;
else
_health -= amount;
}
void ClapTrap::beRepaired(unsigned int amount) {
if (_energy == 0 || _health == 0)
cout << _name << " is no more able to repair anyone" << endl;
else
{
cout << _name << " was repaired for " << amount << " hp" << endl;
_health += amount;
_energy--;
}
}
std::string ClapTrap::getName(void) const {
return (_name);
}
ClapTrap *ClapTrap::getClapTrap(std::string name) {
int i = 0;
while (i < ClapTrap::amount && i < 128)
{
if (name == ClapTrap::registery[i]->getName())
return (ClapTrap::registery[i]);
i++;
}
return (NULL);
}