pybevy 0.2.1

PyBevy: A Python Real-Time Engine Built on Bevy
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
use bevy::ecs::entity::Entity;
use pyo3::{
    exceptions::{PyTypeError, PyValueError},
    prelude::*,
    types::PyTuple,
};

use super::{PyChildOf, PyEntity, commands::PyCommands, helpers::validity_guard::ValidityFlag};
use crate::ecs::observer_registry::ObserverRegistry;

/// Represents a handle to perform deferred operations on an entity.
/// Operations are queued and applied later when the Commands are flushed.
#[pyclass(name = "EntityCommands")]
#[derive(Debug, Clone)]
pub struct PyEntityCommands {
    pub(crate) id: Entity,
    // Store commands pointer - only valid during command queue operations
    // This will be None for simple entity ID returns
    commands_ptr: Option<usize>,
    // Store world pointer - used when spawned from World directly
    // This allows immediate operations like observe() to work
    world_ptr: Option<usize>,
    // Runtime validity check - prevents use after system execution
    // This is cloned from the parent PyCommands/PyWorld
    validity: Option<ValidityFlag>,
}

// SAFETY: PyEntityCommands is Send because:
// - Entity is Copy + Send
// - The raw pointer is stored as usize (just an address)
// - Access through get_commands() requires the PyCommands instance to still be valid
unsafe impl Send for PyEntityCommands {}

// SAFETY: PyEntityCommands is Sync because:
// - All fields are either Copy or contain addresses
// - Actual access to commands is controlled by the PyCommands validity checking
unsafe impl Sync for PyEntityCommands {}

impl PyEntityCommands {
    pub(crate) fn with_commands(entity: Entity, commands: &PyCommands) -> Self {
        Self {
            id: entity,
            commands_ptr: Some(commands as *const PyCommands as usize),
            world_ptr: None,
            validity: Some(commands.validity()),
        }
    }

    pub(crate) fn with_world(entity: Entity, world: &super::world::PyWorld) -> Self {
        Self {
            id: entity,
            commands_ptr: None,
            world_ptr: Some(world as *const super::world::PyWorld as usize),
            validity: world.validity(),
        }
    }

    /// Check if this EntityCommands instance is still valid for use
    fn check_valid(&self) -> PyResult<()> {
        if let Some(ref validity) = self.validity {
            Ok(validity.check()?)
        } else {
            Ok(()) // No validity tracking (e.g., simple entity ID returns or owned worlds)
        }
    }

    fn get_commands(&self) -> PyResult<Option<&PyCommands>> {
        self.check_valid()?;
        Ok(self
            .commands_ptr
            .map(|ptr| unsafe { &*(ptr as *const PyCommands) }))
    }

    fn get_world(&self) -> PyResult<Option<&super::world::PyWorld>> {
        self.check_valid()?;
        Ok(self
            .world_ptr
            .map(|ptr| unsafe { &*(ptr as *const super::world::PyWorld) }))
    }

    /// Create temporary PyCommands from the world pointer for entity operations.
    /// Returns None if no world pointer is available.
    fn temp_commands_from_world(&self) -> PyResult<Option<PyCommands>> {
        if let Some(world) = self.get_world()? {
            let world_ptr = world.world_ptr();
            let validity = world.validity().unwrap_or_else(ValidityFlag::new);
            // SAFETY: We're creating a temporary PyCommands that wraps the World pointer.
            // The world pointer is valid because we just checked validity via get_world().
            let temp_commands = unsafe { PyCommands::from_world_temporary(world_ptr, validity) };
            Ok(Some(temp_commands))
        } else {
            Ok(None)
        }
    }

    /// Get a PyCommands reference, either from stored commands or by creating
    /// temporary commands from the world pointer. Returns the commands and
    /// whether they are temporary (and thus must not be referenced after this call).
    fn get_commands_or_world(&self) -> PyResult<Option<CommandsSource<'_>>> {
        if let Some(commands) = self.get_commands()? {
            Ok(Some(CommandsSource::Commands(commands)))
        } else if let Some(temp) = self.temp_commands_from_world()? {
            Ok(Some(CommandsSource::TempFromWorld(temp)))
        } else {
            Ok(None)
        }
    }
}

/// Either a borrowed reference to stored PyCommands or a temporary one created from World.
enum CommandsSource<'a> {
    Commands(&'a PyCommands),
    TempFromWorld(PyCommands),
}

impl<'a> CommandsSource<'a> {
    fn as_ref(&self) -> &PyCommands {
        match self {
            CommandsSource::Commands(c) => c,
            CommandsSource::TempFromWorld(c) => c,
        }
    }
}

#[pymethods]
impl PyEntityCommands {
    /// Get the entity ID
    pub fn id(&self) -> PyEntity {
        PyEntity(self.id)
    }

