Skip to main content

Component

Trait Component 

Source
pub trait Component {
    // Required methods
    fn on_start(&mut self);
    fn on_update(&mut self, delta_time: f64);
    fn on_render(
        &self,
        context: &CanvasRenderingContext2d,
        transform: &Transform2D,
    );
    fn on_destroy(&mut self);
    fn name(&self) -> &str;
}
Expand description

The base trait for all entity components.

Components encapsulate behavior that can be attached to an Entity. The engine calls lifecycle methods at appropriate times during the scheduler loop.

Required Methods§

Source

fn on_start(&mut self)

Called once when the component is first added to an active entity in a scene.

Source

fn on_update(&mut self, delta_time: f64)

Called every fixed timestep with the elapsed time since the last update.

§Arguments
  • f64 - The delta time in seconds.
Source

fn on_render(&self, context: &CanvasRenderingContext2d, transform: &Transform2D)

Called every render frame to draw the component onto the canvas.

§Arguments
  • &CanvasRenderingContext2d - The canvas 2D rendering context.
  • &Transform2D - The world-space transform of the owning entity.
Source

fn on_destroy(&mut self)

Called once when the component is being removed or the entity is destroyed.

Source

fn name(&self) -> &str

Returns the name of this component type for debugging and identification.

§Returns
  • &str - The component name.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§