Saved Navigation Data as Asset and load on scene load #439

Merged
maverickdgg merged 7 commits from Navigation into main 2023-03-24 18:05:56 +08:00
7 changed files with 74 additions and 9 deletions
Showing only changes of commit af77bd4812 - Show all commits

Binary file not shown.

View File

@ -0,0 +1,3 @@
Name: Level2_AITest_NavData
ID: 255209218
Type: 15

View File

@ -1,3 +1,4 @@
- NavData: 255209218
- EID: 20
Name: ===== Light =====
IsActive: true
@ -2945,6 +2946,7 @@
Scripts:
- Type: PlayerController
Enabled: true
smokeCount: 4
respawnPoint: 239
currentState: 0
walkMaxMoveVel: 2.5
@ -2963,6 +2965,8 @@
heavyMultiper: 0.5
silhouettePlayer: 462
silhouetteBag: 465
leftParticle: 51000
rightParticle: 51000
- Type: PickAndThrow
Enabled: true
throwForce: [10, 4, 10]
@ -2975,6 +2979,8 @@
lerpPickUpDuration: 0.75
tweenAimDuration: 0.300000012
aimingFOV: 15
trajMaxSteps: 50
trajTimeSteps: 0.0299999993
- Type: PlayerAnimations
Enabled: true
playerIdleClip: 227450439

View File

@ -447,6 +447,9 @@ namespace SHADE
{
navSystem->Clear();
navSystem->GenerateNavigationGridData();
std::string name = SHSceneManager::GetSceneName();
name += "_NavData";
navSystem->SaveToFile(name);
}
}

View File

@ -8,6 +8,7 @@
#include "Scene/SHSceneManager.h"
#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h"
#include "Assets/SHAssetManager.h"
#include "Assets/Asset Types/SHNavDataAsset.h"
#include <vector>
#include <stack>
@ -89,12 +90,43 @@ namespace SHADE
origin = SHVec3{ 0.0f };
size = SHVec3{ 0.0f };
navigationGrid.clear();
navDataAsset = 0;
}
void SHNavigationSystem::SaveToFile(std::string const& name) noexcept
void SHNavigationSystem::LoadFromFile(AssetID id) noexcept
{
auto data = SHAssetManager::GetData<SHNavDataAsset>(id);
if (data != nullptr)
{
this->origin = data->origin;
this->navigationGrid = data->grid;
this->size = data->size;
this->numRows = data->rows;
this->numCols = data->cols;
}
navDataAsset = id;
}
AssetID SHNavigationSystem::SaveToFile(std::string const& name) noexcept
{
AssetID result = SHAssetManager::CreateNewAsset(AssetType::NAV_DATA, name);
auto data = SHAssetManager::GetData<SHNavDataAsset>(result);
data->origin = this->origin;
data->grid = this->navigationGrid;
data->size = this->size;
data->rows = this->numRows;
data->cols = this->numCols;
SHAssetManager::SaveAsset(result);
navDataAsset = result;
return result;
}
SHVec3 SHNavigationSystem::GetGridWorldPos(NavigationGridIndex index) noexcept

View File

@ -5,7 +5,7 @@
#include "SHNavigationComponent.h"
#include "Math/Vector/SHVec2.h"
#include "Math/Vector/SHVec3.h"
#include "Assets/SHAssetMacros.h"
#include "SH_API.h"
@ -73,6 +73,8 @@ namespace SHADE
//Size of the navigation area
SHVec3 size_editor{ 0.0f };
AssetID navDataAsset{};
void GenerateNavigationGridData(SHVec3 origin, SHVec3 size, uint16_t numRow, uint16_t numCol) noexcept;
@ -84,9 +86,10 @@ namespace SHADE
NavigationGridIndex GetNavigationGridIndex(SHVec3 const& worldPosition) noexcept;
void Clear() noexcept;
void SaveToFile(std::string const& name) noexcept;
void Clear() noexcept;
void LoadFromFile(AssetID id) noexcept;
AssetID SaveToFile(std::string const& name) noexcept;
class SH_API NavigationSystemGenerateRoutine final: public SHSystemRoutine
{

View File

@ -16,6 +16,7 @@
#include "Scripting/SHScriptEngine.h"
#include "Tools/FileIO/SHFileIO.h"
#include "Prefab/SHPrefabManager.h"
#include <Navigation/SHNavigationSystem.h>
namespace SHADE
{
@ -50,6 +51,12 @@ namespace SHADE
auto const& children = root->GetChildren();
out << YAML::BeginSeq;
if (auto navSystem = SHSystemManager::GetSystem<SHNavigationSystem>())
{
YAML::Node navData;
navData["NavData"] = navSystem->navDataAsset;
out << navData;
}
auto pred = [&out](SHSceneNode* node) {out << SerializeEntityToNode(node); };
sceneGraph.Traverse(pred);
@ -113,9 +120,20 @@ namespace SHADE
}
YAML::Node entities = YAML::Load(assetData->data);
CreatedEntitiesList createdEntities{};
auto entitiesBegin = entities.begin();
if (auto navSystem = SHSystemManager::GetSystem<SHNavigationSystem>())
{
navSystem->Clear();
YAML::Node navDataNode = *entitiesBegin;
if (navDataNode["NavData"].IsDefined())
{
AssetID navDataAssetID = navDataNode["NavData"].as<AssetID>();
navSystem->LoadFromFile(navDataAssetID);
++entitiesBegin;
}
}
//Create Entities
for (auto it = entities.begin(); it != entities.end(); ++it)
for (auto it = entitiesBegin; it != entities.end(); ++it)
{
DeserializeEntity(it, (*it), createdEntities);
}
@ -126,14 +144,14 @@ namespace SHADE
}
auto entityVecIt = createdEntities.begin();
AssetQueue assetQueue;
for (auto it = entities.begin(); it != entities.end(); ++it)
for (auto it = entitiesBegin; it != entities.end(); ++it)
{
SHSerializationHelper::FetchAssetsFromComponent<SHRenderable>((*it)[ComponentsNode], createdEntities[(*it)[EIDNode].as<EntityID>()], assetQueue);
}
LoadAssetsFromAssetQueue(assetQueue);
//Initialize Entity
entityVecIt = createdEntities.begin();
for (auto it = entities.begin(); it != entities.end(); ++it)
for (auto it = entitiesBegin; it != entities.end(); ++it)
{
InitializeEntity(*it, createdEntities[(*it)[EIDNode].as<EntityID>()]);
}