Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/screens/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
85 changes: 79 additions & 6 deletions src/screens/title.rs
Original file line number Diff line number Diff line change
@@ -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)),
);
}

Expand All @@ -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<AssetServer>) {
let font_handle = asset_server.load("font.ttf");
commands.spawn((
Expand Down Expand Up @@ -67,7 +76,8 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res<AssetServer>) {
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(),
Expand All @@ -91,7 +101,8 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res<AssetServer>) {
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(),
Expand All @@ -115,7 +126,8 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res<AssetServer>) {
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(),
Expand All @@ -129,11 +141,61 @@ fn spawn_title_screen(mut commands: Commands, asset_server: Res<AssetServer>) {
));
}

fn input_system(
input_query: Query<&ActionState<Action>>,
mut screen_state: ResMut<NextState<Screen>>,
mut message_writer: MessageWriter<AppExit>,
mut active_index: ResMut<ActiveIndex>,
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<Interaction>>,
mut screen_state: ResMut<NextState<Screen>>,
mut message_writer: MessageWriter<AppExit>,
mut active_index: ResMut<ActiveIndex>,
) {
for (interaction, menu_button, children) in interaction_query {
match *interaction {
Expand All @@ -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));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/sundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);