/ CPP_Modules / CPP_Module_04 / ex03 / Character.cpp
Character.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   Character.cpp                                      :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/08/17 22:54:45 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/08/18 18:43:55 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "Character.hpp"
 14  #include <iostream>
 15  
 16  /******************************
 17   * Constructor and Destructor *
 18   ******************************/
 19  
 20  Character::Character(void)
 21  {
 22  	for (std::size_t i = 0; i < 4; i++)
 23  		this->inventory[i] = 0;
 24  }
 25  
 26  Character::~Character(void)
 27  {
 28  	for (std::size_t i = 0; i < 4; i++)
 29  		delete this->inventory[i];
 30  }
 31  
 32  Character::Character(std::string const& name) : name(name)
 33  {
 34  	for (std::size_t i = 0; i < 4; i++)
 35  		this->inventory[i] = 0;
 36  }
 37  
 38  Character::Character(Character const& target)
 39  {
 40  	if (this != &target)
 41  	{
 42  		for (std::size_t i = 0; i < 4; i++)
 43  			this->inventory[i] = 0;
 44  		*this = target;
 45  	}
 46  }
 47  
 48  Character&	Character::operator=(Character const& target)
 49  {
 50  	if (this != &target)
 51  	{
 52  		this->name = target.name;
 53  		for (std::size_t i = 0; i < 4; i++)
 54  		{
 55  			delete this->inventory[i];
 56  			if (target.inventory[i])
 57  				this->inventory[i] = target.inventory[i]->clone();
 58  			else
 59  				this->inventory[i] = 0;
 60  		}
 61  	}
 62  	return *this;
 63  }
 64  
 65  /******************************
 66   *      Getter  function      *
 67   ******************************/
 68  
 69  std::string const&	Character::getName(void) const
 70  {
 71  	return this->name;
 72  }
 73  
 74  AMateria const*	Character::getItem(std::size_t idx) const
 75  {
 76  	return this->inventory[idx];
 77  }
 78  
 79  /******************************
 80   *   Public Member function   *
 81   ******************************/
 82  
 83  void	Character::equip(AMateria* m)
 84  {
 85  	for (std::size_t i = 0; i < 4; i++)
 86  	{
 87  		if (!this->inventory[i])
 88  		{
 89  			this->inventory[i] = m;
 90  			return ;
 91  		}
 92  	}
 93  	std::cout << "[" << this->getName() << "] : Inventory is full" << std::endl;
 94  }
 95  
 96  void	Character::unequip(int idx)
 97  {
 98  	if (idx < 0 || idx >= 4)
 99  	{
100  		std::cout << "[Character] : Invalid index" << std::endl;
101  		return ;
102  	}
103  	this->inventory[idx] = 0;
104  }
105  
106  void	Character::use(int idx, ICharacter& target)
107  {
108  	if (idx < 0 || idx >= 4)
109  	{
110  		std::cout << "[Character] : Invalid index" << std::endl;
111  		return ;
112  	}
113  	if (this->inventory[idx])
114  		this->inventory[idx]->use(target);
115  }
116  
117  void	Character::showInventory(void)
118  {
119  	std::cout << "[INFO] : " << this->getName() << "'s Inventory : " << std::endl;
120  	for (std::size_t i = 0; i < 4; i++)
121  	{
122  		if (this->inventory[i])
123  			std::cout << "[" << i << "] : " << this->inventory[i]->getType() << std::endl;
124  		else
125  			std::cout << "[" << i << "] : " << "(empty)" << std::endl;
126  	}
127  	std::cout << std::endl;
128  }