CPP04 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Character.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Character.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/05/30 00:01:26 by kamitsui #+# #+# */
9/* Updated: 2025/06/02 05:46:35 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
23#include "Character.hpp"
24#include "AMateria.hpp" // Included for AMateria definitions
25#include "Logger.hpp"
26#include <sstream>
27
33Character::Character(std::string const &name) : name(name) {
34 LOG_INFO("Character constructor called for " + this->name);
35 for (int i = 0; i < 4; ++i)
36 this->inventory[i] = NULL;
37}
38
46Character::Character(const Character &other) : name(other.name) {
47 LOG_INFO("Character copy constructor called for " + this->name);
48 for (int i = 0; i < 4; ++i)
49 this->inventory[i] = NULL; // Ensure all slots are NULL before equipping
50 for (int i = 0; i < 4; ++i) {
51 if (other.inventory[i])
52 this->equip(other.inventory[i]->clone()); // Deep copy: clone and equip
53 }
54}
55
65 LOG_INFO("Character copy assignment operator called for " + other.name);
66 if (this != &other) {
67 this->name = other.name;
68 // Delete current materias before copying new ones to prevent leaks
69 for (int i = 0; i < 4; ++i) {
70 if (this->inventory[i])
71 delete this->inventory[i];
72 this->inventory[i] = NULL;
73 }
74 // Equip cloned materias from the 'other' character
75 for (int i = 0; i < 4; ++i) {
76 if (other.inventory[i])
77 this->equip(other.inventory[i]->clone()); // Deep copy: clone and equip
78 }
79 }
80 return *this;
81}
82
89 LOG_INFO("Character destructor called for " + this->name);
90 for (int i = 0; i < 4; ++i) {
91 if (this->inventory[i])
92 delete this->inventory[i];
93 }
94}
95
100std::string const &Character::getName() const { return this->name; }
101
108 if (!m) // Do nothing if materia is NULL
109 return;
111 for (int i = 0; i < 4; ++i) {
112 if (!this->inventory[i]) { // Find the first empty slot
113 this->inventory[i] = m;
114 oss << this->name << " equipped " << m->getType() << " at slot " << i;
115 LOG_INFO(oss.str());
116 return;
117 }
118 }
119 // If loop finishes, inventory is full
120 oss << this->name << "'s inventory is full, cannot equip " << m->getType();
121 LOG_INFO(oss.str());
122 delete m; // Delete the passed materia if it couldn't be equipped to prevent leak ( as per tip ? )
123}
124
131void Character::unequip(int idx) {
133 if (idx >= 0 && idx < 4 && this->inventory[idx]) {
134 oss << this->name << " unequips " << this->inventory[idx]->getType() << " from slot " << idx;
135 this->inventory[idx] = NULL; // Set slot to NULL, but don't delete the Materia
136 } else {
137 oss << this->name << " tried to unequip from invalid slot " << idx;
138 }
139 LOG_INFO(oss.str());
140}
141
148void Character::use(int idx, ICharacter &target) {
150 if (idx >= 0 && idx < 4 && this->inventory[idx]) {
151 this->inventory[idx]->use(target);
152 } else {
153 oss << this->name << " tried to use invalid slot " << idx;
154 LOG_INFO(oss.str());
155 }
156}
157
163bool Character::isMateriaEquipped(int idx) const { return (idx >= 0 && idx < 4 && inventory[idx] != NULL); }
164
176 if (idx >= 0 && idx < 4) {
177 return this->inventory[idx];
178 }
179 return NULL; // Return NULL for invalid indices
180}
Declares the AMateria abstract base class.
Declares the Character class.
Defines the Logger class for console-based logging.
#define LOG_INFO(msg)
Macro for logging informational messages.
Definition Logger.hpp:70
Represents an abstract base class for all magical materias.
Definition AMateria.hpp:42
virtual AMateria * clone() const =0
std::string const & getType() const
Gets the type of the materia.
Definition AMateria.cpp:78
virtual void use(ICharacter &target)
Uses the materia on a target character. This is a virtual function with a base implementation that pr...
Definition AMateria.cpp:86
Represents a character capable of equipping and using Materias.
Definition Character.hpp:39
Character & operator=(const Character &other)
Copy assignment operator for Character. Assigns values from another Character object,...
Definition Character.cpp:64
bool isMateriaEquipped(int idx) const
Checks if a Materia is equipped at a specific inventory slot.
void unequip(int idx)
Unequips a Materia from a specific inventory slot. The Materia object itself is NOT deleted,...
virtual ~Character()
Destroys the Character object. Deallocates all dynamically allocated Materia objects currently in the...
Definition Character.cpp:88
Character(std::string const &name)
Constructs a Character object with a given name. Initializes the character's name and sets all invent...
Definition Character.cpp:33
AMateria * getMateriaAtSlot(int idx) const
Gets a pointer to the Materia at a specific inventory slot.
void use(int idx, ICharacter &target)
Uses the Materia at a specific inventory slot on a target character. Performs bounds checking and che...
std::string const & getName() const
Gets the name of the character.
void equip(AMateria *m)
Equips a Materia into the first available inventory slot (0-3). If the inventory is full,...
An interface for any character that can interact with Materias.
T str(T... args)