Esempio n. 1
0
void Game::draw()
{
    al_clear_to_color(al_map_rgb(0, 0, 0));

    // draw player
    float x1 = m_player->getX();
    float x2 = x1 + (m_player->getWidth() * PLAYER_WIDTH);
    float y1 = m_player->getY();
    float y2 = y1 + (m_player->getHeight() * PLAYER_HEIGHT);
    al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(0, 255, 0));

    float playerX = x1;

    // move camera
    if (playerX <= SCREEN_WIDTH / 2)
        //camX = 0;
        ;
    else if (playerX >= m_levelWidth - SCREEN_WIDTH / 2)
        m_camX = m_levelWidth - SCREEN_WIDTH;
        //;
    else
    {
        m_camX = playerX - SCREEN_WIDTH / 2;
    }
    ALLEGRO_TRANSFORM trans;
    al_identity_transform(&trans);
    al_translate_transform(&trans, -m_camX, 0);
    al_use_transform(&trans);

    for (list<StationaryObject*>::iterator it = m_stationaryobjects.begin(); it != m_stationaryobjects.end(); it++)
    {
        StationaryObject* current = *it;
        int width = current->getWidth() * TILE_WIDTH;
        int height = current->getHeight() * TILE_HEIGHT;
        float x1 = current->getX();
        float x2 = x1 + width;
        float y1 = current->getY();
        float y2 = y1 + height;

        if (x2 >= m_camX || x1 >= m_camX)
        {
            char tile = current->getTile();
            switch (tile)
            {
            case 'w':
                al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 0));
                break;
            case 's':
                al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 255, 0));
                break;
            }
        }
    }

    al_flip_display();
}
Esempio n. 2
0
int Game::parseMapFile() // returns value for m_levelWidth
{
    ifstream mapFile;
    mapFile.open(m_mapFile);
    if (!mapFile.is_open())
    {
        fprintf(stderr, "Could not open map file.\n");
        exit(0);
    }

    int highest_x = 0;

    int lineNum = 1;
    string line;
    int commaCount = 0;
    while (getline(mapFile, line))
    {
        char tile;
        int row, col, width, height;
        int num = -1;

        for (size_t k = 0; k < line.length(); k++)
        {
            if (line[k] == ',')
            {
                commaCount++;
                //num = -1;
            }
            else if (isdigit(line[k]))
            {
                if (num != -1)
                {
                    num = (num * 10) + (line[k] - '0');
                }
                else
                    num = line[k] - '0';

                continue;
            }

            if (k == 0) // tile char
            {
                tile = line[k];
                if (tile != 'w' && tile != 'p' && tile != 's' && tile != 'm')
                {
                    fprintf(stderr, "%s %c %s %d \n", "Map file error:\nInvalid tile char: '", tile, "' at line: ", lineNum);
                    exit(0);
                }
            }
            else
            {
                switch (commaCount)
                {
                case 1:
                    break;
                case 2: // row
                    row = num;
                    num = -1;
                    if (row < 0 || row >= NUM_ROWS)
                    {
                        fprintf(stderr, "%s %d %s %d \n", "Map file error:\nInvalid row: '", row, "' at line: ", lineNum);
                        exit(0);
                    }
                    break;
                case 3: // col
                    col = num;
                    num = -1;
                    if (col < 0 || col >= MAX_NUM_COLS)
                    {
                        fprintf(stderr, "%s %d %s %d \n", "Map file error:\nInvalid col: '", col, "' at line: ", lineNum);
                        exit(0);
                    }
                    break;
                case 4: // width
                    width = num;
                    num = -1;
                    if (width < 1 || width > MAX_NUM_COLS)
                    {
                        fprintf(stderr, "%s %d %s %d \n", "Map file error:\nInvalid width: '", width, "' at line: ", lineNum);
                        exit(0);
                    }
                    break;
                case 5: // height
                    height = num;
                    num = -1;
                    if (height < 1 || height > NUM_ROWS)
                    {
                        fprintf(stderr, "%s %d %s %d \n", "Map file error:\nInvalid height: '", height, "' at line: ", lineNum);
                        exit(0);
                    }
                    break;
                default:
                    fprintf(stderr, "%s %d \n", "Map file error:\nExtra parameters at line: ", lineNum);
                    exit(0);
                }
            }
        }

        // create the gameobject
        switch (tile)
        {
        case 'w':
            addNewStationaryObject(new Wall(row, col, width, height, this, lineNum));
            break;
        case 'p':
        {
            if (m_player != NULL)
            {
                fprintf(stderr, "%s%d\n", "Map file error:\nCan only have one player. Extra player at line: ", lineNum);
                exit(0);
            }

            m_player = new Player(row, col, width, height, this, lineNum);
            break;
        }
        case 's':
            addNewStationaryObject(new StationaryEnemy(row, col, width, height, this, lineNum));
            break;
        case 'm':
            break;
        }

        if (tile == 'w' || tile == 's')
        {
            StationaryObject* last = m_stationaryobjects.back();
            int last_x = last->getX();
            if (last_x > highest_x)
                highest_x = last_x;
        }

        lineNum++;
        commaCount = 0;
    }

    if (m_player == NULL)
    {
        fprintf(stderr, "%s\n", "Map file error:\nA player must be added.");
        exit(0);
    }

    // remove chars in map for player
    int player_row = m_player->getR();
    int player_col = m_player->getC();
    for (int r = player_row; r < (player_row + m_player->getHeight()); r++)
    {
        for (int c = player_col; c < (player_col + m_player->getWidth()); c++)
            setMap(r, c, NULL);
    }

    // remove chars in map for movingobjects
    // not yet implemented because there are not movingobjects yet.


    mapFile.close();

    return highest_x;
}