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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use bevy_pipelines_ready::{PipelinesReady, PipelinesReadyPlugin};
use crate::{util::cleanup, AppState};
#[derive(AssetCollection, Resource)]
pub struct GltfAssets {
#[asset(path = "bevybird_gold.glb#Scene0")]
pub birb_gold: Handle<Scene>,
#[asset(path = "bevybird.glb#Scene0")]
pub birb: Handle<Scene>,
}
#[derive(AssetCollection, Resource)]
pub struct FontAssets {
#[asset(path = "Amatic-Bold.ttf")]
pub main: Handle<Font>,
}
#[derive(AssetCollection, Resource)]
pub struct AudioAssets {
#[asset(path = "menu.ogg")]
pub menu: Handle<AudioSource>,
#[asset(path = "play.ogg")]
pub game: Handle<AudioSource>,
#[asset(path = "flap.ogg")]
pub flap: Handle<AudioSource>,
#[asset(path = "badflap.ogg")]
pub badflap: Handle<AudioSource>,
#[asset(path = "score.ogg")]
pub score: Handle<AudioSource>,
#[asset(path = "crash.ogg")]
pub crash: Handle<AudioSource>,
#[asset(path = "bump.ogg")]
pub bump: Handle<AudioSource>,
}
#[cfg(not(target_arch = "wasm32"))]
const EXPECTED_PIPELINES: usize = 14;
#[cfg(target_arch = "wasm32")]
const EXPECTED_PIPELINES: usize = 10;
#[derive(Component)]
struct LoadingOnly;
pub struct LoadingPlugin;
impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
app.add_loading_state(
LoadingState::new(AppState::LoadingAssets)
.load_collection::<GltfAssets>()
.load_collection::<FontAssets>()
.load_collection::<AudioAssets>()
.continue_to_state(AppState::LoadingPipelines),
);
app.add_plugins(PipelinesReadyPlugin);
app.add_systems(Startup, loading);
app.add_systems(OnEnter(AppState::LoadingPipelines), preload);
app.add_systems(
Update,
(
print_pipelines.run_if(resource_changed::<PipelinesReady>),
check_pipelines
.run_if(in_state(AppState::LoadingPipelines))
.run_if(resource_changed::<PipelinesReady>),
),
);
app.add_systems(OnExit(AppState::LoadingPipelines), cleanup::<LoadingOnly>);
}
}
fn loading(mut commands: Commands) {
commands.spawn((
TextBundle {
text: Text::from_section(
"Loading...",
TextStyle {
font_size: 20.,
..default()
},
),
style: Style {
position_type: PositionType::Absolute,
bottom: Val::Px(5.),
left: Val::Px(5.),
..default()
},
z_index: ZIndex::Global(100),
..default()
},
LoadingOnly,
));
}
fn preload(mut commands: Commands, gltf_assets: Res<GltfAssets>) {
commands.spawn((
SceneBundle {
scene: gltf_assets.birb.clone(),
transform: Transform::from_scale(Vec3::splat(0.1)),
..default()
},
LoadingOnly,
));
commands.spawn((
SceneBundle {
scene: gltf_assets.birb_gold.clone(),
transform: Transform::from_scale(Vec3::splat(0.1)),
..default()
},
LoadingOnly,
));
}
fn print_pipelines(ready: Res<PipelinesReady>) {
info!("Pipelines Ready: {}/{}", ready.get(), EXPECTED_PIPELINES);
}
fn check_pipelines(ready: Res<PipelinesReady>, mut next_state: ResMut<NextState<AppState>>) {
if ready.get() >= EXPECTED_PIPELINES {
next_state.set(AppState::StartScreen);
}
}