//! The screen state for the main gameplay. use bevy::{input::common_conditions::input_just_pressed, prelude::*}; use crate::maze::spawn_level_command; use crate::player::spawn_player_command; use crate::{asset_tracking::LoadResource, audio::Music, screens::Screen}; pub(super) fn plugin(app: &mut App) { app.add_systems( OnEnter(Screen::Gameplay), ( spawn_level_command, spawn_player_command.after(spawn_level_command), ), ); app.load_resource::(); 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))), ); } #[derive(Resource, Asset, Reflect, Clone)] pub struct GameplayMusic { #[dependency] handle: Handle, entity: Option, } impl FromWorld for GameplayMusic { fn from_world(world: &mut World) -> Self { let assets = world.resource::(); Self { handle: assets.load("audio/music/Fluffing A Duck.ogg"), entity: None, } } } fn play_gameplay_music(mut commands: Commands, mut music: ResMut) { music.entity = Some( commands .spawn(( AudioPlayer::(music.handle.clone()), PlaybackSettings::LOOP, Music, )) .id(), ); } fn stop_music(mut commands: Commands, mut music: ResMut) { if let Some(entity) = music.entity.take() { commands.entity(entity).despawn_recursive(); } } fn return_to_title_screen(mut next_screen: ResMut>) { next_screen.set(Screen::Title); }