Revamped TMXParser.h such that there is a separation between function declarations and definitions for modular support.
This commit is contained in:
@@ -7,6 +7,9 @@
|
|||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "olcUTIL_Camera2D.h"
|
#include "olcUTIL_Camera2D.h"
|
||||||
#include "Effect.h"
|
#include "Effect.h"
|
||||||
|
#include "Map.h"
|
||||||
|
#include "TMXParser.h"
|
||||||
|
|
||||||
|
|
||||||
class Crawler : public olc::PixelGameEngine
|
class Crawler : public olc::PixelGameEngine
|
||||||
{
|
{
|
||||||
@@ -17,6 +20,7 @@ class Crawler : public olc::PixelGameEngine
|
|||||||
GFX_Heart,GFX_BLOCK_BUBBLE,GFX_Ranger_Sheet,GFX_Wizard_Sheet,
|
GFX_Heart,GFX_BLOCK_BUBBLE,GFX_Ranger_Sheet,GFX_Wizard_Sheet,
|
||||||
GFX_Battlecry_Effect,GFX_Mana,GFX_SonicSlash;
|
GFX_Battlecry_Effect,GFX_Mana,GFX_SonicSlash;
|
||||||
std::vector<Effect>foregroundEffects,backgroundEffects;
|
std::vector<Effect>foregroundEffects,backgroundEffects;
|
||||||
|
std::map<MapName,Map>MAP_DATA;
|
||||||
vf2d worldShake={};
|
vf2d worldShake={};
|
||||||
float worldShakeTime=0;
|
float worldShakeTime=0;
|
||||||
float lastWorldShakeAdjust=0;
|
float lastWorldShakeAdjust=0;
|
||||||
|
|||||||
@@ -171,6 +171,7 @@
|
|||||||
<ClInclude Include="DamageNumber.h" />
|
<ClInclude Include="DamageNumber.h" />
|
||||||
<ClInclude Include="DEFINES.h" />
|
<ClInclude Include="DEFINES.h" />
|
||||||
<ClInclude Include="Effect.h" />
|
<ClInclude Include="Effect.h" />
|
||||||
|
<ClInclude Include="Map.h" />
|
||||||
<ClInclude Include="Monster.h" />
|
<ClInclude Include="Monster.h" />
|
||||||
<ClInclude Include="olcPGEX_TransformedView.h" />
|
<ClInclude Include="olcPGEX_TransformedView.h" />
|
||||||
<ClInclude Include="olcPixelGameEngine.h" />
|
<ClInclude Include="olcPixelGameEngine.h" />
|
||||||
|
|||||||
@@ -81,6 +81,9 @@
|
|||||||
<ClInclude Include="TMXParser.h">
|
<ClInclude Include="TMXParser.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Map.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Player.cpp">
|
<ClCompile Include="Player.cpp">
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
enum MapName{
|
||||||
|
LEVEL1,
|
||||||
|
LEVEL2,
|
||||||
|
};
|
||||||
+109
-96
@@ -7,60 +7,24 @@ using namespace olc;
|
|||||||
struct XMLTag{
|
struct XMLTag{
|
||||||
std::string tag;
|
std::string tag;
|
||||||
std::map<std::string,std::string> data;
|
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);
|
||||||
std::string displayStr="";
|
friend std::ostream& operator << (std::ostream& os, XMLTag& rhs);
|
||||||
for (std::map<std::string,std::string>::iterator it=data.begin();it!=data.end();it++) {
|
int GetInteger(std::string dataTag);
|
||||||
displayStr+=" "+it->first+": "+it->second+"\n";
|
double GetDouble(std::string dataTag);
|
||||||
}
|
bool GetBool(std::string dataTag);
|
||||||
return displayStr;
|
|
||||||
}
|
|
||||||
friend 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 std::stoi(data[dataTag]);
|
|
||||||
}
|
|
||||||
double GetDouble(std::string dataTag) {
|
|
||||||
return std::stod(data[dataTag]);
|
|
||||||
}
|
|
||||||
bool GetBool(std::string dataTag) {
|
|
||||||
if (data[dataTag]=="0") {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LayerTag{
|
struct LayerTag{
|
||||||
XMLTag tag;
|
XMLTag tag;
|
||||||
std::vector<std::vector<int>> tiles;
|
std::vector<std::vector<int>> tiles;
|
||||||
std::string str() {
|
std::string 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{
|
struct SpawnerTag{
|
||||||
XMLTag ObjectData;
|
XMLTag ObjectData;
|
||||||
std::vector<XMLTag>properties;
|
std::vector<XMLTag>properties;
|
||||||
std::string str() {
|
std::string str();
|
||||||
std::string displayStr=ObjectData.tag+"\n"+ObjectData.FormatTagData(ObjectData.data);
|
friend std::ostream& operator << (std::ostream& os, SpawnerTag& rhs);
|
||||||
for(XMLTag tag:properties){
|
|
||||||
displayStr+=" ("+tag.FormatTagData(tag.data)+")\n";
|
|
||||||
}
|
|
||||||
return displayStr;
|
|
||||||
}
|
|
||||||
friend std::ostream& operator << (std::ostream& os, SpawnerTag& rhs) {
|
|
||||||
os << rhs.str()<<"\n";
|
|
||||||
|
|
||||||
return os; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Map{
|
struct Map{
|
||||||
@@ -68,45 +32,96 @@ struct Map{
|
|||||||
XMLTag TilesetData;
|
XMLTag TilesetData;
|
||||||
std::vector<LayerTag> LayerData;
|
std::vector<LayerTag> LayerData;
|
||||||
std::vector<SpawnerTag> SpawnerData;
|
std::vector<SpawnerTag> SpawnerData;
|
||||||
std::string FormatLayerData(std::ostream& os, std::vector<LayerTag>tiles) {
|
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;
|
||||||
|
}
|
||||||
|
std::ostream& operator << (std::ostream& os, XMLTag& rhs){
|
||||||
|
os <<
|
||||||
|
rhs.tag <<"\n"<<
|
||||||
|
rhs.FormatTagData(rhs.data) <<"\n";
|
||||||
|
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
int XMLTag::GetInteger(std::string dataTag) {
|
||||||
|
return std::stoi(data[dataTag]);
|
||||||
|
}
|
||||||
|
double XMLTag::GetDouble(std::string dataTag) {
|
||||||
|
return std::stod(data[dataTag]);
|
||||||
|
}
|
||||||
|
bool XMLTag::GetBool(std::string dataTag) {
|
||||||
|
if (data[dataTag]=="0") {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
std::ostream& operator << (std::ostream& os, SpawnerTag& rhs) {
|
||||||
|
os << rhs.str()<<"\n";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
std::string Map::FormatLayerData(std::ostream& os, std::vector<LayerTag>tiles) {
|
||||||
std::string displayStr;
|
std::string displayStr;
|
||||||
for (int i=0;i<LayerData.size();i++) {
|
for (int i=0;i<LayerData.size();i++) {
|
||||||
displayStr+=LayerData[i].str();
|
displayStr+=LayerData[i].str();
|
||||||
}
|
}
|
||||||
return displayStr;
|
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;
|
std::string displayStr;
|
||||||
for (int i=0;i<SpawnerData.size();i++) {
|
for (int i=0;i<SpawnerData.size();i++) {
|
||||||
displayStr+=SpawnerData[i].str();
|
displayStr+=SpawnerData[i].str();
|
||||||
}
|
}
|
||||||
return displayStr;
|
return displayStr;
|
||||||
}
|
}
|
||||||
friend std::ostream& operator << (std::ostream& os, Map& rhs) {
|
std::ostream& operator <<(std::ostream& os, Map& rhs) {
|
||||||
os <<
|
os <<
|
||||||
rhs.MapData <<"\n"<<
|
rhs.MapData <<"\n"<<
|
||||||
rhs.TilesetData <<"\n"<<
|
rhs.TilesetData <<"\n"<<
|
||||||
rhs.FormatLayerData(os,rhs.LayerData) <<"\n"<<
|
rhs.FormatLayerData(os,rhs.LayerData) <<"\n"<<
|
||||||
rhs.FormatSpawnerData(os,rhs.SpawnerData)<<"\n";
|
rhs.FormatSpawnerData(os,rhs.SpawnerData)<<"\n";
|
||||||
|
|
||||||
return os; }
|
return os;
|
||||||
};
|
}
|
||||||
|
Map TMXParser::GetData() {
|
||||||
class TMXParser{
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
Map GetData() {
|
|
||||||
return parsedMapInfo;
|
return parsedMapInfo;
|
||||||
}
|
}
|
||||||
|
void TMXParser::ParseTag(std::string tag) {
|
||||||
private:
|
|
||||||
|
|
||||||
Map parsedMapInfo;
|
|
||||||
bool buildingSpawner=false;
|
|
||||||
SpawnerTag obj;
|
|
||||||
|
|
||||||
void ParseTag(std::string tag) {
|
|
||||||
XMLTag newTag;
|
XMLTag newTag;
|
||||||
//First character is a '<' so we discard it.
|
//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.
|
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") {
|
if (newTag.tag=="map") {
|
||||||
parsedMapInfo.MapData=newTag;
|
parsedMapInfo.MapData=newTag;
|
||||||
} else
|
} else
|
||||||
if (newTag.tag=="tileset") {
|
if (newTag.tag=="tileset") {
|
||||||
parsedMapInfo.TilesetData=newTag;
|
parsedMapInfo.TilesetData=newTag;
|
||||||
} else
|
} else
|
||||||
if (newTag.tag=="layer") {
|
if (newTag.tag=="layer") {
|
||||||
LayerTag l = {newTag};
|
LayerTag l = {newTag};
|
||||||
parsedMapInfo.LayerData.push_back(l);
|
parsedMapInfo.LayerData.push_back(l);
|
||||||
} else
|
} else
|
||||||
if (newTag.tag=="object") {
|
if (newTag.tag=="object"&&newTag.data["type"]=="SpawnGroup") {
|
||||||
if(buildingSpawner){
|
if(buildingSpawner){
|
||||||
parsedMapInfo.SpawnerData.push_back(obj);
|
parsedMapInfo.SpawnerData.push_back(obj);
|
||||||
}
|
}
|
||||||
buildingSpawner=true;
|
buildingSpawner=true;
|
||||||
obj={newTag};
|
obj={newTag};
|
||||||
goto spawnerResetSkip;
|
goto spawnerResetSkip;
|
||||||
} else
|
} else
|
||||||
if (newTag.tag=="property"&&buildingSpawner) {
|
if (newTag.tag=="property"&&buildingSpawner) {
|
||||||
if(newTag.data["propertytype"]=="MonsterName"){
|
if(newTag.data["propertytype"]=="MonsterName"){
|
||||||
obj.properties.push_back(newTag);
|
obj.properties.push_back(newTag);
|
||||||
}
|
}
|
||||||
goto spawnerResetSkip;
|
goto spawnerResetSkip;
|
||||||
} else {
|
} else {
|
||||||
std::cout<<"Unsupported tag format! Ignoring."<<"\n";
|
std::cout<<"Unsupported tag format! Ignoring."<<"\n";
|
||||||
}
|
}
|
||||||
if(buildingSpawner){
|
if(buildingSpawner){
|
||||||
parsedMapInfo.SpawnerData.push_back(obj);
|
parsedMapInfo.SpawnerData.push_back(obj);
|
||||||
}
|
}
|
||||||
buildingSpawner=false;
|
buildingSpawner=false;
|
||||||
spawnerResetSkip:
|
spawnerResetSkip:
|
||||||
std::cout<<"\n"<<"=============\n";
|
std::cout<<"\n"<<"=============\n";
|
||||||
}
|
}
|
||||||
|
TMXParser::TMXParser(std::string file){
|
||||||
public:
|
|
||||||
TMXParser(std::string file){
|
|
||||||
std::ifstream f(file,std::ios::in);
|
std::ifstream f(file,std::ios::in);
|
||||||
|
|
||||||
std::string accumulator="";
|
std::string accumulator="";
|
||||||
@@ -236,4 +249,4 @@ public:
|
|||||||
|
|
||||||
std::cout<<"Parsed Map Data:\n"<<parsedMapInfo<<"\n";
|
std::cout<<"Parsed Map Data:\n"<<parsedMapInfo<<"\n";
|
||||||
}
|
}
|
||||||
};
|
#endif
|
||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
#define VERSION_MAJOR 0
|
#define VERSION_MAJOR 0
|
||||||
#define VERSION_MINOR 2
|
#define VERSION_MINOR 2
|
||||||
#define VERSION_PATCH 0
|
#define VERSION_PATCH 0
|
||||||
#define VERSION_BUILD 104
|
#define VERSION_BUILD 107
|
||||||
|
|
||||||
#define stringify(a) stringify_(a)
|
#define stringify(a) stringify_(a)
|
||||||
#define stringify_(a) #a
|
#define stringify_(a) #a
|
||||||
|
|||||||
@@ -2,3 +2,5 @@
|
|||||||
#include "olcPixelGameEngine.h"
|
#include "olcPixelGameEngine.h"
|
||||||
#define OLC_PGEX_TRANSFORMEDVIEW
|
#define OLC_PGEX_TRANSFORMEDVIEW
|
||||||
#include "olcPGEX_TransformedView.h"
|
#include "olcPGEX_TransformedView.h"
|
||||||
|
#define TMX_PARSER_SETUP
|
||||||
|
#include "TMXParser.h"
|
||||||
Reference in New Issue
Block a user