1use bevy::{
2 app::{App, AppExit, Plugin, Startup},
3 input::InputPlugin,
4 prelude::*,
5 MinimalPlugins,
6};
7
8use super::{AutoplayPlugin, LoadFromFileAndPlay};
9
10#[derive(Resource)]
11struct TestSessionFilename(String);
12
13#[derive(Event)]
14pub enum TestResult {
15 #[allow(dead_code)]
16 Success,
17 #[allow(dead_code)]
18 Failure(String),
19}
20
21pub struct AutoplayTestPlugin(pub String);
22
23impl Plugin for AutoplayTestPlugin {
24 fn build(&self, app: &mut App) {
25 app.add_plugins((MinimalPlugins, InputPlugin, AutoplayPlugin))
26 .insert_resource(TestSessionFilename(self.0.clone()))
27 .add_event::<TestResult>()
28 .add_systems(Startup, playback_recording)
29 .add_systems(Update, check_for_result);
30 }
31}
32
33fn playback_recording(
34 mut ev_load_play: EventWriter<LoadFromFileAndPlay>,
35 filename: Res<TestSessionFilename>,
36 mut _time: ResMut<Time<Virtual>>,
37) {
38 ev_load_play.send(LoadFromFileAndPlay(filename.0.clone()));
40}
41
42fn check_for_result(mut exit: EventWriter<AppExit>, mut ev_result: EventReader<TestResult>) {
43 if let Some(ev) = ev_result.read().next() {
44 match ev {
45 TestResult::Success => exit.send(AppExit),
46 TestResult::Failure(msg) => panic!("{}", msg),
47 };
48 }
49}