gizmo-engine 0.9.1

A custom ECS and physics engine aimed for realistic simulations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Bevy-like Command API — for spawning an object in a single line inside the setup closure.
//!
//! ```no_run
//! use gizmo::prelude::*;
//! # use gizmo::spawner::Commands;
//! App::<()>::new("demo", 1280, 720)
//!     .set_setup(|world, renderer| {
//!         let mut cmd = Commands::new(world, renderer);
//!         cmd.spawn_cube(Vec3::new(0.0, 0.0, -10.0), Color::RED).with_name("Player");
//!         cmd.spawn_camera(Vec3::new(0.0, 2.0, 5.0));
//!     })
//!     .run()
//!     .unwrap();
//! ```
//!
//! `no_run` because `App::run` opens a winit window and creates a wgpu surface: it cannot
//! execute in a doc-test, and would never return if it could.

use crate::color::Color;
use gizmo_core::{Entity, EntityName, World};
use gizmo_math::{Quat, Vec3};
use gizmo_physics_core::{Collider, Transform};
use gizmo_physics_rigid::components::{RigidBody, Velocity};
use gizmo_renderer::{
    asset::AssetManager,
    components::{Camera, DirectionalLight, Material, MeshRenderer, PointLight},
    Renderer,
};

// ─── Hata Tipleri ───────────────────────────────────────────────────────────

/// Errors that can arise during GLTF/GLB spawn operations.
///
/// 1.0 error contract: a concrete, `match`-able type instead of a stringly-typed error.
///
/// `#[non_exhaustive]`: as the loader in the lower layer (`gizmo-renderer`) moves to
/// a concrete error type, new variants (Io, Parse, GpuUpload, …) may be added here;
/// for that reason consumers must keep a `_ =>` arm.
#[derive(Debug)]
#[non_exhaustive]
pub enum GltfLoadError {
    /// The underlying loader (`AssetManager::load_gltf_scene` /
    /// `load_gltf_from_import`) returned an error. `path` is the file path that was
    /// being loaded; `source` is the lower layer's error description.
    ///
    /// Note: because the lower layer still returns `Result<_, String>`, the source
    /// is carried here as a `String`; when the lower layer moves to a concrete
    /// `Error` type this variant will be updated so that it chains with
    /// `#[source]`.
    Load {
        /// The file path that was being loaded.
        path: String,
        /// The error description coming from the lower layer.
        source: String,
    },
}

impl std::fmt::Display for GltfLoadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GltfLoadError::Load { path, source } => {
                write!(f, "GLTF '{}' yuklenemedi: {}", path, source)
            }
        }
    }
}

impl std::error::Error for GltfLoadError {}

// ─── Commands ─────────────────────────────────────────────────────────────────

pub struct Commands<'a> {
    pub world: &'a mut World,
    pub renderer: &'a Renderer,
    pub asset_manager: Option<AssetManager>,
}

impl<'a> Drop for Commands<'a> {
    fn drop(&mut self) {
        if let Some(am) = self.asset_manager.take() {
            self.world.insert_resource(am);
        }
    }
}

impl<'a> Commands<'a> {
    pub fn new(world: &'a mut World, renderer: &'a Renderer) -> Self {
        let am = world.remove_resource::<AssetManager>().unwrap_or_default();
        Self {
            world,
            renderer,
            asset_manager: Some(am),
        }
    }

    // ── Primitifler ────────────────────────────────────────────────────────────

