diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/breakout/breakout.rs | 5 | ||||
-rw-r--r-- | examples/breakout/client.rs | 191 | ||||
-rw-r--r-- | examples/breakout/server.rs | 72 | ||||
-rw-r--r-- | examples/chat/client.rs | 14 | ||||
-rw-r--r-- | examples/chat/server.rs | 4 |
5 files changed, 151 insertions, 135 deletions
diff --git a/examples/breakout/breakout.rs b/examples/breakout/breakout.rs index 123817c..e57e2d7 100644 --- a/examples/breakout/breakout.rs +++ b/examples/breakout/breakout.rs @@ -61,6 +61,7 @@ struct CollisionEvent; #[derive(Component)] struct Score; +#[derive(Resource)] struct CollisionSound(Handle<AudioSource>); pub type BrickId = u64; @@ -152,7 +153,7 @@ fn main() { SystemSet::new() // https://github.com/bevyengine/bevy/issues/1839 // Run on a fixed Timestep,on all clients, in GameState::Running - .with_run_criteria(FixedTimestep::step(TIME_STEP as f64).chain( + .with_run_criteria(FixedTimestep::step(TIME_STEP as f64).pipe( |In(input): In<ShouldRun>, state: Res<State<GameState>>| match state.current() { GameState::Running => input, _ => ShouldRun::No, @@ -169,7 +170,7 @@ fn main() { SystemSet::new() // https://github.com/bevyengine/bevy/issues/1839 // Run on a fixed Timestep, only for the hosting client, in GameState::Running - .with_run_criteria(FixedTimestep::step(TIME_STEP as f64).chain( + .with_run_criteria(FixedTimestep::step(TIME_STEP as f64).pipe( |In(input): In<ShouldRun>, state: Res<State<GameState>>, server: Res<Server>| match state.current() { diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs index 9546b26..eb7a8bb 100644 --- a/examples/breakout/client.rs +++ b/examples/breakout/client.rs @@ -4,13 +4,14 @@ use bevy::{ prelude::{ default, AssetServer, Audio, BuildChildren, Bundle, Button, ButtonBundle, Camera2dBundle, Changed, Color, Commands, Component, DespawnRecursiveExt, Entity, EventReader, EventWriter, - Input, KeyCode, Local, Query, Res, ResMut, State, TextBundle, Transform, Vec2, Vec3, With, - Without, + Input, KeyCode, Local, Query, Res, ResMut, Resource, State, TextBundle, Transform, Vec2, + Vec3, With, Without, }, sprite::{Sprite, SpriteBundle}, text::{Text, TextSection, TextStyle}, ui::{ - AlignItems, Interaction, JustifyContent, PositionType, Size, Style, UiColor, UiRect, Val, + AlignItems, BackgroundColor, Interaction, JustifyContent, PositionType, Size, Style, + UiRect, Val, }, }; use bevy_quinnet::{ @@ -45,22 +46,23 @@ const BOLD_FONT: &str = "fonts/FiraSans-Bold.ttf"; const NORMAL_FONT: &str = "fonts/FiraMono-Medium.ttf"; const COLLISION_SOUND_EFFECT: &str = "sounds/breakout_collision.ogg"; -#[derive(Debug, Clone, Default)] +#[derive(Resource, Debug, Clone, Default)] pub(crate) struct ClientData { self_id: ClientId, } -#[derive(Default)] +#[derive(Resource, Default)] pub(crate) struct NetworkMapping { // Network entity id to local entity id map: HashMap<Entity, Entity>, } -#[derive(Default)] +#[derive(Resource, Default)] pub struct BricksMapping { map: HashMap<BrickId, Entity>, } // This resource tracks the game's score +#[derive(Resource)] pub(crate) struct Scoreboard { pub(crate) score: i32, } @@ -104,44 +106,46 @@ pub(crate) fn start_connection(client: ResMut<Client>) { fn spawn_paddle(commands: &mut Commands, position: &Vec3, owned: bool) -> Entity { commands - .spawn() - .insert_bundle(SpriteBundle { - transform: Transform { - translation: *position, - scale: PADDLE_SIZE, - ..default() - }, - sprite: Sprite { - color: if owned { - PADDLE_COLOR - } else { - OPPONENT_PADDLE_COLOR + .spawn(( + SpriteBundle { + transform: Transform { + translation: *position, + scale: PADDLE_SIZE, + ..default() + }, + sprite: Sprite { + color: if owned { + PADDLE_COLOR + } else { + OPPONENT_PADDLE_COLOR + }, + ..default() }, ..default() }, - ..default() - }) - .insert(Paddle) + Paddle, + )) .id() } fn spawn_ball(commands: &mut Commands, pos: &Vec3, direction: &Vec2, owned: bool) -> Entity { commands - .spawn() - .insert(Ball) - .insert_bundle(SpriteBundle { - transform: Transform { - scale: BALL_SIZE, - translation: *pos, - ..default() - }, - sprite: Sprite { - color: ball_color_from_bool(owned), + .spawn(( + Ball, + SpriteBundle { + transform: Transform { + scale: BALL_SIZE, + translation: *pos, + ..default() + }, + sprite: Sprite { + color: ball_color_from_bool(owned), + ..default() + }, ..default() }, - ..default() - }) - .insert(Velocity(direction.normalize() * BALL_SPEED)) + Velocity(direction.normalize() * BALL_SPEED), + )) .id() } @@ -161,20 +165,21 @@ pub(crate) fn spawn_bricks( ); let brick = commands - .spawn() - .insert(Brick(brick_id)) - .insert_bundle(SpriteBundle { - sprite: Sprite { - color: BRICK_COLOR, - ..default() - }, - transform: Transform { - translation: brick_position.extend(0.0), - scale: Vec3::new(BRICK_SIZE.x, BRICK_SIZE.y, 1.0), + .spawn(( + Brick(brick_id), + SpriteBundle { + sprite: Sprite { + color: BRICK_COLOR, + ..default() + }, + transform: Transform { + translation: brick_position.extend(0.0), + scale: Vec3::new(BRICK_SIZE.x, BRICK_SIZE.y, 1.0), + ..default() + }, ..default() }, - ..default() - }) + )) .id(); bricks.map.insert(brick_id, brick); brick_id += 1; @@ -328,7 +333,7 @@ pub(crate) fn play_collision_sound( pub(crate) fn setup_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) { // Camera - commands.spawn_bundle(Camera2dBundle::default()); + commands.spawn(Camera2dBundle::default()); let button_style = Style { size: Size::new(Val::Px(150.0), Val::Px(65.0)), @@ -346,30 +351,34 @@ pub(crate) fn setup_main_menu(mut commands: Commands, asset_server: Res<AssetSer color: BUTTON_TEXT_COLOR, }; commands - .spawn_bundle(ButtonBundle { - style: button_style.clone(), - color: NORMAL_BUTTON_COLOR.into(), - ..default() - }) - .insert(MenuItem::Host) + .spawn(( + ButtonBundle { + style: button_style.clone(), + background_color: NORMAL_BUTTON_COLOR.into(), + ..default() + }, + MenuItem::Host, + )) .with_children(|parent| { - parent.spawn_bundle(TextBundle::from_section("Host", text_style.clone())); + parent.spawn(TextBundle::from_section("Host", text_style.clone())); }); commands - .spawn_bundle(ButtonBundle { - style: button_style, - color: NORMAL_BUTTON_COLOR.into(), - ..default() - }) - .insert(MenuItem::Join) + .spawn(( + ButtonBundle { + style: button_style, + background_color: NORMAL_BUTTON_COLOR.into(), + ..default() + }, + MenuItem::Join, + )) .with_children(|parent| { - parent.spawn_bundle(TextBundle::from_section("Join", text_style)); + parent.spawn(TextBundle::from_section("Join", text_style)); }); } pub(crate) fn handle_menu_buttons( mut interaction_query: Query< - (&Interaction, &mut UiColor, &MenuItem), + (&Interaction, &mut BackgroundColor, &MenuItem), (Changed<Interaction>, With<Button>), >, mut game_state: ResMut<State<GameState>>, @@ -405,41 +414,39 @@ pub(crate) fn setup_breakout(mut commands: Commands, asset_server: Res<AssetServ commands.insert_resource(CollisionSound(ball_collision_sound)); // Scoreboard - commands - .spawn() - .insert_bundle( - TextBundle::from_sections([ - TextSection::new( - "Score: ", - TextStyle { - font: asset_server.load(BOLD_FONT), - font_size: SCOREBOARD_FONT_SIZE, - color: TEXT_COLOR, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load(NORMAL_FONT), + commands.spawn(( + TextBundle::from_sections([ + TextSection::new( + "Score: ", + TextStyle { + font: asset_server.load(BOLD_FONT), font_size: SCOREBOARD_FONT_SIZE, - color: SCORE_COLOR, - }), - ]) - .with_style(Style { - position_type: PositionType::Absolute, - position: UiRect { - top: SCOREBOARD_TEXT_PADDING, - left: SCOREBOARD_TEXT_PADDING, - ..default() + color: TEXT_COLOR, }, - ..default() + ), + TextSection::from_style(TextStyle { + font: asset_server.load(NORMAL_FONT), + font_size: SCOREBOARD_FONT_SIZE, + color: SCORE_COLOR, }), - ) - .insert(Score); + ]) + .with_style(Style { + position_type: PositionType::Absolute, + position: UiRect { + top: SCOREBOARD_TEXT_PADDING, + left: SCOREBOARD_TEXT_PADDING, + ..default() + }, + ..default() + }), + Score, + )); // Walls - commands.spawn_bundle(WallBundle::new(WallLocation::Left)); - commands.spawn_bundle(WallBundle::new(WallLocation::Right)); - commands.spawn_bundle(WallBundle::new(WallLocation::Bottom)); - commands.spawn_bundle(WallBundle::new(WallLocation::Top)); + commands.spawn(WallBundle::new(WallLocation::Left)); + commands.spawn(WallBundle::new(WallLocation::Right)); + commands.spawn(WallBundle::new(WallLocation::Bottom)); + commands.spawn(WallBundle::new(WallLocation::Top)); } pub(crate) fn apply_velocity(mut query: Query<(&mut Transform, &Velocity), With<Ball>>) { diff --git a/examples/breakout/server.rs b/examples/breakout/server.rs index c4c20af..5ac1fc4 100644 --- a/examples/breakout/server.rs +++ b/examples/breakout/server.rs @@ -2,8 +2,8 @@ use std::collections::HashMap; use bevy::{ prelude::{ - default, Bundle, Commands, Component, Entity, EventReader, Query, ResMut, Transform, Vec2, - Vec3, With, + default, Bundle, Commands, Component, Entity, EventReader, Query, ResMut, Resource, + Transform, Vec2, Vec3, With, }, sprite::collide_aabb::{collide, Collision}, transform::TransformBundle, @@ -48,7 +48,7 @@ pub(crate) struct Player { input: PaddleInput, } -#[derive(Debug, Clone, Default)] +#[derive(Resource, Debug, Clone, Default)] pub(crate) struct Players { map: HashMap<ClientId, Player>, } @@ -298,10 +298,10 @@ fn start_game(commands: &mut Commands, server: &mut ResMut<Server>, players: &Re } // Spawn walls - commands.spawn_bundle(WallBundle::new(WallLocation::Left)); - commands.spawn_bundle(WallBundle::new(WallLocation::Right)); - commands.spawn_bundle(WallBundle::new(WallLocation::Bottom)); - commands.spawn_bundle(WallBundle::new(WallLocation::Top)); + commands.spawn(WallBundle::new(WallLocation::Left)); + commands.spawn(WallBundle::new(WallLocation::Right)); + commands.spawn(WallBundle::new(WallLocation::Bottom)); + commands.spawn(WallBundle::new(WallLocation::Top)); // Spawn bricks // Negative scales result in flipped sprites / meshes, @@ -352,18 +352,18 @@ fn start_game(commands: &mut Commands, server: &mut ResMut<Server>, players: &Re ); // brick - commands - .spawn() - .insert(Brick(brick_id)) - .insert_bundle(TransformBundle { + commands.spawn(( + Brick(brick_id), + TransformBundle { local: Transform { translation: brick_position.extend(0.0), scale: Vec3::new(BRICK_SIZE.x, BRICK_SIZE.y, 1.0), ..default() }, ..default() - }) - .insert(Collider); + }, + Collider, + )); brick_id += 1; } } @@ -388,19 +388,20 @@ fn start_game(commands: &mut Commands, server: &mut ResMut<Server>, players: &Re fn spawn_paddle(commands: &mut Commands, client_id: ClientId, pos: &Vec3) -> Entity { commands - .spawn() - .insert(Paddle { - player_id: client_id, - }) - .insert_bundle(TransformBundle { - local: Transform { - translation: *pos, - scale: PADDLE_SIZE, + .spawn(( + Paddle { + player_id: client_id, + }, + TransformBundle { + local: Transform { + translation: *pos, + scale: PADDLE_SIZE, + ..default() + }, ..default() }, - ..default() - }) - .insert(Collider) + Collider, + )) .id() } @@ -411,19 +412,20 @@ fn spawn_ball( direction: &Vec2, ) -> Entity { commands - .spawn() - .insert(Ball { - last_hit_by: client_id, - }) - .insert_bundle(TransformBundle { - local: Transform { - scale: BALL_SIZE, - translation: *pos, + .spawn(( + Ball { + last_hit_by: client_id, + }, + TransformBundle { + local: Transform { + scale: BALL_SIZE, + translation: *pos, + ..default() + }, ..default() }, - ..default() - }) - .insert(Velocity(direction.normalize() * BALL_SPEED)) + Velocity(direction.normalize() * BALL_SPEED), + )) .id() } diff --git a/examples/chat/client.rs b/examples/chat/client.rs index 0e6fad7..15d515a 100644 --- a/examples/chat/client.rs +++ b/examples/chat/client.rs @@ -7,7 +7,10 @@ use std::{ use bevy::{ app::{AppExit, ScheduleRunnerPlugin}, log::LogPlugin, - prelude::{info, warn, App, Commands, CoreStage, EventReader, EventWriter, Res, ResMut}, + prelude::{ + info, warn, App, Commands, CoreStage, Deref, DerefMut, EventReader, EventWriter, Res, + ResMut, Resource, + }, }; use bevy_quinnet::{ client::{ @@ -23,12 +26,15 @@ use protocol::{ClientMessage, ServerMessage}; mod protocol; -#[derive(Debug, Clone, Default)] +#[derive(Resource, Debug, Clone, Default)] struct Users { self_id: ClientId, names: HashMap<ClientId, String>, } +#[derive(Resource, Deref, DerefMut)] +struct TerminalReceiver(mpsc::Receiver<String>); + pub fn on_app_exit(app_exit_events: EventReader<AppExit>, client: Res<Client>) { if !app_exit_events.is_empty() { client.send_message(ClientMessage::Disconnect {}).unwrap(); @@ -76,7 +82,7 @@ fn handle_server_messages(mut client: ResMut<Client>, mut users: ResMut<Users>) fn handle_terminal_messages( client: ResMut<Client>, - mut terminal_messages: ResMut<mpsc::Receiver<String>>, + mut terminal_messages: ResMut<TerminalReceiver>, mut app_exit_events: EventWriter<AppExit>, ) { while let Ok(message) = terminal_messages.try_recv() { @@ -101,7 +107,7 @@ fn start_terminal_listener(mut commands: Commands) { .unwrap(); }); - commands.insert_resource(from_terminal_receiver); + commands.insert_resource(TerminalReceiver(from_terminal_receiver)); } fn start_connection(client: ResMut<Client>) { diff --git a/examples/chat/server.rs b/examples/chat/server.rs index 26fa75e..facaefa 100644 --- a/examples/chat/server.rs +++ b/examples/chat/server.rs @@ -13,7 +13,7 @@ use protocol::{ClientMessage, ServerMessage}; mod protocol; -#[derive(Debug, Clone, Default)] +#[derive(Resource, Debug, Clone, Default)] struct Users { names: HashMap<ClientId, String>, } @@ -54,7 +54,7 @@ fn handle_client_messages(mut server: ResMut<Server>, mut users: ResMut<Users>) } ClientMessage::Disconnect {} => { // We tell the server to disconnect this user - server.disconnect_client(client_id); + server.disconnect_client(client_id).unwrap(); handle_disconnect(&mut server, &mut users, client_id); } ClientMessage::ChatMessage { message } => { |