/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ClapTrap.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */ /* Updated: 2022/06/23 17:47:34 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "ClapTrap.hpp" int ClapTrap::amount = 0; ClapTrap *ClapTrap::registery[128] = {NULL}; ClapTrap::ClapTrap (void) { std::cout << "ClapTrap default constructor called " << endl; } ClapTrap::ClapTrap (std::string name, size_t hp, size_t ep, size_t atk) : _name(name), _health(hp), _energy(ep), _attack(atk) { if (ClapTrap::amount >= 128) { delete ClapTrap::registery[amount % 128]; cout << "There are too many Traps " << ClapTrap::registery[amount % 128]->getName() << " destroyed\n"; } 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) { std::cout << "ClapTrap assignment operator called" << endl; _name = src.getName(); return (*this); } ClapTrap::~ClapTrap (void) { std::cout << "ClapTrap " << _name << " was destroyed" << endl; } 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) { for (int i = 0; i < 128; i++) { if (name == ClapTrap::registery[i]->getName()) return (ClapTrap::registery[i]); } return (NULL); }