ID Resolution for prefabs

This commit is contained in:
SHAM-DP 2023-02-27 15:00:03 +08:00
parent 31491d06ef
commit ad875050cd
7 changed files with 69 additions and 169 deletions

View File

@ -1,147 +0,0 @@
- EID: 0
PrefabID: 117058283
Name: ====Raccoon====
IsActive: true
NumberOfChildren: 2
Components: ~
Scripts: ~
- EID: 1
Name: Player
IsActive: true
NumberOfChildren: 3
Components:
Transform Component:
Translate: {x: 2.35245037, y: 0.38365531, z: 7.10571432}
Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 0.999999881, y: 1, z: 0.999999881}
IsActive: true
Renderable Component:
Mesh: 149697411
Material: 126974645
IsActive: true
RigidBody Component:
Type: Dynamic
Drag: 0.00999999978
Angular Drag: 0.100000001
Use Gravity: false
Interpolate: false
Sleeping Enabled: true
Freeze Position X: false
Freeze Position Y: true
Freeze Position Z: false
Freeze Rotation X: true
Freeze Rotation Y: true
Freeze Rotation Z: true
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Collision Tag: 0
Type: Box
Half Extents: {x: 0.400000006, y: 0.5, z: 0.300000012}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0.25, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts:
- Type: PlayerController
Enabled: true
respawnPoint: 239
currentState: 0
maxMoveVel: 3
moveForce: 50
sprintMultiplier: 1.5
rotationFactorPerFrame: 5
maxJumpHeight: 2
maxJumpTime: 0.75
fallMultipler: 3
lightMultiper: 0.899999976
mediumMultiper: 0.699999988
heavyMultiper: 0.5
- Type: PickAndThrow
Enabled: true
throwForce: [10, 8, 10]
cameraArmOffSet: [0.25, 0.600000024, 0.200000003]
delayTimer: 1
aimingLength: 1
throwItem: false
rayDistance: 0.75
rayHeight: 0.100000001
- EID: 2
Name: HoldingPoint
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: 0.899999976, z: 0.200000286}
Rotate: {x: 0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
Scripts: ~
- EID: 3
Name: PlayerCamera
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: -5.96046448e-08, z: 0}
Rotate: {x: 0, y: 6.28318548, z: 2.23517329e-08}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
Camera Component:
Position: {x: 2.12735963, y: 0.362327784, z: 7.98933029}
Pitch: 0
Yaw: 360
Roll: 1.28065994e-06
Width: 2560
Near: 0.00999999978
Far: 10000
Perspective: true
FOV: 45
IsActive: true
Camera Arm Component:
Arm Pitch: 0
Arm Yaw: 0
Arm Length: 3
Look At Camera Origin: true
Target Offset: {x: 0, y: 0.75, z: 0}
Camera Collision: true
IsActive: true
Scripts:
- Type: SHADE_Scripting.ThirdPersonCamera
Enabled: true
armLength: 3
turnSpeedPitch: 0.200000003
turnSpeedYaw: 0.400000006
pitchClamp: 45
inverseXControls: false
inverseYControls: false
lowerClamp: 5
- EID: 4
Name: PlayerBag
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: -2.98023224e-08, z: 4.76837158e-07}
Rotate: {x: 0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
Renderable Component:
Mesh: 144838771
Material: 123745521
IsActive: true
Scripts: ~
- EID: 5
Name: RespawnPoint
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 2.5, y: 0.660660267, z: 7}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
Scripts: ~

View File

@ -1,3 +0,0 @@
Name: ====Raccoon====
ID: 117058283
Type: 6

View File

@ -65,7 +65,7 @@ namespace SHADE
AssetID const assetID = SHAssetManager::CreateNewAsset(AssetType::PREFAB, entity->name);
AddEntity(assetID, eid);
auto assetData = SHAssetManager::GetData<SHPrefabAsset>(assetID);
assetData->data = SHSerialization::SerializeEntityToString(eid);
assetData->data = SHSerialization::SerializeEntityToString(eid, assetID);
SHAssetManager::SaveAsset(assetID);
}

