# Bevy Sequential Actions
[![crates.io](https://img.shields.io/crates/v/bevy-sequential-actions?style=flat-square)](https://crates.io/crates/bevy-sequential-actions)
[![docs.rs](https://img.shields.io/docsrs/bevy-sequential-actions?style=flat-square)](https://docs.rs/bevy_sequential_actions)
[![MIT/Apache 2.0](https://img.shields.io/crates/l/bevy-sequential-actions?style=flat-square)](https://github.com/hikikones/bevy-sequential-actions#license)
A simple library for managing and sequencing various actions in [Bevy](https://bevyengine.org).
## 📜 Getting Started
#### Plugin
The quickest way for getting started is adding the `SequentialActionsPlugin` to your `App`.
```rust
use bevy_sequential_actions::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, SequentialActionsPlugin))
.run();
}
```
#### Implementing an Action
An action is anything that implements the `Action` trait.
The trait contains various methods that together defines the _lifecycle_ of an action.
From this, you can create any action that can last as long as you like,
and do as much as you like.
An entity with actions is referred to as an `agent`.
A simple wait action follows.
```rust
pub struct WaitAction {
duration: f32, // Seconds
current: Option, // None
}
impl Action for WaitAction {
// By default, this method is called every frame in the Last schedule.
fn is_finished(&self, agent: Entity, world: &World) -> bool {
// Determine if wait timer has reached zero.
world.get::(agent).unwrap().0 <= 0.0
}
// This method is called when an action is started.
fn on_start(&mut self, agent: Entity, world: &mut World) -> bool {
// Take current time (if paused), or use full duration.
let duration = self.current.take().unwrap_or(self.duration);
// Run the wait timer system on the agent.
world.entity_mut(agent).insert(WaitTimer(duration));
// Is action already finished?
// Returning true here will immediately advance the action queue.
self.is_finished(agent, world)
}
// This method is called when an action is stopped.
fn on_stop(&mut self, agent: Option, world: &mut World, reason: StopReason) {
// Do nothing if agent has been despawned.
let Some(agent) = agent else { return };
// Take the wait timer component from the agent.
let wait_timer = world.entity_mut(agent).take::();
// Store current time when paused.
if reason == StopReason::Paused {
self.current = Some(wait_timer.unwrap().0);
}
}
// Optional. This method is called when an action is added to the queue.
fn on_add(&mut self, agent: Entity, world: &mut World) {}
// Optional. This method is called when an action is removed from the queue.
fn on_remove(&mut self, agent: Option, world: &mut World) {}
// Optional. The last method that is called with full ownership.
fn on_drop(self: Box, agent: Option, world: &mut World, reason: DropReason) {}
}
#[derive(Component)]
struct WaitTimer(f32);
fn wait_system(mut wait_timer_q: Query<&mut WaitTimer>, time: Res