我正在编写一个从XML文档中读取信息并构建地图对象的地图类。地图首先从XML文件中读取切片类型定义,并将其存储在地图中,通过其ID为每个切片的信息编制索引。之后,它会读取另一个包含地图横向信息(图块坐标等)的文件,并尝试填充图块数组。
这是这样的:
class TileInfo {
//Information about tile stats, bitmaps, movement cost etc
};
class Tile {
TileInfo* info;
vector<TileInfo*> lastSeenByPlayer; //due to fow each player can be seeing different tiles since they haven't updated yet
//other tile specific tile information like coordinates etc
public:
Tile(int x, int y, TileInfo* info /*other parameters*/);
};
Tile::Tile(int x, int y, TileInfo* info /*...*/) {
this->info = tileInfo;
this->x = x;
this->y = y;
for(unsigned int i = 0; i < NUM_PLAYERS; i++)
lastSeenByPlayer.push_back(info);
}
class GameMap {
map<int,TileInfo> tileInformation;
vector<vector<Tile>> tiles;
Tile loadTile(int tileID, int x, int y);
void loadTerrainFromFile(string filename);
};
Tile GameMap::loadTile(int tileID, int x, int y) {
map<int,TileInfo>::iterator it = tileInformation.find(tileID);
if( it!=tileInformation.end())
TileInfo* info = &(it->second);
else
throw errorLoadingTile();
return Tile(x,y,info /*other information*/);
}
void GameMap::loadTerrainFromFile(string filename) {
//open file and read information
//at this point the GameMap already has the map with the tile information filled
//from when it was created
for(unsigned int x = 0; x < mapWidth; x++)
for(unsigned int y = 0; y < mapHeight; y++) {
cout << "Works up to here" << endl;
tiles[y][x] = loadTile(tileIDs[x][y],x,y);
cout << "Crashes before this" << endl;
}
//do more stuff
};
我认为这与复制构造函数有关,但是Tile没有删除任何东西的析构函数,所以它不能删除TileInfo *,这就是地图的责任。任何帮助将不胜感激。