Skip to main content

gizmo_core/
component.rs

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// --- Hiyerarşi (Scene Graph) Bileşenleri ---
15#[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/// Entity isim bileşeni — Editor, Lua ve Scene Serialization tarafından kullanılır.
22#[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/// Görünmezlik etiketi: Eğer bu component bir objede varsa render edilmez (veya aktifliği kapatılır).
32#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
33pub struct IsHidden;
34
35/// Prefab spawn talebi. Entity'ye eklendiğinde prefab yükleme sistemi tarafından işlenir
36/// ve işlendikten sonra component kaldırılır.
37#[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    /// Prefab adını döndürür.
46    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
59// ============================================================
60//  Bundle Trait — Birden fazla component'i tek seferde ekleme
61// ============================================================
62
63/// Bevy tarzı Bundle desteği.
64/// Bir struct bu trait'i implemente ederse, `world.spawn_bundle(...)` ile
65/// tüm bileşenler tek seferde eklenebilir.
66///
67/// ```ignore
68/// world.spawn_bundle(CameraBundle {
69///     position: Vec3::new(0.0, 3.0, 10.0),
70///     fov: 60.0_f32.to_radians(),
71///     ..default()
72/// });
73/// ```
74pub trait Bundle {
75    /// Bundle içindeki tüm bileşenleri verilen entity'ye ekler.
76    fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity);
77}
78
79/// Herhangi bir Bundle'a çalışma zamanında veya derleme zamanında dinamik olarak
80/// ekstra component eklemeyi sağlayan zincirlenebilir wrapper.
81pub 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
93/// Tüm Bundle tipleri için otomatik `.with(Component)` zincirleme desteği.
94pub trait BundleExt: Bundle + Sized {
95    /// Bu bundle'ın üzerine ek bir bileşen daha ekler.
96    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 {}