-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_Hero.cpp
86 lines (77 loc) · 1.71 KB
/
class_Hero.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
80
81
82
83
84
85
86
#include "Hero_Game.h"
///
/// All members of the Hero class
///
// Setters of life & power
void Hero::setLife(float l)
{
if( l >= 0 )
life = l;
else
life = 0;
}
void Hero::setPower(float p)
{
if( p >= 0 )
power = p;
else
power = 0;
}
// Finds where is the hero
void Hero::setHeroPos(Map &map)
{
for(int i=0; i<map.getLines(); i++)
for(int j=0; j<map.getColons(); j++)
if( map.arr[i][j] == '$')
{
line = i;
colon = j;
}
}
// Prints the table of the hero
void Hero::Print() const
{
cout<<endl;
cout<<"_____________________"<<endl
<<" LIFE POWER MANA|"<<endl
<<setw(5)<<this->getLife()<<" "
<<setw(7)<<this->getPower()<<" "
<<setw(6)<<this->getMana()<<"|"
<<"\n____________________|"<<endl;
if( hasSword() )
cout<<" SWOWRD ";
if( hasHelmet() )
cout<<" HELMET ";
if( hasShield() )
cout<<" SHIELD ";
}
// Saves all the information about the hero in a binary file
void Hero::SeveToFile(ofstream &file)
{
file.write((char*)&life,sizeof(life));
file.write((char*)&power,sizeof(power));
file.write((char*)&mana,sizeof(mana));
file.write((char*)&sword,sizeof(sword));
file.write((char*)&helmet,sizeof(helmet));
file.write((char*)&shield,sizeof(shield));
if (!file)
std::cerr << "Hero not saved correctly!";
}
// Crates new hero from a binary file
Hero::Hero(std::ifstream &file)
{
if(file.good())
{
file.read((char*)&life,sizeof(float));
file.read((char*)&power,sizeof(float));
file.read((char*)&mana,sizeof(int));
file.read((char*)&sword,sizeof(bool));
file.read((char*)&helmet,sizeof(bool));
file.read((char*)&shield,sizeof(bool));
}
else
{
isRunning = false;
fail = true;
}
}