-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventar.cpp
79 lines (71 loc) · 1.95 KB
/
Inventar.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Inventar.hpp"
EventGenerator *Inventar::getEquipment(unsigned index, unsigned y, unsigned x, unsigned bot, unsigned top, unsigned cost)
{
if (!index)
return new Weapon(y, x, bot, top);
else if (index == 1)
return new Armor(y, x, bot, top);
else if (index == 2)
return new Spell(y, x, bot, top, true, cost);
else if (index == 3)
return new Potion(y, x, bot, top);
throw MyException("Bad index", "EventGenerator *Inventar::getEquipment(unsigned, unsigned, unsigned, unsigned, unsigned, unsigned)");
}
void Inventar::print(const String &owner) const
{
Constants::out << owner << "'s inventar:\n\n";
for (unsigned i = 0; i < m_size; ++i)
this->operator[](i)->print();
}
bool Inventar::put(const HeroEquipment &el)
{
if (m_size < limit)
{
push_back(el.clone());
return true;
}
return false;
}
Inventar::~Inventar()
{
HeroEquipment *ptr;
while (!empty())
{
ptr = pop_back();
delete ptr;
}
}
HeroEquipment *Inventar::remove(unsigned index)
{
return pop_at(index);
}
void Inventar::write(std::ofstream &ofs) const
{
ofs << m_size << '\n';
for (unsigned i = 0; i < m_size; ++i)
{
if (data[i])
data[i]->save(ofs);
}
}
HeroEquipment *Inventar::readEquipment(std::ifstream &ifs, unsigned buf)
{
unsigned bonus;
float cost;
unsigned length;
ifs >> cost >> bonus >> length;
ifs.ignore();
char *name = new char[length + 1];
ifs.getline(name, length + 1);
HeroEquipment *res = nullptr;
if (buf == (unsigned)HeroEquipment::ID::ARMOR)
res = new Armor(bonus, name);
else if (buf == (unsigned)HeroEquipment::ID::POTION)
res = new Potion(bonus, name);
else if (buf == (int)HeroEquipment::ID::WEAPON)
res = new Weapon(bonus, name);
else if (buf == (int)HeroEquipment::ID::SPELL)
res = new Spell(bonus, cost, name);
delete[] name;
return res;
}