-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelLoader.cpp
More file actions
138 lines (110 loc) · 3.61 KB
/
Copy pathLevelLoader.cpp
File metadata and controls
138 lines (110 loc) · 3.61 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
////////////////////////////////////// INCLUDES & FORWARDS /////////////////////////////////////////////
#include "LevelLoader.h"
#include <algorithm>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
////////////////////////////////////////// discoverLevelFiles /////////////////////////////////////////////
// Made with AI
std::vector<std::string> LevelLoader::discoverLevelFiles()
{
std::vector<std::string> files;
std::error_code ec;
for (const auto& entry : fs::directory_iterator(".", ec))
{
if (entry.is_regular_file())
{
std::string filename = entry.path().filename().string();
if (filename.length() >= 7 &&
filename.substr(filename.length() - 7) == ".screen")
files.push_back(filename);
else if (filename.length() >= 11 &&
filename.substr(filename.length() - 11) == ".screen.txt")
files.push_back(filename);
}
}
std::sort(files.begin(), files.end());
return files;
}
////////////////////////////////////////// loadScreenFile /////////////////////////////////////////////
Screen *LevelLoader::loadScreenFile(const std::string& filename, RoomMetadata &metadata)
{
std::ifstream file(filename);
if (!file.is_open()) return nullptr;
std::string layout[MAX_Y];
for (int y = 0; y < MAX_Y; y++)
{
if (!std::getline(file, layout[y])) layout[y] = std::string(MAX_X, ' ');
while (layout[y].length() < static_cast<size_t>(MAX_X)) layout[y] += ' ';
}
std::string key;
while (file >> key)
{
if (key == "SPAWN")
{
int x, y;
file >> x >> y;
if (x < 0 || x >= MAX_X || y < 0 || y >= MAX_Y) return nullptr;
metadata.spawnPoint = Point(x, y);
}
else if (key == "SPAWN_PREV")
{
int x, y;
file >> x >> y;
if (x < 0 || x >= MAX_X || y < 0 || y >= MAX_Y) return nullptr;
metadata.spawnPointFromNext = Point(x, y);
}
else if (key == "NEXT_ROOM") file >> metadata.nextRoomId;
else if (key == "PREV_ROOM") file >> metadata.prevRoomId;
else if (key == "DOOR")
{
int id, keys, switches;
int targetRoom = -1;
file >> id >> keys >> switches;
while (file.peek() == ' ') file.ignore();
if (file.peek() != '\n' && file.peek() != EOF)
{
char c = file.peek();
if (isdigit(c) || c == '-') file >> targetRoom;
}
metadata.doorConfigs.push_back(std::make_tuple(id, keys, switches, targetRoom));
}
else if (key == "DARK_ZONE")
{
int x1, y1, x2, y2;
file >> x1 >> y1 >> x2 >> y2;
if (x1 < 0 || x1 >= MAX_X || y1 < 0 || y1 >= MAX_Y || x2 < 0 || x2 >= MAX_X || y2 < 0 || y2 >= MAX_Y)
return nullptr;
metadata.darkZones.push_back(DarkZone(x1, y1, x2, y2));
}
}
file.close();
return new Screen(layout);
}
////////////////////////////////////////// loadRiddleFile /////////////////////////////////////////////
int LevelLoader::loadRiddleFile()
{
std::ifstream file("riddle.txt");
if (!file.is_open()) return 0;
RiddleDatabase::clearRiddles();
int count = 0;
std::string line;
while (std::getline(file, line))
{
if (line.find("---RIDDLE---") != std::string::npos)
{
std::string question;
std::string options[4];
int answer;
std::getline(file, question);
for (int i = 0; i < 4; i++) std::getline(file, options[i]);
file >> answer;
file.ignore();
RiddleDatabase::addRiddle(
RiddleData(count, question, options, answer - 1));
count++;
}
}
file.close();
return count;
}