添加 Components 和 Scene 序列化支持

- Component: 添加 Serialize/Deserialize 虚函数
- TransformComponent: 实现 Transform 数据的序列化/反序列化
- GameObject: 实现对象序列化/反序列化
- Scene: 实现 Save/Load 方法,支持场景文件保存和加载
- 测试: 添加 Save_And_Load 和 Save_ContainsGameObjectData 测试
This commit is contained in:
2026-03-22 03:42:40 +08:00
parent a9d5a68dd6
commit 70571d11df
10 changed files with 189 additions and 15 deletions

View File

@@ -2,6 +2,7 @@
#include "Components/GameObject.h"
#include "Scene/Scene.h"
#include <algorithm>
#include <sstream>
namespace XCEngine {
namespace Components {
@@ -273,5 +274,47 @@ void TransformComponent::NotifyHierarchyChanged() {
SetDirty();
}
void TransformComponent::Serialize(std::ostream& os) const {
os << "position=" << m_localPosition.x << "," << m_localPosition.y << "," << m_localPosition.z << ";";
os << "rotation=" << m_localRotation.x << "," << m_localRotation.y << "," << m_localRotation.z << "," << m_localRotation.w << ";";
os << "scale=" << m_localScale.x << "," << m_localScale.y << "," << m_localScale.z << ";";
}
void TransformComponent::Deserialize(std::istream& is) {
std::string token;
while (std::getline(is, token, ';')) {
if (token.empty()) continue;
size_t eqPos = token.find('=');
if (eqPos == std::string::npos) continue;
std::string key = token.substr(0, eqPos);
std::string value = token.substr(eqPos + 1);
if (key == "position") {
std::replace(value.begin(), value.end(), ',', ' ');
std::istringstream vs(value);
float x, y, z;
vs >> x >> y >> z;
m_localPosition = Math::Vector3(x, y, z);
} else if (key == "rotation") {
std::replace(value.begin(), value.end(), ',', ' ');
std::istringstream vs(value);
float x, y, z, w;
vs >> x >> y >> z >> w;
m_localRotation = Math::Quaternion(x, y, z, w);
} else if (key == "scale") {
std::replace(value.begin(), value.end(), ',', ' ');
std::istringstream vs(value);
float x, y, z;
vs >> x >> y >> z;
m_localScale = Math::Vector3(x, y, z);
}
}
SetDirty();
}
} // namespace Components
} // namespace XCEngine