Skip to main content

euv_engine/entity/
trait.rs

1use super::*;
2
3/// The common lifecycle stage shared by `Component` and `Scene`.
4///
5/// Both abstractions have a per-tick update callback (`on_update`), and both
6/// share the same `f64` delta-time argument. Lifting that single method into
7/// a supertrait lets generic code iterate over mixed collections (e.g. a
8/// registry of components and scenes) and call `on_update` uniformly.
9///
10/// `Component` adds `on_start`, `on_render` with a `Transform2D`, `on_destroy`,
11/// and `name`. `Scene` adds `on_enter`, `on_render` without a transform,
12/// `on_exit`, and `name`. `Lifecycle` intentionally exposes only the shared
13/// surface — the domain-specific entry/exit and render hooks stay on the
14/// sub-traits where their semantics differ.
15pub trait Lifecycle {
16    /// Advances the object's internal state by the given delta time.
17    ///
18    /// # Arguments
19    ///
20    /// - `f64` - The delta time in seconds since the last update.
21    fn on_update(&mut self, delta_time: f64);
22}
23
24/// The base trait for all entity components.
25///
26/// Components encapsulate behavior that can be attached to an `Entity`.
27/// The engine calls lifecycle methods at appropriate times during the scheduler loop.
28pub trait Component: Lifecycle {
29    /// Called once when the component is first added to an active entity
30    /// in a scene.
31    fn on_start(&mut self);
32
33    /// Called every render frame to record the component's draw commands.
34    ///
35    /// # Arguments
36    ///
37    /// - `&mut DrawList` - The draw list to record commands into.
38    /// - `&Transform2D` - The world-space transform of the owning entity.
39    fn on_render(&self, draw_list: &mut DrawList, transform: &Transform2D);
40
41    /// Called once when the component is being removed or the entity is destroyed.
42    fn on_destroy(&mut self);
43
44    /// Returns the name of this component type for debugging and identification.
45    ///
46    /// # Returns
47    ///
48    /// - `&str` - The component name.
49    fn name(&self) -> &str;
50}