bevyjam/src/menu.rs
2022-08-25 15:39:16 +02:00

67 lines
1.5 KiB
Rust

use crate::AppState;
use bevy::{
input::{keyboard::KeyCode, Input},
prelude::*,
};
pub struct MenuPlugin;
#[derive(Component)]
struct Menu;
impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) {
app.add_system_set(SystemSet::on_enter(AppState::Menu).with_system(setup))
.add_system_set(SystemSet::on_update(AppState::Menu).with_system(keyboard_input_system))
.add_system_set(SystemSet::on_exit(AppState::Menu).with_system(despawn));
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.get_handle("UacariLegacy-Thin.ttf");
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"BEVYJAM",
TextStyle {
font: font.clone(),
font_size: 96.0,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::CENTER),
transform: Transform::from_xyz(0., 128.0, 0.),
..Default::default()
})
.insert(Menu);
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"Press ENTER",
TextStyle {
font,
font_size: 32.0,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::CENTER),
..Default::default()
})
.insert(Menu);
}
fn despawn(mut commands: Commands, menu_query: Query<Entity, With<Menu>>) {
for entity in menu_query.iter() {
commands.entity(entity).despawn_recursive();
}
}
fn keyboard_input_system(
keyboard_input: Res<Input<KeyCode>>,
mut app_state: ResMut<State<AppState>>,
) {
if keyboard_input.just_pressed(KeyCode::Return) {
app_state.replace(AppState::Game).unwrap()
}
}