2

我从一个关卡文件加载地图,这些文件只是与图块表中的图块相对应的数字。

这是关卡文件

[Map]
0 1 0 0 0 0 0 0
0 0 20 0 0 0 0 0
0 0 0 40 0 0 0 0
0 0 0 0 63 0 0 0
0 0 0 0 0 79 0 0
0 0 0 0 0 0 0 0

这是解释它的代码

void LoadMap(const char *filename, std::vector< std::vector <int> > &map)
{
    std::ifstream openfile(filename); 
    if(openfile.is_open())
    {
        std::string line, value;
        int space;

        while(!openfile.eof())
        {
            std::getline(openfile, line);

            if(line.find("[TileSet]") != std::string::npos)
            {
                state = TileSet;
                continue;
            }
            else if (line.find("[Map]") != std::string::npos)
            {
                state = Map;
                continue;
          }

            switch(state)
            {
            case TileSet:
                if(line.length() > 0)
                    tileSet = al_load_bitmap(line.c_str());
                break;
            case Map: 

                std::stringstream str(line);
                std::vector<int> tempVector;

                while(!str.eof())
                {
                    std::getline(str, value, ' ');
                    if(value.length() > 0)
                        tempVector.push_back(atoi(value.c_str()));
                }
                map.push_back(tempVector);
                break;
            }
        }
    }
    else
    {
  }
  }

这就是它的外观 http://i.imgur.com/6W49eWf.jpg

好的,所以我的 Tilesheet 是 1000 x 200,它看起来像这样http://i.imgur.com/Y83zBxj.png

在地图文件中放入 20 或 40 时,如何使其环绕到 20 或 40?

void DrawMap(std::vector <std::vector <int> > map)
{    
    for(int i, j = 0; i < map.size(); i ++)
    {
        for(j = 0; j < map[i].size(); j ++)
        {
          al_draw_bitmap_region(tileSet, map[i][j] * TileSizeX, 0, TileSizeX, TileSizeY, j   * TileSizeX, i * TileSizeX, NULL);
        }
    }
}

TileSizeX 和 TileSizeY 也是 50

4

1 回答 1

1

您需要计算您的目标图块所在的图块集的哪个单元格。你有图块集索引。根据图块集的尺寸,使用一点数学来确定该图块的列和行。

//This is how many columns your tileset can have.
//You could even dynamically calculate this if you wanted.
static const int TILESET_COLCOUNT = 20;

void DrawMap(std::vector<std::vector<int> > map)
{    
    int mapRowCount = map.size();

    for (int i = 0; i < mapRowCount; ++i)
    {
        int mapColCount = map[i].size();

        for (int j = 0; j < mapColCount; ++j)
        {
            //This is your tileset index in your level map.
            int tilesetIndex = map[i][j];

            //The tileset row can be calculated by dividing the tileset index by the number of columns in a tileset row.
            int tilesetRow = floor(tilesetIndex / TILESET_COLCOUNT);

            //The tileset column can be calculated by retrieving the remainder of the modulus operation on the total number of columns in a row.
            int tilesetCol = tilesetIndex % TILESET_COLCOUNT;

            al_draw_bitmap_region(
                tileSet, //The tileset
                tilesetCol * TileSizeX, //multiply the tileset column by the size of a tile to get the source x
                tilesetRow * TileSizeY, //multiply the tileset row by the size of a tile to get the source y
                TileSizeX, //width
                TileSizeY, //height
                j * TileSizeX, //destination x
                i * TileSizeX, //destination y
                NULL //flags
            );
        }
    }
}
于 2013-01-22T19:02:04.113 回答