vkmEngine 1.0.15
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/ocean.h"
31#include "ecs/component/render/particle_emitter.h"
32#include "ecs/component/render/planar_reflector.h"
33#include "ecs/component/render/reflection_probe.h"
34#include "ecs/component/render/terrain.h"
35#include "ecs/component/ui/ui_button.h"
36#include "ecs/component/ui/ui_canvas.h"
37#include "ecs/component/ui/ui_choice.h"
38#include "ecs/component/ui/ui_element.h"
39#include "ecs/component/ui/ui_image.h"
40#include "ecs/component/core/net_local.h"
41#include "ecs/component/ui/ui_scroll.h"
42#include "ecs/component/ui/ui_slider.h"
43#include "ecs/component/ui/ui_toggle.h"
44#include "ecs/component/ui/ui_text.h"
45#include "system/script/script_component.h"
46
47namespace Vkm::Engine {
48
49class Scene;
50
51class ResourceManager;
52
64#define VKM_SCENE_COMPONENTS(P, R, E) \
65 P(Name, "Name") \
66 P(Transform, "Transform") \
67 P(Camera, "Camera") \
68 P(Light, "Light") \
69 P(Rigidbody, "Rigidbody") \
70 R(Collider, "Collider") \
71 P(CharacterController, "CharacterController") \
72 E(Joint, "Joint") \
73 E(Ragdoll, "Ragdoll") \
74 R(Mesh, "Mesh") \
75 R(LOD, "LOD") \
76 R(Decal, "Decal") \
77 R(ParticleEmitter, "ParticleEmitter") \
78 P(ParticleTrigger, "ParticleTrigger") \
79 R(AudioSource, "AudioSource") \
80 P(AudioListener, "AudioListener") \
81 P(IrradianceVolume, "IrradianceVolume") \
82 P(Ocean, "Ocean") \
83 P(ReflectionProbe, "ReflectionProbe") \
84 P(PlanarReflector, "PlanarReflector") \
85 R(Terrain, "Terrain") \
86 P(Animation, "Animation") \
87 R(Animator, "Animator") \
88 P(BoneSocket, "BoneSocket") \
89 P(ScriptComponent, "Script") \
90 P(UICanvas, "UICanvas") \
91 P(UIElement, "UIElement") \
92 R(UIImage, "UIImage") \
93 P(UIText, "UIText") \
94 P(UIButton, "UIButton") \
95 P(UIScroll, "UIScroll") \
96 E(UISlider, "UISlider") \
97 E(UIToggle, "UIToggle") \
98 E(UIChoice, "UIChoice") \
99 P(NetLocal, "NetLocal")
100
110
111 nlohmann::json save(const PhysicsSettings&);
112 void load(const nlohmann::json&, PhysicsSettings&);
113
114 nlohmann::json save(const Name&);
115 void load(const nlohmann::json&, Name&);
116
122 nlohmann::json save(const Environment&);
123 void load(const nlohmann::json&, Environment&);
124
125 nlohmann::json save(const Transform&);
126 void load(const nlohmann::json&, Transform&);
127
128 nlohmann::json save(const Camera&);
129 void load(const nlohmann::json&, Camera&);
130
131 nlohmann::json save(const Light&);
132 void load(const nlohmann::json&, Light&);
133
139 nlohmann::json save(const Rigidbody&);
140 void load(const nlohmann::json&, Rigidbody&);
141
148 nlohmann::json save(const Collider&, const ResourceManager&);
149 void load(const nlohmann::json&, Collider&, const ResourceManager&);
150
157 nlohmann::json save(const CharacterController&);
158 void load(const nlohmann::json&, CharacterController&);
159
163 nlohmann::json save(const Joint&, const EntityNamer&);
164 void load(const nlohmann::json&, Joint&, const EntityResolver&);
165
172 nlohmann::json save(const Ragdoll&, const EntityNamer&);
173 void load(const nlohmann::json&, Ragdoll&, const EntityResolver&);
174
175 nlohmann::json save(const Mesh&, const ResourceManager&);
176 void load(const nlohmann::json&, Mesh&, const ResourceManager&);
177
183 nlohmann::json save(const Animator&, const ResourceManager&);
184 void load(const nlohmann::json&, Animator&, const ResourceManager&);
185
192 nlohmann::json save(const BoneSocket&);
193 void load(const nlohmann::json&, BoneSocket&);
194
195 nlohmann::json save(const LOD&, const ResourceManager&);
196 void load(const nlohmann::json&, LOD&, const ResourceManager&);
197
198 nlohmann::json save(const Decal&, const ResourceManager&);
199 void load(const nlohmann::json&, Decal&, const ResourceManager&);
200
201 nlohmann::json save(const ParticleEmitter&, const ResourceManager&);
202 void load(const nlohmann::json&, ParticleEmitter&, const ResourceManager&);
203
204 nlohmann::json save(const ParticleTrigger&);
205 void load(const nlohmann::json&, ParticleTrigger&);
206
213 nlohmann::json save(const AudioSource&, const ResourceManager&);
214 void load(const nlohmann::json&, AudioSource&, const ResourceManager&);
215
216 nlohmann::json save(const AudioListener&);
217 void load(const nlohmann::json&, AudioListener&);
218
219 nlohmann::json save(const IrradianceVolume&);
220 void load(const nlohmann::json&, IrradianceVolume&);
221
222 nlohmann::json save(const Ocean&);
223 void load(const nlohmann::json&, Ocean&);
224
225 nlohmann::json save(const ReflectionProbe&);
226 void load(const nlohmann::json&, ReflectionProbe&);
227 nlohmann::json save(const PlanarReflector&);
228 void load(const nlohmann::json&, PlanarReflector&);
229
230 nlohmann::json save(const Terrain&, const ResourceManager&);
231 void load(const nlohmann::json&, Terrain&, const ResourceManager&);
232
233 nlohmann::json save(const UICanvas&);
234 void load(const nlohmann::json&, UICanvas&);
235
236 nlohmann::json save(const UIElement&);
237 void load(const nlohmann::json&, UIElement&);
238
239 nlohmann::json save(const UIImage&, const ResourceManager&);
240 void load(const nlohmann::json&, UIImage&, const ResourceManager&);
241
242 nlohmann::json save(const UIText&);
243 void load(const nlohmann::json&, UIText&);
244
245 nlohmann::json save(const UIButton&);
246 void load(const nlohmann::json&, UIButton&);
247
248 nlohmann::json save(const UIScroll&);
249 void load(const nlohmann::json&, UIScroll&);
250
251 nlohmann::json save(const NetLocal&);
252 void load(const nlohmann::json&, NetLocal&);
253
254 nlohmann::json save(const UISlider&, const EntityNamer&);
255 void load(const nlohmann::json&, UISlider&, const EntityResolver&);
256
257 nlohmann::json save(const UIToggle&, const EntityNamer&);
258 void load(const nlohmann::json&, UIToggle&, const EntityResolver&);
259
260 nlohmann::json save(const UIChoice&, const EntityNamer&);
261 void load(const nlohmann::json&, UIChoice&, const EntityResolver&);
262
268 nlohmann::json save(const Hierarchy&);
279 uint32_t loadParentIndex(const nlohmann::json& json);
280
284 nlohmann::json save(const Animation&);
285 void load(const nlohmann::json&, Animation&);
286
294 nlohmann::json save(const ScriptComponent&);
295 void load(const nlohmann::json&, ScriptComponent&);
296
304 struct AssetRefs {
305 std::vector<MeshHandle> meshes;
306 std::vector<MaterialHandle> materials;
307 std::vector<SkeletonHandle> skeletons;
308 std::vector<TextureHandle> textures;
309 std::vector<AnimationClipHandle> clips;
310 std::vector<AudioClipHandle> sounds;
311 std::vector<TerrainHandle> terrains;
312 };
313
321 void emitAssetRefs(const Mesh&, AssetRefs&);
322 void emitAssetRefs(const LOD&, AssetRefs&);
323 void emitAssetRefs(const Decal&, AssetRefs&);
325 void emitAssetRefs(const AudioSource&, AssetRefs&);
326 void emitAssetRefs(const Terrain&, AssetRefs&);
327 void emitAssetRefs(const Animator&, AssetRefs&);
328 void emitAssetRefs(const UIImage&, AssetRefs&);
329
337 std::string field;
338 std::string name;
339 AssetType type = AssetType::Count;
340 };
341
353 std::vector<UnresolvedRef> takeUnresolvedRefs();
354
362 class UnresolvedScope {
363 public:
364 UnresolvedScope() = default;
365 ~UnresolvedScope();
366
367 UnresolvedScope(const UnresolvedScope& other) = delete;
368 UnresolvedScope& operator=(const UnresolvedScope& other) = delete;
369
370 UnresolvedScope(UnresolvedScope && other) = delete;
371 UnresolvedScope& operator=(UnresolvedScope && other) = delete;
372 };
373
374} // namespace ComponentSerializer
375
376} // namespace Vkm::Engine
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:109
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:26
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:17
Collision geometry attached to an entity, evaluated in its Transform frame.
Definition collider.h:84
Every asset a set of components references, one list per kind.
Definition component_serializer.h:304
An asset name a load failed to resolve, and the field it was read from.
Definition component_serializer.h:336
AssetType type
Which kind the loader looked it up as.
Definition component_serializer.h:339
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:137
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:36
Component representing a renderable mesh (geometry + material) in the world.
Definition mesh.h:34
Component for giving entities a human-readable name.
Definition name.h:15
Keeps an entity, and everything below it, off the wire: each end runs its own.
Definition net_local.h:18
The scene's sea: wind-driven waves moving every surface whose material is waves.
Definition ocean.h:40
CPU-simulated particle emitter: fire, smoke, sparks, impacts, trails, heat haze.
Definition particle_emitter.h:74
Makes a child entity's ParticleEmitter a sub-emitter of its parent's.
Definition particle_emitter.h:165
The physics world's own parameters, read once per fixed step.
Definition physics_settings.h:14
A flat mirror: a rectangle on the entity's XZ plane, facing its +Y, that reflects the scene as it is ...
Definition planar_reflector.h:18
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:32
ECS component attaching owned, polymorphic Behaviors to an entity.
Definition script_component.h:36
A terrain in the world: its asset, centred on the entity.
Definition terrain.h:15
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
One of a list of options, stepped through by two UIButtons: a setting's "< High >" row.
Definition ui_choice.h:21
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
A value dragged along a UIButton's width: the button is the track.
Definition ui_slider.h:20
Text drawn within its element's rect, on as many lines as it breaks into.
Definition ui_text.h:24
A UIButton that is on or off, flipped by its click.
Definition ui_toggle.h:20