space_prefab/component/
mod.rs

1/// Module contatins structures for determining mesh shapes
2pub mod shape;
3pub use shape::*;
4
5/// Module contatins structures for determining standart material
6pub mod material;
7pub use material::*;
8
9/// Module contatins structures for determining camera
10pub mod camera;
11pub use camera::*;
12
13/// Module contatins structures for determining player start
14pub mod player_start;
15pub use player_start::*;
16
17/// NOT USED. Planned to be used in future for auto structs
18pub mod path;
19
20use bevy::{prelude::*, reflect::*, utils::HashMap};
21
22/// External dependencies
23pub mod ext {
24    pub use space_shared::ext::*;
25}
26
27/// Component to define path to gltf asset that will be loaded after prefab spawn
28#[derive(Component, Reflect, Clone)]
29#[reflect(Component)]
30pub struct GltfPrefab {
31    pub path: String,
32    pub scene: String,
33}
34
35impl Default for GltfPrefab {
36    fn default() -> Self {
37        Self {
38            scene: "Scene0".to_string(),
39            path: String::new(),
40        }
41    }
42}
43
44/// Marker for entities spawned from gltf scene
45#[derive(Component, Reflect, Default)]
46pub struct SceneAutoChild;
47
48/// Not used right now. Planned to be easy method for creating prefab structs from usual structs with assets
49#[derive(Component, Reflect, Clone, Default)]
50#[reflect(Component)]
51pub struct AutoStruct<T: Reflect + Default + Clone> {
52    pub data: T,
53    pub asset_paths: HashMap<String, String>,
54}
55
56impl<T: Reflect + FromReflect + Default + Clone> AutoStruct<T> {
57    pub fn new(data: &T, _assets: &AssetServer) -> Self {
58        let mut paths = HashMap::new();
59
60        if let ReflectRef::Struct(s) = data.reflect_ref() {
61            for idx in 0..s.field_len() {
62                let field_name = s.name_at(idx).unwrap();
63                let field = s.field_at(idx).unwrap();
64                if let Some(handle) = field.downcast_ref::<Handle<Image>>() {
65                    if let Some(path) = handle.path() {
66                        let path = path.path().to_str().unwrap().to_string();
67                        paths.insert(field_name.to_string(), path);
68                    }
69                }
70            }
71        }
72
73        Self {
74            data: data.clone(),
75            asset_paths: paths,
76        }
77    }
78
79    pub fn get_data(&self, assets: &AssetServer) -> T {
80        let mut res = self.data.clone();
81        {
82            let res_reflect = res.as_reflect_mut();
83            if let ReflectMut::Struct(s) = res_reflect.reflect_mut() {
84                for (field_name, path) in self.asset_paths.iter() {
85                    if let Some(field) = s.field_mut(field_name) {
86                        #[allow(clippy::option_if_let_else)]
87                        if let Some(handle) = field.downcast_mut::<Handle<Image>>() {
88                            *handle = assets.load(path);
89                        } else if let Some(handle) = field.downcast_mut::<Handle<Mesh>>() {
90                            *handle = assets.load(path);
91                        }
92                    }
93                }
94            }
95        }
96        T::default()
97    }
98}
99
100/// This component used in prefab to determine links between entities. It is needed to create custom UI in `bevy_inspector_egui`. You must implement the [`MapEntities`](bevy::ecs::entity::MapEntities) trait for your component to make it work. See the `FollowCamera` struct from `examples/platformer.rs`.
101#[derive(Reflect, Clone)]
102#[reflect(Default)]
103pub struct EntityLink {
104    pub entity: Entity,
105}
106
107impl Default for EntityLink {
108    fn default() -> Self {
109        Self {
110            entity: Entity::PLACEHOLDER,
111        }
112    }
113}
114
115/// Component to define path to mesh asset that will be loaded after prefab spawn
116#[derive(Component, Reflect, Clone, Default)]
117#[reflect(Component, Default)]
118pub struct AssetMesh {
119    pub path: String,
120}
121
122/// Component to define path to material asset that will be loaded after prefab spawn
123#[derive(Component, Reflect, Clone, Default)]
124#[reflect(Component, Default)]
125pub struct AssetMaterial {
126    pub path: String,
127}