View File

@ -17,8 +17,7 @@ namespace SHADE
{
PES_UNCHANGED = 0,
PES_MODIFIED,
PES_ADDED,
PES_REMOVED
PES_ADDED
};
using PrefabMap = std::unordered_map<AssetID, std::vector<EntityID>>;

View File

@ -58,6 +58,33 @@ namespace SHADE
out << YAML::EndSeq;
}
void RecursivelyResolveEntityIDs(YAML::iterator nodeIt, SHSerialization::CreatedEntitiesList& createdEntities)
{
for (YAML::iterator it = nodeIt->begin(); it != nodeIt->end(); ++it)
{
if(it->second.IsDefined())
{
auto tag = it->second.Tag();
if (!tag.empty() && tag == "EID") //Node has EID tag
{
EntityID eid = it->second.as<EntityID>();
if(createdEntities.contains(eid))
it->second = createdEntities[eid];
}
}
RecursivelyResolveEntityIDs(it, createdEntities);
for (YAML::iterator it2 = it->first.begin(); it2 != it->first.end(); ++it2)
{
RecursivelyResolveEntityIDs(it2, createdEntities);
}
for (YAML::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
{
RecursivelyResolveEntityIDs(it2, createdEntities);
}
}
}
static EntityID DeserializeEntity(YAML::iterator& it, YAML::Node const& node, SHSerialization::CreatedEntitiesList& createdEntities, EntityID parentEID = MAX_EID)
{
EntityID eid{MAX_EID}, oldEID{MAX_EID};
@ -89,10 +116,6 @@ namespace SHADE
}
}
// Deserialise scripts
if (node[ScriptsNode])
SHSystemManager::GetSystem<SHScriptEngine>()->DeserialiseScripts(eid, node[ScriptsNode]);
auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
if (node[IsActiveNode])
{
@ -131,6 +154,12 @@ namespace SHADE
SHSerializationHelper::FetchAssetsFromComponent<SHRenderable>((*it)[ComponentsNode], createdEntities[(*it)[EIDNode].as<EntityID>()], assetQueue);
}
LoadAssetsFromAssetQueue(assetQueue);
for (auto it = entities.begin(); it != entities.end(); ++it)
{
RecursivelyResolveEntityIDs(it, createdEntities);
}
//Initialize Entity
entityVecIt = createdEntities.begin();
for (auto it = entities.begin(); it != entities.end(); ++it)
@ -141,16 +170,16 @@ namespace SHADE
return assetData->name;
}
void SHSerialization::EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out, bool isPrefab /*= false*/, EntityID* entityIndex /*= nullptr*/)
void SHSerialization::EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out, CreatedEntitiesList* createdEntitiesList /*= nullptr*/, bool isPrefab /*= false*/, EntityID* entityIndex /*= nullptr*/)
{
out << SerializeEntityToNode(entityNode, isPrefab, entityIndex);
out << SerializeEntityToNode(entityNode, createdEntitiesList, isPrefab, entityIndex);
if(isPrefab)
++(*entityIndex);
auto const& children = entityNode->GetChildren();
for (auto const& child : children)
{
EmitEntity(child, out, isPrefab, entityIndex);
EmitEntity(child, out, createdEntitiesList, isPrefab, entityIndex);
}
}
@ -159,6 +188,7 @@ namespace SHADE
YAML::Emitter out;
YAML::Node node;
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
out << YAML::BeginSeq;
EntityID entityIndex = 0;
for (auto const& eid : entities)
@ -167,10 +197,11 @@ namespace SHADE
EmitEntity(entityNode, out);
}
out << YAML::EndSeq;
return std::string(out.c_str());
}
std::string SHSerialization::SerializeEntityToString(EntityID eid) noexcept
std::string SHSerialization::SerializeEntityToString(EntityID eid, AssetID prefabAssetID /*= 0*/) noexcept
{
YAML::Emitter out;
YAML::Node node;
@ -179,9 +210,12 @@ namespace SHADE
EntityID entityIndex = 0;
auto entityNode = sceneGraph.GetNode(eid);
EmitEntity(entityNode, out, true, &entityIndex);
CreatedEntitiesList list;
EmitEntity(entityNode, out, &list, true, &entityIndex);
out << YAML::EndSeq;
node = YAML::Load(out.c_str());
for(auto it = node.begin(); it != node.end(); ++it)
RecursivelyResolveEntityIDs(it, list);
return std::string(out.c_str());
}
@ -200,7 +234,7 @@ namespace SHADE
}
}
YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode, bool isPrefab /*= false*/, EntityID* entityIndex /*= nullptr*/)
YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode, CreatedEntitiesList* createdEntitiesList /*= nullptr*/, bool isPrefab /*= false*/, EntityID* entityIndex /*= nullptr*/)
{
if(!sceneNode)
return YAML::Node();
@ -216,7 +250,10 @@ namespace SHADE
node.SetStyle(YAML::EmitterStyle::Block);
node[EIDNode] = (entityIndex) ? *entityIndex : eid;
if(createdEntitiesList && entityIndex)
{
createdEntitiesList->insert({eid, *entityIndex});
}
AssetID prefabAssetID = SHPrefabManager::GetPrefabAssetID(eid);
if(prefabAssetID != 0)
{
@ -273,6 +310,10 @@ namespace SHADE
}
//auto entityVecIt = createdEntities.begin();
for (auto it = entities.begin(); it != entities.end(); ++it)
{
RecursivelyResolveEntityIDs(it, createdEntities);
}
for (auto it = entities.begin(); it != entities.end(); ++it)
{
InitializeEntity(*it, createdEntities[(*it)[EIDNode].as<EntityID>()]);
}
@ -367,6 +408,7 @@ namespace SHADE
ResolveSerializedEntityID(out, it, (*it), createdEntities);
}
out << YAML::EndSeq;
return out.c_str();
}
@ -389,5 +431,12 @@ namespace SHADE
SHSerializationHelper::InitializeComponentFromNode<SHLightComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHAnimatorComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHUIComponent>(componentsNode, eid);
// Deserialise scripts
if (entityNode[ScriptsNode].IsDefined())
SHSystemManager::GetSystem<SHScriptEngine>()->DeserialiseScripts(eid, entityNode[ScriptsNode]);
}
}

