use std::any::Any;
pub trait Component: 'static + Any + Send + Sync + Clone {}
#[macro_export]
macro_rules! impl_component {
($($t:ty),+ $(,)?) => {
$(
impl $crate::Component for $t {}
)+
};
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Parent(pub u32);
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Children(pub Vec<u32>);
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct EntityName(pub String);
impl EntityName {
pub fn new(name: &str) -> Self {
Self(name.to_string())
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct IsHidden;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct IsDeleted;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct PrefabRequest(pub String);
impl PrefabRequest {
pub fn new(name: &str) -> Self {
Self(name.to_string())
}
pub fn name(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for EntityName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct MeshSource(pub String);
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct MaterialSource {
pub albedo: [f32; 4],
pub roughness: f32,
pub metallic: f32,
pub unlit: f32,
pub texture_source: Option<String>,
}
impl_component!(Parent, Children, EntityName, IsHidden, PrefabRequest, IsDeleted, MeshSource, MaterialSource);
pub trait Bundle {
fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity);
}
pub struct DynamicBundle<B: Bundle, C: Component> {
pub bundle: B,
pub component: C,
}
impl<B: Bundle, C: Component> Bundle for DynamicBundle<B, C> {
fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity) {
self.bundle.apply(world, entity);
world.add_component(entity, self.component);
}
}
pub trait BundleExt: Bundle + Sized {
fn with<C: Component>(self, component: C) -> DynamicBundle<Self, C> {
DynamicBundle {
bundle: self,
component,
}
}
}
impl<T: Bundle> BundleExt for T {}