所以我有一个使用递归生成关卡的类。BSP地牢生成的简单示例。我在这里使用代码。但是,我用 C++ 编写,所以我不能有一个静态类。
问题是我有这个,里面有类的指针。
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
public:
Rectangle(int top, int left, int height, int width);
~Rectangle();
bool split();
void generate_dungeon();
private:
static int min_size;
Rectangle* dungeon;
Rectangle* left_child;
Rectangle* right_child;
int top, left, width, height;
};
#endif
编辑
std::vector<Rectangle*> rectangles;
Rectangle root(0,0,map_height,map_width);
//std::cout<<root.split()<<std::endl;
rectangles.push_back(&root);
//std::cout<<rectangles.at(0).split()<<std::endl;
while(rectangles.size() < 5)
{
int split_idx = Rng::rand_int(0, (rectangles.size()-1));
Rectangle* to_split = rectangles.at(split_idx);
std::cout<<rectangles.size()<<std::endl;
if((*to_split).split())
{
rectangles.push_back(((*to_split)get_left()));
rectangles.push_back(((*to_split)get_right()));
}
}
root.generate_dungeon();
这解决了问题。之后我打电话给clear std::vector
。