cpp03/ex03/ClapTrap.cpp

78 lines
2.5 KiB
C++
Raw Normal View History

2025-05-20 11:24:16 +02:00
#include "ClapTrap.hpp"
#include <iostream>
ClapTrap::ClapTrap()
: name("[name unset]"), hit_points(10), energy_points(10),
attack_damage(0) {
std::cout << "ClapTrap::ClapTrap()" << std::endl;
}
2025-05-27 13:39:30 +02:00
ClapTrap::ClapTrap(const ClapTrap &other) {
name = other.name;
hit_points = other.hit_points;
energy_points = other.energy_points;
attack_damage = other.attack_damage;
std::cout << "ClapTrap::ClapTrap(const ClapTrap &other)" << std::endl;
}
2025-05-20 11:24:16 +02:00
ClapTrap::ClapTrap(std::string name)
: name(name), hit_points(10), energy_points(10), attack_damage(0) {
std::cout << "ClapTrap::ClapTrap(" << name << ")" << std::endl;
}
ClapTrap::~ClapTrap() { std::cout << "ClapTrap::~ClapTrap()" << std::endl; }
ClapTrap &ClapTrap::operator=(const ClapTrap &other) {
std::cout << "ClapTrap::operator=()" << std::endl;
name = other.name;
hit_points = other.hit_points;
energy_points = other.energy_points;
attack_damage = other.attack_damage;
return *this;
}
void ClapTrap::attack(const std::string &target) {
if (energy_points > 0) {
std::cout << "ClapTrap " << name << " attacks " << target << ", causing "
<< attack_damage << " points of damage!" << std::endl;
energy_points--;
} else {
std::cout << "ClapTrap " << name << " tries to attack " << target
<< ", but has no more energy! " << std::endl;
}
}
void ClapTrap::takeDamage(unsigned int amount) {
std::cout << "ClapTrap " << name << " took " << amount << " points of damage!"
<< std::endl;
if (amount > hit_points)
amount = hit_points;
hit_points -= amount;
if (hit_points == 0)
std::cout << "ClapTrap " << name << " died!" << std::endl;
}
void ClapTrap::beRepaired(unsigned int amount) {
if (energy_points > 0) {
std::cout << "ClapTrap " << name << " is repaired for " << amount
<< " hit points!" << std::endl;
hit_points += amount;
energy_points--;
} else {
std::cout << "ClapTrap " << name
<< " tries to repair itself, but has no more energy! "
<< std::endl;
}
}
unsigned int ClapTrap::get_hit_points() const { return hit_points; }
unsigned int ClapTrap::get_energy_points() const { return energy_points; }
unsigned int ClapTrap::get_attack_damage() const { return attack_damage; }
void ClapTrap::showStats() const {
std::cout << "ClapTrap " << name << " stats: hit_points: " << hit_points
<< " energy_points: " << energy_points
<< " attack_damage: " << attack_damage << std::endl;
}