vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
component_serializer.h
1#pragma once
2
3#include <string>
4#include <vector>
5
6#include <nlohmann/json.hpp>
7
8#include "ecs/entity_mapping.h"
9#include "ecs/environment.h"
10#include "resource/asset_type.h"
11#include "ecs/component/animation/animation.h"
12#include "ecs/component/animation/animator.h"
13#include "ecs/component/animation/bone_socket.h"
14#include "ecs/component/audio/audio_listener.h"
15#include "ecs/component/audio/audio_source.h"
16#include "ecs/component/core/hierarchy.h"
17#include "ecs/component/core/name.h"
18#include "ecs/component/core/transform.h"
19#include "ecs/component/physics/character_controller.h"
20#include "ecs/component/physics/collider.h"
21#include "ecs/component/physics/joint.h"
22#include "ecs/component/physics/ragdoll.h"
23#include "ecs/component/physics/rigidbody.h"
24#include "ecs/component/render/camera.h"
25#include "ecs/component/render/decal.h"
26#include "ecs/component/render/irradiance_volume.h"
27#include "ecs/component/render/light.h"
28#include "ecs/component/render/lod.h"
29#include "ecs/component/render/mesh.h"
30#include "ecs/component/render/particle_emitter.h"
31#include "ecs/component/render/reflection_probe.h"
32#include "ecs/component/ui/ui_button.h"
33#include "ecs/component/ui/ui_canvas.h"
34#include "ecs/component/ui/ui_element.h"
35#include "ecs/component/ui/ui_image.h"
36#include "ecs/component/ui/ui_scroll.h"
37#include "ecs/component/ui/ui_text.h"
38#include "system/script/script_component.h"
39
40namespace Vkm::Engine {
41
42class Scene;
43
44class ResourceManager;
45
57#define VKM_SCENE_COMPONENTS(P, R, E) \
58 P(Name, "Name") \
59 P(Transform, "Transform") \
60 P(Camera, "Camera") \
61 P(Light, "Light") \
62 P(Rigidbody, "Rigidbody") \
63 R(Collider, "Collider") \
64 P(CharacterController, "CharacterController") \
65 E(Joint, "Joint") \
66 E(Ragdoll, "Ragdoll") \
67 R(Mesh, "Mesh") \
68 R(LOD, "LOD") \
69 R(Decal, "Decal") \
70 P(ParticleEmitter, "ParticleEmitter") \
71 R(AudioSource, "AudioSource") \
72 P(AudioListener, "AudioListener") \
73 P(IrradianceVolume, "IrradianceVolume") \
74 P(ReflectionProbe, "ReflectionProbe") \
75 P(Animation, "Animation") \
76 R(Animator, "Animator") \
77 P(BoneSocket, "BoneSocket") \
78 P(ScriptComponent, "Script") \
79 P(UICanvas, "UICanvas") \
80 P(UIElement, "UIElement") \
81 R(UIImage, "UIImage") \
82 P(UIText, "UIText") \
83 P(UIButton, "UIButton") \
84 P(UIScroll, "UIScroll")
85
95
96 nlohmann::json save(const PhysicsSettings&);
97 void load(const nlohmann::json&, PhysicsSettings&);
98
99 nlohmann::json save(const Name&);
100 void load(const nlohmann::json&, Name&);
101
107 nlohmann::json save(const Environment&);
108 void load(const nlohmann::json&, Environment&);
109
110 nlohmann::json save(const Transform&);
111 void load(const nlohmann::json&, Transform&);
112
113 nlohmann::json save(const Camera&);
114 void load(const nlohmann::json&, Camera&);
115
116 nlohmann::json save(const Light&);
117 void load(const nlohmann::json&, Light&);
118
124 nlohmann::json save(const Rigidbody&);
125 void load(const nlohmann::json&, Rigidbody&);
126
133 nlohmann::json save(const Collider&, const ResourceManager&);
134 void load(const nlohmann::json&, Collider&, const ResourceManager&);
135
142 nlohmann::json save(const CharacterController&);
143 void load(const nlohmann::json&, CharacterController&);
144
148 nlohmann::json save(const Joint&, const EntityNamer&);
149 void load(const nlohmann::json&, Joint&, const EntityResolver&);
150
157 nlohmann::json save(const Ragdoll&, const EntityNamer&);
158 void load(const nlohmann::json&, Ragdoll&, const EntityResolver&);
159
160 nlohmann::json save(const Mesh&, const ResourceManager&);
161 void load(const nlohmann::json&, Mesh&, const ResourceManager&);
162
168 nlohmann::json save(const Animator&, const ResourceManager&);
169 void load(const nlohmann::json&, Animator&, const ResourceManager&);
170
177 nlohmann::json save(const BoneSocket&);
178 void load(const nlohmann::json&, BoneSocket&);
179
180 nlohmann::json save(const LOD&, const ResourceManager&);
181 void load(const nlohmann::json&, LOD&, const ResourceManager&);
182
183 nlohmann::json save(const Decal&, const ResourceManager&);
184 void load(const nlohmann::json&, Decal&, const ResourceManager&);
185
186 nlohmann::json save(const ParticleEmitter&);
187 void load(const nlohmann::json&, ParticleEmitter&);
188
195 nlohmann::json save(const AudioSource&, const ResourceManager&);
196 void load(const nlohmann::json&, AudioSource&, const ResourceManager&);
197
198 nlohmann::json save(const AudioListener&);
199 void load(const nlohmann::json&, AudioListener&);
200
201 nlohmann::json save(const IrradianceVolume&);
202 void load(const nlohmann::json&, IrradianceVolume&);
203
204 nlohmann::json save(const ReflectionProbe&);
205 void load(const nlohmann::json&, ReflectionProbe&);
206
207 nlohmann::json save(const UICanvas&);
208 void load(const nlohmann::json&, UICanvas&);
209
210 nlohmann::json save(const UIElement&);
211 void load(const nlohmann::json&, UIElement&);
212
213 nlohmann::json save(const UIImage&, const ResourceManager&);
214 void load(const nlohmann::json&, UIImage&, const ResourceManager&);
215
216 nlohmann::json save(const UIText&);
217 void load(const nlohmann::json&, UIText&);
218
219 nlohmann::json save(const UIButton&);
220 void load(const nlohmann::json&, UIButton&);
221
222 nlohmann::json save(const UIScroll&);
223 void load(const nlohmann::json&, UIScroll&);
224
230 nlohmann::json save(const Hierarchy&);
241 uint32_t loadParentIndex(const nlohmann::json& json);
242
246 nlohmann::json save(const Animation&);
247 void load(const nlohmann::json&, Animation&);
248
256 nlohmann::json save(const ScriptComponent&);
257 void load(const nlohmann::json&, ScriptComponent&);
258
266 struct AssetRefs {
267 std::vector<MeshHandle> meshes;
268 std::vector<MaterialHandle> materials;
269 std::vector<SkeletonHandle> skeletons;
270 std::vector<TextureHandle> textures;
271 std::vector<AnimationClipHandle> clips;
272 std::vector<AudioClipHandle> sounds;
273 };
274
282 void emitAssetRefs(const Mesh&, AssetRefs&);
283 void emitAssetRefs(const LOD&, AssetRefs&);
284 void emitAssetRefs(const Decal&, AssetRefs&);
285 void emitAssetRefs(const AudioSource&, AssetRefs&);
286 void emitAssetRefs(const Animator&, AssetRefs&);
287 void emitAssetRefs(const UIImage&, AssetRefs&);
288
295 std::string field;
296 std::string name;
297 AssetType type = AssetType::Count;
298 };
299
311 std::vector<UnresolvedRef> takeUnresolvedRefs();
312
320 class UnresolvedScope {
321 public:
322 UnresolvedScope() = default;
323 ~UnresolvedScope();
324
325 UnresolvedScope(const UnresolvedScope& other) = delete;
326 UnresolvedScope& operator=(const UnresolvedScope& other) = delete;
327
328 UnresolvedScope(UnresolvedScope && other) = delete;
329 UnresolvedScope& operator=(UnresolvedScope && other) = delete;
330 };
331
332} // namespace ComponentSerializer
333
334} // namespace Vkm::Engine
Open type-erased resource registry with typed handles and generational lifetimes.
Definition resource_manager.h:46
Central registry managing entities and an open set of component types.
Definition scene.h:29
Per-component (de)serialization to JSON.
Definition component_serializer.h:94
uint32_t loadParentIndex(const nlohmann::json &json)
Read a saved Hierarchy's parent reference as a saved slot.
void emitAssetRefs(const Collider &, AssetRefs &)
Record every asset the component references into refs.
std::vector< UnresolvedRef > takeUnresolvedRefs()
Take the references the loads since the last call could not resolve.
Component representing an animation that can be applied to an entity.
Definition animation.h:17
What drives a rig: the skeleton, the clip playing on it, and the playback position.
Definition animator.h:45
The ear: the pose every spatial source is heard relative to.
Definition audio_listener.h:16
A sound emitter: one clip, how loud, and where it is heard from.
Definition audio_source.h:17
Carried by a weapon, a hat or a muzzle flash to ride a bone of the rig it hangs off.
Definition bone_socket.h:23
Component representing a camera, containing projection and view parameters.
Definition camera.h:25
Turns a desired direction into velocity on a Rigidbody, and reports what the body stands on.
Definition character_controller.h:16
Collision geometry attached to an entity, evaluated in its Transform frame.
Definition collider.h:72
Every asset a set of components references, one list per kind.
Definition component_serializer.h:266
An asset name a load failed to resolve, and the field it was read from.
Definition component_serializer.h:294
AssetType type
Which kind the loader looked it up as.
Definition component_serializer.h:297
A projected decal - bullet holes, blood, scorch marks.
Definition decal.h:16
The scene's lighting environment: sky, night sky and fog.
Definition environment.h:130
Component representing parent-child relationships in an entity hierarchy.
Definition hierarchy.h:16
Baked global illumination: a grid of irradiance probes filling a box.
Definition irradiance_volume.h:21
Ties this entity's body to another, at a point on each.
Definition joint.h:31
Distance-selected geometry for an entity.
Definition lod.h:25
Component representing a light source in the scene.
Definition light.h:34
Component representing a renderable mesh (geometry + material) in the world.
Definition mesh.h:13
Component for giving entities a human-readable name.
Definition name.h:15
CPU-simulated particle emitter - muzzle flashes, impacts, smoke puffs.
Definition particle_emitter.h:18
The physics world's own parameters, read once per fixed step.
Definition physics_settings.h:14
Turns a rig over to physics, and back.
Definition ragdoll.h:49
Local reflection + irradiance probe.
Definition reflection_probe.h:18
Dynamics state for a physics body: motion, material response and mass.
Definition rigidbody.h:31
ECS component attaching owned, polymorphic Behaviors to an entity.
Definition script_component.h:36
Local position, rotation and scale of an entity.
Definition transform.h:17
An interactive, hit-tested button drawn over its element's rect.
Definition ui_button.h:20
Root of a screen-space UI layer.
Definition ui_canvas.h:18
The 2D rect of a UI node - the screen-space analogue of Transform.
Definition ui_element.h:44
A filled quad drawn over its element's resolved rect.
Definition ui_image.h:16
Makes an element's children scroll inside it, and clips them to it.
Definition ui_scroll.h:18
Text drawn within its element's rect, on as many lines as it breaks into.
Definition ui_text.h:24