//! The credits menu. use bevy::{ecs::spawn::SpawnIter, input::common_conditions::input_just_pressed, prelude::*}; use crate::{asset_tracking::LoadResource, audio::music, menus::Menu, theme::prelude::*}; pub(super) fn plugin(app: &mut App) { app.add_systems(OnEnter(Menu::Credits), spawn_credits_menu); app.add_systems( Update, go_back.run_if(in_state(Menu::Credits).and(input_just_pressed(KeyCode::Escape))), ); app.load_resource::(); app.add_systems(OnEnter(Menu::Credits), start_credits_music); } fn spawn_credits_menu(mut commands: Commands) { commands.spawn(( widget::ui_root("Credits Menu"), GlobalZIndex(2), DespawnOnExit(Menu::Credits), children![ widget::header("Created by"), created_by(), widget::header("Assets"), assets(), widget::button("Back", go_back_on_click), ], )); } 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), column_gap: px(30), 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.is_multiple_of(2) { JustifySelf::End } else { JustifySelf::Start }, ..default() }, ) }, ))), ) } fn go_back_on_click(_: On>, mut next_menu: ResMut>) { next_menu.set(Menu::Main); } fn go_back(mut next_menu: ResMut>) { next_menu.set(Menu::Main); } #[derive(Resource, Asset, Clone, Reflect)] #[reflect(Resource)] struct CreditsAssets { #[dependency] music: Handle, } impl FromWorld for CreditsAssets { fn from_world(world: &mut World) -> Self { let assets = world.resource::(); Self { music: assets.load("audio/music/Monkeys Spinning Monkeys.ogg"), } } } fn start_credits_music(mut commands: Commands, credits_music: Res) { commands.spawn(( Name::new("Credits Music"), DespawnOnExit(Menu::Credits), music(credits_music.music.clone()), )); }