khora_data/scene/recipe.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Defines the data format for the "Recipe" serialization strategy.
16//!
17//! This format represents a scene as an executable sequence of commands, which provides
18//! great flexibility for tools, streaming, and scene patching.
19
20use bincode::{Decode, Encode};
21use khora_core::ecs::entity::EntityId;
22use serde::{Deserialize, Serialize};
23
24/// The root container for a scene recipe. It's simply a list of commands.
25#[derive(Debug, Serialize, Deserialize, Encode, Decode)]
26pub struct SceneRecipe {
27 /// The ordered list of commands to execute to reconstruct the scene.
28 pub commands: Vec<SceneCommand>,
29}
30
31/// A single, atomic operation required to construct a scene.
32#[derive(Debug, Serialize, Deserialize, Encode, Decode)]
33pub enum SceneCommand {
34 /// Spawns a new, empty entity with a specific ID from the original scene.
35 Spawn {
36 /// The ID to assign to the newly spawned entity.
37 id: EntityId,
38 },
39 /// Adds a component to a specified entity.
40 AddComponent {
41 /// The ID of the entity to which the component should be added.
42 entity_id: EntityId,
43 /// The full type name of the component (e.g., "khora_data::ecs::components::Transform").
44 /// This will be used for reflection during deserialization.
45 component_type: String,
46 /// The component data, serialized into a compact binary format using bincode.
47 component_data: Vec<u8>,
48 },
49 /// Establishes a parent-child relationship between two entities.
50 SetParent {
51 /// The ID of the child entity.
52 child_id: EntityId,
53 /// The ID of the parent entity.
54 parent_id: EntityId,
55 },
56}