pub mod shape;
pub use shape::*;
pub mod material;
pub use material::*;
pub mod camera;
pub use camera::*;
pub mod player_start;
pub use player_start::*;
pub mod path;
use bevy::{prelude::*, reflect::*, utils::HashMap};
pub mod ext {
pub use space_shared::ext::*;
}
#[derive(Component, Reflect, Clone)]
#[reflect(Component)]
pub struct GltfPrefab {
pub path: String,
pub scene: String,
}
impl Default for GltfPrefab {
fn default() -> Self {
Self {
scene: "Scene0".to_string(),
path: String::new(),
}
}
}
#[derive(Component, Reflect, Default)]
pub struct SceneAutoChild;
#[derive(Component, Reflect, Clone, Default)]
#[reflect(Component)]
pub struct AutoStruct<T: Reflect + Default + Clone> {
pub data: T,
pub asset_paths: HashMap<String, String>,
}
impl<T: Reflect + FromReflect + Default + Clone> AutoStruct<T> {
pub fn new(data: &T, _assets: &AssetServer) -> Self {
let mut paths = HashMap::new();
if let ReflectRef::Struct(s) = data.reflect_ref() {
for idx in 0..s.field_len() {
let field_name = s.name_at(idx).unwrap();
let field = s.field_at(idx).unwrap();
if let Some(handle) = field.downcast_ref::<Handle<Image>>() {
if let Some(path) = handle.path() {
let path = path.path().to_str().unwrap().to_string();
paths.insert(field_name.to_string(), path);
}
}
}
}
Self {
data: data.clone(),
asset_paths: paths,
}
}
pub fn get_data(&self, assets: &AssetServer) -> T {
let mut res = self.data.clone();
{
let res_reflect = res.as_reflect_mut();
if let ReflectMut::Struct(s) = res_reflect.reflect_mut() {
for (field_name, path) in self.asset_paths.iter() {
if let Some(field) = s.field_mut(field_name) {
#[allow(clippy::option_if_let_else)]
if let Some(handle) = field.downcast_mut::<Handle<Image>>() {
*handle = assets.load(path);
} else if let Some(handle) = field.downcast_mut::<Handle<Mesh>>() {
*handle = assets.load(path);
}
}
}
}
}
T::default()
}
}
#[derive(Reflect, Clone)]
#[reflect(Default)]
pub struct EntityLink {
pub entity: Entity,
}
impl Default for EntityLink {
fn default() -> Self {
Self {
entity: Entity::PLACEHOLDER,
}
}
}
#[derive(Component, Reflect, Clone, Default)]
#[reflect(Component, Default)]
pub struct AssetMesh {
pub path: String,
}
#[derive(Component, Reflect, Clone, Default)]
#[reflect(Component, Default)]
pub struct AssetMaterial {
pub path: String,
}