Repository navigation
Expand file tree
/
Copy pathlevel_loader.cpp
More file actions
163 lines (146 loc) · 3.76 KB
/
Copy pathlevel_loader.cpp
File metadata and controls
163 lines (146 loc) · 3.76 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
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
152
153
154
155
156
157
158
159
160
161
162
163
#include "level_loader.h"
using std::ifstream;
using std::stringstream;
/**
* Method: parse_color
* Purpose: Maps a colour string from the file to a colour
*
* @param color_name the color name string read from the file
*/
color level_loader::parse_color(const string &color_name)
{
if (color_name == "DARK_BLUE")
{
return COLOR_DARK_BLUE;
}
if (color_name == "DARK_GREEN")
{
return COLOR_DARK_GREEN;
}
if (color_name == "DARK_VIOLET")
{
return COLOR_DARK_VIOLET;
}
if (color_name == "BLACK")
{
return COLOR_BLACK;
}
if (color_name == "PURPLE")
{
return COLOR_PURPLE;
}
if (color_name == "DARK_RED")
{
return COLOR_DARK_RED;
}
// Default if colours not recognised
return COLOR_BLACK;
}
/**
* Method: parse_obstacle_type
* Purpose: Maps a string type read from the file to an obstacle_type enum value
*
* @param type_name the string obstacle type read from the file
*/
obstacle_type level_loader::parse_obstacle_type(const string &type_name)
{
if (type_name == "SPIKE")
{
return SPIKE;
}
if (type_name == "DOUBLE_SPIKE")
{
return DOUBLE_SPIKE;
}
if (type_name == "BLOCK")
{
return BLOCK;
}
if (type_name == "TALL_BLOCK")
{
return TALL_BLOCK;
}
if (type_name == "CEILING_SPIKE")
{
return CEILING_SPIKE;
}
// Default if string type not recognised
return SPIKE;
}
/**
* Procedure: parse_settings
* Purpose: Reads a SPEED or BG_COLOR line and sets it on the level
*
* @param keyword the first string read from the file line
* @param value the second string read from the same file line representing its value
* @param level the difficulty mode playing
*/
void level_loader::parse_settings(const string &keyword, const string &value, level &level)
{
if (keyword == "SPEED")
{
level.set_scroll_speed(stod(value));
}
else if (keyword == "BG_COLOR")
{
level.set_bg_color(parse_color(value));
}
}
/**
* Procedure: parse_obstacle
* Purpose: Reads an OBSTACLE line and adds it to the level
* File Format: OBSTACLE TYPE X Y
*
* @param type_name the string obstacle type read from the file
* @param x the horizontal position read from the level file
* @param y the vertical position read from the level file
* @param level the difficulty mode playing
*/
void level_loader::parse_obstacle(const string &type_name, double x, double y, level &level)
{
obstacle_type type = parse_obstacle_type(type_name);
obstacle obstacle(type, x, y);
level.add_obstacle(obstacle);
}
/**
* Function: load
* Purpose: Opens the level file, reads it line by line, calls parse_settings
* or parse_obstacle for each line, returns a fully built level object
*
* @param filename the directory location of the file to read
*/
level level_loader::load(const string &filename)
{
level level;
// Open the file using fstream
ifstream file(filename);
if (!file.is_open())
{
write_line("Error: Could not open level file: " + filename);
return level;
}
string line;
// Read the file line by line
while (getline(file, line))
{
// Use stringstream to split the line into words
stringstream stream(line);
string keyword;
stream >> keyword;
if (keyword == "SPEED" || keyword == "BG_COLOR")
{
string value;
stream >> value;
parse_settings(keyword, value, level);
}
else if (keyword == "OBSTACLE")
{
string type_name;
double x, y;
stream >> type_name >> x >> y;
parse_obstacle(type_name, x, y, level);
}
}
file.close();
return level;
}