1
0
This repository has been archived on 2025-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
CPP_Module_03/ex01/ClapTrap.cpp

112 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:29:16 by adjoly #+# #+# */
/* Updated: 2024/11/29 15:21:39 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
#include <string>
#include <iostream>
void logClap(std::string emoji, std::string who, std::string str) {
std::cout << "" << emoji << "」ClapTrap(" << who << "): " << str << std::endl;
}
void logClap(std::string emoji, std::string str) {
std::cout << "" << emoji << "」ClapTrap: " << str << std::endl;
}
ClapTrap::ClapTrap(void) :
_name("Kanel"), _health(10), _energyPoints(10), _attackDamage(0) {
logClap("", "default constructor called");
}
ClapTrap::~ClapTrap(void) {
logClap("", "destructor called");
}
ClapTrap::ClapTrap(std::string name) :
_name(name), _health(10), _energyPoints(10), _attackDamage(0) {
logClap("", _name, "constructor called");
}
ClapTrap::ClapTrap(const ClapTrap &other) {
*this = other;
logClap("", _name, "copy constructor called");
}
ClapTrap &ClapTrap::operator=(const ClapTrap &cpy) {
logClap("🟰", _name, "copy assignement constructor called");
if (this != &cpy) {
_name = cpy.getName();
_health = cpy.getHealth();
_energyPoints = cpy.getEnergyPoints();
_attackDamage= cpy.getAttackDamage();
}
return (*this);
}
void ClapTrap::attack(const std::string& target) {
if (_health == 0) {
logClap("💀", _name, "can't attack already dead");
return ;
} else if (_energyPoints == 0) {
logClap("💤", _name, "can't attack no energy left GO TO SLEEP!");
return ;
}
logClap("💥", _name, "attacks " + target + " causing " + iToS(getAttackDamage()) + " points of damage!");
_energyPoints--;
}
void ClapTrap::takeDamage(unsigned int amount) {
logClap("🥊", _name, "take " + iToS(amount) + " damage");
if (_health <= amount) {
logClap("💀", _name, "is dead HAHA");
_health = 0;
} else {
_health -= amount;
}
}
void ClapTrap::beRepaired(uint amount) {
if (_health == 0) {
logClap("💀", _name, "can't repair already dead");
return ;
} else if (_energyPoints == 0) {
logClap("💤", _name, "can't repair no energy left GO TO SLEEP!");
return ;
}
logClap("🩹", _name, iToS(amount) + "");
_health += amount;
_energyPoints--;
}
uint ClapTrap::getAttackDamage(void) const { return (_attackDamage); }
uint ClapTrap::getEnergyPoints(void) const { return (_energyPoints); }
uint ClapTrap::getHealth(void) const { return (_health); }
std::string ClapTrap::getName(void) const { return (_name); }
void ClapTrap::setAttackDamage(uint in) {
logClap("🟰", _name, "set attack damage");
_attackDamage = in;
}
void ClapTrap::setEnergyPoints(uint in) {
logClap("🟰", _name, "set energy points");
_energyPoints = in;
}
void ClapTrap::setHealth(uint in) {
logClap("🟰", _name, "set health");
_health = in;
}
void ClapTrap::setName(std::string in) {
logClap("🟰", _name, "set a new name");
_name = in;
}