bevy/examples/ecs/state.rs
bjorn3 6d6bc2a8b4 Merge AppBuilder into App (#2531)
This is extracted out of eb8f973646476b4a4926ba644a77e2b3a5772159 and includes some additional changes to remove all references to AppBuilder and fix examples that still used App::build() instead of App::new(). In addition I didn't extract the sub app feature as it isn't ready yet.

You can use `git diff --diff-filter=M eb8f973646476b4a4926ba644a77e2b3a5772159` to find all differences in this PR. The `--diff-filtered=M` filters all files added in the original commit but not in this commit away.

Co-Authored-By: Carter Anderson <mcanders1@gmail.com>
2021-07-27 20:21:06 +00:00

168 lines
5.2 KiB
Rust

use bevy::prelude::*;
/// This example illustrates how to use States to control transitioning from a Menu state to an
/// InGame state.
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_resource::<ButtonMaterials>()
.add_state(AppState::Menu)
.add_system_set(SystemSet::on_enter(AppState::Menu).with_system(setup_menu.system()))
.add_system_set(SystemSet::on_update(AppState::Menu).with_system(menu.system()))
.add_system_set(SystemSet::on_exit(AppState::Menu).with_system(cleanup_menu.system()))
.add_system_set(SystemSet::on_enter(AppState::InGame).with_system(setup_game.system()))
.add_system_set(
SystemSet::on_update(AppState::InGame)
.with_system(movement.system())
.with_system(change_color.system()),
)
.run();
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
enum AppState {
Menu,
InGame,
}
struct MenuData {
button_entity: Entity,
}
fn setup_menu(
mut commands: Commands,
asset_server: Res<AssetServer>,
button_materials: Res<ButtonMaterials>,
) {
// ui camera
commands.spawn_bundle(UiCameraBundle::default());
let button_entity = commands
.spawn_bundle(ButtonBundle {
style: Style {
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
// center button
margin: Rect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
..Default::default()
},
material: button_materials.normal.clone(),
..Default::default()
})
.with_children(|parent| {
parent.spawn_bundle(TextBundle {
text: Text::with_section(
"Play",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
Default::default(),
),
..Default::default()
});
})
.id();
commands.insert_resource(MenuData { button_entity });
}
fn menu(
mut state: ResMut<State<AppState>>,
button_materials: Res<ButtonMaterials>,
mut interaction_query: Query<
(&Interaction, &mut Handle<ColorMaterial>),
(Changed<Interaction>, With<Button>),
>,
) {
for (interaction, mut material) in interaction_query.iter_mut() {
match *interaction {
Interaction::Clicked => {
*material = button_materials.pressed.clone();
state.set(AppState::InGame).unwrap();
}
Interaction::Hovered => {
*material = button_materials.hovered.clone();
}
Interaction::None => {
*material = button_materials.normal.clone();
}
}
}
}
fn cleanup_menu(mut commands: Commands, menu_data: Res<MenuData>) {
commands.entity(menu_data.button_entity).despawn_recursive();
}
fn setup_game(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let texture_handle = asset_server.load("branding/icon.png");
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(SpriteBundle {
material: materials.add(texture_handle.into()),
..Default::default()
});
}
const SPEED: f32 = 100.0;
fn movement(
time: Res<Time>,
input: Res<Input<KeyCode>>,
mut query: Query<&mut Transform, With<Sprite>>,
) {
for mut transform in query.iter_mut() {
let mut direction = Vec3::ZERO;
if input.pressed(KeyCode::Left) {
direction.x -= 1.0;
}
if input.pressed(KeyCode::Right) {
direction.x += 1.0;
}
if input.pressed(KeyCode::Up) {
direction.y += 1.0;
}
if input.pressed(KeyCode::Down) {
direction.y -= 1.0;
}
if direction != Vec3::ZERO {
transform.translation += direction.normalize() * SPEED * time.delta_seconds();
}
}
}
fn change_color(
time: Res<Time>,
mut assets: ResMut<Assets<ColorMaterial>>,
query: Query<&Handle<ColorMaterial>, With<Sprite>>,
) {
for handle in query.iter() {
let material = assets.get_mut(handle).unwrap();
material
.color
.set_b((time.seconds_since_startup() * 5.0).sin() as f32 + 2.0);
}
}
struct ButtonMaterials {
normal: Handle<ColorMaterial>,
hovered: Handle<ColorMaterial>,
pressed: Handle<ColorMaterial>,
}
impl FromWorld for ButtonMaterials {
fn from_world(world: &mut World) -> Self {
let mut materials = world.get_resource_mut::<Assets<ColorMaterial>>().unwrap();
ButtonMaterials {
normal: materials.add(Color::rgb(0.15, 0.15, 0.15).into()),
hovered: materials.add(Color::rgb(0.25, 0.25, 0.25).into()),
pressed: materials.add(Color::rgb(0.35, 0.75, 0.35).into()),
}
}
}