From 3377daa34200890a4e976743f54c09241b5b4062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Wed, 13 Oct 2021 01:44:26 +0200 Subject: [PATCH 01/18] add an example using UI & states to create a game menu --- Cargo.toml | 4 + examples/README.md | 1 + examples/game/game_menu.rs | 660 +++++++++++++++++++++++++++++++++++++ 3 files changed, 665 insertions(+) create mode 100644 examples/game/game_menu.rs diff --git a/Cargo.toml b/Cargo.toml index 57e3f75ce3d4f..3f2a6fd6c1d53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -330,6 +330,10 @@ path = "examples/game/alien_cake_addict.rs" name = "breakout" path = "examples/game/breakout.rs" +[[example]] +name = "game_menu" +path = "examples/game/game_menu.rs" + # Input [[example]] name = "char_input_events" diff --git a/examples/README.md b/examples/README.md index 189dfede11b55..58c991ad24aed 100644 --- a/examples/README.md +++ b/examples/README.md @@ -176,6 +176,7 @@ Example | File | Description --- | --- | --- `alien_cake_addict` | [`game/alien_cake_addict.rs`](./game/alien_cake_addict.rs) | Eat the cakes. Eat them all. An example 3D game `breakout` | [`game/breakout.rs`](./game/breakout.rs) | An implementation of the classic game "Breakout" +`game_menu` | [`game/game_menu.rs`](./game/game_menu.rs) | A simple game menu ## Input diff --git a/examples/game/game_menu.rs b/examples/game/game_menu.rs new file mode 100644 index 0000000000000..c437d5e79d321 --- /dev/null +++ b/examples/game/game_menu.rs @@ -0,0 +1,660 @@ +use bevy::prelude::*; + +const TEXT_COLOR: Color = Color::rgb(0.9, 0.9, 0.9); + +#[derive(Clone, Eq, PartialEq, Debug, Hash)] +enum GameState { + Menu, + Game, +} + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_startup_system(setup) + .add_state(GameState::Menu) + .add_plugin(menu::MenuPlugin) + .add_plugin(game::GamePlugin) + .run(); +} + +fn setup(mut commands: Commands) { + commands.spawn_bundle(UiCameraBundle::default()); +} + +mod game { + use bevy::prelude::*; + + pub struct GamePlugin; + + impl Plugin for GamePlugin { + fn build(&self, app: &mut App) { + app.add_system_set(SystemSet::on_enter(super::GameState::Game).with_system(game_setup)) + .add_system_set(SystemSet::on_update(super::GameState::Game).with_system(game)) + .add_system_set( + SystemSet::on_exit(super::GameState::Game) + .with_system(super::despawn_screen::), + ); + } + } + + #[derive(Component)] + struct ScreenGame; + + #[derive(Component)] + struct GameTimer(Timer); + + fn game_setup( + mut commands: Commands, + asset_server: Res, + mut materials: ResMut>, + ) { + let font = asset_server.load("fonts/FiraSans-Bold.ttf"); + + commands + .spawn_bundle(NodeBundle { + style: Style { + margin: Rect::all(Val::Auto), + flex_direction: FlexDirection::ColumnReverse, + align_items: AlignItems::Center, + ..Default::default() + }, + material: materials.add(Color::BLACK.into()), + ..Default::default() + }) + .insert(ScreenGame) + .with_children(|parent| { + parent.spawn_bundle(TextBundle { + style: Style { + margin: Rect::all(Val::Px(50.0)), + ..Default::default() + }, + text: Text::with_section( + "Good Game!", + TextStyle { + font: font.clone(), + font_size: 80.0, + color: super::TEXT_COLOR, + }, + Default::default(), + ), + ..Default::default() + }); + parent.spawn_bundle(TextBundle { + style: Style { + margin: Rect::all(Val::Px(50.0)), + ..Default::default() + }, + text: Text::with_section( + "Will be back to the menu shortly...", + TextStyle { + font: font.clone(), + font_size: 80.0, + color: super::TEXT_COLOR, + }, + Default::default(), + ), + ..Default::default() + }); + }); + commands.spawn_bundle((GameTimer(Timer::from_seconds(5.0, false)), ScreenGame)); + } + + fn game( + time: Res