aboutsummaryrefslogtreecommitdiff
path: root/cpp04/ex03/MateriaSource.cpp
blob: 6747a93acb5a228b048dc4ac4942b1f0e0022a60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   MateriaSource.cpp                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/14 17:19:10 by charles           #+#    #+#             */
/*   Updated: 2020/11/17 08:32:37 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "MateriaSource.hpp"

MateriaSource::MateriaSource()
    : m_learned_size(0) {}

MateriaSource::MateriaSource(MateriaSource const& other)
    : m_learned_size(0) { *this = other; }

MateriaSource& MateriaSource::operator=(MateriaSource const& other)
{
    destroyLearned();
    m_learned_size = other.m_learned_size;
    for (int i = 0; i < m_learned_size; i++)
        m_learned[i] = other.m_learned[i]->clone();
    return *this;
}

MateriaSource::~MateriaSource() { destroyLearned(); }

void MateriaSource::learnMateria(AMateria* materia)
{
    if (m_learned_size >= LEARNED_MAX_SIZE || materia == NULL)
    {
        delete materia;
        return;
    }
    m_learned[m_learned_size] = materia;
    m_learned_size++;
}

AMateria* MateriaSource::createMateria(std::string const& type)
{
    for (int i = 0; i < m_learned_size; i++)
        if (m_learned[i]->getType() == type)
            return m_learned[i]->clone();
    return NULL;
}

void MateriaSource::destroyLearned()
{
    for (int i = 0; i < m_learned_size; i++)
        delete m_learned[i];
}