    /// Spawns a colored cube in a single line. `.with_name()` can be added via the builder chain.
    pub fn spawn_cube(&mut self, pos: Vec3, color: Color) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_cube(&self.renderer.device);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg).with_unlit(color.to_vec4());
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Spawns a colored sphere in a single line.
    pub fn spawn_sphere(&mut self, pos: Vec3, radius: f32, color: Color) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_sphere(&self.renderer.device, radius, 20, 20);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg).with_unlit(color.to_vec4());
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Spawns a planar ground in a single line.
    pub fn spawn_plane(&mut self, pos: Vec3, size: f32, color: Color) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_plane(&self.renderer.device, size);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg).with_unlit(color.to_vec4());
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Loads an .obj model from disk and spawns it.
    pub fn spawn_model(&mut self, pos: Vec3, path: &str) -> EntityBuilder<'_, 'a> {
        tracing::debug!(path, ?pos, "spawn_model: diskten .obj model yükleniyor");
        let mesh = self
            .asset_manager
            .as_mut()
            .unwrap()
            .load_obj(&self.renderer.device, path);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg);
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    // ── Kamera ────────────────────────────────────────────────────────────────

    /// Spawns the primary 3D perspective camera.
    /// `yaw = -π/2` (looking towards −X), `pitch = 0` (level).
    pub fn spawn_camera(&mut self, pos: Vec3) -> EntityBuilder<'_, 'a> {
        if let Some(mut cameras) = self.world.query_mut::<gizmo_core::prelude::Mut<Camera>>() {
            for (_, mut c) in cameras.iter_mut() {
                c.primary = false;
            }
        }
        let id = self.world.spawn();
        let trans = Transform::new(pos);

        self.world.add_component(id, trans);
        self.world.add_component(
            id,
            Camera {
                fov: 60.0_f32.to_radians(),
                near: 0.1,
                far: 1000.0,
                yaw: -std::f32::consts::FRAC_PI_2,
                pitch: 0.0,
                exposure: 1.0,
                primary: true,
                projection: Default::default(),
            },
        );
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Camera with customizable `fov` (degrees), `near`, `far`.
    pub fn spawn_camera_with(
        &mut self,
        pos: Vec3,
        fov_deg: f32,
        near: f32,
        far: f32,
    ) -> EntityBuilder<'_, 'a> {
        if let Some(mut cameras) = self.world.query_mut::<gizmo_core::prelude::Mut<Camera>>() {
            for (_, mut c) in cameras.iter_mut() {
                c.primary = false;
            }
        }
        let id = self.world.spawn();
        let trans = Transform::new(pos);

        self.world.add_component(id, trans);
        self.world.add_component(
            id,
            Camera {
                fov: fov_deg.to_radians(),
                near,
                far,
                yaw: -std::f32::consts::FRAC_PI_2,
                pitch: 0.0,
                exposure: 1.0,
                primary: true,
                projection: Default::default(),
            },
        );
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    // ── Işıklar ─────────────────────────────────────────────────────────────────────────

    /// Spawns a point light.
    pub fn spawn_point_light(
        &mut self,
        pos: Vec3,
        color: Color,
        intensity: f32,
    ) -> EntityBuilder<'_, 'a> {
        let id = self.world.spawn();
        let trans = Transform::new(pos);

        self.world.add_component(id, trans);
        self.world.add_component(
            id,
            PointLight::new(
                gizmo_math::Vec3::new(color.0.x, color.0.y, color.0.z),
                intensity,
                10.0,
            ),
        );
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Spawns a directional light (sun/moon).
    /// `direction`: the normalized light direction (pointing downwards = negative Y).
    pub fn spawn_sun(
        &mut self,
        _direction: Vec3,
        color: Color,
        intensity: f32,
    ) -> EntityBuilder<'_, 'a> {
        let id = self.world.spawn();
        let pos = Vec3::ZERO; // DirectionalLight position is largely irrelevant
        let trans = Transform::new(pos);

        self.world.add_component(id, trans);
        self.world.add_component(
            id,
            DirectionalLight {
                color: Vec3::new(color.0.x, color.0.y, color.0.z),
                intensity,
                role: crate::renderer::components::LightRole::Sun,
            },
        );
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    // ── Sahne Yardımcıları ────────────────────────────────────────────────────────────

    /// Spawns a skybox (a very large cube with inverted faces). The color determines the
    /// background color.
    pub fn spawn_skybox(&mut self, color: Color) -> EntityBuilder<'_, 'a> {
        // Skip existing check since is_skybox is removed

        // Wait, best approach for skybox is ignoring the duplication request if exists, but we must return an EntityBuilder...
        let mesh = AssetManager::create_inverted_cube(&self.renderer.device);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg).with_unlit(color.to_vec4()).with_skybox();
        let id = self.world.spawn();
        let mut trans = Transform::new(Vec3::ZERO);
        trans.scale = Vec3::new(500.0, 500.0, 500.0);
        trans.update_local_matrix();

        self.world.add_component(id, trans);
        self.world.add_component(id, mesh);
        self.world.add_component(id, mat);
        self.world.add_component(id, MeshRenderer::new());
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    // ── Fizik Spawn ─────────────────────────────────────────────────────────────────────────

    /// Spawns a dynamic cube that participates in the physics simulation.
    /// `half_extents`: Half size on each axis. `mass`: mass in kg (0 = static).
    pub fn spawn_rigid_cube(
        &mut self,
        pos: Vec3,
        half_extents: Vec3,
        color: Color,
        mass: f32,
    ) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_cube(&self.renderer.device);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg).with_unlit(color.to_vec4());
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        // Scale'i half_extents ile eşleştir
        {
            let mut trans_store = self.world.borrow_mut::<Transform>();
            if let Some(mut trans) = trans_store.get_mut(id.id()) {
                trans.scale = half_extents * 2.0;
                trans.update_local_matrix();
            }
        }
        let mut rb = if mass > 0.0 {
            RigidBody::new(mass, true)
        } else {
            RigidBody::new_static()
        };
        let col = Collider::box_collider(half_extents);
        rb.update_inertia_from_collider(&col);
        self.world.add_component(id, rb);
        if mass > 0.0 {
            self.world.add_component(id, Velocity::new(Vec3::ZERO));
        }
        self.world.add_component(id, col);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Spawns a dynamic sphere that participates in the physics simulation.
    pub fn spawn_rigid_sphere(
        &mut self,
        pos: Vec3,
        radius: f32,
        color: Color,
        mass: f32,
    ) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_sphere(&self.renderer.device, radius, 16, 16);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg).with_unlit(color.to_vec4());
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        let mut rb = if mass > 0.0 {
            RigidBody::new(mass, true)
        } else {
            RigidBody::new_static()
        };
        let col = Collider::sphere(radius);
        rb.update_inertia_from_collider(&col);
        self.world.add_component(id, rb);
        if mass > 0.0 {
            self.world.add_component(id, Velocity::new(Vec3::ZERO));
        }
        self.world.add_component(id, col);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Spawns a static (non-moving) ground plane.
    pub fn spawn_static_plane(
        &mut self,
        pos: Vec3,
        size: f32,
        color: Color,
    ) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_plane(&self.renderer.device, size);
        let bg = self.asset_manager.as_mut().unwrap().create_white_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
        );
        let mat = Material::new(bg).with_pbr(color.to_vec4(), 0.9, 0.0);
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        self.world.add_component(id, RigidBody::new_static());
        self.world.add_component(
            id,
            Collider::box_collider(Vec3::new(size / 2.0, 0.05, size / 2.0)),
        );
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    // ── Görsel Yardımcılar ──────────────────────────────────────────────────────────────────────

    /// Loads a textured material and applies it to a cube.
    pub fn spawn_textured_cube(&mut self, pos: Vec3, texture_path: &str) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_cube(&self.renderer.device);
        let bg = match self.asset_manager.as_mut().unwrap().load_material_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
            texture_path,
        ) {
            Ok(bg) => bg,
            Err(e) => {
                // Sessiz `.unwrap_or_else(|_| ...)` yutması yerine: hatayı BAĞLAMLA logla,
                // sonra görsel-bozulmayı önlemek için beyaz dokuya düş.
                tracing::warn!(
                    path = texture_path,
                    error = %e,
                    "spawn_textured_cube: doku yüklenemedi, beyaz dokuya düşülüyor"
                );
                self.asset_manager.as_mut().unwrap().create_white_texture(
                    &self.renderer.device,
                    &self.renderer.queue,
                    &self.renderer.scene.texture_bind_group_layout,
                )
            }
        };
        let mat = Material::new(bg);
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

    /// Loads a textured material and applies it to a plane.
    pub fn spawn_textured_plane(
        &mut self,
        pos: Vec3,
        size: f32,
        texture_path: &str,
    ) -> EntityBuilder<'_, 'a> {
        let mesh = AssetManager::create_plane(&self.renderer.device, size);
        let bg = match self.asset_manager.as_mut().unwrap().load_material_texture(
            &self.renderer.device,
            &self.renderer.queue,
            &self.renderer.scene.texture_bind_group_layout,
            texture_path,
        ) {
            Ok(bg) => bg,
            Err(e) => {
                // Sessiz `.unwrap_or_else(|_| ...)` yutması yerine: hatayı BAĞLAMLA logla,
                // sonra görsel-bozulmayı önlemek için beyaz dokuya düş.
                tracing::warn!(
                    path = texture_path,
                    error = %e,
                    "spawn_textured_plane: doku yüklenemedi, beyaz dokuya düşülüyor"
                );
                self.asset_manager.as_mut().unwrap().create_white_texture(
                    &self.renderer.device,
                    &self.renderer.queue,
                    &self.renderer.scene.texture_bind_group_layout,
                )
            }
        };
        let mat = Material::new(bg);
        let id = spawn_mesh_entity(self.world, pos, mesh, mat);
        EntityBuilder {
            commands: self,
            entity: id,
        }
    }

}

