-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcredits.rs
108 lines (95 loc) · 3.2 KB
/
credits.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! A credits screen that can be accessed from the title screen.
use bevy::{ecs::spawn::SpawnIter, prelude::*, ui::Val::*};
use crate::{asset_tracking::LoadResource, audio::music, screens::Screen, theme::prelude::*};
pub(super) fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::Credits), spawn_credits_screen);
app.register_type::<CreditsMusic>();
app.load_resource::<CreditsMusic>();
app.add_systems(OnEnter(Screen::Credits), start_credits_music);
app.add_systems(OnExit(Screen::Credits), stop_credits_music);
}
fn spawn_credits_screen(mut commands: Commands) {
commands.spawn((
widget::ui_root("Credits Screen"),
StateScoped(Screen::Credits),
children![
widget::header("Created by"),
created_by(),
widget::header("Assets"),
assets(),
widget::button("Back", enter_title_screen),
],
));
}
fn created_by() -> impl Bundle {
grid(vec![
["Joe Shmoe", "Implemented alligator wrestling AI"],
["Jane Doe", "Made the music for the alien invasion"],
])
}
fn assets() -> impl Bundle {
grid(vec![
["Ducky sprite", "CC0 by Caz Creates Games"],
["Button SFX", "CC0 by Jaszunio15"],
["Music", "CC BY 3.0 by Kevin MacLeod"],
[
"Bevy logo",
"All rights reserved by the Bevy Foundation, permission granted for splash screen use when unmodified",
],
])
}
fn grid(content: Vec<[&'static str; 2]>) -> impl Bundle {
(
Name::new("Grid"),
Node {
display: Display::Grid,
row_gap: Px(10.0),
column_gap: Px(30.0),
grid_template_columns: RepeatedGridTrack::px(2, 400.0),
..default()
},
Children::spawn(SpawnIter(content.into_iter().flatten().enumerate().map(
|(i, text)| {
(
widget::label(text),
Node {
justify_self: if i % 2 == 0 {
JustifySelf::End
} else {
JustifySelf::Start
},
..default()
},
)
},
))),
)
}
fn enter_title_screen(_: Trigger<Pointer<Click>>, mut next_screen: ResMut<NextState<Screen>>) {
next_screen.set(Screen::Title);
}
#[derive(Resource, Asset, Clone, Reflect)]
#[reflect(Resource)]
struct CreditsMusic {
#[dependency]
handle: Handle<AudioSource>,
entity: Option<Entity>,
}
impl FromWorld for CreditsMusic {
fn from_world(world: &mut World) -> Self {
let assets = world.resource::<AssetServer>();
Self {
handle: assets.load("audio/music/Monkeys Spinning Monkeys.ogg"),
entity: None,
}
}
}
fn start_credits_music(mut commands: Commands, mut credits_music: ResMut<CreditsMusic>) {
let handle = credits_music.handle.clone();
credits_music.entity = Some(commands.spawn(music(handle)).id());
}
fn stop_credits_music(mut commands: Commands, mut credits_music: ResMut<CreditsMusic>) {
if let Some(entity) = credits_music.entity.take() {
commands.entity(entity).despawn();
}
}