pub trait Action: Send + Sync + 'static {
    fn on_start(
        &mut self,
        entity: Entity,
        world: &mut World,
        commands: &mut ActionCommands
    ); fn on_stop(&mut self, entity: Entity, world: &mut World, reason: StopReason); }
Expand description

The trait that all actions must implement.

All actions must declare when they are finished. This is done by calling finish from either ActionCommands or Commands.

Examples

Empty Action

An action that does nothing.

struct EmptyAction;

impl Action for EmptyAction {
    fn on_start(&mut self, entity: Entity, world: &mut World, commands: &mut ActionCommands) {
        // Action is finished.
        commands.actions(entity).finish();
    }

    fn on_stop(&mut self, entity: Entity, world: &mut World, reason: StopReason) {}
}
Wait Action

An action that waits a specified time in seconds.

pub struct WaitAction {
    duration: f32,
    current: Option<f32>,
}

impl Action for WaitAction {
    fn on_start(&mut self, entity: Entity, world: &mut World, _commands: &mut ActionCommands) {
        let duration = self.current.unwrap_or(self.duration);
        world.entity_mut(entity).insert(Wait(duration));
    }

    fn on_stop(&mut self, entity: Entity, world: &mut World, reason: StopReason) {
        match reason {
            StopReason::Finished | StopReason::Canceled => {
                world.entity_mut(entity).remove::<Wait>();
                self.current = None;
            }
            StopReason::Paused => {
                let wait = world.entity_mut(entity).remove::<Wait>().unwrap();
                self.current = Some(wait.0);
            }
        }
    }
}

#[derive(Component)]
struct Wait(f32);

fn wait(mut wait_q: Query<(Entity, &mut Wait)>, time: Res<Time>, mut commands: Commands) {
    for (entity, mut wait) in wait_q.iter_mut() {
        wait.0 -= time.delta_seconds();
        if wait.0 <= 0.0 {
            // Action is finished.
            commands.actions(entity).finish();
        }
    }
}

Required Methods

The method that is called when an action is started.

The method that is called when an action is stopped.

Trait Implementations

Convert self into Box<dyn Action>.

Implementations on Foreign Types

Implementors