use std::time::Duration; use bevy_app::{prelude::*, AppExit, ScheduleRunnerPlugin}; use bevy_ecs::prelude::*; use bevy_sequential_actions::*; fn main() { App::new() .add_plugins(( ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1.0 / 10.0)), SequentialActionsPlugin, )) .add_systems(Startup, setup) .add_systems(Update, (count, frame_logic).chain()) .run(); } fn setup(mut commands: Commands) { let agent = commands.spawn(SequentialActions).id(); commands.actions(agent).add(CountForeverAction); } fn frame_logic( mut frame: Local, mut commands: Commands, agent_q: Query>, ) { const PAUSE_FRAME: u32 = 10; const RESUME_FRAME: u32 = PAUSE_FRAME * 2; const EXIT_FRAME: u32 = PAUSE_FRAME * 3; const MISSING_FRAMES: u32 = RESUME_FRAME - PAUSE_FRAME; println!("Frame: {}", *frame); if *frame == PAUSE_FRAME { println!("\nPAUSE\n"); commands.actions(agent_q.single()).pause(); } if *frame == RESUME_FRAME { println!("\nRESUME\n"); commands.actions(agent_q.single()).execute(); } if *frame == EXIT_FRAME { println!( "\nEXIT - Frame is now at {}, and Count should be {} (missing {} frames)", *frame, EXIT_FRAME - MISSING_FRAMES, MISSING_FRAMES, ); commands.queue(|world: &mut World| { world.send_event(AppExit::Success); }); } *frame += 1; } struct CountForeverAction; impl Action for CountForeverAction { fn is_finished(&self, _agent: Entity, _world: &World) -> bool { false } fn on_start(&mut self, agent: Entity, world: &mut World) -> bool { let mut agent = world.entity_mut(agent); if agent.contains::() { agent.remove::(); } else { agent.insert(Count::default()); } false } fn on_stop(&mut self, agent: Option, world: &mut World, reason: StopReason) { match reason { StopReason::Finished | StopReason::Canceled => { world.entity_mut(agent.unwrap()).remove::(); } StopReason::Paused => { world.entity_mut(agent.unwrap()).insert(Paused); } } } } #[derive(Default, Component)] struct Count(u32); #[derive(Component)] struct Paused; fn count(mut count_q: Query<&mut Count, Without>) { for mut count in &mut count_q { println!("Count: {}", count.0); count.0 += 1; } }