|
|
|
@ -7,106 +7,121 @@ using namespace olc; |
|
|
|
|
struct XMLTag{ |
|
|
|
|
std::string tag; |
|
|
|
|
std::map<std::string,std::string> data; |
|
|
|
|
const std::string FormatTagData(std::map<std::string,std::string>tiles) { |
|
|
|
|
const std::string FormatTagData(std::map<std::string,std::string>tiles); |
|
|
|
|
friend std::ostream& operator << (std::ostream& os, XMLTag& rhs); |
|
|
|
|
int GetInteger(std::string dataTag); |
|
|
|
|
double GetDouble(std::string dataTag); |
|
|
|
|
bool GetBool(std::string dataTag); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct LayerTag{ |
|
|
|
|
XMLTag tag; |
|
|
|
|
std::vector<std::vector<int>> tiles; |
|
|
|
|
std::string str(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct SpawnerTag{ |
|
|
|
|
XMLTag ObjectData; |
|
|
|
|
std::vector<XMLTag>properties; |
|
|
|
|
std::string str(); |
|
|
|
|
friend std::ostream& operator << (std::ostream& os, SpawnerTag& rhs); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct Map{ |
|
|
|
|
XMLTag MapData; |
|
|
|
|
XMLTag TilesetData; |
|
|
|
|
std::vector<LayerTag> LayerData; |
|
|
|
|
std::vector<SpawnerTag> SpawnerData; |
|
|
|
|
std::string FormatLayerData(std::ostream& os, std::vector<LayerTag>tiles); |
|
|
|
|
std::string FormatSpawnerData(std::ostream& os, std::vector<SpawnerTag>tiles); |
|
|
|
|
friend std::ostream& operator << (std::ostream& os, Map& rhs); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class TMXParser{ |
|
|
|
|
public: |
|
|
|
|
Map GetData(); |
|
|
|
|
private: |
|
|
|
|
Map parsedMapInfo; |
|
|
|
|
bool buildingSpawner=false; |
|
|
|
|
SpawnerTag obj; |
|
|
|
|
void ParseTag(std::string tag); |
|
|
|
|
public: |
|
|
|
|
TMXParser(std::string file); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
#ifdef TMX_PARSER_SETUP |
|
|
|
|
#undef TMX_PARSER_SETUP |
|
|
|
|
const std::string XMLTag::FormatTagData(std::map<std::string,std::string>tiles){ |
|
|
|
|
std::string displayStr=""; |
|
|
|
|
for (std::map<std::string,std::string>::iterator it=data.begin();it!=data.end();it++) { |
|
|
|
|
displayStr+=" "+it->first+": "+it->second+"\n"; |
|
|
|
|
} |
|
|
|
|
return displayStr; |
|
|
|
|
} |
|
|
|
|
friend std::ostream& operator << (std::ostream& os, XMLTag& rhs) {
|
|
|
|
|
std::ostream& operator << (std::ostream& os, XMLTag& rhs){
|
|
|
|
|
os <<
|
|
|
|
|
rhs.tag <<"\n"<<
|
|
|
|
|
rhs.FormatTagData(rhs.data) <<"\n"; |
|
|
|
|
|
|
|
|
|
return os; } |
|
|
|
|
|
|
|
|
|
int GetInteger(std::string dataTag) { |
|
|
|
|
return os; |
|
|
|
|
} |
|
|
|
|
int XMLTag::GetInteger(std::string dataTag) { |
|
|
|
|
return std::stoi(data[dataTag]); |
|
|
|
|
} |
|
|
|
|
double GetDouble(std::string dataTag) { |
|
|
|
|
double XMLTag::GetDouble(std::string dataTag) { |
|
|
|
|
return std::stod(data[dataTag]); |
|
|
|
|
} |
|
|
|
|
bool GetBool(std::string dataTag) { |
|
|
|
|
bool XMLTag::GetBool(std::string dataTag) { |
|
|
|
|
if (data[dataTag]=="0") { |
|
|
|
|
return false; |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct LayerTag{ |
|
|
|
|
XMLTag tag; |
|
|
|
|
std::vector<std::vector<int>> tiles; |
|
|
|
|
std::string str() { |
|
|
|
|
std::string LayerTag::str() { |
|
|
|
|
std::string displayStr=tag.tag+"\n"+tag.FormatTagData(tag.data); |
|
|
|
|
displayStr+=" DATA ("+std::to_string(tiles[0].size())+"x"+std::to_string(tiles.size())+")\n"; |
|
|
|
|
return displayStr; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct SpawnerTag{ |
|
|
|
|
XMLTag ObjectData; |
|
|
|
|
std::vector<XMLTag>properties; |
|
|
|
|
std::string str() { |
|
|
|
|
std::string SpawnerTag::str() { |
|
|
|
|
std::string displayStr=ObjectData.tag+"\n"+ObjectData.FormatTagData(ObjectData.data); |
|
|
|
|
for(XMLTag tag:properties){ |
|
|
|
|
displayStr+=" ("+tag.FormatTagData(tag.data)+")\n"; |
|
|
|
|
} |
|
|
|
|
return displayStr; |
|
|
|
|
} |
|
|
|
|
friend std::ostream& operator << (std::ostream& os, SpawnerTag& rhs) {
|
|
|
|
|
std::ostream& operator << (std::ostream& os, SpawnerTag& rhs) {
|
|
|
|
|
os << rhs.str()<<"\n"; |
|
|
|
|
|
|
|
|
|
return os; } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct Map{ |
|
|
|
|
XMLTag MapData; |
|
|
|
|
XMLTag TilesetData; |
|
|
|
|
std::vector<LayerTag> LayerData; |
|
|
|
|
std::vector<SpawnerTag> SpawnerData; |
|
|
|
|
std::string FormatLayerData(std::ostream& os, std::vector<LayerTag>tiles) { |
|
|
|
|
return os; |
|
|
|
|
} |
|
|
|
|
std::string Map::FormatLayerData(std::ostream& os, std::vector<LayerTag>tiles) { |
|
|
|
|
std::string displayStr; |
|
|
|
|
for (int i=0;i<LayerData.size();i++) { |
|
|
|
|
displayStr+=LayerData[i].str(); |
|
|
|
|
} |
|
|
|
|
return displayStr; |
|
|
|
|
} |
|
|
|
|
std::string FormatSpawnerData(std::ostream& os, std::vector<SpawnerTag>tiles) { |
|
|
|
|
std::string Map::FormatSpawnerData(std::ostream& os, std::vector<SpawnerTag>tiles) { |
|
|
|
|
std::string displayStr; |
|
|
|
|
for (int i=0;i<SpawnerData.size();i++) { |
|
|
|
|
displayStr+=SpawnerData[i].str(); |
|
|
|
|
} |
|
|
|
|
return displayStr; |
|
|
|
|
} |
|
|
|
|
friend std::ostream& operator << (std::ostream& os, Map& rhs) {
|
|
|
|
|
std::ostream& operator <<(std::ostream& os, Map& rhs) {
|
|
|
|
|
os <<
|
|
|
|
|
rhs.MapData <<"\n"<<
|
|
|
|
|
rhs.TilesetData <<"\n"<<
|
|
|
|
|
rhs.FormatLayerData(os,rhs.LayerData) <<"\n"<< |
|
|
|
|
rhs.FormatSpawnerData(os,rhs.SpawnerData)<<"\n"; |
|
|
|
|
|
|
|
|
|
return os; } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class TMXParser{ |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
|
|
Map GetData() { |
|
|
|
|
return os; |
|
|
|
|
} |
|
|
|
|
Map TMXParser::GetData() { |
|
|
|
|
return parsedMapInfo; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
|
|
Map parsedMapInfo; |
|
|
|
|
bool buildingSpawner=false; |
|
|
|
|
SpawnerTag obj; |
|
|
|
|
|
|
|
|
|
void ParseTag(std::string tag) { |
|
|
|
|
void TMXParser::ParseTag(std::string tag) { |
|
|
|
|
XMLTag newTag; |
|
|
|
|
//First character is a '<' so we discard it.
|
|
|
|
|
tag.erase(0,1); tag.erase(tag.length()-1,1); //Erase the first and last characters in the tag. Now parse by spaces.
|
|
|
|
@ -156,39 +171,37 @@ private: |
|
|
|
|
if (newTag.tag=="map") { |
|
|
|
|
parsedMapInfo.MapData=newTag; |
|
|
|
|
} else
|
|
|
|
|
if (newTag.tag=="tileset") { |
|
|
|
|
parsedMapInfo.TilesetData=newTag; |
|
|
|
|
} else
|
|
|
|
|
if (newTag.tag=="layer") { |
|
|
|
|
LayerTag l = {newTag}; |
|
|
|
|
parsedMapInfo.LayerData.push_back(l); |
|
|
|
|
} else
|
|
|
|
|
if (newTag.tag=="object") { |
|
|
|
|
if(buildingSpawner){ |
|
|
|
|
parsedMapInfo.SpawnerData.push_back(obj); |
|
|
|
|
} |
|
|
|
|
buildingSpawner=true; |
|
|
|
|
obj={newTag}; |
|
|
|
|
goto spawnerResetSkip; |
|
|
|
|
} else
|
|
|
|
|
if (newTag.tag=="property"&&buildingSpawner) { |
|
|
|
|
if(newTag.data["propertytype"]=="MonsterName"){ |
|
|
|
|
obj.properties.push_back(newTag); |
|
|
|
|
} |
|
|
|
|
goto spawnerResetSkip; |
|
|
|
|
} else { |
|
|
|
|
std::cout<<"Unsupported tag format! Ignoring."<<"\n"; |
|
|
|
|
} |
|
|
|
|
if(buildingSpawner){ |
|
|
|
|
parsedMapInfo.SpawnerData.push_back(obj); |
|
|
|
|
} |
|
|
|
|
buildingSpawner=false; |
|
|
|
|
spawnerResetSkip: |
|
|
|
|
std::cout<<"\n"<<"=============\n"; |
|
|
|
|
if (newTag.tag=="tileset") { |
|
|
|
|
parsedMapInfo.TilesetData=newTag; |
|
|
|
|
} else
|
|
|
|
|
if (newTag.tag=="layer") { |
|
|
|
|
LayerTag l = {newTag}; |
|
|
|
|
parsedMapInfo.LayerData.push_back(l); |
|
|
|
|
} else
|
|
|
|
|
if (newTag.tag=="object"&&newTag.data["type"]=="SpawnGroup") { |
|
|
|
|
if(buildingSpawner){ |
|
|
|
|
parsedMapInfo.SpawnerData.push_back(obj); |
|
|
|
|
} |
|
|
|
|
buildingSpawner=true; |
|
|
|
|
obj={newTag}; |
|
|
|
|
goto spawnerResetSkip; |
|
|
|
|
} else
|
|
|
|
|
if (newTag.tag=="property"&&buildingSpawner) { |
|
|
|
|
if(newTag.data["propertytype"]=="MonsterName"){ |
|
|
|
|
obj.properties.push_back(newTag); |
|
|
|
|
} |
|
|
|
|
goto spawnerResetSkip; |
|
|
|
|
} else { |
|
|
|
|
std::cout<<"Unsupported tag format! Ignoring."<<"\n"; |
|
|
|
|
} |
|
|
|
|
if(buildingSpawner){ |
|
|
|
|
parsedMapInfo.SpawnerData.push_back(obj); |
|
|
|
|
} |
|
|
|
|
buildingSpawner=false; |
|
|
|
|
spawnerResetSkip: |
|
|
|
|
std::cout<<"\n"<<"=============\n"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
TMXParser(std::string file){ |
|
|
|
|
TMXParser::TMXParser(std::string file){ |
|
|
|
|
std::ifstream f(file,std::ios::in); |
|
|
|
|
|
|
|
|
|
std::string accumulator=""; |
|
|
|
@ -236,4 +249,4 @@ public: |
|
|
|
|
|
|
|
|
|
std::cout<<"Parsed Map Data:\n"<<parsedMapInfo<<"\n"; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
#endif |