space_prefab/component/
mod.rs1pub mod shape;
3pub use shape::*;
4
5pub mod material;
7pub use material::*;
8
9pub mod camera;
11pub use camera::*;
12
13pub mod player_start;
15pub use player_start::*;
16
17pub mod path;
19
20use bevy::{prelude::*, reflect::*, utils::HashMap};
21
22pub mod ext {
24 pub use space_shared::ext::*;
25}
26
27#[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#[derive(Component, Reflect, Default)]
46pub struct SceneAutoChild;
47
48#[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#[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#[derive(Component, Reflect, Clone, Default)]
117#[reflect(Component, Default)]
118pub struct AssetMesh {
119 pub path: String,
120}
121
122#[derive(Component, Reflect, Clone, Default)]
124#[reflect(Component, Default)]
125pub struct AssetMaterial {
126 pub path: String,
127}