1use std::any::Any;
2
3pub trait Component: 'static + Any + Send + Sync + Clone {}
4
5#[macro_export]
6macro_rules! impl_component {
7 ($($t:ty),+ $(,)?) => {
8 $(
9 impl $crate::Component for $t {}
10 )+
11 };
12}
13
14#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
16pub struct Parent(pub u32);
17
18#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
19pub struct Children(pub Vec<u32>);
20
21#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
23pub struct EntityName(pub String);
24
25impl EntityName {
26 pub fn new(name: &str) -> Self {
27 Self(name.to_string())
28 }
29}
30
31#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
33pub struct IsHidden;
34
35#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
37pub struct IsDeleted;
38
39#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
42pub struct PrefabRequest(pub String);
43
44impl PrefabRequest {
45 pub fn new(name: &str) -> Self {
46 Self(name.to_string())
47 }
48
49 pub fn name(&self) -> &str {
51 &self.0
52 }
53}
54
55impl std::fmt::Display for EntityName {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{}", self.0)
58 }
59}
60
61#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
63pub struct MeshSource(pub String);
64
65#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
67pub struct MaterialSource {
68 pub albedo: [f32; 4],
69 pub roughness: f32,
70 pub metallic: f32,
71 pub unlit: f32,
72 pub texture_source: Option<String>,
73}
74
75impl_component!(Parent, Children, EntityName, IsHidden, PrefabRequest, IsDeleted, MeshSource, MaterialSource);
76
77pub trait Bundle {
93 fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity);
95}
96
97pub struct DynamicBundle<B: Bundle, C: Component> {
100 pub bundle: B,
101 pub component: C,
102}
103
104impl<B: Bundle, C: Component> Bundle for DynamicBundle<B, C> {
105 fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity) {
106 self.bundle.apply(world, entity);
107 world.add_component(entity, self.component);
108 }
109}
110
111pub trait BundleExt: Bundle + Sized {
113 fn with<C: Component>(self, component: C) -> DynamicBundle<Self, C> {
115 DynamicBundle {
116 bundle: self,
117 component,
118 }
119 }
120}
121
122impl<T: Bundle> BundleExt for T {}