Newer
Older
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
128
use bevy::app::Plugin;
use bevy::asset::{Handle, LoadState};
use bevy::ecs::schedule::ShouldRun;
use bevy::prelude::*;
use bevy_kira_audio::AudioSource;
/// We store our asset handles in this to avoid Bevy from dropping the assets and reloading
/// when we switch tracks
pub struct AudioResources {
pub white_kitty: Handle<AudioSource>,
pub great_madeja: Handle<AudioSource>,
}
#[derive(Default, Eq, PartialEq, Debug, Clone, Hash)]
pub enum AppState {
#[default]
Loading,
Running,
}
pub fn load_resources(mut commands: Commands, assets: Res<AssetServer>) {
let white_kitty = assets.load("The-White-Kitty.mp3");
let great_madeja = assets.load("The-Great-Madeja.mp3");
commands.insert_resource(AudioResources {
white_kitty,
great_madeja,
})
}
pub fn check_load_state(
assets: Res<AssetServer>,
resources: Res<AudioResources>,
mut appstate: ResMut<State<AppState>>,
) {
let load_state =
assets.get_group_load_state(vec![resources.white_kitty.id, resources.great_madeja.id]);
match load_state {
LoadState::Loaded => {
appstate.set(AppState::Running);
}
LoadState::Loading => {}
_ => {
log::error!("The resources are in a bad state! This is a problem");
}
}
}
pub fn has_audio_resources(res: Option<Res<AudioResources>>) -> ShouldRun {
res.is_some().into()
}
pub fn is_state_loading(state: Res<AppState>) -> ShouldRun {
(*state == AppState::Loading).into()
}
pub fn is_state_running(state: Res<AppState>) -> ShouldRun {
(*state == AppState::Running).into()
}
/// This component allows us to easily grab the on screen text
#[derive(Component)]
pub struct TextMarker;
/// This component allows us to easily grab the blank details text area
#[derive(Component)]
pub struct DetailsMarker;
pub fn create_ui(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
commands
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
flex_direction: FlexDirection::Column,
..Default::default()
},
..Default::default()
})
.with_children(|children| {
children
.spawn_bundle(TextBundle {
text: Text::from_section(
"Loading Audio Tracks",
TextStyle {
color: Color::BLACK,
font_size: 48.0,
font: assets.load("KenneyBlocks.ttf"),
},
),
..Default::default()
})
.insert(TextMarker);
children
.spawn_bundle(TextBundle {
text: Text::from_section(
"...",
TextStyle {
color: Color::BLACK,
font_size: 32.0,
font: assets.load("KenneyBlocks.ttf"),
},
),
..Default::default()
})
.insert(DetailsMarker);
});
}
pub struct SetupPlugin;
impl Plugin for SetupPlugin {
fn build(&self, app: &mut App) {
app.add_state(AppState::Loading)
.insert_resource(WindowDescriptor {
width: 800.0,
height: 600.0,
title: String::from("Kitchen Sink Example"),
..Default::default()
})
.add_startup_system(load_resources)
.add_startup_system(create_ui)
.add_system_set(
SystemSet::on_update(AppState::Loading)
.with_run_criteria(has_audio_resources)
.with_system(check_load_state),
);
}
}