// ─── EntityBuilder — Zincir API ───────────────────────────────────────────────

/// Chain builder for adding extra components to a spawned entity.
pub struct EntityBuilder<'b, 'a> {
    commands: &'b mut Commands<'a>,
    entity: Entity,
}

impl<'b, 'a> EntityBuilder<'b, 'a> {
    /// Assign a name (tag) to the entity. It can be found inside update with
    /// `world.entity_named("...")`.
    pub fn with_name(self, name: &str) -> Self {
        self.commands
            .world
            .add_component(self.entity, EntityName(name.to_string()));
        self
    }

    /// Add any extra component.
    pub fn with<C: gizmo_core::Component + 'static>(self, component: C) -> Self {
        self.commands.world.add_component(self.entity, component);
        self
    }

    /// Consume and return the Entity ID.
    pub fn id(self) -> Entity {
        self.entity
    }
}

impl<'b, 'a> From<EntityBuilder<'b, 'a>> for Entity {
    fn from(b: EntityBuilder<'b, 'a>) -> Entity {
        b.entity
    }
}

// ─── Yardımcı: Mesh entity oluştur ────────────────────────────────────────────────────────────────

pub(super) fn spawn_mesh_entity(
    world: &mut World,
    pos: Vec3,
    mesh: gizmo_renderer::components::Mesh,
    mat: Material,
) -> Entity {
    let id = world.spawn();
    let mut trans = Transform::new(pos);
    trans.update_local_matrix();

    world.add_component(id, trans);
    world.add_component(id, gizmo_physics_core::components::GlobalTransform::default());
    let source = mesh.source.clone();
    world.add_component(id, mesh);
    world.add_component(id, mat);
    world.add_component(id, MeshRenderer::new());
    // Tüm primitif/rigid/textured/model mesh spawn'ları buradan geçer → tek noktada,
    // sıcak-yol-güvenli trace! (kapalıyken bedelsiz) ile spawn edilen mesh + konum kaydı.
    tracing::trace!(entity = id.id(), source = %source, ?pos, "mesh entity spawn'landı");
    id
}

