Character.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* Character.cpp :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2025/12/19 16:40:56 by bchanot #+# #+# */
  9. /* Updated: 2025/12/26 01:10:15 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "Character.hpp"
  13. #include "AMateria.hpp"
  14. /*
  15. ** ------------------------------- CONSTRUCTOR --------------------------------
  16. */
  17. Character::Character() : _name("Noname")
  18. {
  19. for (int i = 0; i < 4; i++)
  20. this->_items[i] = NULL;
  21. }
  22. Character::Character( std::string const & name ) : _name(name)
  23. {
  24. for (int i = 0; i < 4; i++)
  25. this->_items[i] = NULL;
  26. }
  27. Character::Character( const Character & src ) : _name(src._name)
  28. {
  29. for (int i = 0; i < 4; i++)
  30. this->_items[i] = src._items[i]->clone();
  31. }
  32. /*
  33. ** -------------------------------- DESTRUCTOR --------------------------------
  34. */
  35. Character::~Character()
  36. {
  37. for (int i = 0; i < 4; i++)
  38. this->unequip(i);
  39. }
  40. /*
  41. ** --------------------------------- OVERLOAD ---------------------------------
  42. */
  43. Character & Character::operator=( Character const & rhs )
  44. {
  45. if ( this != &rhs )
  46. {
  47. this->_name = rhs._name;
  48. for (int i = 0; i < 4; i++) {
  49. this->unequip(i);
  50. this->_items[i] = rhs._items[i]->clone();
  51. }
  52. }
  53. return *this;
  54. }
  55. std::ostream & operator<<( std::ostream & o, Character const & i )
  56. {
  57. o << i.getName();
  58. return o;
  59. }
  60. /*
  61. ** --------------------------------- METHODS ----------------------------------
  62. */
  63. void Character::equip(AMateria* m) {
  64. if (m) {
  65. for (int i = 0; i < 4; i++) {
  66. if (!this->_items[i]) {
  67. this->_items[i] = m;
  68. return ;
  69. }
  70. }
  71. }
  72. }
  73. void Character::unequip(int idx) {
  74. if (this->_items[idx]) {
  75. // delete this->_items[idx];
  76. this->_items[idx] = NULL;
  77. }
  78. }
  79. void Character::use(int idx, ICharacter& target) {
  80. if (this->_items[idx]) {
  81. this->_items[idx]->use(target);
  82. }
  83. }
  84. /*
  85. ** --------------------------------- ACCESSOR ---------------------------------
  86. */
  87. std::string const & Character::getName() const {
  88. return this->_name;
  89. }
  90. /* ************************************************************************** */