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/// Geçici silinme etiketi (Undo/Redo ve Çöp kutusu için). Eğer bu component varsa, render, fizik ve picking gibi sistemlerden dışlanır.
36#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
37pub struct IsDeleted;
38
39/// Prefab spawn talebi. Entity'ye eklendiğinde prefab yükleme sistemi tarafından işlenir
40/// ve işlendikten sonra component kaldırılır.
41#[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    /// Prefab adını döndürür.
50    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
63// ============================================================
64//  Bundle Trait — Birden fazla component'i tek seferde ekleme
65// ============================================================
66
67/// Bevy tarzı Bundle desteği.
68/// Bir struct bu trait'i implemente ederse, `world.spawn_bundle(...)` ile
69/// tüm bileşenler tek seferde eklenebilir.
70///
71/// ```ignore
72/// world.spawn_bundle(CameraBundle {
73///     position: Vec3::new(0.0, 3.0, 10.0),
74///     fov: 60.0_f32.to_radians(),
75///     ..default()
76/// });
77/// ```
78pub trait Bundle {
79    /// Bundle içindeki tüm bileşenleri verilen entity'ye ekler.
80    fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity);
81}
82
83/// Herhangi bir Bundle'a çalışma zamanında veya derleme zamanında dinamik olarak
84/// ekstra component eklemeyi sağlayan zincirlenebilir wrapper.
85pub 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
97/// Tüm Bundle tipleri için otomatik `.with(Component)` zincirleme desteği.
98pub trait BundleExt: Bundle + Sized {
99    /// Bu bundle'ın üzerine ek bir bileşen daha ekler.
100    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 {}