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)]
38pub struct PrefabRequest(pub String);
39
40impl PrefabRequest {
41 pub fn new(name: &str) -> Self {
42 Self(name.to_string())
43 }
44
45 pub fn name(&self) -> &str {
47 &self.0
48 }
49}
50
51impl std::fmt::Display for EntityName {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 write!(f, "{}", self.0)
54 }
55}
56
57impl_component!(Parent, Children, EntityName, IsHidden, PrefabRequest);
58
59pub trait Bundle {
75 fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity);
77}
78
79pub struct DynamicBundle<B: Bundle, C: Component> {
82 pub bundle: B,
83 pub component: C,
84}
85
86impl<B: Bundle, C: Component> Bundle for DynamicBundle<B, C> {
87 fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity) {
88 self.bundle.apply(world, entity);
89 world.add_component(entity, self.component);
90 }
91}
92
93pub trait BundleExt: Bundle + Sized {
95 fn with<C: Component>(self, component: C) -> DynamicBundle<Self, C> {
97 DynamicBundle {
98 bundle: self,
99 component,
100 }
101 }
102}
103
104impl<T: Bundle> BundleExt for T {}