From 119f7f96e77a2d86d692e82645dde4c2dc054052 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 13 Apr 2020 18:44:40 +0200 Subject: cpp03, except ex04 --- cpp03/ex02/ClapTrap.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'cpp03/ex02/ClapTrap.cpp') diff --git a/cpp03/ex02/ClapTrap.cpp b/cpp03/ex02/ClapTrap.cpp index e69de29..6711799 100644 --- a/cpp03/ex02/ClapTrap.cpp +++ b/cpp03/ex02/ClapTrap.cpp @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 15:28:43 by charles #+# #+# */ +/* Updated: 2020/04/13 15:50:48 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap(): + m_prefix("CL4P-TP "), + m_hitPoints(100), + m_maxHitPoints(100), + m_energyPoints(100), + m_maxEnergyPoints(100), + m_level(1), + m_name(""), + m_meleeAttackDamage(0), + m_rangedAttackDamage(0), + m_armorDamageReduction(0) +{ + std::cout << m_prefix << "New " << m_name << ": your gaming references suck" << std::endl; +} + +ClapTrap::ClapTrap(std::string name): + m_prefix("CL4P-TP "), + m_hitPoints(100), + m_maxHitPoints(100), + m_energyPoints(100), + m_maxEnergyPoints(100), + m_level(1), + m_name(name), + m_meleeAttackDamage(0), + m_rangedAttackDamage(0), + m_armorDamageReduction(0) +{ + std::cout << m_prefix << "New " << m_name << ": your gaming references suck" << std::endl; +} + +ClapTrap::ClapTrap(ClapTrap const& other) +{ + *this = other; +} + +void ClapTrap::operator=(ClapTrap const& other) +{ + m_hitPoints = other.m_hitPoints; + m_maxHitPoints = other.m_maxHitPoints; + m_energyPoints = other.m_energyPoints; + m_maxEnergyPoints = other.m_maxEnergyPoints; + m_level = other.m_level; + m_meleeAttackDamage = other.m_meleeAttackDamage; + m_rangedAttackDamage = other.m_rangedAttackDamage; + m_armorDamageReduction = other.m_armorDamageReduction; +} + +ClapTrap::~ClapTrap() +{ + std::cout << "CL4P-TP " << "Delete "<< m_name << ": your gaming references still suck" << std::endl; +} + +void ClapTrap::rangedAttack(std::string const& target) const +{ + std::cout << m_prefix << m_name + << " attacks " << target + << " at range, causing " << m_rangedAttackDamage + << " points of damage!" << std::endl; +} + +void ClapTrap::meleeAttack(std::string const& target) const +{ + std::cout << m_prefix << m_name + << " attacks " << target + << " in melee mode causing " << m_meleeAttackDamage + << " points of damage!" << std::endl; +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (amount < m_armorDamageReduction) + amount = 0; + else + amount -= m_armorDamageReduction; + if (amount > m_hitPoints) + amount = m_hitPoints; + m_hitPoints -= amount; + std::cout << m_prefix << m_name + << " takes " << amount + << " damage" << std::endl; +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (amount + m_hitPoints > m_maxHitPoints) + amount = m_maxHitPoints - m_hitPoints; + m_hitPoints += amount; + std::cout << m_prefix << m_name + << " gained " << amount + << " hit points" << std::endl; +} -- cgit