169 lines
5.5 KiB
C++
169 lines
5.5 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* :::::::: */
|
|
/* Character.cpp :+: :+: */
|
|
/* +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
|
/* +#+ */
|
|
/* Created: 2025/04/17 13:43:45 by whaffman #+# #+# */
|
|
/* Updated: 2025/04/17 13:43:55 by whaffman ######## odam.nl */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <iostream>
|
|
|
|
#include "AMateria.hpp"
|
|
#include "Character.hpp"
|
|
#include "ICharacter.hpp"
|
|
|
|
#include "colors.h"
|
|
|
|
void Character::dropMateria(AMateria *m)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" dropMateria function called" << std::endl;
|
|
if(blackhole)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" blackhole crunching older materia" << std::endl;
|
|
delete blackhole;
|
|
}
|
|
blackhole = m;
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" blackhole filled with materia of type: " << blackhole->getType() << std::endl;
|
|
}
|
|
|
|
Character::Character() : name("default"), blackhole(nullptr)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character" << RESET <<" Default constructor called" << std::endl;
|
|
for (int i = 0; i < 4; ++i)
|
|
inventory[i] = nullptr;
|
|
}
|
|
|
|
Character::Character(std::string const &name) : name(name), blackhole(nullptr)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Parametric constructor called" << std::endl;
|
|
for (int i = 0; i < 4; ++i)
|
|
inventory[i] = nullptr;
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Inventory initialized" << std::endl;
|
|
}
|
|
|
|
Character::Character(Character const &other) : name(other.name)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Copy constructor called" << std::endl;
|
|
for (int i = 0; i < 4; ++i)
|
|
{
|
|
if (other.inventory[i])
|
|
inventory[i] = other.inventory[i]->clone();
|
|
else
|
|
inventory[i] = nullptr;
|
|
}
|
|
if (other.blackhole)
|
|
blackhole = other.blackhole->clone();
|
|
else
|
|
blackhole = nullptr;
|
|
}
|
|
|
|
Character::~Character()
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Deconstructor called" << std::endl;
|
|
for (int i = 0; i < 4; ++i)
|
|
{
|
|
if (inventory[i])
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Deleting " << inventory[i]->getType() << " from inventory[" << i << "]" << std::endl;
|
|
delete inventory[i];
|
|
}
|
|
}
|
|
if (blackhole)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Deleting blackhole" << std::endl;
|
|
delete blackhole;
|
|
}
|
|
}
|
|
|
|
Character &Character::operator=(Character const &other)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Copy Assignment operator called" << std::endl;
|
|
if (this != &other)
|
|
{
|
|
name = other.name;
|
|
for (int i = 0; i < 4; ++i)
|
|
{
|
|
if (inventory[i])
|
|
delete inventory[i];
|
|
if (other.inventory[i])
|
|
inventory[i] = other.inventory[i];
|
|
else
|
|
inventory[i] = nullptr;
|
|
}
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
std::string const &Character::getName() const
|
|
{
|
|
return name;
|
|
}
|
|
|
|
void Character::equip(AMateria *m)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" equip function called" << std::endl;
|
|
if (!m)
|
|
return;
|
|
for (int i = 0; i < 4; ++i)
|
|
{
|
|
if (!inventory[i])
|
|
{
|
|
inventory[i] = m->clone();
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" equipped " << inventory[i]->getType() << std::endl;
|
|
delete m;
|
|
return;
|
|
}
|
|
}
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Inventory full, cannot equip " << m->getType() << std::endl;
|
|
}
|
|
|
|
void Character::unequip(int idx)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" unequip function called" << std::endl;
|
|
if (idx < 0 || idx >= 4)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Invalid index" << std::endl;
|
|
return;
|
|
}
|
|
if (inventory[idx])
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" unequipped " << inventory[idx]->getType() << std::endl;
|
|
dropMateria(inventory[idx]);
|
|
inventory[idx] = nullptr;
|
|
}
|
|
else
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" No materia equipped at index " << idx << std::endl;
|
|
}
|
|
|
|
void Character::use(int idx, ICharacter &target)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" use function called" << std::endl;
|
|
if (idx < 0 || idx >= 4)
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Invalid index" << std::endl;
|
|
return;
|
|
}
|
|
if (inventory[idx])
|
|
{
|
|
inventory[idx]->use(target);
|
|
}
|
|
else
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" No materia equipped at index " << idx << std::endl;
|
|
}
|
|
|
|
void Character::printInventory() const
|
|
{
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" Inventory:" << std::endl;
|
|
for (int i = 0; i < 4; ++i)
|
|
{
|
|
if (inventory[i])
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" [" << i << "] " << inventory[i]->getType() << std::endl;
|
|
else
|
|
std::cout << BACKGROUND11 << BOLD << "Character: "<< name << RESET <<" [" << i << "] empty" << std::endl;
|
|
}
|
|
}
|