|
| 1 | +/* ************************************************************************** */ |
| 2 | +/* */ |
| 3 | +/* ::: :::::::: */ |
| 4 | +/* ClapTrap.cpp :+: :+: :+: */ |
| 5 | +/* +:+ +:+ +:+ */ |
| 6 | +/* By: jmartin <jmartin@student.42lausanne.ch> +#+ +:+ +#+ */ |
| 7 | +/* +#+#+#+#+#+ +#+ */ |
| 8 | +/* Created: 2022/11/08 15:21:31 by jmartin #+# #+# */ |
| 9 | +/* Updated: 2022/11/08 15:21:32 by jmartin ### ########.fr */ |
| 10 | +/* */ |
| 11 | +/* ************************************************************************** */ |
| 12 | + |
| 13 | +#include "../inc/ClapTrap.hpp" |
| 14 | + |
| 15 | +/****************************************************************************** |
| 16 | + * CONSTRUCTORS / DESTRUCTORS |
| 17 | + */ |
| 18 | + |
| 19 | +ClapTrap::ClapTrap(void): _name(), _hitPoints(10), _energyPoints(10), _attackDamage(0) |
| 20 | +{ |
| 21 | + std::cout << "ClapTrap default constructor called" << std::endl; |
| 22 | +} |
| 23 | + |
| 24 | +ClapTrap::ClapTrap(std::string name): _name(name), _hitPoints(10), _energyPoints(10), _attackDamage(0) |
| 25 | +{ |
| 26 | + std::cout << "ClapTrap " |
| 27 | + << name |
| 28 | + << " is created." |
| 29 | + << std::endl; |
| 30 | +} |
| 31 | + |
| 32 | +ClapTrap::~ClapTrap(void) { |
| 33 | + std::cout << "ClapTrap " |
| 34 | + << this->getName() |
| 35 | + << " is destroyed." |
| 36 | + << std::endl; |
| 37 | +} |
| 38 | + |
| 39 | +ClapTrap::ClapTrap(ClapTrap const &instance): _name(instance.getName()), _hitPoints(instance.getHitPoints()), _energyPoints(instance.getEnergyPoints()), _attackDamage(instance.getAttackDamage()) |
| 40 | +{ |
| 41 | + *this = instance; // *this = ClapTrap instance |
| 42 | + std::cout << "ClapTrap copy constructor called" << std::endl; |
| 43 | + std::cout << "ClapTrap" |
| 44 | + << " is a copy of " |
| 45 | + << instance.getName() |
| 46 | + << std::endl; |
| 47 | + return ; |
| 48 | +} |
| 49 | + |
| 50 | +ClapTrap &ClapTrap::operator=(ClapTrap const &rhs) |
| 51 | +{ |
| 52 | + std::cout << "ClapTrap assignation operator called" << std::endl; |
| 53 | + if (&rhs != this) // self-assignment check expected |
| 54 | + { |
| 55 | + this->_name = rhs.getName(); |
| 56 | + this->_hitPoints = rhs.getHitPoints(); |
| 57 | + this->_energyPoints = rhs.getEnergyPoints(); |
| 58 | + this->_attackDamage = rhs.getAttackDamage(); |
| 59 | + } |
| 60 | + return (*this); |
| 61 | +} |
| 62 | + |
| 63 | +/****************************************************************************** |
| 64 | + * FUNCTION MEMBER |
| 65 | + */ |
| 66 | + |
| 67 | +void ClapTrap::attack(std::string const &target) { |
| 68 | + if (this->getEnergyPoints() < 1 || this->getHitPoints() < 1) |
| 69 | + { |
| 70 | + std::cout << "ClapTrap " |
| 71 | + << this->getName() |
| 72 | + << " has no energy left." |
| 73 | + << std::endl; |
| 74 | + return ; |
| 75 | + } |
| 76 | + energyCost(); |
| 77 | + std::cout << "ClapTrap " |
| 78 | + << this->getName() |
| 79 | + << " attacks " |
| 80 | + << target |
| 81 | + << std::endl; |
| 82 | +} |
| 83 | + |
| 84 | +void ClapTrap::takeDamage(unsigned int amount) { |
| 85 | + if (this->getHitPoints() < 1) |
| 86 | + { |
| 87 | + std::cout << "ClapTrap " |
| 88 | + << this->getName() |
| 89 | + << " has no hit points left." |
| 90 | + << std::endl; |
| 91 | + return ; |
| 92 | + } |
| 93 | + this->setHitPoints(this->getHitPoints() - amount); |
| 94 | + if (this->getHitPoints() < 0) |
| 95 | + this->setHitPoints(0); |
| 96 | + std::cout << "ClapTrap " |
| 97 | + << this->getName() |
| 98 | + << " takes " |
| 99 | + << amount |
| 100 | + << " points of damage!" |
| 101 | + << "\nCurrent hit points: " |
| 102 | + << this->getHitPoints() |
| 103 | + << std::endl; |
| 104 | +} |
| 105 | + |
| 106 | +void ClapTrap::beRepaired(unsigned int amount) { |
| 107 | + if (energyCost() == 0) { |
| 108 | + std::cout << "ClapTrap " |
| 109 | + << this->getName() |
| 110 | + << " has no energy left." |
| 111 | + << std::endl; |
| 112 | + return ; |
| 113 | + } |
| 114 | + if (this->getHitPoints() < 1) |
| 115 | + { |
| 116 | + std::cout << "ClapTrap " |
| 117 | + << this->getName() |
| 118 | + << " has no hit points left." |
| 119 | + << std::endl; |
| 120 | + return ; |
| 121 | + } |
| 122 | + if (amount < 1) { |
| 123 | + std::cout << "Wrong amount of hit points to repair." |
| 124 | + << std::endl; |
| 125 | + return ; |
| 126 | + } |
| 127 | + this->setHitPoints(this->getHitPoints() + amount); |
| 128 | + std::cout << "ClapTrap " |
| 129 | + << this->getName() |
| 130 | + << " is repaired by " |
| 131 | + << amount |
| 132 | + << " points!" |
| 133 | + << "\nCurrent hit points: " |
| 134 | + << this->getHitPoints() |
| 135 | + << std::endl; |
| 136 | +} |
| 137 | + |
| 138 | +int ClapTrap::energyCost(void) { |
| 139 | + if (this->getEnergyPoints() < 1) |
| 140 | + this->setEnergyPoints(0); |
| 141 | + else |
| 142 | + this->setEnergyPoints(this->getEnergyPoints() - 1); |
| 143 | + return (this->getEnergyPoints()); |
| 144 | +} |
| 145 | + |
| 146 | +/****************************************************************************** |
| 147 | + * GETTERS / SETTERS |
| 148 | + */ |
| 149 | + |
| 150 | +std::string ClapTrap::getName(void) const { |
| 151 | + return (this->_name); |
| 152 | +} |
| 153 | + |
| 154 | +int ClapTrap::getHitPoints(void) const { |
| 155 | + return (this->_hitPoints); |
| 156 | +} |
| 157 | + |
| 158 | +int ClapTrap::getEnergyPoints(void) const { |
| 159 | + return (this->_energyPoints); |
| 160 | +} |
| 161 | + |
| 162 | +int ClapTrap::getAttackDamage(void) const { |
| 163 | + return (this->_attackDamage); |
| 164 | +} |
| 165 | + |
| 166 | +void ClapTrap::setName(std::string name) { |
| 167 | + this->_name = name; |
| 168 | +} |
| 169 | + |
| 170 | +void ClapTrap::setHitPoints(unsigned int hitPoints) { |
| 171 | + this->_hitPoints = hitPoints; |
| 172 | +} |
| 173 | + |
| 174 | +void ClapTrap::setEnergyPoints(unsigned int energyPoints) { |
| 175 | + this->_energyPoints = energyPoints; |
| 176 | +} |
| 177 | + |
| 178 | +void ClapTrap::setAttackDamage(unsigned int attackDamage) { |
| 179 | + this->_attackDamage = attackDamage; |
| 180 | +} |
| 181 | + |
0 commit comments