// ─── WorldExt Trait — Update içinde kısa sorgular ─────────────────────────────

/// Convenience methods added on top of World.
/// Brought in automatically with `use gizmo::prelude::*;`.
pub trait WorldExt {
    /// Find the Entity ID (u32) by name.
    fn entity_named(&self, name: &str) -> Option<u32>;

    /// Modify an entity's Transform by name. The Transform matrix is updated automatically.
    fn move_entity_named<F: FnMut(&mut gizmo_physics_core::Transform)>(&mut self, name: &str, f: F);

    /// Get an entity's world position by name.
    fn position_of(&self, name: &str) -> Option<Vec3>;

    /// Modify any component by name.
    ///
    /// # Example
    /// ```
    /// use gizmo::prelude::*;
    /// # #[derive(Clone)] struct Health(u32);
    /// # gizmo::core::impl_component!(Health);
    /// # let mut world = World::new();
    /// # let e = world.spawn();
    /// # world.add_component(e, EntityName("Player".to_string()));
    /// # world.add_component(e, Health(100));
    /// // The second type parameter is the closure's own, so it can only be `_`.
    /// world.modify::<Health, _>("Player", |h| h.0 -= 30);
    /// assert_eq!(world.borrow::<Health>().get(e.id()).unwrap().0, 70);
    ///
    /// // An unknown name is a silent no-op, not a panic.
    /// world.modify::<Health, _>("Nobody", |h| h.0 = 0);
    /// assert_eq!(world.borrow::<Health>().get(e.id()).unwrap().0, 70);
    /// ```
    fn modify<T: gizmo_core::Component + 'static, F: FnMut(&mut T)>(&mut self, name: &str, f: F);
}