View File

@ -38,13 +38,13 @@ namespace SHADE
static std::string DeserializeSceneFromFile(AssetID const& sceneAssetID) noexcept;
static void EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out, bool isPrefab = false, EntityID* entityIndex = nullptr);
static void EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out, CreatedEntitiesList* createdEntitiesList = nullptr, bool isPrefab = false, EntityID* entityIndex = nullptr);
static std::string SerializeEntitiesToString(std::vector<EntityID> const& entities) noexcept;
static std::string SerializeEntityToString(EntityID eid) noexcept;
static std::string SerializeEntityToString(EntityID eid, AssetID prefabAssetID = 0) noexcept;
//static void SerializeEntityToFile(std::filesystem::path const& path);
static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode, bool isPrefab = false, EntityID* entityIndex = nullptr);
static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode, CreatedEntitiesList* createdEntitiesList = nullptr, bool isPrefab = false, EntityID* entityIndex = nullptr);
static CreatedEntitiesList DeserializeEntitiesFromString(std::string const& data, EntityID const& parentEID = MAX_EID) noexcept;

View File

@ -59,6 +59,7 @@ namespace SHADE
else if constexpr (std::is_same_v<FieldType, GameObject>)
{
fieldNode = MAX_EID;
fieldNode.SetTag("EID");
}
else if constexpr (std::is_same_v<FieldType, FontAsset> ||
std::is_same_v<FieldType, MaterialAsset> ||
@ -125,6 +126,7 @@ namespace SHADE
{
GameObject gameObj = safe_cast<GameObject>(object);
fieldNode = gameObj ? gameObj.GetEntity() : MAX_EID;
fieldNode.SetTag("EID");
return true;
}
}