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
61impl_component!(Parent, Children, EntityName, IsHidden, PrefabRequest, IsDeleted);
62
63pub trait Bundle {
79 fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity);
81}
82
83pub struct DynamicBundle<B: Bundle, C: Component> {
86 pub bundle: B,
87 pub component: C,
88}
89
90impl<B: Bundle, C: Component> Bundle for DynamicBundle<B, C> {
91 fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity) {
92 self.bundle.apply(world, entity);
93 world.add_component(entity, self.component);
94 }
95}
96
97pub trait BundleExt: Bundle + Sized {
99 fn with<C: Component>(self, component: C) -> DynamicBundle<Self, C> {
101 DynamicBundle {
102 bundle: self,
103 component,
104 }
105 }
106}
107
108impl<T: Bundle> BundleExt for T {}