diff --git a/src/input/mod.rs b/src/input/mod.rs index 616ce02..a8994a4 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -25,17 +25,36 @@ fn setup_input(mut commands: Commands) { use leafwing_input_manager::prelude::*; let mut player_input_map = InputMap::default(); + player_input_map.insert(Action::Up, GamepadControlDirection::LEFT_UP.threshold(0.25)); + player_input_map.insert( + Action::Down, + GamepadControlDirection::LEFT_DOWN.threshold(0.25), + ); + player_input_map.insert( + Action::Left, + GamepadControlDirection::LEFT_LEFT.threshold(0.25), + ); + player_input_map.insert( + Action::Right, + GamepadControlDirection::LEFT_RIGHT.threshold(0.25), + ); player_input_map.insert(Action::Up, KeyCode::KeyW); player_input_map.insert(Action::Up, KeyCode::ArrowUp); + player_input_map.insert(Action::Up, GamepadButton::DPadUp); player_input_map.insert(Action::Down, KeyCode::KeyS); player_input_map.insert(Action::Down, KeyCode::ArrowDown); + player_input_map.insert(Action::Down, GamepadButton::DPadDown); player_input_map.insert(Action::Left, KeyCode::KeyA); player_input_map.insert(Action::Left, KeyCode::ArrowLeft); + player_input_map.insert(Action::Left, GamepadButton::DPadLeft); player_input_map.insert(Action::Right, KeyCode::KeyD); player_input_map.insert(Action::Right, KeyCode::ArrowRight); + player_input_map.insert(Action::Right, GamepadButton::DPadRight); player_input_map.insert(Action::Select, KeyCode::Space); player_input_map.insert(Action::Shoot, KeyCode::Space); + player_input_map.insert(Action::Shoot, GamepadButton::South); player_input_map.insert(Action::Select, KeyCode::Enter); + player_input_map.insert(Action::Select, GamepadButton::South); player_input_map.insert(Action::Pause, KeyCode::Escape); commands.spawn(player_input_map); diff --git a/src/screens/game.rs b/src/screens/game.rs index fa6d19c..07db3ce 100644 --- a/src/screens/game.rs +++ b/src/screens/game.rs @@ -264,9 +264,9 @@ fn player_input( let thruster = thruster.single_mut(); if let Ok(mut thruster_sprite) = thruster { - if action_state.pressed(&Action::Up) { + if intent.y > 0.0 { thruster_sprite.image = ship_speed_sprites.fast.clone(); - } else if action_state.pressed(&Action::Down) { + } else if intent.y < 0.0 { thruster_sprite.image = ship_speed_sprites.slow.clone(); } else { thruster_sprite.image = ship_speed_sprites.default.clone(); diff --git a/src/screens/title.rs b/src/screens/title.rs index 483b09b..b1e24a9 100644 --- a/src/screens/title.rs +++ b/src/screens/title.rs @@ -1,16 +1,19 @@ use bevy::prelude::*; +use leafwing_input_manager::prelude::ActionState; use crate::{ HIGH_RES_LAYERS, PIXEL_PERFECT_LAYERS, + input::Action, screens::Screen, - sundry::{LIGHT_GRAY, TRANSPARENT_LIGHT_GRAY, WHITE}, + sundry::{MEDIUM_GRAY, TRANSPARENT_MEDIUM_GRAY, WHITE}, }; pub(super) fn plugin(app: &mut App) { + app.insert_resource(ActiveIndex(0)); app.add_systems(OnEnter(Screen::Title), spawn_title_screen) .add_systems( Update, - (button_system, fade_in).run_if(in_state(Screen::Title)), + (button_system, input_system, fade_in).run_if(in_state(Screen::Title)), ); } @@ -24,6 +27,12 @@ enum MenuButton { #[derive(Component)] struct FadeIn; +#[derive(Resource)] +struct ActiveIndex(usize); + +#[derive(Component)] +struct ButtonText(usize); + fn spawn_title_screen(mut commands: Commands, asset_server: Res) { let font_handle = asset_server.load("font.ttf"); commands.spawn(( @@ -67,7 +76,8 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res) { children![( Name::new("New Game Text"), Text::new("NEW GAME"), - TextColor(TRANSPARENT_LIGHT_GRAY), + TextColor(TRANSPARENT_MEDIUM_GRAY), + ButtonText(0), TextFont { font_size: 32.0, font: font_handle.clone(), @@ -91,7 +101,8 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res) { children![( Name::new("Settings"), Text::new("SETTINGS"), - TextColor(TRANSPARENT_LIGHT_GRAY), + TextColor(TRANSPARENT_MEDIUM_GRAY), + ButtonText(1), TextFont { font_size: 32.0, font: font_handle.clone(), @@ -115,7 +126,8 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res) { children![( Name::new("Quit Text"), Text::new("QUIT"), - TextColor(TRANSPARENT_LIGHT_GRAY), + TextColor(TRANSPARENT_MEDIUM_GRAY), + ButtonText(2), TextFont { font_size: 32.0, font: font_handle.clone(), @@ -129,11 +141,61 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res) { )); } +fn input_system( + input_query: Query<&ActionState>, + mut screen_state: ResMut>, + mut message_writer: MessageWriter, + mut active_index: ResMut, + mut text_query: Query<(&ButtonText, &mut TextColor)>, +) { + let action_state = input_query.single().unwrap(); + + if action_state.just_pressed(&Action::Up) { + if active_index.0 == 0 { + active_index.0 = 2; + } else { + active_index.0 -= 1; + } + } + + if action_state.just_pressed(&Action::Down) { + if active_index.0 == 2 { + active_index.0 = 0 + } else { + active_index.0 += 1 + }; + } + + for (button_index, mut text_color) in text_query.iter_mut() { + if button_index.0 == active_index.0 { + text_color.0 = WHITE; + } else { + text_color.0 = MEDIUM_GRAY; + } + } + + if action_state.just_pressed(&Action::Select) { + match active_index.0 { + 0 => { + screen_state.set(Screen::Game); + } + 1 => { + println!("Settings button pressed"); + } + 2 => { + message_writer.write(AppExit::Success); + } + _ => {} + } + } +} + fn button_system( mut commands: Commands, interaction_query: Query<(&Interaction, &MenuButton, &Children), Changed>, mut screen_state: ResMut>, mut message_writer: MessageWriter, + mut active_index: ResMut, ) { for (interaction, menu_button, children) in interaction_query { match *interaction { @@ -152,11 +214,22 @@ fn button_system( commands .entity(*children.first().unwrap()) .insert(TextColor(WHITE)); + match menu_button { + MenuButton::NewGame => { + active_index.0 = 0; + } + MenuButton::Settings => { + active_index.0 = 1; + } + MenuButton::Quit => { + active_index.0 = 2; + } + } } Interaction::None => { commands .entity(*children.first().unwrap()) - .insert(TextColor(LIGHT_GRAY)); + .insert(TextColor(MEDIUM_GRAY)); } } } diff --git a/src/sundry.rs b/src/sundry.rs index dd77c98..59fa980 100644 --- a/src/sundry.rs +++ b/src/sundry.rs @@ -2,5 +2,7 @@ use bevy::prelude::Color; pub const WHITE: Color = Color::srgb_u8(235, 237, 233); // pub const TRANSPARENT_WHITE: Color = Color::srgba_u8(235, 237, 233, 0); -pub const LIGHT_GRAY: Color = Color::srgb_u8(199, 207, 204); -pub const TRANSPARENT_LIGHT_GRAY: Color = Color::srgba_u8(199, 207, 204, 0); +// pub const LIGHT_GRAY: Color = Color::srgb_u8(199, 207, 204); +// pub const TRANSPARENT_LIGHT_GRAY: Color = Color::srgba_u8(199, 207, 204, 0); +pub const MEDIUM_GRAY: Color = Color::srgb_u8(168, 181, 178); +pub const TRANSPARENT_MEDIUM_GRAY: Color = Color::srgba_u8(168, 181, 178, 0);