-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneSerializer.cpp
More file actions
81 lines (65 loc) · 2.61 KB
/
SceneSerializer.cpp
File metadata and controls
81 lines (65 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include "SceneSystem.h"
#include "Components/MeshComponent.h"
#include "Components/RigidBodyComponent.h"
#include <nlohmann/json.hpp>
#include <fstream>
using json = nlohmann::json;
namespace FelissScene {
class SceneSerializer {
public:
static void SaveScene(const Scene& scene, const std::string& filepath) {
json jscene;
for (const auto& [id, entity] : scene.GetAllEntities()) {
json jent;
jent["id"] = entity.id;
jent["name"] = entity.name;
jent["position"] = { entity.transform.position[0], entity.transform.position[1], entity.transform.position[2] };
jent["rotation"] = { entity.transform.rotation[0], entity.transform.rotation[1], entity.transform.rotation[2] };
jent["scale"] = { entity.transform.scale[0], entity.transform.scale[1], entity.transform.scale[2] };
if (auto* mesh = entity.GetComponent<MeshComponent>()) {
jent["mesh"] = mesh->meshPath;
}
if (auto* rb = entity.GetComponent<RigidBodyComponent>()) {
jent["rigidbody"] = {
{"mass", rb->mass},
{"isStatic", rb->isStatic}
};
}
jscene["entities"].push_back(jent);
}
std::ofstream out(filepath);
out << jscene.dump(4);
}
static void LoadScene(Scene& scene, const std::string& filepath) {
std::ifstream in(filepath);
if (!in.is_open()) return;
json jscene;
in >> jscene;
scene.Clear();
for (const auto& jent : jscene["entities"]) {
EntityID id = scene.CreateEntity(jent["name"]);
Entity* e = scene.GetEntity(id);
auto pos = jent["position"];
auto rot = jent["rotation"];
auto scl = jent["scale"];
for (int i = 0; i < 3; ++i) {
e->transform.position[i] = pos[i];
e->transform.rotation[i] = rot[i];
e->transform.scale[i] = scl[i];
}
if (jent.contains("mesh")) {
auto mesh = std::make_shared<MeshComponent>();
mesh->meshPath = jent["mesh"];
e->AddComponent(mesh);
}
if (jent.contains("rigidbody")) {
auto rb = std::make_shared<RigidBodyComponent>();
rb->mass = jent["rigidbody"]["mass"];
rb->isStatic = jent["rigidbody"]["isStatic"];
e->AddComponent(rb);
}
}
}
};
}