-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.h
114 lines (95 loc) · 2.8 KB
/
Level.h
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
#ifndef LEVEL_H_
#define LEVEL_H_
#include "GameConstants.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
class Level
{
public:
enum MazeEntry {
empty, exit, player, horiz_snarlbot, vert_snarlbot,
KleptoBot_factory, angry_KleptoBot_factory, wall, boulder, hole,
jewel, restore_health, extra_life, ammo
};
enum LoadResult {
load_success, load_fail_file_not_found, load_fail_bad_format};
Level(std::string assetDir)
: m_pathPrefix(assetDir)
{
for (int y = 0; y < VIEW_HEIGHT; y++)
for (int x = 0; x < VIEW_WIDTH; x++)
m_maze[y][x] = empty;
if (!m_pathPrefix.empty())
m_pathPrefix += '/';
}
LoadResult loadLevel(std::string filename)
{
std::ifstream levelFile((m_pathPrefix + filename).c_str());
if (!levelFile)
return load_fail_file_not_found;
// get the maze
std::string line;
bool foundExit = false;
bool foundPlayer = false;
for (int y = VIEW_HEIGHT-1; std::getline(levelFile, line); y--)
{
if (y < 0) // too many maze lines?
{
if (line.find_first_not_of(" \t\r") != std::string::npos)
return load_fail_bad_format; // non-blank line
char dummy;
if (levelFile >> dummy) // non-blank rest of file
return load_fail_bad_format;
break;
}
if (line.size() < VIEW_WIDTH || line.find_first_not_of(" \t\r", VIEW_WIDTH) != std::string::npos)
return load_fail_bad_format;
for (int x = 0; x < VIEW_WIDTH; x++)
{
MazeEntry me;
switch (tolower(line[x]))
{
default: return load_fail_bad_format;
case ' ': me = empty; break;
case 'x': me = exit; foundExit = true; break;
case '@': me = player; foundPlayer = true; break;
case 'h': me = horiz_snarlbot; break;
case 'v': me = vert_snarlbot; break;
case '1': me = KleptoBot_factory; break;
case '2': me = angry_KleptoBot_factory; break;
case '#': me = wall; break;
case 'b': me = boulder; break;
case 'o': me = hole; break;
case '*': me = jewel; break;
case 'r': me = restore_health; break;
case 'e': me = extra_life; break;
case 'a': me = ammo; break;
}
m_maze[y][x] = me;
}
}
if (!foundExit || !foundPlayer || !edgesValid())
return load_fail_bad_format;
return load_success;
}
MazeEntry getContentsOf(unsigned int x, unsigned int y) const
{
return (x < VIEW_WIDTH && y < VIEW_HEIGHT) ? m_maze[y][x] : empty;
}
private:
MazeEntry m_maze[VIEW_HEIGHT][VIEW_WIDTH];
std::string m_pathPrefix;
bool edgesValid() const
{
for (int y = 0; y < VIEW_HEIGHT; y++)
if (m_maze[y][0] != wall || m_maze[y][VIEW_WIDTH-1] != wall)
return false;
for (int x = 0; x < VIEW_WIDTH; x++)
if (m_maze[0][x] != wall || m_maze[VIEW_HEIGHT-1][x] != wall)
return false;
return true;
}
};
#endif // LEVEL_H_