简体   繁体   English

大小未知的Tilemap数组

[英]Tilemap array of unknown size

I want the users to pick there map size so I do not know the size of the map at run time. 我希望用户选择那里的地图大小,所以我在运行时不知道地图的大小。 I have been looking around how I can implement this with little success. 我一直在寻找如何才能成功实现这一目标的方法。 Closest to something that is working is with the vector variant but while it runs it still does not work properly. 向量变量最接近有效的方法,但在运行时仍无法正常工作。

I want a 3 dimensional array so in my header i declare the tilemap like this. 我想要一个3维数组,所以在标题中我这样声明了tilemap。

std::vector<std::vector<std::vector<Tile>>> tileMap;

and test it in the class like this: 并在这样的类中对其进行测试:

World::World(int width, int height, int depth)
{
tileMap.resize(width);
tileMap[0].resize(height);
tileMap[0][0].resize(depth);

tileMap.resize(width);

for (int i = 0;i<width;i++)
{
    tileMap[i].resize(height);
}



for (int y = 0; y < height;y++)
{
    for (int x = 0; x < width; x++)
    {
        tileMap[x,y].resize(depth);
    }
}

std::cout << sizeof tileMap / sizeof tileMap[0][0][0] << std::endl;

}

I am aware that not all the dimensions of the array are resized properly but the final line just outputs "0". 我知道并非所有数组的尺寸都正确调整了大小,但最后一行仅输出“ 0”。

Is there i better way to create my tilemap of unknown size? 有更好的方法来创建未知大小的图块吗? I need a lot of sizes like 64*64, 256*256, 64*1024, etc (the z levels will be determined after generating the level). 我需要很多尺寸,例如64 * 64、256 * 256、64 * 1024等(z级别将在生成级别后确定)。 so creating an array for each size seems inefficient. 因此,为每种尺寸创建一个数组似乎效率不高。 But maybe there is a better way of declaring my array or i am doing something wrong somewhere. 但是,也许有更好的方法声明数组,或者我在某处做错了什么。

您想要tileMap.size() * tileMap[0].size() * tileMap[0][0].size()来获取切片数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM