Prefab Data Resolution WIP

This commit is contained in:
SHAM-DP 2023-02-28 21:30:24 +08:00
parent 49ef701994
commit 23c6f57899
3 changed files with 69 additions and 9 deletions

View File

@ -5,7 +5,7 @@
#include "ECS_Base/General/SHFamily.h" #include "ECS_Base/General/SHFamily.h"
#include "ECS_Base/Components/SHComponent.h" #include "ECS_Base/Components/SHComponent.h"
#include <unordered_map> #include <unordered_map>
#include <>
namespace SHADE namespace SHADE
{ {
@ -21,6 +21,7 @@ namespace SHADE
}; };
using PrefabMap = std::unordered_map<AssetID, std::vector<EntityID>>; using PrefabMap = std::unordered_map<AssetID, std::vector<EntityID>>;
//using PrefabDatabase = std::unordered_multimap<AssetID,
using PrefabEntitiesComponentStatusData = std::unordered_map<EntityID, std::unordered_map<SHFamilyID<SHComponent>,PrefabEntityComponentStatus>>; using PrefabEntitiesComponentStatusData = std::unordered_map<EntityID, std::unordered_map<SHFamilyID<SHComponent>,PrefabEntityComponentStatus>>;
static AssetID GetPrefabAssetID(EntityID eid) noexcept; static AssetID GetPrefabAssetID(EntityID eid) noexcept;

View File

@ -16,6 +16,7 @@
#include "Scripting/SHScriptEngine.h" #include "Scripting/SHScriptEngine.h"
#include "Tools/FileIO/SHFileIO.h" #include "Tools/FileIO/SHFileIO.h"
#include "Prefab/SHPrefabManager.h" #include "Prefab/SHPrefabManager.h"
#include "Assets/Asset Types/SHPrefabAsset.h"
namespace SHADE namespace SHADE
{ {
@ -418,15 +419,34 @@ namespace SHADE
void SHSerialization::InitializeEntity(YAML::Node const& entityNode, EntityID const& eid) void SHSerialization::InitializeEntity(YAML::Node const& entityNode, EntityID const& eid)
{ {
auto const componentsNode = entityNode[ComponentsNode]; auto componentsNode = entityNode[ComponentsNode];
if (!componentsNode) if (!componentsNode.IsDefined())
return; return;
SHSerializationHelper::InitializeComponentFromNode<SHTransformComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHTransformComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHColliderComponent>(componentsNode, eid);
bool isPrefab = entityNode[PrefabID].IsDefined();
if(isPrefab)
{
auto prefabAssetID = entityNode[PrefabID].as<AssetID>();
if(auto prefabAsset = SHAssetManager::GetData<SHPrefabAsset>(prefabAssetID))
{
auto prefabNode = YAML::Load(prefabAsset->data);
if(prefabNode[ComponentsNode].IsDefined())
{
componentsNode = prefabNode[ComponentsNode];
}
if (prefabNode[ScriptsNode].IsDefined())
SHSystemManager::GetSystem<SHScriptEngine>()->DeserialiseScripts(eid, prefabNode[ScriptsNode]);
}
}
SHSerializationHelper::InitializeComponentFromNode<SHCameraComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHCameraComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHCameraArmComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHCameraArmComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHRigidBodyComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHRigidBodyComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHRenderable>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHRenderable>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHColliderComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHCanvasComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHCanvasComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHButtonComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHButtonComponent>(componentsNode, eid);
@ -437,7 +457,8 @@ namespace SHADE
SHSerializationHelper::InitializeComponentFromNode<SHAnimatorComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHAnimatorComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHUIComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHUIComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHAudioListenerComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHAudioListenerComponent>(componentsNode, eid);
if (entityNode[ScriptsNode].IsDefined())
if (!isPrefab && entityNode[ScriptsNode].IsDefined())
SHSystemManager::GetSystem<SHScriptEngine>()->DeserialiseScripts(eid, entityNode[ScriptsNode]); SHSystemManager::GetSystem<SHScriptEngine>()->DeserialiseScripts(eid, entityNode[ScriptsNode]);
} }

View File

@ -209,7 +209,7 @@ namespace SHADE
auto component = SHComponentManager::GetComponent_s<ComponentType>(eid); auto component = SHComponentManager::GetComponent_s<ComponentType>(eid);
if (componentsNode.IsNull() || !component) if (componentsNode.IsNull() || !component)
return false; return false;
auto componentNode = GetComponentNode<ComponentType>(componentsNode, eid); auto componentNode = GetComponentNode<ComponentType>(componentsNode, component);
if (componentNode.IsNull() || !componentNode.IsDefined()) if (componentNode.IsNull() || !componentNode.IsDefined())
return false; return false;
if (componentNode[IsActive.data()].IsDefined()) if (componentNode[IsActive.data()].IsDefined())
@ -242,9 +242,46 @@ namespace SHADE
} }
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true> template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static YAML::Node GetComponentNode(YAML::Node const& componentsNode, EntityID const& eid) static bool InitializeComponentFromNode(YAML::Node const& componentsNode, ComponentType* component)
{
if constexpr (YAML::HasYAMLConv<ComponentType>())
{
if (componentsNode.IsNull() || !component)
return false;
auto componentNode = GetComponentNode<ComponentType>(componentsNode, component);
if (componentNode.IsNull() || !componentNode.IsDefined())
return false;
if (componentNode[IsActive.data()].IsDefined())
component->isActive = componentNode[IsActive.data()].as<bool>();
YAML::convert<ComponentType>::decode(componentNode, *component);
return true;
}
else
{
if (componentsNode.IsNull() && !component)
return false;
auto rttrType = rttr::type::get<ComponentType>();
auto componentNode = componentsNode[rttrType.get_name().data()];
if (!componentNode.IsDefined())
return false;
if (componentNode[IsActive.data()].IsDefined())
component->isActive = componentNode[IsActive.data()].as<bool>();
auto properties = rttrType.get_properties();
for (auto const& prop : properties)
{
if (componentNode[prop.get_name().data()].IsDefined())
{
InitializeProperty<ComponentType>(component, prop, componentNode[prop.get_name().data()]);
}
}
}
return true;
}
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static YAML::Node GetComponentNode(YAML::Node const& componentsNode, ComponentType* component)
{ {
auto component = SHComponentManager::GetComponent_s<ComponentType>(eid);
if (componentsNode.IsNull() && !component) if (componentsNode.IsNull() && !component)
return {}; return {};
auto rttrType = rttr::type::get<ComponentType>(); auto rttrType = rttr::type::get<ComponentType>();
@ -307,7 +344,8 @@ namespace SHADE
template<> template<>
static void FetchAssetsFromComponent<SHRenderable>(YAML::Node const& componentsNode, EntityID const& eid, AssetQueue& assetQueue) static void FetchAssetsFromComponent<SHRenderable>(YAML::Node const& componentsNode, EntityID const& eid, AssetQueue& assetQueue)
{ {
auto node = GetComponentNode<SHRenderable>(componentsNode, eid); SHRenderable* renderableComponent = SHComponentManager::GetComponent_s<SHRenderable>(eid);
auto node = GetComponentNode<SHRenderable>(componentsNode, renderableComponent);
if(!node.IsDefined()) if(!node.IsDefined())
return; return;
if (auto const& meshNode = node[YAML::convert<SHRenderable>::MESH_YAML_TAG.data()]; meshNode.IsDefined()) if (auto const& meshNode = node[YAML::convert<SHRenderable>::MESH_YAML_TAG.data()]; meshNode.IsDefined())