/ CPP_Modules / CPP_Module_03 / ex03 / ClapTrap.cpp
ClapTrap.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   ClapTrap.cpp                                       :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/08/07 17:59:26 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/08/12 20:32:24 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "ClapTrap.hpp"
 14  #include <iostream>
 15  
 16  /*
 17   * Constructor & Destructor
 18   */
 19  
 20  ClapTrap::ClapTrap(void) : _hit(10), _energy(10), _attack(0)
 21  {
 22  	std::cout << "ClapTrap default constructor called" << std::endl;
 23  	introduce();
 24  }
 25  
 26  ClapTrap::ClapTrap(std::string const& _name) : _hit(10), _energy(10), _attack(0)
 27  {
 28  	std::cout << "ClapTrap parameterized constructor called" << std::endl;
 29  	const_cast<std::string&>(name) = _name;
 30  	introduce();
 31  }
 32  
 33  ClapTrap::ClapTrap(ClapTrap const& target)
 34  {
 35  	std::cout << "ClapTrap copy constructor called" << std::endl;
 36  	if (this != &target)
 37  		*this = target;
 38  	introduce();
 39  }
 40  
 41  ClapTrap&	ClapTrap::operator=(ClapTrap const& target)
 42  {
 43  	std::cout << "ClapTrap copy assignment operator called" << std::endl;
 44  	if (this != &target)
 45  	{
 46  		const_cast<std::string&>(name) = target.name;
 47  		_hit = target._hit;
 48  		_energy = target._energy;
 49  		_attack = target._attack;
 50  	}
 51  	return (*this);
 52  }
 53  
 54  ClapTrap::~ClapTrap(void)
 55  {
 56  	std::cout << "ClapTrap destructor called" << std::endl;
 57  }
 58  
 59  /*
 60   * Public Member Functions : Getter
 61   */
 62  
 63  std::string	ClapTrap::getName(void) const
 64  {
 65  	return (name);
 66  }
 67  
 68  int	ClapTrap::getHit(void) const
 69  {
 70  	return (_hit);
 71  }
 72  
 73  unsigned int	ClapTrap::getEnergy(void) const
 74  {
 75  	return (_energy);
 76  }
 77  
 78  unsigned int	ClapTrap::getAttack(void) const
 79  {
 80  	return (_attack);
 81  }
 82  
 83  /*
 84   * Public Member Functions : ClapTrap Actions
 85   */
 86  
 87  void	ClapTrap::attack(std::string const& target)
 88  {
 89  	if (_energy > 0)
 90  	{
 91  		std::cout << "ClapTrap " << name << " attacks " << target << " causing " << _attack << " points of damage!" << std::endl;
 92  		_energy--;
 93  	}
 94  	else
 95  		std::cout << "ClapTrap " << name << " is out of energy. Cannot move!" << std::endl;
 96  }
 97  
 98  void	ClapTrap::takeDamage(unsigned int amount)
 99  {
100  	std::cout << getName() << " takes " << amount << " points of damage" << std::endl;
101  	if (_hit > 0)
102  	{
103  		long long	hitTmp = static_cast<long long>(_hit);
104  
105  		hitTmp -= amount;
106  		if (hitTmp <= 0)
107  		{
108  			_hit = 0;
109  			if (_energy > 0)
110  			{
111  				std::cout << getName() << " has been destroyed. All energy is drained" << std::endl;
112  				_energy = 0;
113  			}
114  		}
115  		else
116  			_hit -= amount;
117  	}
118  	else
119  		std::cout << getName() << " is already destroyed" << std::endl;
120  }
121  
122  void	ClapTrap::beRepaired(unsigned int amount)
123  {
124  	if (_energy > 0)
125  	{
126  		std::cout << getName() << " is repaired by " << amount << " points of health" << std::endl;
127  		long long	hitTmp = static_cast<long long>(_hit);
128  
129  		hitTmp += amount;
130  		if (hitTmp > std::numeric_limits<int>::max())
131  			_hit = std::numeric_limits<int>::max();
132  		else
133  			_hit += amount;
134  		_energy--;
135  	}
136  	else
137  		std::cout << getName() << " is out of energy. Cannot move!" << std::endl;
138  }
139  
140  void	ClapTrap::introduce(void)
141  {
142  	std::cout << "["<< getName() << "]: " << "Welcome to Fyrestone! I am CL4P-TP, you may call me by my locally designated name, 'Claptrap'" << std::endl;
143  }