Skip to main content

gizmo_engine/systems/
transform.rs

1pub struct TransformSyncSystem;
2
3impl gizmo_core::system::System for TransformSyncSystem {
4    fn access_info(&self) -> gizmo_core::system::AccessInfo {
5        let mut info = gizmo_core::system::AccessInfo::new();
6        // Since we borrow components, we can mark it as exclusive to be safe, or specify component access.
7        // For simplicity and safety during hierarchy traversal, we'll make it exclusive.
8        info.is_exclusive = true;
9        info
10    }
11
12    fn run(&mut self, world: &gizmo_core::world::World, _dt: f32) {
13        let transforms = world.borrow_mut::<crate::physics::Transform>();
14        for (_, trans) in transforms.iter_mut() {
15            trans.update_local_matrix();
16        }
17    }
18}
19
20pub struct TransformPropagateSystem;
21
22impl gizmo_core::system::System for TransformPropagateSystem {
23    fn access_info(&self) -> gizmo_core::system::AccessInfo {
24        let mut info = gizmo_core::system::AccessInfo::new();
25        info.is_exclusive = true; // Safe fallback for complex queries
26        info
27    }
28
29    fn run(&mut self, world: &gizmo_core::world::World, _dt: f32) {
30        let locals = world.borrow::<crate::physics::Transform>();
31        let mut globals = world.borrow_mut::<crate::physics::GlobalTransform>();
32        let parents = world.borrow::<gizmo_core::component::Parent>();
33        let children_storage = world.borrow::<gizmo_core::component::Children>();
34
35        let mut queue = Vec::new();
36
37        // 1. Kökleri (Root) işle ve GlobalTransform'larını kendi yerellerine eşitle
38        for (id, local) in locals.iter() {
39            if parents.get(id).is_none() {
40                if let Some(global) = globals.get_mut(id) {
41                    global.matrix = local.local_matrix;
42
43                    // Eğer çocukları varsa kuyruğa ekle
44                    if let Some(children) = children_storage.get(id) {
45                        for &child_id in &children.0 {
46                            queue.push((global.matrix, child_id));
47                        }
48                    }
49                }
50            }
51        }
52
53        // 2. Çocukları hiyerarşik olarak BFS ile işle
54        let mut head = 0;
55        while head < queue.len() {
56            let (parent_matrix, current_id) = queue[head];
57            head += 1;
58
59            if let Some(local) = locals.get(current_id) {
60                if let Some(global) = globals.get_mut(current_id) {
61                    global.matrix = parent_matrix * local.local_matrix;
62
63                    if let Some(children) = children_storage.get(current_id) {
64                        for &child_id in &children.0 {
65                            queue.push((global.matrix, child_id));
66                        }
67                    }
68                }
69            }
70        }
71    }
72}