|
| 1 | +// src/menu.rs |
| 2 | +use bevy::prelude::*; |
| 3 | +use crate::{GameConfig, AppState, WINDOW_WIDTH, WINDOW_HEIGHT}; |
| 4 | + |
| 5 | +pub fn menu_plugin(app: &mut App) { |
| 6 | + app |
| 7 | + .add_systems(OnEnter(AppState::Menu), setup_menu) |
| 8 | + .add_systems(Update, menu_interaction.run_if(in_state(AppState::Menu))) |
| 9 | + .add_systems(OnExit(AppState::Menu), cleanup_menu); |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Component)] |
| 13 | +struct MenuButton { |
| 14 | + action: MenuAction, |
| 15 | +} |
| 16 | + |
| 17 | +enum MenuAction { |
| 18 | + Start, |
| 19 | + Settings, |
| 20 | + Exit, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Component)] |
| 24 | +struct SettingsUI; |
| 25 | + |
| 26 | +fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>, config: Res<GameConfig>) { |
| 27 | + // Root UI node |
| 28 | + commands.spawn(NodeBundle { |
| 29 | + style: Style { |
| 30 | + width: Val::Percent(100.0), |
| 31 | + height: Val::Percent(100.0), |
| 32 | + flex_direction: FlexDirection::Column, |
| 33 | + justify_content: JustifyContent::Center, |
| 34 | + align_items: AlignItems::Center, |
| 35 | + ..default() |
| 36 | + }, |
| 37 | + background_color: BackgroundColor(Color::rgba(0.0, 0.0, 0.0, 0.5)), |
| 38 | + ..default() |
| 39 | + }).with_children(|parent| { |
| 40 | + // Title |
| 41 | + parent.spawn(TextBundle::from_section( |
| 42 | + "Bark Squadron", |
| 43 | + TextStyle { |
| 44 | + font_size: 60.0, |
| 45 | + color: Color::WHITE, |
| 46 | + ..default() |
| 47 | + }, |
| 48 | + )); |
| 49 | + |
| 50 | + // Buttons |
| 51 | + spawn_button(parent, "Start Game", MenuAction::Start); |
| 52 | + spawn_button(parent, "Settings", MenuAction::Settings); |
| 53 | + spawn_button(parent, "Exit", MenuAction::Exit); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +fn spawn_button(parent: &mut ChildBuilder, text: &str, action: MenuAction) { |
| 58 | + parent.spawn(( |
| 59 | + ButtonBundle { |
| 60 | + style: Style { |
| 61 | + width: Val::Px(200.0), |
| 62 | + height: Val::Px(50.0), |
| 63 | + margin: UiRect::all(Val::Px(10.0)), |
| 64 | + justify_content: JustifyContent::Center, |
| 65 | + align_items: AlignItems::Center, |
| 66 | + ..default() |
| 67 | + }, |
| 68 | + background_color: BackgroundColor(Color::rgb(0.15, 0.15, 0.15)), |
| 69 | + ..default() |
| 70 | + }, |
| 71 | + MenuButton { action }, |
| 72 | + )).with_children(|parent| { |
| 73 | + parent.spawn(TextBundle::from_section( |
| 74 | + text, |
| 75 | + TextStyle { |
| 76 | + font_size: 30.0, |
| 77 | + color: Color::WHITE, |
| 78 | + ..default() |
| 79 | + }, |
| 80 | + )); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +fn menu_interaction( |
| 85 | + mut interaction_query: Query<(&Interaction, &MenuButton), (Changed<Interaction>, With<Button>)>, |
| 86 | + mut next_state: ResMut<NextState<AppState>>, |
| 87 | + mut commands: Commands, |
| 88 | + mut config: ResMut<GameConfig>, |
| 89 | +) { |
| 90 | + for (interaction, button) in &mut interaction_query { |
| 91 | + if *interaction == Interaction::Pressed { |
| 92 | + match button.action { |
| 93 | + MenuAction::Start => next_state.set(AppState::Game), |
| 94 | + MenuAction::Settings => setup_settings(&mut commands, &config), |
| 95 | + MenuAction::Exit => std::process::exit(0), |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +fn setup_settings(commands: &mut Commands, config: &GameConfig) { |
| 102 | + // Implement settings UI similarly, with buttons for map, day/night, difficulty |
| 103 | + // For brevity, simulate changing |
| 104 | + // In real, spawn UI elements for settings |
| 105 | +} |
| 106 | + |
| 107 | +fn cleanup_menu(mut commands: Commands, query: Query<Entity, With<Node>>) { |
| 108 | + for entity in &query { |
| 109 | + commands.entity(entity).despawn_recursive(); |
| 110 | + } |
| 111 | +} |
0 commit comments