-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgameplay.rs
65 lines (54 loc) · 1.75 KB
/
gameplay.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
//! The screen state for the main gameplay.
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
use crate::{
asset_tracking::LoadResource, audio::Music, demo::level::spawn_level as spawn_level_command,
screens::Screen,
};
pub(super) fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::Gameplay), spawn_level);
app.load_resource::<GameplayMusic>();
app.add_systems(OnEnter(Screen::Gameplay), play_gameplay_music);
app.add_systems(OnExit(Screen::Gameplay), stop_music);
app.add_systems(
Update,
return_to_title_screen
.run_if(in_state(Screen::Gameplay).and(input_just_pressed(KeyCode::Escape))),
);
}
fn spawn_level(mut commands: Commands) {
commands.queue(spawn_level_command);
}
#[derive(Resource, Asset, Reflect, Clone)]
pub struct GameplayMusic {
#[dependency]
handle: Handle<AudioSource>,
entity: Option<Entity>,
}
impl FromWorld for GameplayMusic {
fn from_world(world: &mut World) -> Self {
let assets = world.resource::<AssetServer>();
Self {
handle: assets.load("audio/music/Fluffing A Duck.ogg"),
entity: None,
}
}
}
fn play_gameplay_music(mut commands: Commands, mut music: ResMut<GameplayMusic>) {
music.entity = Some(
commands
.spawn((
AudioPlayer(music.handle.clone()),
PlaybackSettings::LOOP,
Music,
))
.id(),
);
}
fn stop_music(mut commands: Commands, mut music: ResMut<GameplayMusic>) {
if let Some(entity) = music.entity.take() {
commands.entity(entity).despawn_recursive();
}
}
fn return_to_title_screen(mut next_screen: ResMut<NextState<Screen>>) {
next_screen.set(Screen::Title);
}