/
Layer.cpp
79 lines (62 loc) · 1.36 KB
/
Layer.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
#include "Layer.h"
#include "Factory.h"
CLayer::CLayer(void)
{
m_mapTiles = new TileGrid;
}
CLayer::~CLayer(void)
{
//delete m_mapTiles;
}
int CLayer::getWidth()
{
return m_nWidth;
}
int CLayer::getHeight()
{
return m_nHeight;
}
string CLayer::getName()
{
return m_sName;
}
TileGrid * CLayer::getTileGrid()
{
return m_mapTiles;
}
int CLayer::parseTmx(TiXmlNode *xmlnLayer)
{
TiXmlElement *xmleLayer = xmlnLayer->ToElement();
m_sName = xmleLayer->Attribute("name");
m_nWidth = atoi(xmleLayer->Attribute("width"));
m_nHeight = atoi(xmleLayer->Attribute("height"));
parseLayerData(xmlnLayer);
return 0;
}
int CLayer::parseLayerData(TiXmlNode *xmlnLayer)
{
TiXmlNode *xmlnData = xmlnLayer->FirstChild("data");
TiXmlNode *xmlnTile = xmlnData->FirstChild("tile");
TiXmlElement *xmleTile = NULL;
CTile *tlTile = NULL;
TileCoord tc;
int nTileGId;
tc.x = 0;
tc.y = 0;
while (xmlnTile) {
xmleTile = xmlnTile->ToElement();
nTileGId = atoi(xmleTile->Attribute("gid"));
// If no valid GId is assigned, skip insertion
if (nTileGId != 0) {
m_mapTiles->insert(TileGrid::value_type(tc, CFactory::createTile(nTileGId)));
}
xmlnTile = xmlnData->IterateChildren("tile", xmlnTile);
if (tc.x == m_nWidth - 1) {
tc.y += 1;
tc.x = 0;
} else {
tc.x += 1;
}
}
return 0;
}