-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevelLoader.cpp
118 lines (99 loc) · 2.75 KB
/
LevelLoader.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
#include "LevelLoader.h"
#include "LevelState.h"
LevelLoader::LevelLoader(LevelState& levelState) : m_levelState(levelState)
{
}
void LevelLoader::loadLevel(const char *fileNamePath)
{
size_t BUFFER_SIZE = 100;
char mode;
FILE *file = asset_fopen(fileNamePath);
char buf[BUFFER_SIZE];
if (file)
{
while (fgets(buf, BUFFER_SIZE, file))
{
if (buf[0] == '*')
{
mode = buf[1];
continue;
}
else if (buf[0] == '#')
{
continue;
}
// remove newline
buf[strlen(buf) - 1] = '\0';
switch (mode)
{
case 't': // textures
textureMan::loadTexture(buf);
break;
case 'm': // models
modelMan::loadModel(buf);
break;
case 'M': // model objects
loadModel(buf);
break;
case 'f': // fonts
// todo
break;
case 'c': // castle
loadCastle(file, buf);
break;
case 'T': // time to complete
m_levelState.m_amountOfTimeToBuild = strtol(buf, NULL, 10);
case 'O': // observatoin time
m_levelState.m_amountOfTimeToObserve = strtof(buf, NULL);
break;
default:
break;
}
}
}
fclose(file);
}
void LevelLoader::loadModel(const char *modelString)
{
std::istringstream ss(modelString);
std::string model;
std::string texture;
vec3f position;
vec3f scale;
vec3f rotation;
ss >> model >> texture >> position.x >> position.y >> position.z >> scale.x >> scale.y >> scale.z >> rotation.x >> rotation.y >> rotation.z;
TexturedModel texModel(model, texture);
texModel.setPosition(position);
texModel.setScale(scale);
texModel.setRotation(rotation);
m_levelState.models.push_back(std::move(texModel));
}
void LevelLoader::loadCastle(FILE *file, const char *firstLine)
{
size_t BUFFER_SIZE = 100;
char buf[BUFFER_SIZE];
strcpy(buf, firstLine);
size_t row, col, height;
row = 0;
col = 0;
height = 0;
do
{
std::istringstream ss(buf);
if (buf[0] == '#')
{
height++;
continue;
}
for (size_t col = 0; col < 10; ++col)
{
ss >> m_levelState.castle[height][row][col];
if (m_levelState.castle[height][row][col])
{
m_levelState.m_numBlocksInCastle++;
}
}
row++;
row %= 10;
} while (fgets(buf, BUFFER_SIZE, file) && buf[0] != '*');
}