impl WorldExt for World {
    fn entity_named(&self, name: &str) -> Option<u32> {
        let mut names = self.query::<&EntityName>()?;
        for (id, n) in names.iter_mut() {
            if n.0 == name {
                return Some(id);
            }
        }
        None
    }

    fn move_entity_named<F: FnMut(&mut gizmo_physics_core::Transform)>(&mut self, name: &str, mut f: F) {
        let target: Option<u32> = {
            if let Some(mut names) = self.query::<&EntityName>() {
                let mut found = None;
                for (id, n) in names.iter_mut() {
                    if n.0 == name {
                        found = Some(id);
                        break;
                    }
                }
                found
            } else {
                None
            }
        };
        if let Some(target_id) = target {
            if let Some(mut transforms) =
                self.query_mut::<gizmo_core::prelude::Mut<gizmo_physics_core::Transform>>()
            {
                for (tid, mut trans) in transforms.iter_mut() {
                    if tid == target_id {
                        f(&mut trans);
                        trans.update_local_matrix();
                    }
                }
            }
        }
    }

    fn position_of(&self, name: &str) -> Option<Vec3> {
        let target_id = self.entity_named(name)?;
        let transforms = self.borrow::<gizmo_physics_core::Transform>();
        transforms.get(target_id).map(|t| t.position)
    }

    fn modify<T: gizmo_core::Component + 'static, F: FnMut(&mut T)>(
        &mut self,
        name: &str,
        mut f: F,
    ) {
        let target: Option<u32> = {
            if let Some(mut names) = self.query::<&EntityName>() {
                let mut found = None;
                for (id, n) in names.iter_mut() {
                    if n.0 == name {
                        found = Some(id);
                        break;
                    }
                }
                found
            } else {
                None
            }
        };
        if let Some(target_id) = target {
            {
                let mut storage = self.borrow_mut::<T>();
                if let Some(mut comp) = storage.get_mut(target_id) {
                    f(&mut *comp);
                }
            }
        }
    }
}

// ─── InputExt Trait — KeyCode doğrudan kabul eden kısaltmalar ─────────────────
// gizmo-core'da winit bağımlılığı olmadığı için bu trait gizmo crate'inde tanımlıdır.

/// Ergonomic methods added on top of `Input`.
/// Brought in automatically with `use gizmo::prelude::*;`.
///
/// # Example
/// ```
/// use gizmo::prelude::*;
/// # use gizmo::core::input::Input;
/// # use winit::keyboard::KeyCode as Key;
/// # let mut input = Input::new();
/// # input.on_key_pressed(Key::KeyW as u32);
/// # input.on_key_pressed(Key::Space as u32);
/// let mut z = 0.0_f32;
/// let dt = 1.0 / 60.0;
/// if input.pressed(Key::KeyW) { z -= 5.0 * dt; }
/// let jumped = input.just_pressed(Key::Space);
///
/// assert!(z < 0.0 && jumped);
/// // `just_pressed` is edge-triggered: it is false on the next frame, `pressed` is not.
/// # input.begin_frame();
/// assert!(input.pressed(Key::KeyW) && !input.just_pressed(Key::Space));
/// ```
pub trait InputExt {
    /// Is the key held down? Takes `KeyCode` variants like `Key::KeyW`, `Key::Space` directly.
    fn pressed(&self, keycode: winit::keyboard::KeyCode) -> bool;

    /// Was the key pressed for the first time this frame? (one-shot trigger)
    fn just_pressed(&self, keycode: winit::keyboard::KeyCode) -> bool;

    /// Was the key released this frame?
    fn just_released(&self, keycode: winit::keyboard::KeyCode) -> bool;
}

impl InputExt for gizmo_core::input::Input {
    #[inline]
    fn pressed(&self, keycode: winit::keyboard::KeyCode) -> bool {
        self.is_key_pressed(keycode as u32)
    }
    #[inline]
    fn just_pressed(&self, keycode: winit::keyboard::KeyCode) -> bool {
        self.is_key_just_pressed(keycode as u32)
    }
    #[inline]
    fn just_released(&self, keycode: winit::keyboard::KeyCode) -> bool {
        self.is_key_just_released(keycode as u32)
    }
}

// glTF/GLB spawning (the heaviest, most distinct concern) lives in `gltf`; it re-composes onto
// `Commands` as inherent methods. Public paths (spawner::Commands/WorldExt/…) are unchanged.
mod gltf;