集成阶段 6a:将 NavData 集成到资源管线

大多数游戏开发项目都有一个精心设计的管线,将游戏资源打包在一起,并在运行时根据需要将其流入到内存中。如果您已经有这样的系统,便可以轻松将 NavData 与相同地块中使用的其他类型的游戏资源(如纹理和模型)一起打包。

成功生成运行后,您可以使用以下代码将每个地块的 NavData 作为原始数据进行访问,并将其复制到 unsigned int 8char 的数组中。然后,您可以使用适合于管线的任何方法将该原始数据打包。

bool Generate()
{
    ...
    // Launch the generation process using the configuration we set up.
    if (Kaim::Result::Fail(generator.Generate(generatorInputOutput)))
    {
        return false;
    }

    // Get a GeneratorSector object for each sector.
    // (we only generated one sector)
    Kaim::GeneratorSector* generatorSector = 
                generatorInputOutput.GetSectorWithIndex(0);
 
    // Get the NavData.
    Kaim::NavData* pathDataPtr = generatorSector->GetNavData();

    // Access the blob aggregate and retrieve its size.
    Kaim::BlobAggregate& aggregate = *pathDataPtr->m_blobAggregate;
    KyUInt32 aggregateSize = aggregate.ComputeByteSize();

    // Allocate your own array.
    char* myArray = new char[aggregateSize];

    // Copy the NavData there.
    aggregate.SaveToMemory(myArray);

    // Embed your copy into your own actor, an entity, or into your system.
    myLevel.m_navDataBlob = myArray;

    return true;
}

在运行时将包含地块 NavData 的数据块加载到内存之后,您可以通过调用 NavData::LoadFromMemory() 并传递缓冲区,使用它来设置 NavData 对象。

class MyGameLevel
{
public:
    bool Initialize(Kaim::World* world);
protected:
    Kaim::Ptr<Kaim::NavData> m_navData;
    char* m_navDataBlob;
};

bool MyGameLevel::Initialize(Kaim::World* world)
{
    // Instantiate and load the NavData from the buffer array
    m_navData = *KY_NEW Kaim::NavData;
    KyResult loadingResult = KY_ERROR;
    loadingResult = m_navData->LoadFromMemory(m_navDataBlob); 
 
    // Check that the NavData have been correctly loaded
    if (KY_FAILED(loadingResult))
        return false;
 
    // Add the NavData to the database
    m_navData->Init(world->GetDatabase(0));
    m_navData->AddToDatabaseImmediate();
 
    return true;
}