    /// Insert components into this entity
    #[pyo3(signature = (*components))]
    pub fn insert(
        &self,
        py: Python,
        components: &Bound<'_, PyTuple>,
    ) -> PyResult<PyEntityCommands> {
        if let Some(source) = self.get_commands_or_world()? {
            crate::ecs::commands::insert_components_to_entity_helper(
                source.as_ref(),
                py,
                self.id,
                components,
            )?;
            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot insert components: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Remove components from this entity
    #[pyo3(signature = (*components))]
    pub fn remove(
        &self,
        py: Python,
        components: &Bound<'_, PyTuple>,
    ) -> PyResult<PyEntityCommands> {
        if let Some(source) = self.get_commands_or_world()? {
            crate::ecs::commands::remove_components_from_entity_helper(
                source.as_ref(),
                py,
                self.id,
                components,
            )?;
            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot remove components: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Despawn this entity
    pub fn despawn(&self) -> PyResult<()> {
        if let Some(source) = self.get_commands_or_world()? {
            source.as_ref().despawn(&PyEntity(self.id))
        } else {
            Err(PyValueError::new_err(
                "Cannot despawn: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Add a child entity to this entity
    pub fn add_child(&self, child: &PyEntity) -> PyResult<PyEntityCommands> {
        if let Some(source) = self.get_commands_or_world()? {
            crate::ecs::commands::add_child_helper(source.as_ref(), self.id, child.0)?;
            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot add child: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Set the parent of this entity
    pub fn set_parent(&self, parent: &PyEntity) -> PyResult<PyEntityCommands> {
        if let Some(source) = self.get_commands_or_world()? {
            crate::ecs::commands::set_parent_helper(source.as_ref(), self.id, parent.0)?;
            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot set parent: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Remove the parent relationship from this entity
    pub fn remove_parent(&self) -> PyResult<PyEntityCommands> {
        if let Some(source) = self.get_commands_or_world()? {
            crate::ecs::commands::remove_parent_helper(source.as_ref(), self.id)?;
            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot remove parent: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Remove specific children from this entity
    #[pyo3(signature = (*children))]
    pub fn remove_children(
        &self,
        children: &Bound<'_, pyo3::types::PyTuple>,
    ) -> PyResult<PyEntityCommands> {
        if let Some(source) = self.get_commands_or_world()? {
            let child_ids: Vec<Entity> = children
                .iter()
                .map(|item| {
                    item.extract::<PyEntity>()
                        .map(|e| e.0)
                        .map_err(|_| PyTypeError::new_err("Expected Entity objects"))
                })
                .collect::<PyResult<Vec<_>>>()?;

            crate::ecs::commands::remove_children_helper(source.as_ref(), self.id, &child_ids)?;
            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot remove children: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Remove all children from this entity
    pub fn clear_children(&self) -> PyResult<PyEntityCommands> {
        if let Some(source) = self.get_commands_or_world()? {
            crate::ecs::commands::clear_children_helper(source.as_ref(), self.id)?;
            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot clear children: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Spawn children entities using a callback function
    pub fn with_children(&self, py: Python, func: Bound<'_, PyAny>) -> PyResult<PyEntityCommands> {
        let ty = func.get_type();

        if !ty.is_callable() {
            return Err(PyValueError::new_err("Parameter must be callable"));
        }

        if let Some(source) = self.get_commands_or_world()? {
            let related_spawner = Py::new(
                py,
                PyRelatedSpawnerCommands::with_commands(self.id, source.as_ref()),
            )?;

            func.call1((related_spawner,))?;

            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "Cannot spawn children: EntityCommands not associated with a Commands or World object.",
            ))
        }
    }

    /// Register an observer for this specific entity
    ///
    /// The observer will only trigger when events target this entity.
    ///
    /// # Example
    /// ```python
    /// def on_damage(trigger: On[TakeDamage]) -> None:
    ///     print(f"Entity {trigger.entity()} took damage")
    ///
    /// commands.spawn(Player()).observe(on_damage)
    /// ```
    pub fn observe(&self, py: Python, observer: Bound<'_, PyAny>) -> PyResult<PyEntityCommands> {
        // Try to get world access from either Commands or World
        let world_mut = if let Some(commands) = self.get_commands()? {
            // Via Commands (immediate mode only)
            commands.try_world_mut()?
        } else if let Some(world) = self.get_world()? {
            // Via World (direct access)
            Some(world.world_mut()?)
        } else {
            None
        };

        if let Some(world) = world_mut {
            // Immediate registration - we have World access
            let _observer_entity =
                ObserverRegistry::register_observer_for_entity(py, &observer, self.id, world)?;
            Ok(self.clone())
        } else if let Some(commands) = self.get_commands()? {
            // Deferred registration - queue a command
            let entity_id = self.id;
            let observer_py: Py<PyAny> = observer.unbind();

            commands.execute_or_queue(move |world| {
                Python::attach(|py| {
                    let observer_bound = observer_py.bind(py);
                    if let Err(e) = ObserverRegistry::register_observer_for_entity(
                        py,
                        observer_bound,
                        entity_id,
                        world,
                    ) {
                        eprintln!(
                            "Error: Failed to register observer via deferred command: {:?}",
                            e
                        );
                    }
                });
            })?;

            Ok(self.clone())
        } else {
            Err(PyValueError::new_err(
                "EntityCommands.observe() requires either World or Commands access.",
            ))
        }
    }
}

/// Helper for spawning entities that are related to a target entity (e.g., children)
#[pyclass(name = "RelatedSpawnerCommands")]
pub struct PyRelatedSpawnerCommands {
    target: Entity,
    commands_ptr: usize,
    // Runtime validity check - prevents use after system execution
    validity: ValidityFlag,
}

// SAFETY: PyRelatedSpawnerCommands is Send because:
// - Entity is Copy + Send
// - The raw pointer is stored as usize (just an address)
// - ValidityFlag is Arc<AtomicBool> which is Send + Sync
// - Access through get_commands() requires validity check
unsafe impl Send for PyRelatedSpawnerCommands {}

// SAFETY: PyRelatedSpawnerCommands is Sync because:
// - All fields are either Copy or thread-safe (ValidityFlag)
// - Actual access to commands is controlled by validity checking
unsafe impl Sync for PyRelatedSpawnerCommands {}

impl PyRelatedSpawnerCommands {
    fn with_commands(target: Entity, commands: &PyCommands) -> Self {
        Self {
            target,
            commands_ptr: commands as *const PyCommands as usize,
            validity: commands.validity(),
        }
    }

    fn get_commands(&self) -> PyResult<&PyCommands> {
        self.validity.check()?;
        Ok(unsafe { &*(self.commands_ptr as *const PyCommands) })
    }

    /// Create a ChildOf component for the target entity
    fn create_child_of_component(py: Python, target: Entity) -> PyResult<Py<PyAny>> {
        let child_of = Py::new(py, PyChildOf::new(PyEntity(target)))?;
        Ok(child_of.into_any())
    }
}

#[pymethods]
impl PyRelatedSpawnerCommands {
    #[new]
    pub fn new(py: Python, commands: Py<PyCommands>, target: PyEntity) -> PyResult<Self> {
        let commands_ref = commands.bind(py).borrow();
        let commands_ptr = &*commands_ref as *const PyCommands as usize;
        let validity = commands_ref.validity();
        Ok(Self {
            target: target.0,
            commands_ptr,
            validity,
        })
    }

    /// Spawn an empty entity as a child
    pub fn spawn_empty(&self, py: Python) -> PyResult<PyEntityCommands> {
        if self.commands_ptr == 0 {
            return Err(PyValueError::new_err(
                "RelatedSpawnerCommands not properly initialized",
            ));
        }

        let commands = self.get_commands()?;
        let mut entity_cmd = commands.spawn_empty(py)?;

        // Insert ChildOf component to establish parent-child relationship
        let child_of = Self::create_child_of_component(py, self.target)?;
        let child_of_tuple = PyTuple::new(py, vec![child_of])?;
        entity_cmd.insert(py, &child_of_tuple)?;

        // Update with commands pointer and validity
        entity_cmd.commands_ptr = Some(self.commands_ptr);
        entity_cmd.world_ptr = None;
        entity_cmd.validity = Some(self.validity.clone());

        Ok(entity_cmd)
    }

    /// Spawn an entity with components as a child
    #[pyo3(signature = (*components))]
    pub fn spawn(&self, py: Python, components: &Bound<'_, PyTuple>) -> PyResult<PyEntityCommands> {
        if self.commands_ptr == 0 {
            return Err(PyValueError::new_err(
                "RelatedSpawnerCommands not properly initialized",
            ));
        }

        let commands = self.get_commands()?;
        let mut entity_cmd = commands.spawn(py, components)?;

        // Insert ChildOf component to establish parent-child relationship
        let child_of = Self::create_child_of_component(py, self.target)?;
        let child_of_tuple = PyTuple::new(py, vec![child_of])?;
        entity_cmd.insert(py, &child_of_tuple)?;

        // Update with commands pointer and validity
        entity_cmd.commands_ptr = Some(self.commands_ptr);
        entity_cmd.world_ptr = None;
        entity_cmd.validity = Some(self.validity.clone());

        Ok(entity_cmd)
    }

    /// Get the target entity ID
    pub fn target_entity(&self) -> PyEntity {
        PyEntity(self.target)
    }
}