-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapManager.cpp
122 lines (107 loc) · 2.72 KB
/
MapManager.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
120
121
122
#include "MapManager.h"
#include "Map.h"
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
MapManager::MapManager()
{}
MapManager::~MapManager()
{
for(std::vector<Map*>::iterator it = MapList.begin(); it != MapList.end(); it++){
delete (*it);
}
}
void MapManager::newMap(std::string name)
{
bool exists = false;
for(std::vector<Map*>::iterator it = MapList.begin(); it != MapList.end(); it++){
if((*it)->getMapName().compare(name) == 0){
exists = true;
}
}
if(!exists)
{
MapList.push_back(new Map(name));
}
}
void MapManager::addToMap(std::string obj)
{
Map* temp;
for(std::vector<Map*>::iterator it = MapList.begin(); it != MapList.end(); it++){
if((*it)->getMapName().compare(currentMap) == 0){
temp = (*it);
}
}
temp->addToMap(obj);
//(*MapList.begin())->addToMap(obj);
}
std::vector<std::string>* MapManager::getMapData()
{
for(std::vector<Map*>::iterator it = MapList.begin(); it != MapList.end(); it++){
if((*it)->getMapName().compare(currentMap) == 0){
return (*it)->getMap();
}
}
return nullptr;
//return (*MapList.begin())->getMap();
}
std::string MapManager::getMapName()
{
//return (*MapList.begin())->getMapName();
return currentMap;
}
void MapManager::loadMap(std::string fname)
{
fname = "./res/map/" + fname;
std::cout << "Loading File: " << fname << "\n";
std::ifstream ifs(fname, std::ios::binary);
std::string s;
ifs >> s;
currentMap = s;
if(!mapExists(s))
{
newMap(s);
ifs >> s;
while(s.compare("mapend")){
//std::cout<< s << "\n";
addToMap(s);
ifs >> s;
}
}
else
{
std::cout << "Map Already exists!!!!\n";
}
ifs.close();
std::cout << "File Loaded \n";
}
void MapManager::saveMap(const char* fname)
{
std::cout << "Generating a new Map file\n";
std::ofstream ofs(fname, std::ios::binary);
std::cout << "Writing to file...\n";
std::string MapName = getMapName();
ofs << MapName << " ";
std::vector<std::string>* pMapData = getMapData();
for(std::vector<std::string>::iterator it = (*pMapData).begin(); it != (*pMapData).end(); it++){
ofs << (*it);
ofs << " ";
}
ofs << "mapend";
std::cout << "Done\n";
ofs.close();
}
void MapManager::showMap()
{
(*MapList.begin())->showData();
}
bool MapManager::mapExists(std::string name)
{
for(std::vector<Map*>::iterator it = MapList.begin(); it != MapList.end(); it++){
if((*it)->getMapName().compare(name) == 0){
return true;
}
}
return false;
}