-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
115 lines (98 loc) · 1.68 KB
/
Copy pathMap.cpp
File metadata and controls
115 lines (98 loc) · 1.68 KB
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
#include "Map.h"
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
Map::Map(const std::string& nextMap)
: m_nextMap(nextMap), m_currentMap(nextMap)
{
loadMap();
}
void Map::loadMap()
{
std::ifstream mapfile("./Maps/" + m_nextMap);
if (!mapfile.is_open())
{
std::cerr << "! Failed loading map: " + m_nextMap << std::endl;
return;
}
bool bValid = false;
std::string line;
while (std::getline(mapfile, line))
{
if (!bValid)
{
if (line == "--MAPFILE--")
{
bValid = true;
m_currentMap = m_nextMap;
continue;
}
else
{
std::cerr << "! Invalid file: " + m_nextMap << std::endl;
return;
}
}
std::stringstream ss(line);
std::string tag;
ss >> tag;
if (tag == "NEXT:")
{
ss >> m_nextMap;
}
else if (tag == "SIZE:")
{
ss >> m_mapSize.x >> m_mapSize.y;
}
else if (tag == "START:")
{
ss >> m_playerStartingPosition.x >> m_playerStartingPosition.y;
}
else if (tag == "TILE:")
{
int x, y;
ss >> x >> y;
m_tiles.insert(y * m_mapSize.x + x);
}
}
m_tilesToPaint = m_mapSize.x * m_mapSize.y - m_tiles.size();
}
void Map::purgeMap()
{
if (!m_tiles.empty())
{
m_tiles.clear();
}
m_playerStartingPosition = { -1, -1 };
}
bool Map::loadNext()
{
purgeMap();
if (m_nextMap != "NULL")
{
loadMap();
return true;
}
return false;
}
std::string Map::getCurrentMap() const
{
return m_currentMap;
}
sf::Vector2i Map::getMapSize() const
{
return m_mapSize;
}
const std::unordered_set<int>& Map::getTiles() const
{
return m_tiles;
}
sf::Vector2i Map::getPlayerStartingPosition() const
{
return m_playerStartingPosition;
}
int Map::getTilesToPaint() const
{
return m_tilesToPaint;
}