summaryrefslogtreecommitdiff
path: root/source/data/LevelReader.cpp
blob: e5364d8f8fef8aec7ad92e33e38da5fa1527d58a (plain)
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
#include "LevelReader.h"

LevelReader::LevelReader(string filename)
    :_filename(filename)
{}

void LevelReader::readLevel(Maze& maze, list<PlayerCar>& players, list<EnemyCar>& enemies, list<Checkpoint>& checkpoints, list<Rock>& rocks)
{
    ifstream file(_filename.c_str());
    if (!file)
    {
        al_show_native_message_box(NULL, "Fatal error", "Fatal error", "The requested level file could not be opened.", NULL, ALLEGRO_MESSAGEBOX_ERROR);
        throw FileOpenError();
    }

    int maxX = 0;
    int maxY = 0;

    string line;
    char element;
    int y = 0;
    vector <pair<int, int> > walls;

    while (!file.eof())
    {
        getline (file, line);

        for (int x = 0; x < static_cast<int>(line.length()); ++x)
        {
            element = line.at(x);
            switch (element)
            {
                case PLAYER_CHAR: players.push_back(PlayerCar(x,y));
                break;
                case ENEMY_CHAR: enemies.push_back (EnemyCar(x,y));
                break;
                case CHECKPOINT_CHAR: checkpoints.push_back(Checkpoint(x,y));
                break;
                case ROCK_CHAR: rocks.push_back(Rock(x,y));
                break;
                case WALL_CHAR: walls.push_back (make_pair(x,y));
                break;
            }
            if (maxX < x) maxX = x;
            if (maxY < y) maxY = y;
        }

        ++y;
    }

    maze.generateMaze (walls, maxX, maxY);

    file.close();
}