generated from loopystudios/bevy-shell-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.rs
85 lines (73 loc) · 2.13 KB
/
helpers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use bevy::prelude::*;
use bevy::render::camera::ScalingMode;
use bevy_inspector_egui::WorldInspectorPlugin;
use bevy_rapier3d::render::RapierDebugRenderPlugin;
use std::thread;
pub fn on_main_thread() -> bool {
matches!(thread::current().name(), Some("main"))
}
pub struct Test<A> {
pub setup: fn(&mut App) -> A,
pub setup_graphics: fn(&mut App),
pub frames: u64,
pub check: fn(&App, A),
}
pub fn default_setup_graphics(app: &mut App) {
app.world.spawn().insert_bundle(Camera3dBundle {
projection: OrthographicProjection {
scale: 3.0,
scaling_mode: ScalingMode::FixedVertical(5.0),
..default()
}
.into(),
transform: Transform::from_xyz(0.0, 9.0, -6.0)
.looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
app.world
.insert_resource(ClearColor(Color::rgb(0.1, 0.1, 0.1)));
app.world.spawn().insert_bundle(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
}
fn app() -> (App, bool) {
let mut app = App::new();
let on_main_thread = if on_main_thread() {
println!("Test running on main thread, will display window");
true
} else {
println!("Test not running on main thread, will run headlessly");
false
};
if on_main_thread {
app.add_plugins(DefaultPlugins)
.add_plugin(RapierDebugRenderPlugin::default());
} else {
app.add_plugins(DefaultPlugins::for_testing());
}
app.add_plugin(WorldInspectorPlugin::new());
(app, on_main_thread)
}
impl<A> Test<A> {
pub fn run(self) {
let (mut app, on_main_thread) = app();
if on_main_thread {
(self.setup_graphics)(&mut app);
}
let res = (self.setup)(&mut app);
if on_main_thread {
app.run();
} else {
for _ in 0..self.frames {
app.update();
}
(self.check)(&app, res)
}
}
}