From: Lukas Jiriste Date: Fri, 18 Oct 2024 07:42:32 +0000 (+0200) Subject: Add ClapTrap class for ex00 X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=fbaff81c862435f2a3bc3451cd115faad376d024;p=42%2FCPP_Module_03 Add ClapTrap class for ex00 --- fbaff81c862435f2a3bc3451cd115faad376d024 diff --git a/ex00/ClapTrap.cpp b/ex00/ClapTrap.cpp new file mode 100644 index 0000000..b2695ce --- /dev/null +++ b/ex00/ClapTrap.cpp @@ -0,0 +1,81 @@ +#include "ClapTrap.h" +#include +#include + +ClapTrap::ClapTrap(std::string name): + m_name(name), m_hp(10), m_energy(10), m_attack(0) +{ + std::cout << "ClapTrap " << m_name << " has spawned.\n"; +} + +ClapTrap::ClapTrap(const ClapTrap &other) +{ + std::cout << "ClapTrap " << m_name << " has spawned.\n"; + *this = other; +} + +ClapTrap::~ClapTrap() +{ + std::cout << "ClapTrap " << m_name << " has despawned.\n"; +} + +ClapTrap &ClapTrap::operator=(const ClapTrap &other) +{ + if (this == &other) + return (*this); + m_name = other.m_name; + m_hp = other.m_hp; + m_energy = other.m_energy; + m_attack = other.m_attack; + return (*this); +} + +void ClapTrap::attack(const std::string &target) +{ + if (m_hp == 0) + std::cout << "ClapTrap " << m_name + << " is dead, hence cannot attack.\n"; + else if (m_energy == 0) + std::cout << "ClapTrap " << m_name + << " does not have enough energy to attack.\n"; + else + { + std::cout << "ClapTrap " << m_name + << " attacks " << target << " and deals " << m_attack << " damage.\n"; + --m_energy; + } +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (m_hp == 0) + std::cout << "ClapTrap " << m_name << " is alread dead.\n"; + else if (amount > m_hp) + { + std::cout << "ClapTrap " << m_name + << " takes " << amount << " damage and dies\n"; + m_hp = 0; + } + else + { + std::cout << "ClapTrap " << m_name + << " takes " << amount << " damage.\n"; + m_hp -= amount; + } +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (m_hp == 0) + std::cout << "ClapTrap " << m_name + << " is dead, hence cannot repair itself.\n"; + else if (m_energy == 0) + std::cout << "ClapTrap " << m_name + << " does not have enough energy to repair itself.\n"; + else + { + std::cout << "ClapTrap " << m_name + << " repairs iself for " << amount << " hit points.\n"; + m_hp += amount; + } +} diff --git a/ex00/ClapTrap.h b/ex00/ClapTrap.h new file mode 100644 index 0000000..aa781d1 --- /dev/null +++ b/ex00/ClapTrap.h @@ -0,0 +1,26 @@ +#ifndef CLAPTRAP_H +# define CLAPTRAP_H + +# include + +class ClapTrap +{ + private: + std::string m_name; + unsigned int m_hp; + unsigned int m_energy; + unsigned int m_attack; + + public: + ClapTrap(std::string name = "DEFAULT"); + ClapTrap(const ClapTrap &other); + ~ClapTrap(); + + ClapTrap &operator=(const ClapTrap &other); + + void attack(const std::string &target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); +}; + +#endif // CLAPTRAP_H