-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaypointManager.cpp
151 lines (126 loc) · 4.15 KB
/
WaypointManager.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "WaypointManager.h"
#include <SDL.h>
#include <iostream>
#include "Waypoint.h"
#include "TinyXML\tinyxml.h"
//Initialise the instance to null.
WaypointManager* WaypointManager::mInstance = NULL;
//--------------------------------------------------------------------------------------------------
WaypointManager::WaypointManager()
{
}
//--------------------------------------------------------------------------------------------------
WaypointManager::~WaypointManager()
{
mInstance = NULL;
for(unsigned int i = 0; i < mWaypoints.size(); i++)
delete mWaypoints[i];
mWaypoints.clear();
}
//--------------------------------------------------------------------------------------------------
WaypointManager* WaypointManager::Instance()
{
if(!mInstance)
{
mInstance = new WaypointManager;
}
return mInstance;
}
//--------------------------------------------------------------------------------------------------
void WaypointManager::Init(SDL_Renderer* renderer)
{
mInstance->LoadWaypoints(renderer);
}
//--------------------------------------------------------------------------------------------------
void WaypointManager::RenderWaypoints()
{
#ifdef WAYPOINTS_VISIBLE
for(unsigned int i = 0; i < mWaypoints.size(); i++)
{
mWaypoints[i]->Render();
}
#endif
}
//--------------------------------------------------------------------------------------------------
void WaypointManager::LoadWaypoints(SDL_Renderer* renderer)
{
//Get the whole xml document.
TiXmlDocument doc;
if(!doc.LoadFile(kTilemapPath))
{
cerr << doc.ErrorDesc() << endl;
}
//Now get the root element.
TiXmlElement* root = doc.FirstChildElement();
if(!root)
{
cerr << "Failed to load file: No root element." << endl;
doc.Clear();
}
else
{
float x = 0;
float y = 0;
int id = -1;
vector<int> connections;
//Jump to the first 'objectgroup' element.
for(TiXmlElement* groupElement = root->FirstChildElement("objectgroup"); groupElement != NULL; groupElement = groupElement->NextSiblingElement())
{
string name = groupElement->Attribute("name");
if(name == "WaypointLayer")
{
//Jump to the first 'object' element - within 'objectgroup'
for(TiXmlElement* objectElement = groupElement->FirstChildElement("object"); objectElement != NULL; objectElement = objectElement->NextSiblingElement())
{
string name = objectElement->Attribute("name");
if(name == "Waypoint")
{
x = (float)atof(objectElement->Attribute("x"));
y = (float)atof(objectElement->Attribute("y"));
//Jump to the first 'properties' element - within 'object'
for(TiXmlElement* propertiesElement = objectElement->FirstChildElement("properties"); propertiesElement != NULL; propertiesElement = propertiesElement->NextSiblingElement())
{
//Loop through the 'property' elements - within 'properties'
for(TiXmlElement* propertyElement = propertiesElement->FirstChildElement("property"); propertyElement != NULL; propertyElement = propertyElement->NextSiblingElement())
{
string name = propertyElement->Attribute("name");
if(name == "Connections")
{
//Split up the comma delimited connections.
stringstream ss(propertyElement->Attribute("value"));
int i;
while(ss >> i)
{
connections.push_back(i);
if(ss.peek() == ',')
ss.ignore();
}
}
else if(name == "ID")
{
id = atoi(propertyElement->Attribute("value"));
}
}
//Add the new waypoint with the read in details.
mWaypoints.push_back(new Waypoint(renderer, id, Vector2D(x,y), connections));
connections.clear();
}
}
}
}
}
}
}
//--------------------------------------------------------------------------------------------------
Waypoint* WaypointManager::GetWaypointWithID(int id)
{
//Search for this waypoint and return it.
for(unsigned int i = 0; i < mWaypoints.size(); i++)
{
if(mWaypoints[i]->GetID() == id)
return mWaypoints[i];
}
//If we reach here there was no such waypoint.
return NULL;
}
//--------------------------------------------------------------------------------------------------