-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManager.cpp
119 lines (108 loc) · 3.95 KB
/
FileManager.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "FileManager.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
void FileManager::LoadFile(Unit* target, Point& position, int& health, int& score, Queue& units, LTexture* commando, LTexture* soldier, LTexture* jahaaz, LTexture* bosss, LTexture* goli,
LTexture* tank, LTexture* bosshealthbar, LTexture* missileshot, LTexture* explosion, LTexture* bossexplosion,
LTexture* power)
{
FILE* filePointer; //A pointer is created for file manipulation
filePointer = fopen("game.txt", "r"); //Opening file as read only
if (filePointer == NULL)
{
perror ("Error opening file");
return;
}
fgets(buff, 32, (FILE*)filePointer);
fgets(buff, 32, (FILE*)filePointer);
position.x=atoi(buff); //gets position for x-axis
fgets(buff, 32, (FILE*)filePointer);
position.y=atoi(buff); //gets position for y-axis
fgets(buff, 32, (FILE*)filePointer);
health=atoi(buff); //gets health
fgets(buff, 32, (FILE*)filePointer);
score=atoi(buff); //gets score
Unit* unit= NULL; //pointer is initially set to NULL
while(fgets(buff, 32, (FILE*)filePointer)!=NULL) //If line read is not NULL
{
Point point;
fgets(buff, 32, (FILE*)filePointer);
point.x=atoi(buff);
fgets(buff, 32, (FILE*)filePointer);
point.y=atoi(buff);
fgets(buff, 32, (FILE*)filePointer);
int state = atoi(buff);
fgets(buff, 32, (FILE*)filePointer);
int type = atoi(buff);
if (type==1) //checks for the type whether a soldier or a tank or an aircraft etc
{
unit=new Commando(commando, point, state);
}
else if (type==2)
{
unit=new Soldier(soldier, point, state);
}
else if (type==3)
{
unit=new AirCraft(jahaaz, point, state);
}
else if (type==4)
{
unit=new Tank(tank, point, state);
}
else if (type==5)
{
unit=new Boss(bosss, bosshealthbar, point, target);
}
else if (type==11)
{
unit=new Default(goli, point, position);
}
else if (type==12)
{
unit=new MachineGun(goli, point, position);
}
else if (type==13)
{
unit=new Missiles(missileshot, point, target);
}
else if (type==14)
{
unit=new EBullet(goli, point, position);
}
else if (type==21)
{
unit=new Explosion(explosion, point, state);
}
else if (type==22)
{
unit=new Explosion(bossexplosion, point, state);
}
else if (type==50)
{
unit=new Powerups(power, point, state);
}
else if (type==51)
{
unit=new Powerups(power, point, state);
}
units.Enqueue(unit);
}
}
void FileManager::SaveFile(Unit* player, Queue& units, int& score)
{
FILE* filewriter; //A new pointer is created
filewriter = fopen("game.txt", "w"); //Opening file as read only
if (filewriter == NULL)
{
perror ("New Game");
return;
}
fputs("[Player]\n", (FILE*)filewriter);
fputs(itoa(player->Getx(), buff, 10), (FILE*)filewriter); fputs("\n", (FILE*)filewriter);
fputs(itoa(player->Gety(), buff, 10), (FILE*)filewriter); fputs("\n", (FILE*)filewriter);
fputs(itoa(player->GetHealth(), buff, 10), (FILE*)filewriter); fputs("\n", (FILE*)filewriter);
fputs(itoa(score, buff, 10), (FILE*)filewriter); fputs("\n", (FILE*)filewriter);
units.Save(filewriter);
fclose(filewriter);
}