我目前正在为游戏编写代码,我有点卡在保存和加载关卡上。对于写作,我使用这段代码:
bool WorldGen::GenerateNewWorld(unsigned int seed, int width)
{
std::cout << "World generating..." << std::endl;
int heigth = 1; //2D perlin noise instead of 3D
m_WorldSizeX = width;
m_WorldSizeY = 1800; //add a int height if implementing different world sizes
// Create a PerlinNoise object with a random permutation vector generated with seed
PerlinNoise(seed);
std::vector<byte> arrMaxHeight;
// looping through all the x locations and deciding the y value
for (unsigned int i = 0; i < heigth; ++i) { // y
for(unsigned int j = 0; j < width; ++j) { // x
double x = (double)j / ((double)width);
double y = (double)i / ((double)heigth);
// Typical Perlin noise
double n = noise(10 * x, 10 * y, 0.8);
//n is the ground added on top of the base layer (n = highest peak at point j)
arrMaxHeight.push_back((int)(n * 255));
}
}
std::wofstream fileStream;
fileStream.open(L"GameSave/world/World.txt");
if (fileStream.fail())
{
return false;
}
//fileStream << L"[I could put something up here but that's still in development!]" << std::endl;
byte blockType = 0;
std::vector<byte> arrBlockType;
for (int i = 0; i < m_WorldSizeX; i++)
{
for (int j = 0; j < m_WorldSizeY; j++)
{
if (j > arrMaxHeight.at(i))
{
//block is not air
blockType = 1;
}
else
{
//block is air
blockType = 0;
}
arrBlockType.push_back(blockType);
fileStream << blockType << "/n";
}
}
fileStream.close();
return true;
}
现在这还不错,在大约 5 分钟内生成世界并将其发送到 world.txt 没有任何问题,但我的加载(每行读取 world.txt 行)需要很长时间。大约需要 30 多分钟才能完全读取文本文件中使用的所有行std::wifstream
及其getline()
功能。它读取所有行并将它们添加到 a 中std::vector
,然后从该向量创建“块”。块的创建在几秒钟内完成,但 wifstream 真的很慢。
这是worldLoad的代码:
std::wifstream ifileStream;
ifileStream.open("GameSave/world/World.txt");
if (ifileStream.fail())
{
std::cout << "Could not open World.txt" << std::endl;
return;
}
std::wcout << "LoadWorld Started";
std::wstring extractedLine;
while (!ifileStream.eof())
{
std::getline(ifileStream, extractedLine);
m_ArrBlockData.push_back(StringToByte(extractedLine));
std::wcout << m_ArrBlockData.size() << "\n";
}
DOUBLE2 location;
for (size_t i = 0; i < m_ArrBlockData.size(); i++)
{
location.y = (i % m_WorldSizeY) * 16;
if (location.y == 0)
{
location.x += 16;
}
Block *block = new Block(location, m_ArrBlockData.at(i));
m_ArrBlocks.push_back(block);
std::wcout << "Bock Created" << std::endl;
}
关于如何优化这个的任何想法?我正在考虑只读取播放器周围的块类型,但这仍然需要我将所有块放入一个向量中,然后才能对其进行操作。
亲切的问候,珍妮丝
PS:DOUBLE2 是一个自定义变量,包含 2 个双打 DOUBLE2(double x, double y)