ClapTrap.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ClapTrap.cpp :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2025/12/18 13:30:29 by bchanot #+# #+# */
  9. /* Updated: 2025/12/18 14:26:20 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ClapTrap.hpp"
  13. #include <limits>
  14. /*
  15. ** ------------------------------- CONSTRUCTOR --------------------------------
  16. */
  17. ClapTrap::ClapTrap() : _name("Default"), _hitPoints(10), _energyPoints(10), _attackDamage(0)
  18. {
  19. std::cout << "ClapTrap " << this->_name << " has been created" << std::endl;
  20. }
  21. ClapTrap::ClapTrap(std::string const & name) : _name(name), _hitPoints(10), _energyPoints(10), _attackDamage(0)
  22. {
  23. std::cout << "ClapTrap " << this->_name << " has been created" << std::endl;
  24. }
  25. ClapTrap::ClapTrap( const ClapTrap & src ) : _name(src._name), _hitPoints(src._hitPoints), _energyPoints(src._energyPoints), _attackDamage(src._attackDamage)
  26. {
  27. std::cout << "Copy of ClapTrap " << this->_name << " has been created" << std::endl;
  28. }
  29. /*
  30. ** -------------------------------- DESTRUCTOR --------------------------------
  31. */
  32. ClapTrap::~ClapTrap()
  33. {
  34. std::cout << "ClapTrap " << this->_name << " has been destroyed !" << std::endl;
  35. }
  36. /*
  37. ** --------------------------------- OVERLOAD ---------------------------------
  38. */
  39. ClapTrap & ClapTrap::operator=( ClapTrap const & rhs )
  40. {
  41. (void)rhs;
  42. //if ( this != &rhs )
  43. //{
  44. //this->_value = rhs.getValue();
  45. //}
  46. return *this;
  47. }
  48. std::ostream & operator<<( std::ostream & o, ClapTrap const & i )
  49. {
  50. (void)i;
  51. //o << "Value = " << i.getValue();
  52. return o;
  53. }
  54. /*
  55. ** --------------------------------- METHODS ----------------------------------
  56. */
  57. void ClapTrap::attack(const std::string& target) {
  58. if (this->_hitPoints > 0) {
  59. if (this->_energyPoints > 0) {
  60. this->_energyPoints--;
  61. std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage !" << std::endl;
  62. } else
  63. std::cout << "ClapTrap " << this->_name << " Doesn't have enough energy to attack !" << std::endl;
  64. } else
  65. std::cout << "ClapTrap " << this->_name << " is dead.. How could he attack ?" << std::endl;
  66. }
  67. void ClapTrap::takeDamage(unsigned int amount) {
  68. if (this->_hitPoints > 0) {
  69. if (this->_hitPoints < amount) {
  70. this->_hitPoints = 0;
  71. std::cout << this->_name << " is taking " << amount << " damage.. Causing his death ! he is now 0 HP..." << std::endl;
  72. } else {
  73. this->_hitPoints -= amount;
  74. std::cout << this->_name << " is taking " << amount << " damage. He got now " << this->_hitPoints << " HP" << std::endl;
  75. }
  76. } else
  77. std::cout << this->_name << " is 0 HP, already dead. Cannot attack a dead ClapTrap. Do we .. ?" << std::endl;
  78. }
  79. void ClapTrap::beRepaired(unsigned int amount) {
  80. if (this->_hitPoints > 0) {
  81. if (this->_energyPoints > 0) {
  82. if (this->_hitPoints + amount > std::numeric_limits<short unsigned int>::max())
  83. this->_hitPoints = std::numeric_limits<short unsigned int>::max();
  84. else
  85. this->_hitPoints += amount;
  86. this->_energyPoints--;
  87. std::cout << this->_name << " is getting repaired about " << amount << " HP. Getting now " << this->_hitPoints << " HP." << std::endl;
  88. } else
  89. std::cout << "ClapTrap " << this->_name << " Doesn't have enought energy..." << std::endl;
  90. } else
  91. std::cout << this->_name << " is 0 HP, it mean DEAD. so, he cannot do anything more..." << std::endl;
  92. }
  93. /*
  94. ** --------------------------------- ACCESSOR ---------------------------------
  95. */
  96. /* ************************************************************************** */