Skip to main content

goud_engine/context_registry/scene/
data.rs

1//! Data types for scene serialization.
2//!
3//! These types represent the serialized form of a scene: entities and their
4//! components as JSON-friendly structures that can be saved/loaded.
5
6use std::collections::HashMap;
7
8use crate::ecs::entity::Entity;
9
10// =============================================================================
11// SerializedEntity
12// =============================================================================
13
14/// A serialized entity identifier.
15///
16/// Captures the index and generation of an [`Entity`] so it can be
17/// reconstructed or used as a key in a remap table.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
19pub struct SerializedEntity {
20    /// The entity slot index.
21    pub index: u32,
22    /// The entity generation.
23    pub generation: u32,
24}
25
26impl SerializedEntity {
27    /// Creates a `SerializedEntity` from a live [`Entity`].
28    #[inline]
29    pub fn from_entity(entity: Entity) -> Self {
30        Self {
31            index: entity.index(),
32            generation: entity.generation(),
33        }
34    }
35}
36
37// =============================================================================
38// EntityData
39// =============================================================================
40
41/// Serialized representation of a single entity and its components.
42#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
43pub struct EntityData {
44    /// The original entity identifier.
45    pub id: SerializedEntity,
46    /// Map of component type name to serialized component value.
47    pub components: HashMap<String, serde_json::Value>,
48}
49
50// =============================================================================
51// SceneData
52// =============================================================================
53
54/// The complete serialized form of a scene.
55///
56/// Contains the scene name and a list of all serialized entities with
57/// their components.
58#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
59pub struct SceneData {
60    /// The name of the scene.
61    pub name: String,
62    /// All entities in the scene.
63    pub entities: Vec<EntityData>,
64}
65
66// =============================================================================
67// EntityRemap
68// =============================================================================
69
70/// Maps old (serialized) entity IDs to newly spawned entities.
71///
72/// After deserializing a scene, components that reference other entities
73/// (e.g., [`Parent`](crate::ecs::components::Parent),
74/// [`Children`](crate::ecs::components::Children)) need their entity
75/// references updated to point to the newly created entities.
76#[derive(Debug, Clone)]
77pub struct EntityRemap(pub HashMap<SerializedEntity, Entity>);